forked from bsmayer/cluer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest-processor.js
178 lines (151 loc) · 4.35 KB
/
request-processor.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const axios = require('axios');
const { translate, resolvePath } = require('./json-path-translator');
async function processRequest(endpoint, params, body) {
const handlers = endpoint.lifecycleHandlers ? require(endpoint.lifecycleHandlers) : null;
// Lifecycle execution - beforeAnyBackendRequest
endpoint = await callLifecycleMethod({
module: handlers,
method: 'beforeAnyBackendRequest',
context: endpoint
}) || endpoint;
let backendResponses = {};
const { backends, response } = endpoint;
await Promise.all(backends.map(async backend => {
try {
// Lifecycle execution - beforeBackendRequest
backend = await callLifecycleMethod({
module: handlers,
method: 'beforeBackendRequest',
node: backend.key,
context: backend
}) || backend;
// Handle path params
if (backend.path.includes(':')) {
const paths = backend.path.split('/');
if (paths && paths.length) {
for (const parameter of paths.filter(x => x.startsWith(':', 0))) {
const paramName = parameter.substring(1);
backend.path = backend.path.replace(new RegExp(parameter, 'g'), params[paramName]);
}
}
}
// Handle querystrings
const querystring = {};
if (backend.querystring && backend.querystring.length) {
for (const query of backend.querystring) {
if (params[query])
querystring[query] = params[query];
}
}
// Handle body
let payload;
if (backend.body) {
if (typeof backend.body === 'string' && backend.body === '$body') {
payload = body;
}
else if (typeof backend.body === 'string' && backend.body !== '$body') {
payload = resolvePath({ $body: body }, backend.body);
}
else if (typeof backend.body === 'object' && Object.keys(backend.body).length > 0) {
payload = {};
for (const entry of Object.entries(backend.body)) {
translate({
backendResponses: { $body: body },
dest: payload,
entry
});
}
}
}
// Create API request
const request = await axios({
method: backend.method,
url: `${backend.host}${backend.path}`,
query: querystring,
data: payload
});
let backendResponse = request.data;
// Lifecycle execution - afterBackendRequest
backendResponse = await callLifecycleMethod({
module: handlers,
method: 'afterBackendRequest',
node: backend.key,
context: backendResponse
}) || backendResponse;
return backendResponses['$' + backend.key] = backendResponse;
}
catch(error) {
let responseError = error.response && error.response.data ? error.response.data : error.message;
// Lifecycle execution - onBackendRequestError
responseError = await callLifecycleMethod({
module: handlers,
method: 'onBackendRequestError',
node: backend.key,
context: responseError
}) || responseError;
return Promise.reject(responseError);
}
})).catch(async error => {
// Lifecycle execution - onAnyBackendRequestError
error = await callLifecycleMethod({
module: handlers,
method: 'onAnyBackendRequestError',
context: error
}) || error;
return Promise.reject(error);
});
// Lifecycle execution - afterAllBackendRequests
backendResponses = await callLifecycleMethod({
module: handlers,
method: 'afterAllBackendRequests',
context: backendResponses
}) || backendResponses;
let dest = {};
// Place all objects into the root of destination object
if (Array.isArray(response.root)) {
for(const root of response.root) {
const backendResponse = backendResponses[root];
for(const [key, value] of Object.entries(backendResponse)) {
dest[key] = value
}
}
if (response.set) {
for (const entry of Object.entries(response.set)) {
translate({
backendResponses,
dest,
entry
});
}
}
}
else {
for (const entry of Object.entries(response.root)) {
translate({
backendResponses,
dest,
entry
});
}
}
// lifecycle execution - onFinishMappingResponse
dest = await callLifecycleMethod({
module: handlers,
method: 'onFinishMappingResponse',
context: dest
}) || dest;
return Promise.resolve(dest);
}
async function callLifecycleMethod({ module, method, node, context }) {
if (!module || !method)
return;
let fn;
if (node && module[node])
fn = module[node][method];
else if (module[method])
fn = module[method];
if (!fn)
return;
return await fn(context);
}
module.exports = { processRequest };