forked from cujojs/rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wire.js
71 lines (62 loc) · 1.79 KB
/
wire.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
70
71
/*
* Copyright 2012-2016 the original author or authors
* @license MIT, see LICENSE.txt for details
*
* @author Scott Andrews
*/
'use strict';
var client, when, pipeline, plugin;
client = require('./client/default');
when = require('when');
pipeline = require('when/pipeline');
function normalizeRestFactoryConfig(spec, wire) {
var config = {};
config.parent = wire(spec.parent || client);
config.interceptors = when.all((Array.isArray(spec) ? spec : spec.interceptors || []).map(function (interceptorDef) {
var interceptorConfig = interceptorDef.config;
delete interceptorDef.config;
return when.all([
wire(typeof interceptorDef === 'string' ? { module: interceptorDef } : interceptorDef),
wire(interceptorConfig)
]).spread(function (interceptor, config) {
return { interceptor: interceptor, config: config };
});
}));
return config;
}
/**
* Creates a rest client for the "rest" factory.
* @param resolver
* @param spec
* @param wire
*/
function restFactory(resolver, spec, wire) {
var config = normalizeRestFactoryConfig(spec.rest || spec.options, wire);
return config.parent.then(function (parent) {
return config.interceptors.then(function (interceptorDefs) {
pipeline(interceptorDefs.map(function (interceptorDef) {
return function (parent) {
return interceptorDef.interceptor(parent, interceptorDef.config);
};
}), parent).then(resolver.resolve, resolver.reject);
});
});
}
/**
* The plugin instance. Can be the same for all wiring runs
*/
plugin = {
resolvers: {
client: function () {
throw new Error('rest.js: client! wire reference resolved is deprecated, use \'rest\' facotry instead');
}
},
factories: {
rest: restFactory
}
};
module.exports = {
wire$plugin: function restPlugin(/* ready, destroyed, options */) {
return plugin;
}
};