From 41ac2bf00269bd5c33670861435732a2ae724046 Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Thu, 26 May 2016 15:22:36 -0400 Subject: [PATCH] rename ajaxt o xhr, expose jsonp --- index.js | 4 +- request/request.js | 14 ++--- request/tests/index.html | 2 +- request/tests/test-jsonp.js | 4 +- request/tests/{test-ajax.js => test-xhr.js} | 52 +++++++++---------- .../{test-ajaxMock.js => test-xhrMock.js} | 6 +-- test-utils/{ajaxMock.js => xhrMock.js} | 0 7 files changed, 42 insertions(+), 40 deletions(-) rename request/tests/{test-ajax.js => test-xhr.js} (80%) rename test-utils/tests/{test-ajaxMock.js => test-xhrMock.js} (97%) rename test-utils/{ajaxMock.js => xhrMock.js} (100%) diff --git a/index.js b/index.js index 8a048fea..a46f7a55 100644 --- a/index.js +++ b/index.js @@ -3,8 +3,10 @@ var m = require("./render/hyperscript") var renderService = require("./render/render")(window) var redrawService = require("./api/pubsub")() +var requestService = require("./request/request")(window, Promise) -m.request = require("./request/request")(window, Promise).ajax +m.request = requestService.xhr +m.request = requestService.jsonp m.route = require("./api/router")(window, renderService, redrawService) m.mount = require("./api/mount")(renderService, redrawService) m.trust = require("./render/trust") diff --git a/request/request.js b/request/request.js index 5ddc620d..75160272 100644 --- a/request/request.js +++ b/request/request.js @@ -5,7 +5,7 @@ var buildQueryString = require("../querystring/build") module.exports = function($window, Promise) { var callbackCount = 0 - function ajax(args) { + function xhr(args) { return new Promise(function(resolve, reject) { var useBody = args.useBody != null ? args.useBody : args.method !== "GET" && args.method !== "TRACE" @@ -60,21 +60,21 @@ module.exports = function($window, Promise) { function jsonp(args) { return new Promise(function(resolve, reject) { - var callbackKey = "_mithril_" + Math.round(Math.random() * 1e16) + "_" + callbackCount++ + var callbackName = args.callbackName || "_mithril_" + Math.round(Math.random() * 1e16) + "_" + callbackCount++ var script = $window.document.createElement("script") - $window[callbackKey] = function(data) { + $window[callbackName] = function(data) { script.parentNode.removeChild(script) resolve(data) - $window[callbackKey] = undefined + delete $window[callbackName] } script.onerror = function() { script.parentNode.removeChild(script) reject(new Error("JSONP request failed")) - $window[callbackKey] = undefined + delete $window[callbackName] } if (args.data == null) args.data = {} args.url = interpolate(args.url, args.data) - args.data[args.callbackKey || "callback"] = callbackKey + args.data[args.callbackKey || "callback"] = callbackName script.src = assemble(args.url, args.data) $window.document.documentElement.appendChild(script) }) @@ -110,5 +110,5 @@ module.exports = function($window, Promise) { function extract(xhr) {return xhr.responseText} - return {ajax: ajax, jsonp: jsonp} + return {xhr: xhr, jsonp: jsonp} } diff --git a/request/tests/index.html b/request/tests/index.html index a4918fbd..2d5f3120 100644 --- a/request/tests/index.html +++ b/request/tests/index.html @@ -13,7 +13,7 @@ - + diff --git a/request/tests/test-jsonp.js b/request/tests/test-jsonp.js index 04b47c76..0335bbe3 100644 --- a/request/tests/test-jsonp.js +++ b/request/tests/test-jsonp.js @@ -1,14 +1,14 @@ "use strict" var o = require("../../ospec/ospec") -var ajaxMock = require("../../test-utils/ajaxMock") +var xhrMock = require("../../test-utils/xhrMock") var Request = require("../../request/request") var parseQueryString = require("../../querystring/parse") o.spec("jsonp", function() { var mock, jsonp o.beforeEach(function() { - mock = ajaxMock() + mock = xhrMock() jsonp = new Request(mock, Promise).jsonp }) diff --git a/request/tests/test-ajax.js b/request/tests/test-xhr.js similarity index 80% rename from request/tests/test-ajax.js rename to request/tests/test-xhr.js index 682c7a8e..7be6e14e 100644 --- a/request/tests/test-ajax.js +++ b/request/tests/test-xhr.js @@ -1,14 +1,14 @@ "use strict" var o = require("../../ospec/ospec") -var ajaxMock = require("../../test-utils/ajaxMock") +var xhrMock = require("../../test-utils/xhrMock") var Request = require("../../request/request") -o.spec("ajax", function() { - var mock, ajax +o.spec("xhr", function() { + var mock, xhr o.beforeEach(function() { - mock = ajaxMock() - ajax = new Request(mock, Promise).ajax + mock = xhrMock() + xhr = new Request(mock, Promise).xhr }) o.spec("success", function() { @@ -19,7 +19,7 @@ o.spec("ajax", function() { return {status: 200, responseText: JSON.stringify({a: 1})} } }) - ajax({method: "GET", url: "/item"}).then(function(data) { + xhr({method: "GET", url: "/item"}).then(function(data) { o(data).deepEquals({a: 1}) }).then(function() { done() @@ -31,7 +31,7 @@ o.spec("ajax", function() { return {status: 200, responseText: JSON.stringify({a: 1})} } }) - ajax({method: "GET", url: "/item"}).then(function(data) { + xhr({method: "GET", url: "/item"}).then(function(data) { o(data).deepEquals({a: 1}) }).then(done) }) @@ -41,7 +41,7 @@ o.spec("ajax", function() { return {status: 200, responseText: JSON.stringify({a: request.query})} } }) - ajax({method: "GET", url: "/item", data: {x: "y"}}).then(function(data) { + xhr({method: "GET", url: "/item", data: {x: "y"}}).then(function(data) { o(data).deepEquals({a: "?x=y"}) }).then(done) }) @@ -51,7 +51,7 @@ o.spec("ajax", function() { return {status: 200, responseText: JSON.stringify({a: JSON.parse(request.body)})} } }) - ajax({method: "POST", url: "/item", data: {x: "y"}}).then(function(data) { + xhr({method: "POST", url: "/item", data: {x: "y"}}).then(function(data) { o(data).deepEquals({a: {x: "y"}}) }).then(done) }) @@ -61,7 +61,7 @@ o.spec("ajax", function() { return {status: 200, responseText: JSON.stringify({a: request.query})} } }) - ajax({method: "GET", url: "/item", data: {x: ":y"}}).then(function(data) { + xhr({method: "GET", url: "/item", data: {x: ":y"}}).then(function(data) { o(data).deepEquals({a: "?x=%3Ay"}) }).then(done) }) @@ -71,7 +71,7 @@ o.spec("ajax", function() { return {status: 200, responseText: JSON.stringify({a: JSON.parse(request.body)})} } }) - ajax({method: "POST", url: "/item", data: {x: ":y"}}).then(function(data) { + xhr({method: "POST", url: "/item", data: {x: ":y"}}).then(function(data) { o(data).deepEquals({a: {x: ":y"}}) }).then(done) }) @@ -81,7 +81,7 @@ o.spec("ajax", function() { return {status: 200, responseText: JSON.stringify({a: request.url, b: request.query})} } }) - ajax({method: "GET", url: "/item/:x", data: {x: "y"}}).then(function(data) { + xhr({method: "GET", url: "/item/:x", data: {x: "y"}}).then(function(data) { o(data).deepEquals({a: "/item/y", b: {}}) }).then(done) }) @@ -91,7 +91,7 @@ o.spec("ajax", function() { return {status: 200, responseText: JSON.stringify({a: request.url, b: JSON.parse(request.body)})} } }) - ajax({method: "POST", url: "/item/:x", data: {x: "y"}}).then(function(data) { + xhr({method: "POST", url: "/item/:x", data: {x: "y"}}).then(function(data) { o(data).deepEquals({a: "/item/y", b: {}}) }).then(done) }) @@ -101,7 +101,7 @@ o.spec("ajax", function() { return {status: 200, responseText: JSON.stringify({a: request.url})} } }) - ajax({method: "GET", url: "/item/:x"}).then(function(data) { + xhr({method: "GET", url: "/item/:x"}).then(function(data) { o(data).deepEquals({a: "/item/:x"}) }).then(done) }) @@ -111,7 +111,7 @@ o.spec("ajax", function() { return {status: 200, responseText: JSON.stringify({a: request.url})} } }) - ajax({method: "GET", url: "/item/:x"}).then(function(data) { + xhr({method: "GET", url: "/item/:x"}).then(function(data) { o(data).deepEquals({a: "/item/:x"}) }).then(done) }) @@ -125,7 +125,7 @@ o.spec("ajax", function() { return {status: 200, responseText: JSON.stringify([{id: 1}, {id: 2}, {id: 3}])} } }) - ajax({method: "GET", url: "/item", type: Entity}).then(function(data) { + xhr({method: "GET", url: "/item", type: Entity}).then(function(data) { o(data).deepEquals([{_id: 1}, {_id: 2}, {_id: 3}]) }).then(done) }) @@ -139,7 +139,7 @@ o.spec("ajax", function() { return {status: 200, responseText: JSON.stringify({id: 1})} } }) - ajax({method: "GET", url: "/item", type: Entity}).then(function(data) { + xhr({method: "GET", url: "/item", type: Entity}).then(function(data) { o(data).deepEquals({_id: 1}) }).then(done) }) @@ -153,7 +153,7 @@ o.spec("ajax", function() { return {status: 200, responseText: JSON.stringify({body: request.query})} } }) - ajax({method: "GET", url: "/item", serialize: serialize, data: {id: 1}}).then(function(data) { + xhr({method: "GET", url: "/item", serialize: serialize, data: {id: 1}}).then(function(data) { o(data.body).equals("?id=1") }).then(done) }) @@ -167,7 +167,7 @@ o.spec("ajax", function() { return {status: 200, responseText: JSON.stringify({body: request.body})} } }) - ajax({method: "POST", url: "/item", serialize: serialize, data: {id: 1}}).then(function(data) { + xhr({method: "POST", url: "/item", serialize: serialize, data: {id: 1}}).then(function(data) { o(data.body).equals("id=1") }).then(done) }) @@ -181,7 +181,7 @@ o.spec("ajax", function() { return {status: 200, responseText: JSON.stringify({test: 123})} } }) - ajax({method: "GET", url: "/item", deserialize: deserialize}).then(function(data) { + xhr({method: "GET", url: "/item", deserialize: deserialize}).then(function(data) { o(data).equals("{\"test\":123}") }).then(done) }) @@ -195,7 +195,7 @@ o.spec("ajax", function() { return {status: 200, responseText: JSON.stringify({test: 123})} } }) - ajax({method: "POST", url: "/item", deserialize: deserialize}).then(function(data) { + xhr({method: "POST", url: "/item", deserialize: deserialize}).then(function(data) { o(data).equals("{\"test\":123}") }).then(done) }) @@ -209,7 +209,7 @@ o.spec("ajax", function() { return {status: 200, responseText: ""} } }) - ajax({method: "GET", url: "/item", extract: extract}).then(function(data) { + xhr({method: "GET", url: "/item", extract: extract}).then(function(data) { o(data).deepEquals({test: 123}) }).then(done) }) @@ -223,7 +223,7 @@ o.spec("ajax", function() { return {status: 200, responseText: ""} } }) - ajax({method: "POST", url: "/item", extract: extract}).then(function(data) { + xhr({method: "POST", url: "/item", extract: extract}).then(function(data) { o(data).deepEquals({test: 123}) }).then(done) }) @@ -233,7 +233,7 @@ o.spec("ajax", function() { return {status: 200, responseText: ""} } }) - ajax({method: "POST", url: "/item", config: config}).then(done) + xhr({method: "POST", url: "/item", config: config}).then(done) function config(xhr) { o(typeof xhr.setRequestHeader).equals("function") @@ -249,7 +249,7 @@ o.spec("ajax", function() { return {status: 500, responseText: JSON.stringify({error: "error"})} } }) - ajax({method: "GET", url: "/item"}).catch(function(e) { + xhr({method: "GET", url: "/item"}).catch(function(e) { o(e.message).equals(JSON.stringify({error: "error"})) }).then(done) }) @@ -259,7 +259,7 @@ o.spec("ajax", function() { return {status: 500, responseText: "error"} } }) - ajax({method: "GET", url: "/item"}).catch(function(e) { + xhr({method: "GET", url: "/item"}).catch(function(e) { o(e.message).equals("error") }).then(done) }) diff --git a/test-utils/tests/test-ajaxMock.js b/test-utils/tests/test-xhrMock.js similarity index 97% rename from test-utils/tests/test-ajaxMock.js rename to test-utils/tests/test-xhrMock.js index 8f5190be..6157b79d 100644 --- a/test-utils/tests/test-ajaxMock.js +++ b/test-utils/tests/test-xhrMock.js @@ -1,13 +1,13 @@ "use strict" var o = require("../../ospec/ospec") -var ajaxMock = require("../../test-utils/ajaxMock") +var xhrMock = require("../../test-utils/xhrMock") var parseQueryString = require("../../querystring/parse") -o.spec("ajaxMock", function() { +o.spec("xhrMock", function() { var $window, ajax o.beforeEach(function() { - $window = ajaxMock() + $window = xhrMock() }) o.spec("xhr", function() { diff --git a/test-utils/ajaxMock.js b/test-utils/xhrMock.js similarity index 100% rename from test-utils/ajaxMock.js rename to test-utils/xhrMock.js