Commit 49ba9a70 authored by Sean Hammond's avatar Sean Hammond Committed by GitHub

Merge pull request #19 from hypothesis/remove-prepend-transform

Simplify request preparation in `store.js`
parents 52b4235d a2d9b07b
'use strict';
var angular = require('angular');
var get = require('lodash.get');
var retryUtil = require('./retry-util');
var urlUtil = require('./util/url-util');
function prependTransform(defaults, transform) {
// We can't guarantee that the default transformation is an array
var result = angular.isArray(defaults) ? defaults.slice(0) : [defaults];
result.unshift(transform);
return result;
}
// stripInternalProperties returns a shallow clone of `obj`, lacking all
// properties that begin with a character that marks them as internal
// (currently '$' or '_');
/**
* Return a shallow clone of `obj` with all client-only properties removed.
* Client-only properties are marked by a '$' prefix.
*/
function stripInternalProperties(obj) {
var result = {};
......@@ -39,8 +32,8 @@ function forEachSorted(obj, iterator, context) {
function serializeValue(v) {
if (angular.isObject(v)) {
return angular.isDate(v) ? v.toISOString() : angular.toJson(v);
if (typeof v === 'object') {
return v instanceof Date ? v.toISOString() : JSON.stringify(v);
}
return v;
}
......@@ -69,8 +62,8 @@ function serializeParams(params) {
if (value === null || typeof value === 'undefined') {
return;
}
if (angular.isArray(value)) {
angular.forEach(value, function(v, k) {
if (Array.isArray(value)) {
value.forEach(function(v) {
parts.push(encodeUriQuery(key) + '=' + encodeUriQuery(serializeValue(v)));
});
} else {
......@@ -95,15 +88,11 @@ function createAPICall($http, links, route) {
var descriptor = get(links, route);
var url = urlUtil.replaceURLParams(descriptor.url, params);
var req = {
data: data,
data: data ? stripInternalProperties(data) : null,
method: descriptor.method,
params: url.params,
paramSerializer: serializeParams,
url: url.url,
transformRequest: prependTransform(
$http.defaults.transformRequest,
stripInternalProperties
),
};
return $http(req);
}).then(function (result) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment