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"
|
"use strict"
|
||||||
|
|
||||||
var buildQueryString = require("../querystring/build")
|
var buildQueryString = require("../querystring/build")
|
||||||
|
var Promise = require("../promise/promise")
|
||||||
|
|
||||||
module.exports = function($window, Stream) {
|
module.exports = function($window, Stream) {
|
||||||
var callbackCount = 0
|
var callbackCount = 0
|
||||||
|
|
||||||
var oncompletion
|
var oncompletion
|
||||||
function setCompletionCallback(callback) {oncompletion = callback}
|
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()
|
var stream = Stream()
|
||||||
if (args.initialValue !== undefined) stream(args.initialValue)
|
if (args.initialValue !== undefined) stream(args.initialValue)
|
||||||
args.method = args.method.toUpperCase()
|
args.method = args.method.toUpperCase()
|
||||||
|
|
||||||
var useBody = typeof args.useBody === "boolean" ? args.useBody : args.method !== "GET" && args.method !== "TRACE"
|
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.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.deserialize !== "function") args.deserialize = deserialize
|
||||||
if (typeof args.extract !== "function") args.extract = extract
|
if (typeof args.extract !== "function") args.extract = extract
|
||||||
|
|
||||||
args.url = interpolate(args.url, args.data)
|
args.url = interpolate(args.url, args.data)
|
||||||
if (useBody) args.data = args.serialize(args.data)
|
if (useBody) args.data = args.serialize(args.data)
|
||||||
else args.url = assemble(args.url, args.data)
|
else args.url = assemble(args.url, args.data)
|
||||||
|
|
||||||
var xhr = new $window.XMLHttpRequest()
|
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)
|
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) {
|
if (args.serialize === JSON.stringify && useBody) {
|
||||||
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8")
|
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8")
|
||||||
}
|
}
|
||||||
if (args.deserialize === deserialize) {
|
if (args.deserialize === deserialize) {
|
||||||
xhr.setRequestHeader("Accept", "application/json, text/*")
|
xhr.setRequestHeader("Accept", "application/json, text/*")
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof args.config === "function") xhr = args.config(xhr, args) || xhr
|
if (typeof args.config === "function") xhr = args.config(xhr, args) || xhr
|
||||||
|
|
||||||
xhr.onreadystatechange = function() {
|
xhr.onreadystatechange = function() {
|
||||||
if (xhr.readyState === 4) {
|
if (xhr.readyState === 4) {
|
||||||
try {
|
try {
|
||||||
|
|
@ -54,17 +66,17 @@ module.exports = function($window, Stream) {
|
||||||
if (typeof oncompletion === "function") oncompletion()
|
if (typeof oncompletion === "function") oncompletion()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (useBody && (args.data != null)) xhr.send(args.data)
|
if (useBody && (args.data != null)) xhr.send(args.data)
|
||||||
else xhr.send()
|
else xhr.send()
|
||||||
|
|
||||||
return stream
|
return stream
|
||||||
}
|
}
|
||||||
|
|
||||||
function jsonp(args) {
|
function jsonp(args) {
|
||||||
var stream = Stream()
|
var stream = Stream()
|
||||||
if (args.initialValue !== undefined) stream(args.initialValue)
|
if (args.initialValue !== undefined) stream(args.initialValue)
|
||||||
|
|
||||||
var callbackName = args.callbackName || "_mithril_" + Math.round(Math.random() * 1e16) + "_" + callbackCount++
|
var callbackName = args.callbackName || "_mithril_" + Math.round(Math.random() * 1e16) + "_" + callbackCount++
|
||||||
var script = $window.document.createElement("script")
|
var script = $window.document.createElement("script")
|
||||||
$window[callbackName] = function(data) {
|
$window[callbackName] = function(data) {
|
||||||
|
|
@ -116,7 +128,7 @@ module.exports = function($window, Stream) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function extract(xhr) {return xhr.responseText}
|
function extract(xhr) {return xhr.responseText}
|
||||||
|
|
||||||
function cast(type, data) {
|
function cast(type, data) {
|
||||||
if (typeof type === "function") {
|
if (typeof type === "function") {
|
||||||
if (data instanceof Array) {
|
if (data instanceof Array) {
|
||||||
|
|
@ -128,6 +140,6 @@ module.exports = function($window, Stream) {
|
||||||
}
|
}
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
return {request: request, jsonp: jsonp, setCompletionCallback: setCompletionCallback}
|
return {request: request, jsonp: jsonp, setCompletionCallback: setCompletionCallback}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,13 +27,49 @@ o.spec("xhr", function() {
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
o("works via POST", function(done) {
|
o("implicit GET method", function(done){
|
||||||
|
var s = new Date
|
||||||
mock.$defineRoutes({
|
mock.$defineRoutes({
|
||||||
"GET /item": function() {
|
"GET /item": function() {
|
||||||
return {status: 200, responseText: JSON.stringify({a: 1})}
|
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})
|
o(data).deepEquals({a: 1})
|
||||||
}).run(done)
|
}).run(done)
|
||||||
})
|
})
|
||||||
|
|
@ -253,7 +289,7 @@ o.spec("xhr", function() {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
xhr({method: "POST", url: "/item", config: config}).run(done)
|
xhr({method: "POST", url: "/item", config: config}).run(done)
|
||||||
|
|
||||||
function config(xhr) {
|
function config(xhr) {
|
||||||
o(typeof xhr.setRequestHeader).equals("function")
|
o(typeof xhr.setRequestHeader).equals("function")
|
||||||
o(typeof xhr.open).equals("function")
|
o(typeof xhr.open).equals("function")
|
||||||
|
|
@ -267,7 +303,7 @@ o.spec("xhr", function() {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
var items = xhr({method: "GET", url: "/items", initialValue: []})
|
var items = xhr({method: "GET", url: "/items", initialValue: []})
|
||||||
|
|
||||||
o(items()).deepEquals([])
|
o(items()).deepEquals([])
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue