Fetch-style signature overload
This commit is contained in:
parent
bcb81c4b03
commit
beccd16dd7
2 changed files with 66 additions and 18 deletions
|
|
@ -1,40 +1,52 @@
|
|||
"use strict"
|
||||
|
||||
var buildQueryString = require("../querystring/build")
|
||||
var Promise = require("../promise/promise")
|
||||
|
||||
module.exports = function($window, Stream) {
|
||||
var callbackCount = 0
|
||||
|
||||
var oncompletion
|
||||
function setCompletionCallback(callback) {oncompletion = callback}
|
||||
|
||||
function request(args) {
|
||||
|
||||
function request(args, extra) {
|
||||
if(typeof args === "string"){
|
||||
var url = args
|
||||
|
||||
if(typeof extra === "object") args = extra
|
||||
else args = {}
|
||||
|
||||
if(typeof args.url === "undefined") args.url = url
|
||||
}
|
||||
|
||||
if(typeof args.method === "undefined") args.method = "GET"
|
||||
|
||||
var stream = Stream()
|
||||
if (args.initialValue !== undefined) stream(args.initialValue)
|
||||
args.method = args.method.toUpperCase()
|
||||
|
||||
|
||||
var useBody = typeof args.useBody === "boolean" ? args.useBody : args.method !== "GET" && args.method !== "TRACE"
|
||||
|
||||
|
||||
if (typeof args.serialize !== "function") args.serialize = typeof FormData !== "undefined" && args.data instanceof FormData ? function(value) {return value} : JSON.stringify
|
||||
if (typeof args.deserialize !== "function") args.deserialize = deserialize
|
||||
if (typeof args.extract !== "function") args.extract = extract
|
||||
|
||||
|
||||
args.url = interpolate(args.url, args.data)
|
||||
if (useBody) args.data = args.serialize(args.data)
|
||||
else args.url = assemble(args.url, args.data)
|
||||
|
||||
|
||||
var xhr = new $window.XMLHttpRequest()
|
||||
xhr.open(args.method, args.url, typeof args.async === "boolean" ? args.async : true, typeof args.user === "string" ? args.user : undefined, typeof args.password === "string" ? args.password : undefined)
|
||||
|
||||
|
||||
if (args.serialize === JSON.stringify && useBody) {
|
||||
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8")
|
||||
}
|
||||
if (args.deserialize === deserialize) {
|
||||
xhr.setRequestHeader("Accept", "application/json, text/*")
|
||||
}
|
||||
|
||||
|
||||
if (typeof args.config === "function") xhr = args.config(xhr, args) || xhr
|
||||
|
||||
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4) {
|
||||
try {
|
||||
|
|
@ -54,17 +66,17 @@ module.exports = function($window, Stream) {
|
|||
if (typeof oncompletion === "function") oncompletion()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (useBody && (args.data != null)) xhr.send(args.data)
|
||||
else xhr.send()
|
||||
|
||||
|
||||
return stream
|
||||
}
|
||||
|
||||
function jsonp(args) {
|
||||
var stream = Stream()
|
||||
if (args.initialValue !== undefined) stream(args.initialValue)
|
||||
|
||||
|
||||
var callbackName = args.callbackName || "_mithril_" + Math.round(Math.random() * 1e16) + "_" + callbackCount++
|
||||
var script = $window.document.createElement("script")
|
||||
$window[callbackName] = function(data) {
|
||||
|
|
@ -116,7 +128,7 @@ module.exports = function($window, Stream) {
|
|||
}
|
||||
|
||||
function extract(xhr) {return xhr.responseText}
|
||||
|
||||
|
||||
function cast(type, data) {
|
||||
if (typeof type === "function") {
|
||||
if (data instanceof Array) {
|
||||
|
|
@ -128,6 +140,6 @@ module.exports = function($window, Stream) {
|
|||
}
|
||||
return data
|
||||
}
|
||||
|
||||
|
||||
return {request: request, jsonp: jsonp, setCompletionCallback: setCompletionCallback}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,13 +27,49 @@ o.spec("xhr", function() {
|
|||
done()
|
||||
})
|
||||
})
|
||||
o("works via POST", function(done) {
|
||||
o("implicit GET method", function(done){
|
||||
var s = new Date
|
||||
mock.$defineRoutes({
|
||||
"GET /item": function() {
|
||||
return {status: 200, responseText: JSON.stringify({a: 1})}
|
||||
}
|
||||
})
|
||||
xhr({method: "GET", url: "/item"}).run(function(data) {
|
||||
xhr({url: "/item"}).run(function(data) {
|
||||
o(data).deepEquals({a: 1})
|
||||
}).run(function() {
|
||||
done()
|
||||
})
|
||||
})
|
||||
o("first argument can be a string aliasing url property", function(done){
|
||||
var s = new Date
|
||||
mock.$defineRoutes({
|
||||
"GET /item": function() {
|
||||
return {status: 200, responseText: JSON.stringify({a: 1})}
|
||||
}
|
||||
})
|
||||
xhr("/item").run(function(data) {
|
||||
o(data).deepEquals({a: 1})
|
||||
}).run(function() {
|
||||
done()
|
||||
})
|
||||
})
|
||||
o("works via POST", function(done) {
|
||||
mock.$defineRoutes({
|
||||
"POST /item": function() {
|
||||
return {status: 200, responseText: JSON.stringify({a: 1})}
|
||||
}
|
||||
})
|
||||
xhr({method: "POST", url: "/item"}).run(function(data) {
|
||||
o(data).deepEquals({a: 1})
|
||||
}).run(done)
|
||||
})
|
||||
o("first argument can act as URI with second argument providing options", function(done) {
|
||||
mock.$defineRoutes({
|
||||
"POST /item": function() {
|
||||
return {status: 200, responseText: JSON.stringify({a: 1})}
|
||||
}
|
||||
})
|
||||
xhr("/item", {method: "POST"}).run(function(data) {
|
||||
o(data).deepEquals({a: 1})
|
||||
}).run(done)
|
||||
})
|
||||
|
|
@ -253,7 +289,7 @@ o.spec("xhr", function() {
|
|||
}
|
||||
})
|
||||
xhr({method: "POST", url: "/item", config: config}).run(done)
|
||||
|
||||
|
||||
function config(xhr) {
|
||||
o(typeof xhr.setRequestHeader).equals("function")
|
||||
o(typeof xhr.open).equals("function")
|
||||
|
|
@ -267,7 +303,7 @@ o.spec("xhr", function() {
|
|||
}
|
||||
})
|
||||
var items = xhr({method: "GET", url: "/items", initialValue: []})
|
||||
|
||||
|
||||
o(items()).deepEquals([])
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue