Skip to content

Commit 95bf958

Browse files
authored
Merge pull request MithrilJS#1393 from barneycarroll/fetch-signature-overload
Fetch-style signature overload
2 parents bcb81c4 + beccd16 commit 95bf958

File tree

2 files changed

+66
-18
lines changed

2 files changed

+66
-18
lines changed

request/request.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,52 @@
11
"use strict"
22

33
var buildQueryString = require("../querystring/build")
4+
var Promise = require("../promise/promise")
45

56
module.exports = function($window, Stream) {
67
var callbackCount = 0
78

89
var oncompletion
910
function setCompletionCallback(callback) {oncompletion = callback}
10-
11-
function request(args) {
11+
12+
function request(args, extra) {
13+
if(typeof args === "string"){
14+
var url = args
15+
16+
if(typeof extra === "object") args = extra
17+
else args = {}
18+
19+
if(typeof args.url === "undefined") args.url = url
20+
}
21+
22+
if(typeof args.method === "undefined") args.method = "GET"
23+
1224
var stream = Stream()
1325
if (args.initialValue !== undefined) stream(args.initialValue)
1426
args.method = args.method.toUpperCase()
15-
27+
1628
var useBody = typeof args.useBody === "boolean" ? args.useBody : args.method !== "GET" && args.method !== "TRACE"
17-
29+
1830
if (typeof args.serialize !== "function") args.serialize = typeof FormData !== "undefined" && args.data instanceof FormData ? function(value) {return value} : JSON.stringify
1931
if (typeof args.deserialize !== "function") args.deserialize = deserialize
2032
if (typeof args.extract !== "function") args.extract = extract
21-
33+
2234
args.url = interpolate(args.url, args.data)
2335
if (useBody) args.data = args.serialize(args.data)
2436
else args.url = assemble(args.url, args.data)
25-
37+
2638
var xhr = new $window.XMLHttpRequest()
2739
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)
28-
40+
2941
if (args.serialize === JSON.stringify && useBody) {
3042
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8")
3143
}
3244
if (args.deserialize === deserialize) {
3345
xhr.setRequestHeader("Accept", "application/json, text/*")
3446
}
35-
47+
3648
if (typeof args.config === "function") xhr = args.config(xhr, args) || xhr
37-
49+
3850
xhr.onreadystatechange = function() {
3951
if (xhr.readyState === 4) {
4052
try {
@@ -54,17 +66,17 @@ module.exports = function($window, Stream) {
5466
if (typeof oncompletion === "function") oncompletion()
5567
}
5668
}
57-
69+
5870
if (useBody && (args.data != null)) xhr.send(args.data)
5971
else xhr.send()
60-
72+
6173
return stream
6274
}
6375

6476
function jsonp(args) {
6577
var stream = Stream()
6678
if (args.initialValue !== undefined) stream(args.initialValue)
67-
79+
6880
var callbackName = args.callbackName || "_mithril_" + Math.round(Math.random() * 1e16) + "_" + callbackCount++
6981
var script = $window.document.createElement("script")
7082
$window[callbackName] = function(data) {
@@ -116,7 +128,7 @@ module.exports = function($window, Stream) {
116128
}
117129

118130
function extract(xhr) {return xhr.responseText}
119-
131+
120132
function cast(type, data) {
121133
if (typeof type === "function") {
122134
if (data instanceof Array) {
@@ -128,6 +140,6 @@ module.exports = function($window, Stream) {
128140
}
129141
return data
130142
}
131-
143+
132144
return {request: request, jsonp: jsonp, setCompletionCallback: setCompletionCallback}
133145
}

request/tests/test-request.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,49 @@ o.spec("xhr", function() {
2727
done()
2828
})
2929
})
30-
o("works via POST", function(done) {
30+
o("implicit GET method", function(done){
31+
var s = new Date
3132
mock.$defineRoutes({
3233
"GET /item": function() {
3334
return {status: 200, responseText: JSON.stringify({a: 1})}
3435
}
3536
})
36-
xhr({method: "GET", url: "/item"}).run(function(data) {
37+
xhr({url: "/item"}).run(function(data) {
38+
o(data).deepEquals({a: 1})
39+
}).run(function() {
40+
done()
41+
})
42+
})
43+
o("first argument can be a string aliasing url property", function(done){
44+
var s = new Date
45+
mock.$defineRoutes({
46+
"GET /item": function() {
47+
return {status: 200, responseText: JSON.stringify({a: 1})}
48+
}
49+
})
50+
xhr("/item").run(function(data) {
51+
o(data).deepEquals({a: 1})
52+
}).run(function() {
53+
done()
54+
})
55+
})
56+
o("works via POST", function(done) {
57+
mock.$defineRoutes({
58+
"POST /item": function() {
59+
return {status: 200, responseText: JSON.stringify({a: 1})}
60+
}
61+
})
62+
xhr({method: "POST", url: "/item"}).run(function(data) {
63+
o(data).deepEquals({a: 1})
64+
}).run(done)
65+
})
66+
o("first argument can act as URI with second argument providing options", function(done) {
67+
mock.$defineRoutes({
68+
"POST /item": function() {
69+
return {status: 200, responseText: JSON.stringify({a: 1})}
70+
}
71+
})
72+
xhr("/item", {method: "POST"}).run(function(data) {
3773
o(data).deepEquals({a: 1})
3874
}).run(done)
3975
})
@@ -253,7 +289,7 @@ o.spec("xhr", function() {
253289
}
254290
})
255291
xhr({method: "POST", url: "/item", config: config}).run(done)
256-
292+
257293
function config(xhr) {
258294
o(typeof xhr.setRequestHeader).equals("function")
259295
o(typeof xhr.open).equals("function")
@@ -267,7 +303,7 @@ o.spec("xhr", function() {
267303
}
268304
})
269305
var items = xhr({method: "GET", url: "/items", initialValue: []})
270-
306+
271307
o(items()).deepEquals([])
272308
})
273309
})

0 commit comments

Comments
 (0)