UNPKG

1.02 kBJavaScriptView Raw
1var isPlainObject = require('x-common-utils/isPlainObject');
2var isFunction = require('x-common-utils/isFunction');
3var hasOwn = require('./hasOwn');
4
5/**
6 * The function to add custom mixins to the instance of `Response` or `ResponseError`.
7 *
8 * @param {Response|ResponseError} target The target to add the custome mixins.
9 * @param {RequestOptions} options The request options.
10 * @param {string} optionName The option name the mixins container.
11 */
12function addMixin(target, options, optionName) {
13 var mixins = options[optionName];
14 var name;
15 var mixin;
16
17 if (isPlainObject(mixins)) {
18 for (name in mixins) {
19 if (hasOwn.call(mixins, name)) {
20 mixin = mixins[name];
21 if (isFunction(mixin)) {
22 if (name in target) {
23 throw new Error('mixin name conflict "' + name + '"');
24 }
25 target[name] = mixin;
26 }
27 }
28 }
29 }
30}
31
32module.exports = addMixin;