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'; 'use strict';
var angular = require('angular');
var get = require('lodash.get'); var get = require('lodash.get');
var retryUtil = require('./retry-util'); var retryUtil = require('./retry-util');
var urlUtil = require('./util/url-util'); var urlUtil = require('./util/url-util');
function prependTransform(defaults, transform) { /**
// We can't guarantee that the default transformation is an array * Return a shallow clone of `obj` with all client-only properties removed.
var result = angular.isArray(defaults) ? defaults.slice(0) : [defaults]; * Client-only properties are marked by a '$' prefix.
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 '_');
function stripInternalProperties(obj) { function stripInternalProperties(obj) {
var result = {}; var result = {};
...@@ -39,8 +32,8 @@ function forEachSorted(obj, iterator, context) { ...@@ -39,8 +32,8 @@ function forEachSorted(obj, iterator, context) {
function serializeValue(v) { function serializeValue(v) {
if (angular.isObject(v)) { if (typeof v === 'object') {
return angular.isDate(v) ? v.toISOString() : angular.toJson(v); return v instanceof Date ? v.toISOString() : JSON.stringify(v);
} }
return v; return v;
} }
...@@ -69,8 +62,8 @@ function serializeParams(params) { ...@@ -69,8 +62,8 @@ function serializeParams(params) {
if (value === null || typeof value === 'undefined') { if (value === null || typeof value === 'undefined') {
return; return;
} }
if (angular.isArray(value)) { if (Array.isArray(value)) {
angular.forEach(value, function(v, k) { value.forEach(function(v) {
parts.push(encodeUriQuery(key) + '=' + encodeUriQuery(serializeValue(v))); parts.push(encodeUriQuery(key) + '=' + encodeUriQuery(serializeValue(v)));
}); });
} else { } else {
...@@ -95,15 +88,11 @@ function createAPICall($http, links, route) { ...@@ -95,15 +88,11 @@ function createAPICall($http, links, route) {
var descriptor = get(links, route); var descriptor = get(links, route);
var url = urlUtil.replaceURLParams(descriptor.url, params); var url = urlUtil.replaceURLParams(descriptor.url, params);
var req = { var req = {
data: data, data: data ? stripInternalProperties(data) : null,
method: descriptor.method, method: descriptor.method,
params: url.params, params: url.params,
paramSerializer: serializeParams, paramSerializer: serializeParams,
url: url.url, url: url.url,
transformRequest: prependTransform(
$http.defaults.transformRequest,
stripInternalProperties
),
}; };
return $http(req); return $http(req);
}).then(function (result) { }).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