-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
206 lines (187 loc) · 5.76 KB
/
index.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
'use strict';
// labeled blocks are there for readability
// this file is long and busy, so you can collapse them and see the label
const deepClone = require('../../lib/deep-clone');
const evaluateHooks = require('./evaluate-hooks');
const pipeResource = async ({
requestData,
responseData,
resource,
lenses,
options,
hooks,
}) => {
console.log(': piping request');
let pipedRequestData = deepClone(requestData);
let pipedResponseData = deepClone(responseData);
let pipedResource = deepClone(resource);
let hookErrors = [];
let lenseError = null;
beforeAll: {
const beforeAllReturned = await evaluateHooks({
requestData,
responseData,
resource,
hooks: hooks.beforeAll,
lens: null,
lenses,
});
hookErrors.concat(beforeAllReturned.errors);
if (beforeAllReturned.responseData || beforeAllReturned.resource) {
return {
pipedResponseData: beforeAllReturned.responseData || pipedResponseData,
pipedResource: beforeAllReturned.resource || pipedResource,
hookErrors,
lenseError,
};
}
}
pipingResource: for (const lens of lenses) {
// console.log(': ' + lens.queryKey);
if (!lens.requested) {
continue;
}
beforeEach: {
const beforeEachReturned = await evaluateHooks({
requestData,
responseData,
resource,
hooks: hooks.beforeEach,
lens,
lenses,
});
hookErrors.concat(beforeEachReturned.errors);
if (beforeEachReturned.responseData || beforeEachReturned.resource) {
return {
pipedResponseData:
beforeEachReturned.responseData || pipedResponseData,
pipedResource: beforeEachReturned.resource || pipedResource,
hookErrors,
lenseError,
};
}
}
const config = Object.assign({}, lens);
delete config.module;
let onErrorReturned = {};
let returned = {
requestData: pipedRequestData,
responseData: pipedResponseData,
resource: pipedResource,
};
try {
const config = Object.assign({}, lens);
delete config.module;
console.log(': ' + config.queryKey);
const clonedArgs = {
config,
requestData: deepClone(pipedRequestData),
responseData: deepClone(pipedResponseData),
resource: deepClone(pipedResource),
options: deepClone(options),
lenses: deepClone(lenses),
};
for (const lens of clonedArgs.lenses) {
lens.use = (args = {}) =>
// todo: a deep assign from args to clonedArgs
// easy for modifying ie. a single property of the config
lens.module(Object.assign({}, clonedArgs, { config: lens }, args));
clonedArgs.lenses[lens.queryKey] = lens;
}
returned = await lens.module(clonedArgs);
if (returned && returned.abort === true) {
return {
abort: lens.queryKey,
};
}
// todo: validate objects before cloning
// if they fail validation, use the previous
if (returned) {
pipedRequestData = deepClone(returned.requestData || pipedRequestData);
pipedResponseData = deepClone(
returned.responseData || pipedResponseData,
);
pipedResource = deepClone(returned.resource || pipedResource);
}
} catch (error) {
console.log(': error in ' + lens.queryKey);
lenseError = {
error,
lens,
};
onError: {
onErrorReturned = await evaluateHooks({
requestData: pipedRequestData,
responseData: pipedResponseData,
resource: pipedResource,
hooks: hooks.onError,
lens,
lenses,
error,
});
hookErrors.concat(onErrorReturned.errors);
}
// console.log(onErrorReturned)
if (!onErrorReturned.recover) {
// send no matter what, do not recover from an error in a lens
resource.content = `an error occurred in the "${lens.queryKey}" lens.\n\nadd the --debug option to learn more\n\nor just remove it from the URL and refresh`;
if (resource.info) {
resource.info.ext = '.txt';
} else {
resource.info = { ext: '.txt' };
}
responseData.status = 500;
// in case of a lens error, fall back to the original resource
pipedResponseData = onErrorReturned.responseData || responseData;
pipedResource = onErrorReturned.resource || resource;
break pipingResource;
}
}
afterEach: {
const afterEachReturned = await evaluateHooks({
requestData,
responseData,
resource,
hooks: hooks.afterEach,
lens,
lenses,
});
hookErrors.concat(afterEachReturned.errors);
if (afterEachReturned.responseData || afterEachReturned.resource) {
return {
pipedResponseData:
afterEachReturned.responseData || pipedResponseData,
pipedResource: afterEachReturned.resource || pipedResource,
hookErrors,
lenseError,
};
}
}
}
afterAll: {
const afterAllReturned = await evaluateHooks({
requestData: pipedRequestData,
responseData: pipedResponseData,
resource: pipedResource,
hooks: hooks.afterAll,
lens: null,
lenses,
});
hookErrors.concat(afterAllReturned.errors);
if (afterAllReturned.responseData || afterAllReturned.resource) {
return {
pipedResponseData: afterAllReturned.responseData || pipedResponseData,
pipedResource: afterAllReturned.resource || pipedResource,
hookErrors,
lenseError,
};
}
}
return {
pipedResponseData: pipedResponseData,
pipedResource: pipedResource,
hookErrors,
lenseError,
};
};
module.exports = pipeResource;