This repository has been archived by the owner on Jun 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 146
/
defaultRequest.js
69 lines (59 loc) · 1.83 KB
/
defaultRequest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* Copyright 2013-2016 the original author or authors
* @license MIT, see LICENSE.txt for details
*
* @author Scott Andrews
*/
'use strict';
var interceptor, mixinUtil, defaulter;
interceptor = require('../interceptor');
mixinUtil = require('../util/mixin');
defaulter = (function () {
function mixin(prop, target, defaults) {
if (prop in target || prop in defaults) {
target[prop] = mixinUtil({}, defaults[prop], target[prop]);
}
}
function copy(prop, target, defaults) {
if (prop in defaults && !(prop in target)) {
target[prop] = defaults[prop];
}
}
var mappings = {
method: copy,
path: copy,
params: mixin,
headers: mixin,
entity: copy,
mixin: mixin
};
return function (target, defaults) {
for (var prop in mappings) {
/*jshint forin: false */
mappings[prop](prop, target, defaults);
}
return target;
};
}());
/**
* Provide default values for a request. These values will be applied to the
* request if the request object does not already contain an explicit value.
*
* For 'params', 'headers', and 'mixin', individual values are mixed in with the
* request's values. The result is a new object representiing the combined
* request and config values. Neither input object is mutated.
*
* @param {Client} [client] client to wrap
* @param {string} [config.method] the default method
* @param {string} [config.path] the default path
* @param {Object} [config.params] the default params, mixed with the request's existing params
* @param {Object} [config.headers] the default headers, mixed with the request's existing headers
* @param {Object} [config.mixin] the default "mixins" (http/https options), mixed with the request's existing "mixins"
*
* @returns {Client}
*/
module.exports = interceptor({
request: function handleRequest(request, config) {
return defaulter(request, config);
}
});