remove mutating of interpolate data
This commit is contained in:
parent
b8e8afffa8
commit
4a98bb0503
3 changed files with 32 additions and 9 deletions
|
|
@ -1,12 +1,15 @@
|
||||||
"use strict"
|
"use strict"
|
||||||
|
|
||||||
module.exports = function(object) {
|
module.exports = function(object, dataKeys) {
|
||||||
if (Object.prototype.toString.call(object) !== "[object Object]") return ""
|
if (Object.prototype.toString.call(object) !== "[object Object]") return ""
|
||||||
|
|
||||||
var args = []
|
var args = []
|
||||||
for (var key in object) {
|
dataKeys = dataKeys || Object.keys(object)
|
||||||
|
for (var i = 0; i < dataKeys.length; i++) {
|
||||||
|
var key = dataKeys[i]
|
||||||
destructure(key, object[key])
|
destructure(key, object[key])
|
||||||
}
|
}
|
||||||
|
|
||||||
return args.join("&")
|
return args.join("&")
|
||||||
|
|
||||||
function destructure(key, value) {
|
function destructure(key, value) {
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ module.exports = function($window, Promise) {
|
||||||
|
|
||||||
var oncompletion
|
var oncompletion
|
||||||
function setCompletionCallback(callback) {oncompletion = callback}
|
function setCompletionCallback(callback) {oncompletion = callback}
|
||||||
|
|
||||||
function finalizer() {
|
function finalizer() {
|
||||||
var count = 0
|
var count = 0
|
||||||
function complete() {if (--count === 0 && typeof oncompletion === "function") oncompletion()}
|
function complete() {if (--count === 0 && typeof oncompletion === "function") oncompletion()}
|
||||||
|
|
@ -48,9 +49,10 @@ module.exports = function($window, Promise) {
|
||||||
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)
|
var dataKeys = args.data && Object.keys(args.data)
|
||||||
if (useBody) args.data = args.serialize(args.data)
|
args.url = interpolate(args.url, args.data, dataKeys)
|
||||||
else args.url = assemble(args.url, args.data)
|
if (useBody) args.data = args.serialize(args.data, dataKeys)
|
||||||
|
else args.url = assemble(args.url, args.data, dataKeys)
|
||||||
|
|
||||||
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)
|
||||||
|
|
@ -122,7 +124,7 @@ module.exports = function($window, Promise) {
|
||||||
return args.background === true? promise : finalize(promise)
|
return args.background === true? promise : finalize(promise)
|
||||||
}
|
}
|
||||||
|
|
||||||
function interpolate(url, data) {
|
function interpolate(url, data, dataKeys) {
|
||||||
if (data == null) return url
|
if (data == null) return url
|
||||||
|
|
||||||
var tokens = url.match(/:[^\/]+/gi) || []
|
var tokens = url.match(/:[^\/]+/gi) || []
|
||||||
|
|
@ -130,14 +132,20 @@ module.exports = function($window, Promise) {
|
||||||
var key = tokens[i].slice(1)
|
var key = tokens[i].slice(1)
|
||||||
if (data[key] != null) {
|
if (data[key] != null) {
|
||||||
url = url.replace(tokens[i], data[key])
|
url = url.replace(tokens[i], data[key])
|
||||||
delete data[key]
|
|
||||||
|
if (Array.isArray(dataKeys)) {
|
||||||
|
var keyIndex = dataKeys.indexOf(key)
|
||||||
|
if (keyIndex > -1) {
|
||||||
|
dataKeys.splice(keyIndex)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return url
|
return url
|
||||||
}
|
}
|
||||||
|
|
||||||
function assemble(url, data) {
|
function assemble(url, data, dataKeys) {
|
||||||
var querystring = buildQueryString(data)
|
var querystring = buildQueryString(data, dataKeys)
|
||||||
if (querystring !== "") {
|
if (querystring !== "") {
|
||||||
var prefix = url.indexOf("?") < 0 ? "?" : "&"
|
var prefix = url.indexOf("?") < 0 ? "?" : "&"
|
||||||
url += prefix + querystring
|
url += prefix + querystring
|
||||||
|
|
|
||||||
|
|
@ -380,6 +380,18 @@ o.spec("xhr", function() {
|
||||||
o(xhr.getRequestHeader("Accept")).equals("application/json, text/*")
|
o(xhr.getRequestHeader("Accept")).equals("application/json, text/*")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
o("data maintains after interpolate", function() {
|
||||||
|
mock.$defineRoutes({
|
||||||
|
"PUT /items/:x": function() {
|
||||||
|
return {status: 200, responseText: ""}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
var data = {x: 1, y: 2}
|
||||||
|
var dataCopy = Object.assign({}, data);
|
||||||
|
xhr({method: "PUT", url: "/items/:x", data})
|
||||||
|
|
||||||
|
o(data).deepEquals(dataCopy)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
o.spec("failure", function() {
|
o.spec("failure", function() {
|
||||||
o("rejects on server error", function(done) {
|
o("rejects on server error", function(done) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue