change m.request return value from stream to promise

remove m.prop
add m.Promise
update tests and examples
This commit is contained in:
Leo Horie 2016-11-13 22:44:22 -05:00
parent 8f1a69cfcb
commit bc8cf4ed76
19 changed files with 650 additions and 852 deletions

View file

@ -2,100 +2,105 @@
var buildQueryString = require("../querystring/build")
module.exports = function($window, Stream) {
module.exports = function($window, Promise) {
var callbackCount = 0
var count = 0
var oncompletion
function setCompletionCallback(callback) {oncompletion = callback}
function complete() {if (--count === 0 && typeof oncompletion === "function") oncompletion()}
function finalize(promise) {
var then = promise.then
promise.then = function() {
count++
var next = then.apply(promise, arguments)
next.then(complete, function(e) {
complete()
throw e
})
return finalize(next)
}
return promise
}
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 {
var response = (args.extract !== extract) ? args.extract(xhr, args) : args.deserialize(args.extract(xhr, args))
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
stream(cast(args.type, response))
}
else {
var error = new Error(xhr.responseText)
for (var key in response) error[key] = response[key]
stream.error(error)
}
}
catch (e) {
stream.error(e)
}
if (typeof oncompletion === "function") oncompletion()
return finalize(new Promise(function(resolve, reject) {
if (typeof args === "string") {
var url = args
args = extra || {}
if (args.url == null) args.url = url
}
}
if (useBody && (args.data != null)) xhr.send(args.data)
else xhr.send()
if (args.method == null) args.method = "GET"
args.method = args.method.toUpperCase()
return stream
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 {
var response = (args.extract !== extract) ? args.extract(xhr, args) : args.deserialize(args.extract(xhr, args))
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
resolve(cast(args.type, response))
}
else {
var error = new Error(xhr.responseText)
for (var key in response) error[key] = response[key]
reject(error)
}
}
catch (e) {
reject(e)
}
}
}
if (useBody && (args.data != null)) xhr.send(args.data)
else xhr.send()
}))
}
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) {
script.parentNode.removeChild(script)
stream(cast(args.type, data))
if (typeof oncompletion === "function") oncompletion()
delete $window[callbackName]
}
script.onerror = function() {
script.parentNode.removeChild(script)
stream.error(new Error("JSONP request failed"))
if (typeof oncompletion === "function") oncompletion()
delete $window[callbackName]
}
if (args.data == null) args.data = {}
args.url = interpolate(args.url, args.data)
args.data[args.callbackKey || "callback"] = callbackName
script.src = assemble(args.url, args.data)
$window.document.documentElement.appendChild(script)
return stream
return finalize(new Promise(function(resolve, reject) {
var callbackName = args.callbackName || "_mithril_" + Math.round(Math.random() * 1e16) + "_" + callbackCount++
var script = $window.document.createElement("script")
$window[callbackName] = function(data) {
script.parentNode.removeChild(script)
resolve(cast(args.type, data))
delete $window[callbackName]
}
script.onerror = function() {
script.parentNode.removeChild(script)
reject(new Error("JSONP request failed"))
delete $window[callbackName]
}
if (args.data == null) args.data = {}
args.url = interpolate(args.url, args.data)
args.data[args.callbackKey || "callback"] = callbackName
script.src = assemble(args.url, args.data)
$window.document.documentElement.appendChild(script)
}))
}
function interpolate(url, data) {

View file

@ -12,7 +12,7 @@
<script src="../../test-utils/xhrMock.js"></script>
<script src="../../querystring/build.js"></script>
<script src="../../util/stream.js"></script>
<script src="../../promise/promise.js"></script>
<script src="../../request/request.js"></script>
<script src="test-request.js"></script>
<script src="test-jsonp.js"></script>

View file

@ -3,15 +3,14 @@
var o = require("../../ospec/ospec")
var xhrMock = require("../../test-utils/xhrMock")
var Request = require("../../request/request")
var StreamFactory = require("../../util/stream")
var Promise = require("../../promise/promise")
var parseQueryString = require("../../querystring/parse")
o.spec("jsonp", function() {
var mock, jsonp, spy
o.beforeEach(function() {
mock = xhrMock()
spy = o.spy()
jsonp = new Request(mock, StreamFactory(spy)).jsonp
jsonp = new Request(mock, Promise).jsonp
})
o("works", function(done) {
@ -21,9 +20,9 @@ o.spec("jsonp", function() {
return {status: 200, responseText: queryData["callback"] + "(" + JSON.stringify({a: 1}) + ")"}
}
})
jsonp({url: "/item"}).run(function(data) {
jsonp({url: "/item"}).then(function(data) {
o(data).deepEquals({a: 1})
}).run(done)
}).then(done)
})
o("works w/ other querystring params", function(done) {
mock.$defineRoutes({
@ -32,10 +31,10 @@ o.spec("jsonp", function() {
return {status: 200, responseText: queryData["callback"] + "(" + JSON.stringify(queryData) + ")"}
}
})
jsonp({url: "/item", data: {a: "b", c: "d"}}).run(function(data) {
jsonp({url: "/item", data: {a: "b", c: "d"}}).then(function(data) {
delete data["callback"]
o(data).deepEquals({a: "b", c: "d"})
}).run(done)
}).then(done)
})
o("works w/ custom callbackKey", function(done) {
mock.$defineRoutes({
@ -44,9 +43,9 @@ o.spec("jsonp", function() {
return {status: 200, responseText: queryData["cb"] + "(" + JSON.stringify({a: 2}) + ")"}
}
})
jsonp({url: "/item", callbackKey: "cb"}).run(function(data) {
jsonp({url: "/item", callbackKey: "cb"}).then(function(data) {
o(data).deepEquals({a: 2})
}).run(done)
}).then(done)
})
o("handles error", function(done) {
jsonp({url: "/item", callbackKey: "cb"}).catch(function(e) {

View file

@ -3,14 +3,13 @@
var o = require("../../ospec/ospec")
var xhrMock = require("../../test-utils/xhrMock")
var Request = require("../../request/request")
var StreamFactory = require("../../util/stream")
var Promise = require("../../promise/promise")
o.spec("xhr", function() {
var mock, xhr, spy
var mock, xhr
o.beforeEach(function() {
mock = xhrMock()
spy = o.spy()
xhr = new Request(mock, StreamFactory(spy)).request
xhr = new Request(mock, Promise).request
})
o.spec("success", function() {
@ -21,9 +20,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: 1})}
}
})
xhr({method: "GET", url: "/item"}).run(function(data) {
xhr({method: "GET", url: "/item"}).then(function(data) {
o(data).deepEquals({a: 1})
}).run(function() {
}).then(function() {
done()
})
})
@ -34,9 +33,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: 1})}
}
})
xhr({url: "/item"}).run(function(data) {
xhr({url: "/item"}).then(function(data) {
o(data).deepEquals({a: 1})
}).run(function() {
}).then(function() {
done()
})
})
@ -47,9 +46,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: 1})}
}
})
xhr("/item").run(function(data) {
xhr("/item").then(function(data) {
o(data).deepEquals({a: 1})
}).run(function() {
}).then(function() {
done()
})
})
@ -59,9 +58,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: 1})}
}
})
xhr({method: "POST", url: "/item"}).run(function(data) {
xhr({method: "POST", url: "/item"}).then(function(data) {
o(data).deepEquals({a: 1})
}).run(done)
}).then(done)
})
o("first argument can act as URI with second argument providing options", function(done) {
mock.$defineRoutes({
@ -69,9 +68,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: 1})}
}
})
xhr("/item", {method: "POST"}).run(function(data) {
xhr("/item", {method: "POST"}).then(function(data) {
o(data).deepEquals({a: 1})
}).run(done)
}).then(done)
})
o("works w/ parameterized data via GET", function(done) {
mock.$defineRoutes({
@ -79,9 +78,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: request.query})}
}
})
xhr({method: "GET", url: "/item", data: {x: "y"}}).run(function(data) {
xhr({method: "GET", url: "/item", data: {x: "y"}}).then(function(data) {
o(data).deepEquals({a: "?x=y"})
}).run(done)
}).then(done)
})
o("works w/ parameterized data via POST", function(done) {
mock.$defineRoutes({
@ -89,9 +88,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: JSON.parse(request.body)})}
}
})
xhr({method: "POST", url: "/item", data: {x: "y"}}).run(function(data) {
xhr({method: "POST", url: "/item", data: {x: "y"}}).then(function(data) {
o(data).deepEquals({a: {x: "y"}})
}).run(done)
}).then(done)
})
o("works w/ parameterized data containing colon via GET", function(done) {
mock.$defineRoutes({
@ -99,9 +98,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: request.query})}
}
})
xhr({method: "GET", url: "/item", data: {x: ":y"}}).run(function(data) {
xhr({method: "GET", url: "/item", data: {x: ":y"}}).then(function(data) {
o(data).deepEquals({a: "?x=%3Ay"})
}).run(done)
}).then(done)
})
o("works w/ parameterized data containing colon via POST", function(done) {
mock.$defineRoutes({
@ -109,9 +108,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: JSON.parse(request.body)})}
}
})
xhr({method: "POST", url: "/item", data: {x: ":y"}}).run(function(data) {
xhr({method: "POST", url: "/item", data: {x: ":y"}}).then(function(data) {
o(data).deepEquals({a: {x: ":y"}})
}).run(done)
}).then(done)
})
o("works w/ parameterized url via GET", function(done) {
mock.$defineRoutes({
@ -119,9 +118,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: request.url, b: request.query})}
}
})
xhr({method: "GET", url: "/item/:x", data: {x: "y"}}).run(function(data) {
xhr({method: "GET", url: "/item/:x", data: {x: "y"}}).then(function(data) {
o(data).deepEquals({a: "/item/y", b: {}})
}).run(done)
}).then(done)
})
o("works w/ parameterized url via POST", function(done) {
mock.$defineRoutes({
@ -129,9 +128,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: request.url, b: JSON.parse(request.body)})}
}
})
xhr({method: "POST", url: "/item/:x", data: {x: "y"}}).run(function(data) {
xhr({method: "POST", url: "/item/:x", data: {x: "y"}}).then(function(data) {
o(data).deepEquals({a: "/item/y", b: {}})
}).run(done)
}).then(done)
})
o("ignores unresolved parameter via GET", function(done) {
mock.$defineRoutes({
@ -139,9 +138,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: request.url})}
}
})
xhr({method: "GET", url: "/item/:x"}).run(function(data) {
xhr({method: "GET", url: "/item/:x"}).then(function(data) {
o(data).deepEquals({a: "/item/:x"})
}).run(done)
}).then(done)
})
o("ignores unresolved parameter via POST", function(done) {
mock.$defineRoutes({
@ -149,9 +148,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: request.url})}
}
})
xhr({method: "GET", url: "/item/:x"}).run(function(data) {
xhr({method: "GET", url: "/item/:x"}).then(function(data) {
o(data).deepEquals({a: "/item/:x"})
}).run(done)
}).then(done)
})
o("type parameter works for Array responses", function(done) {
var Entity = function(args) {
@ -163,9 +162,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify([{id: 1}, {id: 2}, {id: 3}])}
}
})
xhr({method: "GET", url: "/item", type: Entity}).run(function(data) {
xhr({method: "GET", url: "/item", type: Entity}).then(function(data) {
o(data).deepEquals([{_id: 1}, {_id: 2}, {_id: 3}])
}).run(done)
}).then(done)
})
o("type parameter works for Object responses", function(done) {
var Entity = function(args) {
@ -177,9 +176,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({id: 1})}
}
})
xhr({method: "GET", url: "/item", type: Entity}).run(function(data) {
xhr({method: "GET", url: "/item", type: Entity}).then(function(data) {
o(data).deepEquals({_id: 1})
}).run(done)
}).then(done)
})
o("serialize parameter works in GET", function(done) {
var serialize = function(data) {
@ -191,9 +190,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({body: request.query})}
}
})
xhr({method: "GET", url: "/item", serialize: serialize, data: {id: 1}}).run(function(data) {
xhr({method: "GET", url: "/item", serialize: serialize, data: {id: 1}}).then(function(data) {
o(data.body).equals("?id=1")
}).run(done)
}).then(done)
})
o("serialize parameter works in POST", function(done) {
var serialize = function(data) {
@ -205,9 +204,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({body: request.body})}
}
})
xhr({method: "POST", url: "/item", serialize: serialize, data: {id: 1}}).run(function(data) {
xhr({method: "POST", url: "/item", serialize: serialize, data: {id: 1}}).then(function(data) {
o(data.body).equals("id=1")
}).run(done)
}).then(done)
})
o("deserialize parameter works in GET", function(done) {
var deserialize = function(data) {
@ -219,9 +218,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({test: 123})}
}
})
xhr({method: "GET", url: "/item", deserialize: deserialize}).run(function(data) {
xhr({method: "GET", url: "/item", deserialize: deserialize}).then(function(data) {
o(data).equals("{\"test\":123}")
}).run(done)
}).then(done)
})
o("deserialize parameter works in POST", function(done) {
var deserialize = function(data) {
@ -233,9 +232,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({test: 123})}
}
})
xhr({method: "POST", url: "/item", deserialize: deserialize}).run(function(data) {
xhr({method: "POST", url: "/item", deserialize: deserialize}).then(function(data) {
o(data).equals("{\"test\":123}")
}).run(done)
}).then(done)
})
o("extract parameter works in GET", function(done) {
var extract = function(data) {
@ -247,9 +246,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: ""}
}
})
xhr({method: "GET", url: "/item", extract: extract}).run(function(data) {
xhr({method: "GET", url: "/item", extract: extract}).then(function(data) {
o(data).equals("{\"test\":123}")
}).run(done)
}).then(done)
})
o("extract parameter works in POST", function(done) {
var extract = function(data) {
@ -261,9 +260,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: ""}
}
})
xhr({method: "POST", url: "/item", extract: extract}).run(function(data) {
xhr({method: "POST", url: "/item", extract: extract}).then(function(data) {
o(data).equals("{\"test\":123}")
}).run(done)
}).then(done)
})
o("ignores deserialize if extract is defined", function(done) {
var extract = function(data) {
@ -276,11 +275,11 @@ o.spec("xhr", function() {
return {status: 200, responseText: ""}
}
})
xhr({method: "GET", url: "/item", extract: extract, deserialize: deserialize}).run(function(data) {
xhr({method: "GET", url: "/item", extract: extract, deserialize: deserialize}).then(function(data) {
o(data).equals(200)
}).run(function() {
}).then(function() {
o(deserialize.callCount).equals(0)
}).run(done)
}).then(done)
})
o("config parameter works", function(done) {
mock.$defineRoutes({
@ -288,7 +287,7 @@ o.spec("xhr", function() {
return {status: 200, responseText: ""}
}
})
xhr({method: "POST", url: "/item", config: config}).run(done)
xhr({method: "POST", url: "/item", config: config}).then(done)
function config(xhr) {
o(typeof xhr.setRequestHeader).equals("function")
@ -296,16 +295,6 @@ o.spec("xhr", function() {
o(typeof xhr.send).equals("function")
}
})
o("initialValue parameter works", function() {
mock.$defineRoutes({
"GET /items": function() {
return {status: 200, responseText: JSON.stringify([{a: 1}])}
}
})
var items = xhr({method: "GET", url: "/items", initialValue: []})
o(items()).deepEquals([])
})
})
o.spec("failure", function() {
o("rejects on server error", function(done) {
@ -317,7 +306,7 @@ o.spec("xhr", function() {
xhr({method: "GET", url: "/item"}).catch(function(e) {
o(e instanceof Error).equals(true)
o(e.message).equals(JSON.stringify({error: "error"}))
}).run(done)
}).then(done)
})
o("extends Error with JSON response", function(done) {
mock.$defineRoutes({
@ -329,7 +318,7 @@ o.spec("xhr", function() {
o(e instanceof Error).equals(true)
o(e.message).equals("error")
o(e.stack).equals("error on line 1")
}).run(done)
}).then(done)
})
o("rejects on non-JSON server error", function(done) {
mock.$defineRoutes({
@ -339,7 +328,7 @@ o.spec("xhr", function() {
})
xhr({method: "GET", url: "/item"}).catch(function(e) {
o(e.message).equals("error")
}).run(done)
}).then(done)
})
})
})