Merge pull request #1548 from hugufc/fix_interpolate

remove mutating of interpolated data
This commit is contained in:
Leo Horie 2017-01-17 23:07:38 -05:00 committed by GitHub
commit 1bb8b48a01
3 changed files with 32 additions and 9 deletions

View file

@ -1,12 +1,15 @@
"use strict"
module.exports = function(object) {
module.exports = function(object, dataKeys) {
if (Object.prototype.toString.call(object) !== "[object Object]") return ""
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])
}
return args.join("&")
function destructure(key, value) {

View file

@ -7,6 +7,7 @@ module.exports = function($window, Promise) {
var oncompletion
function setCompletionCallback(callback) {oncompletion = callback}
function finalizer() {
var count = 0
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.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 dataKeys = args.data && Object.keys(args.data)
args.url = interpolate(args.url, args.data, dataKeys)
if (useBody) args.data = args.serialize(args.data, dataKeys)
else args.url = assemble(args.url, args.data, dataKeys)
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)
@ -122,7 +124,7 @@ module.exports = function($window, Promise) {
return args.background === true? promise : finalize(promise)
}
function interpolate(url, data) {
function interpolate(url, data, dataKeys) {
if (data == null) return url
var tokens = url.match(/:[^\/]+/gi) || []
@ -130,14 +132,20 @@ module.exports = function($window, Promise) {
var key = tokens[i].slice(1)
if (data[key] != null) {
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
}
function assemble(url, data) {
var querystring = buildQueryString(data)
function assemble(url, data, dataKeys) {
var querystring = buildQueryString(data, dataKeys)
if (querystring !== "") {
var prefix = url.indexOf("?") < 0 ? "?" : "&"
url += prefix + querystring

View file

@ -380,6 +380,18 @@ o.spec("xhr", function() {
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("rejects on server error", function(done) {