/
seq.js
63 lines (50 loc) · 2.08 KB
/
seq.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
// FEATURES DEMONSTRATED:
// sequential calls
// optional custom handlers after each call
// adding custom headers to one API call at run time
// manipulating response data from first call
// using output from first call as input to the next call
// add 3rd sequential call at run time in handler for second call - could be conditional
module.exports = function(app) {
return {
// routes can be a string, RegExp or array of either (to match multiple routes)
route: ['/json/seq'],
// use array for sequential calls, object for parallel
// these are set at bootup time
apiCalls: [
{path: '/api/cms/home', handler: 'cmsHandler'},
{path: '/api/getdata/kitchensink', handler: 'kitchenSinkHandler'},
],
// called before any API calls are made
preProcessor: function(req, res) {
this.debug('preProcessor called');
// demonstrate adding custom headers to one call at run time
this.apiCalls[1].customHeaders = [{name: 'x-test', value: 'success'}];
},
// handler called after 1st sequential call
cmsHandler: function(apiResponse, req, res) {
this.debug('cmsHandler!!!');
// demonstrate manipulating call response data
apiResponse.testSequential = 'xxx';
// demonstrate input to second call from output of first call
this.apiCalls[1].params = {userId: apiResponse.userId};
},
// handler called after 2nd sequential call
kitchenSinkHandler: function(apiResponse, req, res) {
this.debug('kitchenSinkHandler!!!');
apiResponse.testSequential2 = 'yyyy';
// demonstrate pushing another sequential call onto the stack at run time
this.apiCalls.push({path: '/api/getdata/somecall', params: {userId2: 'zzzzz'}});
},
// called after all API calls return
postProcessor: function(req, res) {
this.debug('postProcessor called');
// return data from all calls - this will change to res.locals.data1, etc.
res.locals.responseData = {
data1: res.locals.data1,
data2: res.locals.data2,
data3: res.locals.data3,
};
}
};
};