@opra/core 0.25.5 → 0.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/augmentation/container.augmentation.js +2 -0
- package/cjs/http/adapters/express-adapter.host.js +34 -0
- package/cjs/http/{express-adapter.js → adapters/express-adapter.js} +1 -3
- package/cjs/http/{http-adapter.host.js → adapters/node-http-adapter.host.js} +30 -22
- package/cjs/http/adapters/node-http-adapter.js +14 -0
- package/cjs/http/helpers/json-body-loader.js +29 -0
- package/cjs/http/http-adapter-host.js +678 -0
- package/cjs/index.js +4 -3
- package/cjs/platform-adapter.host.js +74 -45
- package/cjs/{endpoint-context.js → request-context.js} +5 -5
- package/cjs/request.host.js +3 -0
- package/esm/augmentation/container.augmentation.js +1 -0
- package/esm/http/adapters/express-adapter.host.js +30 -0
- package/esm/http/{express-adapter.js → adapters/express-adapter.js} +1 -3
- package/esm/http/{http-adapter.host.js → adapters/node-http-adapter.host.js} +28 -20
- package/esm/http/adapters/node-http-adapter.js +11 -0
- package/esm/http/helpers/json-body-loader.js +24 -0
- package/esm/http/http-adapter-host.js +673 -0
- package/esm/index.js +4 -3
- package/esm/platform-adapter.host.js +75 -46
- package/esm/{endpoint-context.js → request-context.js} +4 -4
- package/esm/request.host.js +3 -0
- package/i18n/en/error.json +1 -2
- package/package.json +3 -3
- package/types/augmentation/collection.augmentation.d.ts +19 -16
- package/types/augmentation/container.augmentation.d.ts +13 -0
- package/types/augmentation/resource.augmentation.d.ts +2 -2
- package/types/augmentation/singleton.augmentation.d.ts +13 -9
- package/types/augmentation/storage.augmentation.d.ts +11 -14
- package/types/http/{express-adapter.d.ts → adapters/express-adapter.d.ts} +3 -3
- package/types/http/adapters/express-adapter.host.d.ts +12 -0
- package/types/http/{http-adapter.d.ts → adapters/node-http-adapter.d.ts} +5 -5
- package/types/http/adapters/node-http-adapter.host.d.ts +19 -0
- package/types/http/helpers/json-body-loader.d.ts +5 -0
- package/types/http/http-adapter-host.d.ts +34 -0
- package/types/index.d.ts +4 -3
- package/types/interfaces/request-handler.interface.d.ts +1 -1
- package/types/platform-adapter.d.ts +2 -2
- package/types/platform-adapter.host.d.ts +18 -14
- package/types/{endpoint-context.d.ts → request-context.d.ts} +3 -3
- package/types/request.d.ts +7 -2
- package/types/request.host.d.ts +5 -2
- package/cjs/http/express-adapter.host.js +0 -24
- package/cjs/http/http-adapter-base.js +0 -138
- package/cjs/http/http-adapter.js +0 -16
- package/cjs/http/request-handlers/entity-request-handler.js +0 -429
- package/cjs/http/request-handlers/parse-batch-request.js +0 -169
- package/cjs/http/request-handlers/request-handler-base.js +0 -37
- package/cjs/http/request-handlers/storage-request-handler.js +0 -139
- package/esm/http/express-adapter.host.js +0 -20
- package/esm/http/http-adapter-base.js +0 -134
- package/esm/http/http-adapter.js +0 -13
- package/esm/http/request-handlers/entity-request-handler.js +0 -424
- package/esm/http/request-handlers/parse-batch-request.js +0 -169
- package/esm/http/request-handlers/request-handler-base.js +0 -33
- package/esm/http/request-handlers/storage-request-handler.js +0 -134
- package/types/http/express-adapter.host.d.ts +0 -11
- package/types/http/http-adapter-base.d.ts +0 -23
- package/types/http/http-adapter.host.d.ts +0 -18
- package/types/http/request-handlers/entity-request-handler.d.ts +0 -24
- package/types/http/request-handlers/parse-batch-request.d.ts +0 -0
- package/types/http/request-handlers/request-handler-base.d.ts +0 -16
- package/types/http/request-handlers/storage-request-handler.d.ts +0 -23
|
@@ -0,0 +1,678 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpAdapterHost = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const promises_1 = tslib_1.__importDefault(require("fs/promises"));
|
|
6
|
+
const os_1 = tslib_1.__importDefault(require("os"));
|
|
7
|
+
const valgen = tslib_1.__importStar(require("valgen"));
|
|
8
|
+
const common_1 = require("@opra/common");
|
|
9
|
+
const execution_context_host_js_1 = require("../execution-context.host.js");
|
|
10
|
+
const platform_adapter_host_js_1 = require("../platform-adapter.host.js");
|
|
11
|
+
const request_host_js_1 = require("../request.host.js");
|
|
12
|
+
const request_context_js_1 = require("../request-context.js");
|
|
13
|
+
const response_host_js_1 = require("../response.host.js");
|
|
14
|
+
const json_body_loader_js_1 = require("./helpers/json-body-loader.js");
|
|
15
|
+
const multipart_helper_js_1 = require("./helpers/multipart-helper.js");
|
|
16
|
+
/**
|
|
17
|
+
*
|
|
18
|
+
* @class HttpAdapterHost
|
|
19
|
+
*/
|
|
20
|
+
class HttpAdapterHost extends platform_adapter_host_js_1.PlatformAdapterHost {
|
|
21
|
+
constructor() {
|
|
22
|
+
super(...arguments);
|
|
23
|
+
this._protocol = 'http';
|
|
24
|
+
this._tempDir = os_1.default.tmpdir();
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Main http request handler
|
|
28
|
+
* @param incoming
|
|
29
|
+
* @param outgoing
|
|
30
|
+
* @protected
|
|
31
|
+
*/
|
|
32
|
+
async handleHttp(incoming, outgoing) {
|
|
33
|
+
const context = new execution_context_host_js_1.ExecutionContextHost(this.api, this.platform, { http: { incoming, outgoing } });
|
|
34
|
+
try {
|
|
35
|
+
try {
|
|
36
|
+
/* istanbul ignore next */
|
|
37
|
+
if (!this._api)
|
|
38
|
+
throw new common_1.InternalServerError(`${Object.getPrototypeOf(this).constructor.name} has not been initialized yet`);
|
|
39
|
+
outgoing.setHeader(common_1.HttpHeaderCodes.X_Opra_Version, common_1.OpraSchema.SpecVersion);
|
|
40
|
+
// Expose headers if cors enabled
|
|
41
|
+
if (outgoing.getHeader(common_1.HttpHeaderCodes.Access_Control_Allow_Origin)) {
|
|
42
|
+
// Expose X-Opra-* headers
|
|
43
|
+
outgoing.appendHeader(common_1.HttpHeaderCodes.Access_Control_Expose_Headers, Object.values(common_1.HttpHeaderCodes)
|
|
44
|
+
.filter(k => k.toLowerCase().startsWith('x-opra-')));
|
|
45
|
+
}
|
|
46
|
+
const { parsedUrl } = incoming;
|
|
47
|
+
if (!parsedUrl.path.length) {
|
|
48
|
+
if (incoming.method === 'GET') {
|
|
49
|
+
outgoing.setHeader('content-type', 'application/json');
|
|
50
|
+
outgoing.end(JSON.stringify(this.api.exportSchema({ webSafe: true })));
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
// Process Batch
|
|
54
|
+
if (incoming.method === 'POST' && incoming.headers['content-type'] === 'multipart/mixed') {
|
|
55
|
+
// todo Process Batch
|
|
56
|
+
}
|
|
57
|
+
throw new common_1.BadRequestError();
|
|
58
|
+
}
|
|
59
|
+
let i = 0;
|
|
60
|
+
let requestProcessed = false;
|
|
61
|
+
const next = async () => {
|
|
62
|
+
const interceptor = this._interceptors[i++];
|
|
63
|
+
if (interceptor) {
|
|
64
|
+
await interceptor(context, next);
|
|
65
|
+
await next();
|
|
66
|
+
}
|
|
67
|
+
else if (!requestProcessed) {
|
|
68
|
+
requestProcessed = true;
|
|
69
|
+
await this.handleExecution(context);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
await next();
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
context.errors.push((0, common_1.wrapException)(error));
|
|
76
|
+
}
|
|
77
|
+
// If no response returned to the client we send an error
|
|
78
|
+
if (!outgoing.writableEnded) {
|
|
79
|
+
if (!context.errors.length)
|
|
80
|
+
context.errors.push(new common_1.BadRequestError(`Server can not process this request`));
|
|
81
|
+
await this.handleError(context);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
finally {
|
|
85
|
+
await context.emitAsync('finish');
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async handleExecution(executionContext) {
|
|
89
|
+
// Parse incoming message and create Request object
|
|
90
|
+
const request = await this.parseRequest(executionContext);
|
|
91
|
+
const { outgoing } = executionContext.switchToHttp();
|
|
92
|
+
const response = new response_host_js_1.ResponseHost({ http: outgoing });
|
|
93
|
+
const context = request_context_js_1.RequestContext.from(executionContext, request, response);
|
|
94
|
+
await this.executeRequest(context);
|
|
95
|
+
if (response.errors.length) {
|
|
96
|
+
context.errors.push(...response.errors);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
try {
|
|
100
|
+
await this.sendResponse(context);
|
|
101
|
+
}
|
|
102
|
+
catch (e) {
|
|
103
|
+
if (e instanceof common_1.OpraException)
|
|
104
|
+
throw e;
|
|
105
|
+
if (e instanceof valgen.ValidationError) {
|
|
106
|
+
throw new common_1.InternalServerError({
|
|
107
|
+
message: (0, common_1.translate)('error:RESPONSE_VALIDATION,', 'Response validation failed'),
|
|
108
|
+
code: 'RESPONSE_VALIDATION',
|
|
109
|
+
details: e.issues
|
|
110
|
+
}, e);
|
|
111
|
+
}
|
|
112
|
+
throw new common_1.InternalServerError(e);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async parseRequest(executionContext) {
|
|
116
|
+
const { incoming } = executionContext.switchToHttp();
|
|
117
|
+
const parsedUrl = new common_1.OpraURL(incoming.url);
|
|
118
|
+
let i = 0;
|
|
119
|
+
let p;
|
|
120
|
+
let resource = this.api.root;
|
|
121
|
+
let request;
|
|
122
|
+
// Walk through container
|
|
123
|
+
while (resource instanceof common_1.Container) {
|
|
124
|
+
p = parsedUrl.path[i];
|
|
125
|
+
const r = resource.resources.get(p.resource);
|
|
126
|
+
if (r) {
|
|
127
|
+
resource = r;
|
|
128
|
+
if (resource instanceof common_1.Container) {
|
|
129
|
+
i++;
|
|
130
|
+
}
|
|
131
|
+
else
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
else
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
const urlPath = i > 0 ? parsedUrl.path.slice(i) : parsedUrl.path;
|
|
138
|
+
const searchParams = parsedUrl.searchParams;
|
|
139
|
+
// If there is one more element in the path it may be an action
|
|
140
|
+
if (resource instanceof common_1.Container) {
|
|
141
|
+
if (urlPath.length === 1 && resource.actions.has(urlPath[0].resource)) {
|
|
142
|
+
request = await this._parseRequestAction(executionContext, resource, urlPath, searchParams);
|
|
143
|
+
if (request)
|
|
144
|
+
return request;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
else if (urlPath.length === 2 && resource.actions.has(urlPath[1].resource)) {
|
|
148
|
+
request = await this._parseRequestAction(executionContext, resource, urlPath.slice(1), searchParams);
|
|
149
|
+
if (request)
|
|
150
|
+
return request;
|
|
151
|
+
}
|
|
152
|
+
if (resource instanceof common_1.Storage)
|
|
153
|
+
request = await this._parseRequestStorage(executionContext, resource, urlPath.slice(1), searchParams);
|
|
154
|
+
else if (urlPath.length === 1) { // Collection and Singleton resources should be last element in path
|
|
155
|
+
if (resource instanceof common_1.Collection)
|
|
156
|
+
request = await this._parseRequestCollection(executionContext, resource, urlPath, searchParams);
|
|
157
|
+
else if (resource instanceof common_1.Singleton)
|
|
158
|
+
request = await this._parseRequestSingleton(executionContext, resource, urlPath, searchParams);
|
|
159
|
+
}
|
|
160
|
+
if (request)
|
|
161
|
+
return request;
|
|
162
|
+
const path = urlPath.toString();
|
|
163
|
+
throw new common_1.BadRequestError({
|
|
164
|
+
message: 'No resource or endpoint found at ' + path,
|
|
165
|
+
details: { path }
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
async _parseRequestAction(executionContext, resource, urlPath, searchParams) {
|
|
169
|
+
const p = urlPath[0];
|
|
170
|
+
const { controller, endpoint, handler } = await this.getOperationHandler(resource, p.resource);
|
|
171
|
+
const { incoming } = executionContext.switchToHttp();
|
|
172
|
+
const contentId = incoming.headers['content-id'];
|
|
173
|
+
const params = this.parseParameters(endpoint.parameters, p, searchParams);
|
|
174
|
+
return new request_host_js_1.RequestHost({
|
|
175
|
+
endpoint,
|
|
176
|
+
operation: 'action',
|
|
177
|
+
action: p.resource,
|
|
178
|
+
controller,
|
|
179
|
+
handler,
|
|
180
|
+
http: incoming,
|
|
181
|
+
contentId,
|
|
182
|
+
params
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
async _parseRequestCollection(executionContext, resource, urlPath, searchParams) {
|
|
186
|
+
const { incoming } = executionContext.switchToHttp();
|
|
187
|
+
if ((incoming.method === 'POST' || incoming.method === 'PATCH') && !incoming.is('json'))
|
|
188
|
+
throw new common_1.BadRequestError({ message: 'Unsupported Content-Type' });
|
|
189
|
+
const contentId = incoming.headers['content-id'];
|
|
190
|
+
const p = urlPath[0];
|
|
191
|
+
switch (incoming.method) {
|
|
192
|
+
case 'POST': {
|
|
193
|
+
if (p.key == null) {
|
|
194
|
+
const { controller, endpoint, handler } = await this.getOperationHandler(resource, 'create');
|
|
195
|
+
const jsonReader = (0, json_body_loader_js_1.jsonBodyLoader)({ limit: endpoint.inputMaxContentSize }, endpoint);
|
|
196
|
+
let data = await jsonReader(incoming);
|
|
197
|
+
data = endpoint.decode(data, { coerce: true });
|
|
198
|
+
const params = this.parseParameters(endpoint.parameters, p, searchParams);
|
|
199
|
+
return new request_host_js_1.RequestHost({
|
|
200
|
+
endpoint,
|
|
201
|
+
operation: 'create',
|
|
202
|
+
controller,
|
|
203
|
+
handler,
|
|
204
|
+
http: incoming,
|
|
205
|
+
contentId,
|
|
206
|
+
data,
|
|
207
|
+
params: {
|
|
208
|
+
...params,
|
|
209
|
+
pick: params.pick && resource.normalizeFieldPath(params.pick),
|
|
210
|
+
omit: params.omit && resource.normalizeFieldPath(params.omit),
|
|
211
|
+
include: params.include && resource.normalizeFieldPath(params.include)
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
case 'DELETE': {
|
|
218
|
+
if (p.key != null) {
|
|
219
|
+
const { controller, endpoint, handler } = await this.getOperationHandler(resource, 'delete');
|
|
220
|
+
const params = this.parseParameters(endpoint.parameters, p, searchParams);
|
|
221
|
+
return new request_host_js_1.RequestHost({
|
|
222
|
+
endpoint,
|
|
223
|
+
operation: 'delete',
|
|
224
|
+
controller,
|
|
225
|
+
handler,
|
|
226
|
+
http: incoming,
|
|
227
|
+
contentId,
|
|
228
|
+
key: resource.parseKeyValue(p.key),
|
|
229
|
+
params
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
const { controller, endpoint, handler } = await this.getOperationHandler(resource, 'deleteMany');
|
|
233
|
+
const params = this.parseParameters(endpoint.parameters, p, searchParams);
|
|
234
|
+
return new request_host_js_1.RequestHost({
|
|
235
|
+
endpoint,
|
|
236
|
+
operation: 'deleteMany',
|
|
237
|
+
controller,
|
|
238
|
+
handler,
|
|
239
|
+
http: incoming,
|
|
240
|
+
contentId,
|
|
241
|
+
params: {
|
|
242
|
+
...params,
|
|
243
|
+
filter: params.filter && resource.normalizeFilter(params.filter)
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
case 'GET': {
|
|
248
|
+
if (p.key != null) {
|
|
249
|
+
const { controller, endpoint, handler } = await this.getOperationHandler(resource, 'get');
|
|
250
|
+
const params = this.parseParameters(endpoint.parameters, p, searchParams);
|
|
251
|
+
return new request_host_js_1.RequestHost({
|
|
252
|
+
endpoint,
|
|
253
|
+
operation: 'get',
|
|
254
|
+
controller,
|
|
255
|
+
handler,
|
|
256
|
+
http: incoming,
|
|
257
|
+
contentId,
|
|
258
|
+
key: resource.parseKeyValue(p.key),
|
|
259
|
+
params: {
|
|
260
|
+
...params,
|
|
261
|
+
pick: params.pick && resource.normalizeFieldPath(params.pick),
|
|
262
|
+
omit: params.omit && resource.normalizeFieldPath(params.omit),
|
|
263
|
+
include: params.include && resource.normalizeFieldPath(params.include)
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
const { controller, endpoint, handler } = await this.getOperationHandler(resource, 'findMany');
|
|
268
|
+
const params = this.parseParameters(endpoint.parameters, p, searchParams);
|
|
269
|
+
return new request_host_js_1.RequestHost({
|
|
270
|
+
endpoint,
|
|
271
|
+
operation: 'findMany',
|
|
272
|
+
controller,
|
|
273
|
+
handler,
|
|
274
|
+
http: incoming,
|
|
275
|
+
contentId,
|
|
276
|
+
params: {
|
|
277
|
+
...params,
|
|
278
|
+
pick: params.pick && resource.normalizeFieldPath(params.pick),
|
|
279
|
+
omit: params.omit && resource.normalizeFieldPath(params.omit),
|
|
280
|
+
include: params.include && resource.normalizeFieldPath(params.include),
|
|
281
|
+
sort: params.sort && resource.normalizeSortFields(params.sort),
|
|
282
|
+
filter: params.filter && resource.normalizeFilter(params.filter)
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
case 'PATCH': {
|
|
287
|
+
if (p.key != null) {
|
|
288
|
+
const { controller, endpoint, handler } = await this.getOperationHandler(resource, 'update');
|
|
289
|
+
const jsonReader = (0, json_body_loader_js_1.jsonBodyLoader)({ limit: endpoint.inputMaxContentSize }, endpoint);
|
|
290
|
+
let data = await jsonReader(incoming);
|
|
291
|
+
data = endpoint.decode(data, { coerce: true });
|
|
292
|
+
const params = this.parseParameters(endpoint.parameters, p, searchParams);
|
|
293
|
+
return new request_host_js_1.RequestHost({
|
|
294
|
+
endpoint,
|
|
295
|
+
operation: 'update',
|
|
296
|
+
controller,
|
|
297
|
+
handler,
|
|
298
|
+
http: incoming,
|
|
299
|
+
contentId,
|
|
300
|
+
key: resource.parseKeyValue(p.key),
|
|
301
|
+
data,
|
|
302
|
+
params: {
|
|
303
|
+
...params,
|
|
304
|
+
pick: params.pick && resource.normalizeFieldPath(params.pick),
|
|
305
|
+
omit: params.omit && resource.normalizeFieldPath(params.omit),
|
|
306
|
+
include: params.include && resource.normalizeFieldPath(params.include),
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
const { controller, endpoint, handler } = await this.getOperationHandler(resource, 'updateMany');
|
|
311
|
+
const jsonReader = (0, json_body_loader_js_1.jsonBodyLoader)({ limit: endpoint.inputMaxContentSize }, endpoint);
|
|
312
|
+
let data = await jsonReader(incoming);
|
|
313
|
+
data = endpoint.decode(data, { coerce: true });
|
|
314
|
+
const params = this.parseParameters(endpoint.parameters, p, searchParams);
|
|
315
|
+
return new request_host_js_1.RequestHost({
|
|
316
|
+
endpoint,
|
|
317
|
+
operation: 'updateMany',
|
|
318
|
+
controller,
|
|
319
|
+
handler,
|
|
320
|
+
http: incoming,
|
|
321
|
+
contentId,
|
|
322
|
+
data,
|
|
323
|
+
params: {
|
|
324
|
+
...params,
|
|
325
|
+
filter: params.filter && resource.normalizeFilter(params.filter)
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
throw new common_1.MethodNotAllowedError({
|
|
331
|
+
message: `Collection resource doesn't accept http "${incoming.method}" method`
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
async _parseRequestSingleton(executionContext, resource, urlPath, searchParams) {
|
|
335
|
+
const { incoming } = executionContext.switchToHttp();
|
|
336
|
+
if ((incoming.method === 'POST' || incoming.method === 'PATCH') && !incoming.is('json'))
|
|
337
|
+
throw new common_1.BadRequestError({ message: 'Unsupported Content-Type' });
|
|
338
|
+
const contentId = incoming.headers['content-id'];
|
|
339
|
+
const p = urlPath[0];
|
|
340
|
+
switch (incoming.method) {
|
|
341
|
+
case 'POST': {
|
|
342
|
+
const { controller, endpoint, handler } = await this.getOperationHandler(resource, 'create');
|
|
343
|
+
const jsonReader = (0, json_body_loader_js_1.jsonBodyLoader)({ limit: endpoint.inputMaxContentSize }, endpoint);
|
|
344
|
+
let data = await jsonReader(incoming);
|
|
345
|
+
data = endpoint.decode(data, { coerce: true });
|
|
346
|
+
const params = this.parseParameters(endpoint.parameters, p, searchParams);
|
|
347
|
+
return new request_host_js_1.RequestHost({
|
|
348
|
+
endpoint,
|
|
349
|
+
operation: 'create',
|
|
350
|
+
controller,
|
|
351
|
+
handler,
|
|
352
|
+
http: incoming,
|
|
353
|
+
contentId,
|
|
354
|
+
data,
|
|
355
|
+
params: {
|
|
356
|
+
...params,
|
|
357
|
+
pick: params.pick && resource.normalizeFieldPath(params.pick),
|
|
358
|
+
omit: params.omit && resource.normalizeFieldPath(params.omit),
|
|
359
|
+
include: params.include && resource.normalizeFieldPath(params.include)
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
case 'DELETE': {
|
|
364
|
+
const { controller, endpoint, handler } = await this.getOperationHandler(resource, 'delete');
|
|
365
|
+
const params = this.parseParameters(endpoint.parameters, p, searchParams);
|
|
366
|
+
return new request_host_js_1.RequestHost({
|
|
367
|
+
endpoint,
|
|
368
|
+
operation: 'delete',
|
|
369
|
+
controller,
|
|
370
|
+
handler,
|
|
371
|
+
http: incoming,
|
|
372
|
+
contentId,
|
|
373
|
+
params
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
case 'GET': {
|
|
377
|
+
const { controller, endpoint, handler } = await this.getOperationHandler(resource, 'get');
|
|
378
|
+
const params = this.parseParameters(endpoint.parameters, p, searchParams);
|
|
379
|
+
return new request_host_js_1.RequestHost({
|
|
380
|
+
endpoint,
|
|
381
|
+
operation: 'get',
|
|
382
|
+
controller,
|
|
383
|
+
handler,
|
|
384
|
+
http: incoming,
|
|
385
|
+
contentId,
|
|
386
|
+
params: {
|
|
387
|
+
...params,
|
|
388
|
+
pick: params.pick && resource.normalizeFieldPath(params.pick),
|
|
389
|
+
omit: params.omit && resource.normalizeFieldPath(params.omit),
|
|
390
|
+
include: params.include && resource.normalizeFieldPath(params.include)
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
case 'PATCH': {
|
|
395
|
+
const { controller, endpoint, handler } = await this.getOperationHandler(resource, 'update');
|
|
396
|
+
const jsonReader = (0, json_body_loader_js_1.jsonBodyLoader)({ limit: endpoint.inputMaxContentSize }, endpoint);
|
|
397
|
+
let data = await jsonReader(incoming);
|
|
398
|
+
data = endpoint.decode(data, { coerce: true });
|
|
399
|
+
const params = this.parseParameters(endpoint.parameters, p, searchParams);
|
|
400
|
+
return new request_host_js_1.RequestHost({
|
|
401
|
+
endpoint,
|
|
402
|
+
operation: 'update',
|
|
403
|
+
controller,
|
|
404
|
+
handler,
|
|
405
|
+
http: incoming,
|
|
406
|
+
contentId,
|
|
407
|
+
data,
|
|
408
|
+
params: {
|
|
409
|
+
...params,
|
|
410
|
+
pick: params.pick && resource.normalizeFieldPath(params.pick),
|
|
411
|
+
omit: params.omit && resource.normalizeFieldPath(params.omit),
|
|
412
|
+
include: params.include && resource.normalizeFieldPath(params.include),
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
throw new common_1.MethodNotAllowedError({
|
|
418
|
+
message: `Singleton resource doesn't accept http "${incoming.method}" method`
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
async _parseRequestStorage(executionContext, resource, urlPath, searchParams) {
|
|
422
|
+
const { incoming } = executionContext.switchToHttp();
|
|
423
|
+
const contentId = incoming.headers['content-id'];
|
|
424
|
+
const p = urlPath[0];
|
|
425
|
+
switch (incoming.method) {
|
|
426
|
+
case 'GET': {
|
|
427
|
+
const { controller, endpoint, handler } = await this.getOperationHandler(resource, 'get');
|
|
428
|
+
const params = this.parseParameters(endpoint.parameters, p, searchParams);
|
|
429
|
+
return new request_host_js_1.RequestHost({
|
|
430
|
+
endpoint,
|
|
431
|
+
operation: 'get',
|
|
432
|
+
controller,
|
|
433
|
+
handler,
|
|
434
|
+
http: incoming,
|
|
435
|
+
contentId,
|
|
436
|
+
path: incoming.parsedUrl.path.slice(1).toString().substring(1),
|
|
437
|
+
params
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
case 'DELETE': {
|
|
441
|
+
const { controller, endpoint, handler } = await this.getOperationHandler(resource, 'delete');
|
|
442
|
+
const params = this.parseParameters(endpoint.parameters, p, searchParams);
|
|
443
|
+
return new request_host_js_1.RequestHost({
|
|
444
|
+
endpoint,
|
|
445
|
+
operation: 'delete',
|
|
446
|
+
controller,
|
|
447
|
+
handler,
|
|
448
|
+
http: incoming,
|
|
449
|
+
contentId,
|
|
450
|
+
path: incoming.parsedUrl.path.slice(1).toString().substring(1),
|
|
451
|
+
params
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
case 'POST': {
|
|
455
|
+
const { controller, endpoint, handler } = await this.getOperationHandler(resource, 'post');
|
|
456
|
+
const params = this.parseParameters(endpoint.parameters, p, searchParams);
|
|
457
|
+
await promises_1.default.mkdir(this._tempDir, { recursive: true });
|
|
458
|
+
const multipartIterator = new multipart_helper_js_1.MultipartIterator(incoming, {
|
|
459
|
+
...endpoint.options,
|
|
460
|
+
filename: () => this.serviceName + '_p' + process.pid +
|
|
461
|
+
't' + String(Date.now()).substring(8) + 'r' + (0, common_1.uid)(12)
|
|
462
|
+
});
|
|
463
|
+
multipartIterator.pause();
|
|
464
|
+
// Add an hook to clean up files after request finished
|
|
465
|
+
executionContext.on('finish', async () => {
|
|
466
|
+
multipartIterator.cancel();
|
|
467
|
+
await multipartIterator.deleteFiles().catch(() => void 0);
|
|
468
|
+
});
|
|
469
|
+
return new request_host_js_1.RequestHost({
|
|
470
|
+
endpoint,
|
|
471
|
+
operation: 'post',
|
|
472
|
+
controller,
|
|
473
|
+
handler,
|
|
474
|
+
http: incoming,
|
|
475
|
+
contentId,
|
|
476
|
+
parts: multipartIterator,
|
|
477
|
+
path: incoming.parsedUrl.path.slice(1).toString().substring(1),
|
|
478
|
+
params
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
throw new common_1.MethodNotAllowedError({
|
|
483
|
+
message: `Storage resource doesn't accept http "${incoming.method}" method`
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
parseParameters(paramDefs, pathComponent, searchParams) {
|
|
487
|
+
const out = {};
|
|
488
|
+
// Parse known parameters
|
|
489
|
+
for (const [k, prm] of paramDefs.entries()) {
|
|
490
|
+
const decode = prm.getDecoder();
|
|
491
|
+
let v = searchParams?.getAll(k);
|
|
492
|
+
try {
|
|
493
|
+
if (!prm.isArray) {
|
|
494
|
+
v = v[0];
|
|
495
|
+
v = decode(v, { coerce: true });
|
|
496
|
+
}
|
|
497
|
+
else {
|
|
498
|
+
v = v.map(x => decode(x, { coerce: true })).flat();
|
|
499
|
+
if (!v.length)
|
|
500
|
+
v = undefined;
|
|
501
|
+
}
|
|
502
|
+
if (v !== undefined)
|
|
503
|
+
out[k] = v;
|
|
504
|
+
}
|
|
505
|
+
catch (e) {
|
|
506
|
+
e.message = `Error parsing parameter ${k}. ` + e.message;
|
|
507
|
+
throw e;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
// Add unknown parameters
|
|
511
|
+
if (searchParams) {
|
|
512
|
+
for (const k of searchParams.keys()) {
|
|
513
|
+
let v = searchParams.getAll(k);
|
|
514
|
+
if (v.length < 2)
|
|
515
|
+
v = v[0];
|
|
516
|
+
if (!paramDefs.has(k))
|
|
517
|
+
out[k] = v;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
return out;
|
|
521
|
+
}
|
|
522
|
+
async executeRequest(context) {
|
|
523
|
+
const { request } = context;
|
|
524
|
+
const { response } = context;
|
|
525
|
+
const { resource, handler } = request;
|
|
526
|
+
// Call endpoint handler method
|
|
527
|
+
let value;
|
|
528
|
+
try {
|
|
529
|
+
value = await handler.call(request.controller, context);
|
|
530
|
+
if (response.value == null)
|
|
531
|
+
response.value = value;
|
|
532
|
+
if (request.resource instanceof common_1.Collection || request.resource instanceof common_1.Singleton) {
|
|
533
|
+
const { operation } = request;
|
|
534
|
+
if (operation === 'delete' || operation === 'deleteMany' || operation === 'updateMany') {
|
|
535
|
+
let affected = 0;
|
|
536
|
+
if (typeof value === 'number')
|
|
537
|
+
affected = value;
|
|
538
|
+
if (typeof value === 'boolean')
|
|
539
|
+
affected = value ? 1 : 0;
|
|
540
|
+
if (typeof value === 'object')
|
|
541
|
+
affected = value.affected || value.affectedRows ||
|
|
542
|
+
(operation === 'updateMany' ? value.updated : value.deleted);
|
|
543
|
+
response.value = affected;
|
|
544
|
+
}
|
|
545
|
+
else {
|
|
546
|
+
// "get" and "update" endpoints must return the entity instance, otherwise it means resource not found
|
|
547
|
+
if (value == null && (request.operation === 'get' || request.operation === 'update'))
|
|
548
|
+
throw new common_1.ResourceNotFoundError(resource.name, request.key);
|
|
549
|
+
// "findMany" endpoint should return array of entity instances
|
|
550
|
+
if (request.operation === 'findMany')
|
|
551
|
+
value = value == null ? [] : Array.isArray(value) ? value : [value];
|
|
552
|
+
else
|
|
553
|
+
value = value == null ? {} : Array.isArray(value) ? value[0] : value;
|
|
554
|
+
response.value = value;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
catch (error) {
|
|
559
|
+
response.errors.push(error);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
async sendResponse(context) {
|
|
563
|
+
const { request, response } = context;
|
|
564
|
+
const outgoing = response.switchToHttp();
|
|
565
|
+
if (request.resource instanceof common_1.Storage) {
|
|
566
|
+
outgoing.statusCode = outgoing.statusCode || common_1.HttpStatusCodes.OK;
|
|
567
|
+
if (response.value != null) {
|
|
568
|
+
if (typeof response.value === 'string') {
|
|
569
|
+
if (!outgoing.hasHeader('content-type'))
|
|
570
|
+
outgoing.setHeader('content-type', 'text/plain');
|
|
571
|
+
outgoing.send(response.value);
|
|
572
|
+
}
|
|
573
|
+
else if (Buffer.isBuffer(response.value) || (0, common_1.isReadable)(response.value)) {
|
|
574
|
+
if (!outgoing.hasHeader('content-type'))
|
|
575
|
+
outgoing.setHeader('content-type', 'application/octet-stream');
|
|
576
|
+
outgoing.send(response.value);
|
|
577
|
+
}
|
|
578
|
+
else {
|
|
579
|
+
outgoing.setHeader('content-type', 'application/json; charset=utf-8');
|
|
580
|
+
outgoing.send(JSON.stringify(response.value));
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
outgoing.end();
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
const responseObject = {
|
|
587
|
+
context: request.resource.getFullPath()
|
|
588
|
+
};
|
|
589
|
+
if (request.operation === 'action')
|
|
590
|
+
responseObject.action = request.action;
|
|
591
|
+
else
|
|
592
|
+
responseObject.operation = request.operation;
|
|
593
|
+
const returnType = request.endpoint.returnType;
|
|
594
|
+
let responseValue = response.value;
|
|
595
|
+
if (returnType) {
|
|
596
|
+
responseObject.type = returnType.name || '#anonymous';
|
|
597
|
+
if (response.value != null)
|
|
598
|
+
responseValue = responseObject.data = request.endpoint.encode(response.value, { coerce: true });
|
|
599
|
+
}
|
|
600
|
+
if (request.operation === 'action') {
|
|
601
|
+
if (responseValue != null)
|
|
602
|
+
responseObject.data = responseValue;
|
|
603
|
+
}
|
|
604
|
+
else if (request.resource instanceof common_1.Collection || request.resource instanceof common_1.Singleton) {
|
|
605
|
+
if (request.operation === 'delete' || request.operation === 'deleteMany' ||
|
|
606
|
+
request.operation === 'updateMany') {
|
|
607
|
+
responseObject.affected = responseValue || 0;
|
|
608
|
+
}
|
|
609
|
+
else {
|
|
610
|
+
if (!responseValue)
|
|
611
|
+
throw new common_1.InternalServerError(`"${request.operation}" endpoint should return value`);
|
|
612
|
+
if (request.operation === 'create')
|
|
613
|
+
outgoing.statusCode = 201;
|
|
614
|
+
if (request.operation === 'create' || request.operation === 'update')
|
|
615
|
+
responseObject.affected = 1;
|
|
616
|
+
else if (request.operation === 'get' || request.operation === 'update')
|
|
617
|
+
responseObject.key = request.key;
|
|
618
|
+
if (request.operation === 'findMany' && response.count != null && response.count >= 0)
|
|
619
|
+
responseObject.totalCount = response.count;
|
|
620
|
+
}
|
|
621
|
+
outgoing.statusCode = outgoing.statusCode || common_1.HttpStatusCodes.OK;
|
|
622
|
+
}
|
|
623
|
+
const body = this.i18n.deep(responseObject);
|
|
624
|
+
outgoing.setHeader(common_1.HttpHeaderCodes.Content_Type, 'application/opra+json; charset=utf-8');
|
|
625
|
+
outgoing.send(JSON.stringify(body));
|
|
626
|
+
outgoing.end();
|
|
627
|
+
}
|
|
628
|
+
async handleError(context) {
|
|
629
|
+
const { errors } = context;
|
|
630
|
+
const { outgoing } = context.switchToHttp();
|
|
631
|
+
if (outgoing.headersSent) {
|
|
632
|
+
outgoing.end();
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
errors.forEach(x => {
|
|
636
|
+
if (x instanceof common_1.OpraException) {
|
|
637
|
+
switch (x.severity) {
|
|
638
|
+
case "fatal":
|
|
639
|
+
this._logger.fatal(x);
|
|
640
|
+
break;
|
|
641
|
+
case "warning":
|
|
642
|
+
this._logger.warn(x);
|
|
643
|
+
break;
|
|
644
|
+
default:
|
|
645
|
+
this._logger.error(x);
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
else
|
|
649
|
+
this._logger.fatal(x);
|
|
650
|
+
});
|
|
651
|
+
const wrappedErrors = errors.map(common_1.wrapException);
|
|
652
|
+
// Sort errors from fatal to info
|
|
653
|
+
wrappedErrors.sort((a, b) => {
|
|
654
|
+
const i = common_1.IssueSeverity.Keys.indexOf(a.severity) - common_1.IssueSeverity.Keys.indexOf(b.severity);
|
|
655
|
+
if (i === 0)
|
|
656
|
+
return b.status - a.status;
|
|
657
|
+
return i;
|
|
658
|
+
});
|
|
659
|
+
let status = outgoing.statusCode || 0;
|
|
660
|
+
if (!status || status < Number(common_1.HttpStatusCodes.BAD_REQUEST)) {
|
|
661
|
+
status = wrappedErrors[0].status;
|
|
662
|
+
if (status < Number(common_1.HttpStatusCodes.BAD_REQUEST))
|
|
663
|
+
status = common_1.HttpStatusCodes.INTERNAL_SERVER_ERROR;
|
|
664
|
+
}
|
|
665
|
+
outgoing.statusCode = status;
|
|
666
|
+
const body = {
|
|
667
|
+
errors: wrappedErrors.map(x => this._i18n.deep(x.toJSON()))
|
|
668
|
+
};
|
|
669
|
+
outgoing.setHeader(common_1.HttpHeaderCodes.Content_Type, 'application/opra+json; charset=utf-8');
|
|
670
|
+
outgoing.setHeader(common_1.HttpHeaderCodes.Cache_Control, 'no-cache');
|
|
671
|
+
outgoing.setHeader(common_1.HttpHeaderCodes.Pragma, 'no-cache');
|
|
672
|
+
outgoing.setHeader(common_1.HttpHeaderCodes.Expires, '-1');
|
|
673
|
+
outgoing.setHeader(common_1.HttpHeaderCodes.X_Opra_Version, common_1.OpraSchema.SpecVersion);
|
|
674
|
+
outgoing.send(JSON.stringify(body));
|
|
675
|
+
outgoing.end();
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
exports.HttpAdapterHost = HttpAdapterHost;
|