@opra/core 0.14.0 → 0.15.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.
Files changed (98) hide show
  1. package/cjs/adapter/adapter.js +88 -294
  2. package/cjs/adapter/http/express-adapter.js +27 -0
  3. package/cjs/adapter/http/http-adapter.js +466 -0
  4. package/cjs/adapter/http/http-request-context.host.js +28 -0
  5. package/cjs/adapter/http/http-request.host.js +14 -0
  6. package/cjs/adapter/http/http-response.host.js +14 -0
  7. package/cjs/adapter/internal/metadata.resource.js +27 -0
  8. package/cjs/adapter/request-context.host.js +30 -0
  9. package/cjs/adapter/request.host.js +19 -0
  10. package/cjs/adapter/response.host.js +20 -0
  11. package/cjs/augmentation/resource.augmentation.js +26 -0
  12. package/cjs/index.js +7 -8
  13. package/esm/adapter/adapter.d.ts +52 -28
  14. package/esm/adapter/adapter.js +88 -295
  15. package/esm/adapter/http/express-adapter.d.ts +11 -0
  16. package/esm/adapter/http/express-adapter.js +22 -0
  17. package/esm/adapter/http/http-adapter.d.ts +37 -0
  18. package/esm/adapter/http/http-adapter.js +462 -0
  19. package/esm/adapter/http/http-request-context.host.d.ts +16 -0
  20. package/esm/adapter/http/http-request-context.host.js +24 -0
  21. package/esm/adapter/http/http-request.host.d.ts +7 -0
  22. package/esm/adapter/http/http-request.host.js +10 -0
  23. package/esm/adapter/http/http-response.host.d.ts +7 -0
  24. package/esm/adapter/http/http-response.host.js +10 -0
  25. package/esm/{interfaces → adapter/interfaces}/logger.interface.d.ts +1 -0
  26. package/esm/adapter/interfaces/request-context.interface.d.ts +25 -0
  27. package/esm/adapter/interfaces/request.interface.d.ts +13 -0
  28. package/esm/adapter/interfaces/response.interface.d.ts +22 -0
  29. package/esm/adapter/internal/metadata.resource.d.ts +7 -0
  30. package/esm/adapter/internal/metadata.resource.js +24 -0
  31. package/esm/adapter/request-context.host.d.ts +19 -0
  32. package/esm/adapter/request-context.host.js +26 -0
  33. package/esm/adapter/request.host.d.ts +26 -0
  34. package/esm/adapter/request.host.js +15 -0
  35. package/esm/adapter/response.host.d.ts +20 -0
  36. package/esm/adapter/response.host.js +16 -0
  37. package/esm/augmentation/resource.augmentation.d.ts +33 -0
  38. package/esm/augmentation/resource.augmentation.js +24 -0
  39. package/esm/index.d.ts +7 -8
  40. package/esm/index.js +7 -8
  41. package/i18n/en/error.json +1 -1
  42. package/package.json +9 -7
  43. package/cjs/adapter/classes/execution-context.host.js +0 -16
  44. package/cjs/adapter/classes/express-request-wrapper.host.js +0 -36
  45. package/cjs/adapter/classes/express-response-wrapper.host.js +0 -55
  46. package/cjs/adapter/classes/http-execution-context.host.js +0 -28
  47. package/cjs/adapter/classes/metadata.resource.js +0 -22
  48. package/cjs/adapter/express-adapter.js +0 -26
  49. package/cjs/adapter/http-adapter.js +0 -443
  50. package/cjs/adapter/request-contexts/batch-request-context.js +0 -11
  51. package/cjs/adapter/request-contexts/request-context.js +0 -25
  52. package/cjs/adapter/request-contexts/single-request-context.js +0 -14
  53. package/cjs/interfaces/resource.interface.js +0 -2
  54. package/cjs/services/data-service.js +0 -9
  55. package/cjs/services/json-singleton-service.js +0 -96
  56. package/cjs/utils/create-i18n.js +0 -21
  57. package/cjs/utils/get-caller-file.util.js +0 -24
  58. package/esm/adapter/classes/execution-context.host.d.ts +0 -10
  59. package/esm/adapter/classes/execution-context.host.js +0 -12
  60. package/esm/adapter/classes/express-request-wrapper.host.d.ts +0 -19
  61. package/esm/adapter/classes/express-request-wrapper.host.js +0 -32
  62. package/esm/adapter/classes/express-response-wrapper.host.d.ts +0 -22
  63. package/esm/adapter/classes/express-response-wrapper.host.js +0 -51
  64. package/esm/adapter/classes/http-execution-context.host.d.ts +0 -13
  65. package/esm/adapter/classes/http-execution-context.host.js +0 -24
  66. package/esm/adapter/classes/metadata.resource.d.ts +0 -8
  67. package/esm/adapter/classes/metadata.resource.js +0 -19
  68. package/esm/adapter/express-adapter.d.ts +0 -11
  69. package/esm/adapter/express-adapter.js +0 -21
  70. package/esm/adapter/http-adapter.d.ts +0 -37
  71. package/esm/adapter/http-adapter.js +0 -439
  72. package/esm/adapter/request-contexts/batch-request-context.d.ts +0 -7
  73. package/esm/adapter/request-contexts/batch-request-context.js +0 -7
  74. package/esm/adapter/request-contexts/request-context.d.ts +0 -22
  75. package/esm/adapter/request-contexts/request-context.js +0 -21
  76. package/esm/adapter/request-contexts/single-request-context.d.ts +0 -10
  77. package/esm/adapter/request-contexts/single-request-context.js +0 -10
  78. package/esm/enums/issue-severity.enum.d.ts +0 -1
  79. package/esm/interfaces/execution-context.interface.d.ts +0 -47
  80. package/esm/interfaces/i18n-options.interface.d.ts +0 -28
  81. package/esm/interfaces/resource.interface.d.ts +0 -23
  82. package/esm/interfaces/resource.interface.js +0 -1
  83. package/esm/services/data-service.d.ts +0 -2
  84. package/esm/services/data-service.js +0 -5
  85. package/esm/services/json-singleton-service.d.ts +0 -39
  86. package/esm/services/json-singleton-service.js +0 -91
  87. package/esm/utils/create-i18n.d.ts +0 -3
  88. package/esm/utils/create-i18n.js +0 -16
  89. package/esm/utils/get-caller-file.util.d.ts +0 -1
  90. package/esm/utils/get-caller-file.util.js +0 -20
  91. /package/cjs/{interfaces → adapter/interfaces}/logger.interface.js +0 -0
  92. /package/cjs/{enums/issue-severity.enum.js → adapter/interfaces/request-context.interface.js} +0 -0
  93. /package/cjs/{interfaces/execution-context.interface.js → adapter/interfaces/request.interface.js} +0 -0
  94. /package/cjs/{interfaces/i18n-options.interface.js → adapter/interfaces/response.interface.js} +0 -0
  95. /package/esm/{interfaces → adapter/interfaces}/logger.interface.js +0 -0
  96. /package/esm/{enums/issue-severity.enum.js → adapter/interfaces/request-context.interface.js} +0 -0
  97. /package/esm/{interfaces/execution-context.interface.js → adapter/interfaces/request.interface.js} +0 -0
  98. /package/esm/{interfaces/i18n-options.interface.js → adapter/interfaces/response.interface.js} +0 -0
@@ -1,443 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OpraHttpAdapter = void 0;
4
- const common_1 = require("@opra/common");
5
- const adapter_js_1 = require("./adapter.js");
6
- const single_request_context_js_1 = require("./request-contexts/single-request-context.js");
7
- class OpraHttpAdapter extends adapter_js_1.OpraAdapter {
8
- async parse(executionContext) {
9
- const req = executionContext.getRequest();
10
- const contentType = req.getHeader('content-type');
11
- if (!contentType || contentType === 'application/json') {
12
- const body = req.getBody();
13
- const url = new common_1.OpraURL(req.getUrl());
14
- return this.parseSingleQuery({
15
- executionContext,
16
- url,
17
- method: req.getMethod(),
18
- headers: req.getHeaders(),
19
- body
20
- });
21
- }
22
- if (typeof contentType === 'string' && contentType.startsWith('multipart/mixed')) {
23
- // const m = BOUNDARY_PATTERN.exec(contentType);
24
- // if (!m)
25
- // throw new BadRequestError({message: 'Content-Type header does not match required format'});
26
- // const url = new OpraURL(req.getUrl());
27
- // return await this.parseMultiPart(executionContext, url, req.getHeaders(), req.getStream(), m[1]);
28
- }
29
- throw new common_1.BadRequestError({ message: 'Unsupported Content-Type' });
30
- }
31
- parseSingleQuery(args) {
32
- const { executionContext, url, method, headers, body, contentId } = args;
33
- if (!url.path.size)
34
- throw new common_1.BadRequestError();
35
- if (method !== 'GET' && url.path.size > 1)
36
- throw new common_1.BadRequestError();
37
- const query = this.buildQuery(url, method, body);
38
- if (!query)
39
- throw new common_1.MethodNotAllowedError({
40
- message: `Method "${method}" is not allowed by target endpoint`
41
- });
42
- return new single_request_context_js_1.SingleRequestContext({
43
- service: this.document,
44
- executionContext,
45
- headers,
46
- query,
47
- params: url.searchParams,
48
- contentId,
49
- continueOnError: query.operation === 'read'
50
- });
51
- }
52
- // async parseMultiPart(
53
- // executionContext: TExecutionContext,
54
- // url: OpraURL,
55
- // headers: IncomingHttpHeaders,
56
- // input: Readable,
57
- // boundary: string
58
- // ): Promise<BatchRequestContext> {
59
- // return await new Promise((resolve, reject) => {
60
- // let _resolved = false;
61
- // const dicer = new Dicer({boundary});
62
- // const doReject = (e) => {
63
- // if (_resolved) return;
64
- // _resolved = true;
65
- // reject(e);
66
- // taskQueue.clearQueue();
67
- // dicer.destroy();
68
- // }
69
- // const taskQueue = new TaskQueue({concurrency: 1});
70
- // taskQueue.on('error', doReject);
71
- //
72
- // const queries: SingleRequestContext[] = [];
73
- // let partCounter = 0;
74
- // dicer.on('error', doReject);
75
- // dicer.on('part', part => {
76
- // const partIndex = partCounter++;
77
- // let header: any;
78
- // const chunks: Buffer[] = [];
79
- // part.on('error', doReject);
80
- // part.on('header', (_header) => header = normalizeHeaders(_header));
81
- // part.on('data', (chunk: Buffer) => chunks.push(chunk));
82
- // part.on('end', () => {
83
- // if (_resolved || !(header || chunks.length))
84
- // return;
85
- // const ct = header['content-type'];
86
- // if (ct === 'application/http') {
87
- // taskQueue.enqueue(async () => {
88
- // const data = Buffer.concat(chunks);
89
- // if (!(data && data.length))
90
- // return;
91
- // const r = HttpRequest.parse(data);
92
- // await callMiddlewares(r, [jsonBodyParser]);
93
- // const subUrl = new OpraURL(r.url);
94
- // const contentId = header && header['content-id'];
95
- // queries.push(this.parseSingleQuery({
96
- // executionContext,
97
- // url: subUrl,
98
- // method: r.method,
99
- // headers: r.headers,
100
- // body: r.body,
101
- // contentId
102
- // }));
103
- // });
104
- // } else doReject(new BadRequestError({
105
- // message: 'Unaccepted "content-type" header in multipart data',
106
- // details: {
107
- // position: `${boundary}[${partIndex}]`
108
- // }
109
- // }))
110
- // });
111
- // });
112
- // dicer.on('finish', () => {
113
- // taskQueue.enqueue(() => {
114
- // if (_resolved) return;
115
- // _resolved = true;
116
- // const batch = new BatchRequestContext({
117
- // service: this.document,
118
- // executionContext,
119
- // headers,
120
- // queries,
121
- // params: url.searchParams,
122
- // continueOnError: false
123
- // });
124
- // resolve(batch);
125
- // });
126
- // });
127
- // input.pipe(dicer);
128
- // });
129
- // }
130
- buildQuery(url, method, body) {
131
- const pathLen = url.path.size;
132
- let p = url.path.get(0);
133
- let resource = this._internalResources.get(p.resource) || this.document.getResource(p.resource);
134
- let container;
135
- let pathIndex = 0;
136
- while (resource && resource instanceof common_1.ContainerResourceInfo) {
137
- container = resource;
138
- p = url.path.get(++pathIndex);
139
- resource = container.getResource(p.resource);
140
- }
141
- try {
142
- method = method.toUpperCase();
143
- let query;
144
- if (resource instanceof common_1.SingletonResourceInfo && !p.key) {
145
- switch (method) {
146
- case 'GET': {
147
- const searchParams = url.searchParams;
148
- query = new common_1.SingletonGetQuery(resource, {
149
- pick: searchParams.get('$pick'),
150
- omit: searchParams.get('$omit'),
151
- include: searchParams.get('$include')
152
- });
153
- }
154
- }
155
- }
156
- else if (resource instanceof common_1.CollectionResourceInfo) {
157
- switch (method) {
158
- case 'GET': {
159
- if (p.key) {
160
- const searchParams = url.searchParams;
161
- query = new common_1.CollectionGetQuery(resource, p.key, {
162
- pick: searchParams.get('$pick'),
163
- omit: searchParams.get('$omit'),
164
- include: searchParams.get('$include')
165
- });
166
- }
167
- else {
168
- const searchParams = url.searchParams;
169
- query = new common_1.CollectionSearchQuery(resource, {
170
- filter: searchParams.get('$filter'),
171
- limit: searchParams.get('$limit'),
172
- skip: searchParams.get('$skip'),
173
- distinct: searchParams.get('$distinct'),
174
- count: searchParams.get('$count'),
175
- sort: searchParams.get('$sort'),
176
- pick: searchParams.get('$pick'),
177
- omit: searchParams.get('$omit'),
178
- include: searchParams.get('$include')
179
- });
180
- }
181
- break;
182
- }
183
- case 'DELETE': {
184
- const searchParams = url.searchParams;
185
- query = p.key
186
- ? new common_1.CollectionDeleteQuery(resource, p.key)
187
- : new common_1.CollectionDeleteManyQuery(resource, {
188
- filter: searchParams.get('$filter'),
189
- });
190
- break;
191
- }
192
- case 'POST': {
193
- if (!p.key) {
194
- const searchParams = url.searchParams;
195
- query = new common_1.CollectionCreateQuery(resource, body, {
196
- pick: searchParams.get('$pick'),
197
- omit: searchParams.get('$omit'),
198
- include: searchParams.get('$include')
199
- });
200
- }
201
- break;
202
- }
203
- case 'PATCH': {
204
- if (p.key) {
205
- const searchParams = url.searchParams;
206
- query = new common_1.CollectionUpdateQuery(resource, p.key, body, {
207
- pick: searchParams.get('$pick'),
208
- omit: searchParams.get('$omit'),
209
- include: searchParams.get('$include')
210
- });
211
- }
212
- else {
213
- const searchParams = url.searchParams;
214
- query = new common_1.CollectionUpdateManyQuery(resource, body, {
215
- filter: searchParams.get('$filter')
216
- });
217
- }
218
- break;
219
- }
220
- }
221
- }
222
- else
223
- throw new common_1.InternalServerError();
224
- if (query instanceof common_1.SingletonGetQuery || query instanceof common_1.CollectionGetQuery || query instanceof common_1.FieldGetQuery) {
225
- // Move through properties
226
- let parentType;
227
- const curPath = [];
228
- let parent = query;
229
- while (++pathIndex < pathLen) {
230
- p = url.path.get(pathIndex);
231
- parentType = parent.dataType;
232
- if (parent.dataType instanceof common_1.UnionType) {
233
- if (parent.dataType.name === 'any')
234
- parentType = this.document.getComplexDataType('object');
235
- else
236
- throw new TypeError(`"${resource.name}.${curPath.join()}" is a UnionType and needs type casting.`);
237
- }
238
- if (!(parentType instanceof common_1.ComplexType))
239
- throw new TypeError(`"${resource.name}.${curPath.join()}" is not a ComplexType and has no fields.`);
240
- curPath.push(p.resource);
241
- parent.child = new common_1.FieldGetQuery(parent, p.resource, { castingType: parentType });
242
- parent = parent.child;
243
- }
244
- }
245
- return query;
246
- }
247
- catch (e) {
248
- if (e instanceof common_1.OpraException)
249
- throw e;
250
- throw new common_1.BadRequestError(e);
251
- }
252
- }
253
- async sendResponse(executionContext, requestContext) {
254
- // if (requestContext instanceof BatchRequestContext)
255
- // return this.sendBatchResponse(executionContext, requestContext);
256
- if (requestContext instanceof single_request_context_js_1.SingleRequestContext)
257
- return this.sendSingleResponse(executionContext, requestContext);
258
- /* istanbul ignore next */
259
- throw new TypeError('Invalid request context instance');
260
- }
261
- // protected async sendBatchResponse(executionContext: TExecutionContext, requestContext: BatchRequestContext) {
262
- // const resp = executionContext.getResponse();
263
- // resp.setStatus(HttpStatus.OK);
264
- // resp.setHeader(HttpHeaderCodes.Cache_Control, 'no-cache');
265
- // resp.setHeader(HttpHeaderCodes.Pragma, 'no-cache');
266
- // resp.setHeader(HttpHeaderCodes.Expires, '-1');
267
- // if (requestContext.headers) {
268
- // for (const [k, v] of Object.entries(requestContext.headers)) {
269
- // if (v)
270
- // resp.setHeader(k, v);
271
- // }
272
- // }
273
- // const boundary = 'batch_' + uuid();
274
- // resp.setHeader(HttpHeaderCodes.Content_Type, 'multipart/mixed;boundary=' + boundary);
275
- // resp.setHeader(HttpHeaderCodes.X_Opra_Version, OpraSchema.Version);
276
- //
277
- // const bodyBuilder = new HttpMultipartData();
278
- //
279
- // const chunks: any[] = [];
280
- // let msgIdx = 0;
281
- // for (const ctx of requestContext.queries) {
282
- // msgIdx++;
283
- // const out = this.createOutput(ctx);
284
- //
285
- //
286
- // // chunks.push('--' + boundary + CRLF);
287
- // // let s = 'Content-Type: application/http' + CRLF +
288
- // // 'Content-Transfer-Encoding: binary' + CRLF +
289
- // // 'Content-ID:' + (ctx.contentId || msgIdx) + CRLF +
290
- // // CRLF +
291
- // // 'HTTP/1.1 ' + out.status + (HttpStatus[out.status] || 'Unknown') + CRLF;
292
- //
293
- // let body = out.body;
294
- // const headers = out.headers || {};
295
- // if (body) {
296
- // const contentType = String(headers['content-type'] || '').split(/\s*;\s*/);
297
- // let charset = '';
298
- // if (Highland.isStream(body)) {
299
- // const l = parseInt(String(headers['content-length']), 10);
300
- // if (isNaN(l))
301
- // throw new TypeError('"content-length" header required for streamed responses');
302
- // } else if (typeof body === 'object') {
303
- // if (typeof body.stream === 'function') { // File and Blob
304
- // contentType[0] = body.type || 'binary';
305
- // headers['content-length'] = String(body.size);
306
- // body = body.stream();
307
- // } else if (Buffer.isBuffer(body)) {
308
- // headers['content-length'] = String(body.length);
309
- // } else {
310
- // contentType[0] = contentType[0] || 'application/json';
311
- // charset = 'utf-8';
312
- // body = Buffer.from(JSON.stringify(body), 'utf-8');
313
- // headers['content-length'] = String(body.length);
314
- // }
315
- // } else {
316
- // contentType[0] = contentType[0] || 'text/plain';
317
- // charset = 'utf-8';
318
- // body = Buffer.from(String(body), 'utf-8');
319
- // headers['content-length'] = String(body.length);
320
- // }
321
- // if (contentType[0]) {
322
- // if (charset) {
323
- // const i = contentType.findIndex(x => CHARSET_PATTERN.test(String(x)));
324
- // if (i > 0) contentType[i] = 'charset=' + charset;
325
- // else contentType.join('charset=' + charset);
326
- // }
327
- // headers['content-type'] = contentType.join(';');
328
- // }
329
- // }
330
- // for (const [k, v] of Object.entries(headers))
331
- // s += k + ': ' + (Array.isArray(v) ? v.join(';') : v) + CRLF
332
- //
333
- // chunks.push(s + CRLF);
334
- //
335
- // if (body) {
336
- // if (typeof body === 'string')
337
- // chunks.push(body + CRLF + CRLF);
338
- // else {
339
- // chunks.push(body);
340
- // chunks.push(CRLF + CRLF);
341
- // }
342
- // }
343
- // }
344
- //
345
- // chunks.push('--' + boundary + '--' + CRLF);
346
- //
347
- // resp.setHeader('content-type', 'multipart/mixed;boundary=' + boundary);
348
- // resp.send(Highland(chunks).flatten());
349
- // resp.end();
350
- // }
351
- async sendSingleResponse(executionContext, requestContext) {
352
- const out = this.createOutput(requestContext);
353
- const resp = executionContext.getResponse();
354
- resp.setStatus(out.status);
355
- resp.setHeader(common_1.HttpHeaderCodes.Cache_Control, 'no-cache');
356
- resp.setHeader(common_1.HttpHeaderCodes.Pragma, 'no-cache');
357
- resp.setHeader(common_1.HttpHeaderCodes.Expires, '-1');
358
- if (out.headers) {
359
- for (const [k, v] of Object.entries(out.headers)) {
360
- if (v)
361
- resp.setHeader(k, v);
362
- }
363
- }
364
- resp.setHeader(common_1.HttpHeaderCodes.X_Opra_Version, common_1.OpraSchema.Version);
365
- if (out.body)
366
- resp.send(out.body);
367
- resp.end();
368
- }
369
- createOutput(ctx) {
370
- const { query } = ctx;
371
- let body;
372
- let status = ctx.status || 0;
373
- const errors = ctx.errors.map(e => (0, common_1.wrapException)(e));
374
- if (errors && errors.length) {
375
- // Sort errors from fatal to info
376
- errors.sort((a, b) => {
377
- const i = common_1.IssueSeverity.Keys.indexOf(a.issue.severity) - common_1.IssueSeverity.Keys.indexOf(b.issue.severity);
378
- if (i === 0)
379
- return b.status - a.status;
380
- return i;
381
- });
382
- if (!status || status < common_1.HttpStatusCodes.BAD_REQUEST) {
383
- status = errors[0].status;
384
- if (status < common_1.HttpStatusCodes.BAD_REQUEST)
385
- status = common_1.HttpStatusCodes.INTERNAL_SERVER_ERROR;
386
- }
387
- body = this.i18n.deep({
388
- operation: ctx.query.method,
389
- errors: errors.map(e => e.issue)
390
- });
391
- body = JSON.stringify(body);
392
- ctx.responseHeaders['content-type'] = 'application/json; charset=utf-8';
393
- }
394
- else {
395
- if (typeof ctx.response === 'object' && !((0, common_1.isReadable)(ctx.response) || Buffer.isBuffer(ctx.response))) {
396
- body = this.i18n.deep(ctx.response);
397
- body = JSON.stringify(body);
398
- ctx.responseHeaders['content-type'] = 'application/json; charset=utf-8';
399
- }
400
- status = status || (query.operation === 'create' ? common_1.HttpStatusCodes.CREATED : common_1.HttpStatusCodes.OK);
401
- }
402
- return {
403
- status,
404
- headers: (0, common_1.normalizeHeaders)(ctx.responseHeaders),
405
- body
406
- };
407
- }
408
- async sendError(executionContext, error) {
409
- const resp = executionContext.getResponse();
410
- resp.setStatus(error.status || 500);
411
- resp.setHeader(common_1.HttpHeaderCodes.Content_Type, 'application/json');
412
- resp.setHeader(common_1.HttpHeaderCodes.Cache_Control, 'no-cache');
413
- resp.setHeader(common_1.HttpHeaderCodes.Pragma, 'no-cache');
414
- resp.setHeader(common_1.HttpHeaderCodes.Expires, '-1');
415
- resp.setHeader(common_1.HttpHeaderCodes.X_Opra_Version, common_1.OpraSchema.Version);
416
- const issue = this.i18n.deep(error.issue);
417
- const body = {
418
- operation: 'unknown',
419
- errors: [issue]
420
- };
421
- resp.send(JSON.stringify(body));
422
- }
423
- }
424
- exports.OpraHttpAdapter = OpraHttpAdapter;
425
- // async function callMiddlewares(req: HttpRequest, middlewares: NextHandleFunction[]): Promise<void> {
426
- // return new Promise<void>((resolve, reject) => {
427
- // let i = 0;
428
- // const next = (err?: any) => {
429
- // if (err)
430
- // return reject(err);
431
- // const fn = middlewares[i++];
432
- // if (!fn)
433
- // return resolve();
434
- // try {
435
- // fn(req as any, {} as any, next);
436
- // } catch (e) {
437
- // reject(e);
438
- // }
439
- // }
440
- // next();
441
- // });
442
- //
443
- // }
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BatchRequestContext = void 0;
4
- const request_context_js_1 = require("./request-context.js");
5
- class BatchRequestContext extends request_context_js_1.RequestContext {
6
- constructor(args) {
7
- super(args);
8
- this.queries = args.queries;
9
- }
10
- }
11
- exports.BatchRequestContext = BatchRequestContext;
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RequestContext = void 0;
4
- const common_1 = require("@opra/common");
5
- class RequestContext {
6
- constructor(args) {
7
- this.errors = [];
8
- this.service = args.service;
9
- this.executionContext = args.executionContext;
10
- this.params = this.params || new common_1.OpraURLSearchParams();
11
- this.headers = args.headers;
12
- this.contentId = args.contentId;
13
- this.responseHeaders = {};
14
- this.resultPath = this.resultPath || '';
15
- }
16
- get type() {
17
- return this.executionContext.getType();
18
- }
19
- switchToHttp() {
20
- if (this.type !== 'http')
21
- throw new Error(`You can't access http context within an ${this.type} context`);
22
- return this.executionContext;
23
- }
24
- }
25
- exports.RequestContext = RequestContext;
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SingleRequestContext = void 0;
4
- const request_context_js_1 = require("./request-context.js");
5
- class SingleRequestContext extends request_context_js_1.RequestContext {
6
- constructor(args) {
7
- super(args);
8
- this.query = args.query;
9
- }
10
- get userContext() {
11
- return this.executionContext.userContext;
12
- }
13
- }
14
- exports.SingleRequestContext = SingleRequestContext;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,9 +0,0 @@
1
- "use strict";
2
- /* eslint-disable */
3
- // import { Type } from 'ts-gems';
4
- // import { ComplexType } from '../implementation/data-type/complex-type.js';
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.DataService = void 0;
7
- class DataService {
8
- }
9
- exports.DataService = DataService;
@@ -1,96 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.JsonSingletonService = void 0;
4
- const tslib_1 = require("tslib");
5
- const lodash_isnil_1 = tslib_1.__importDefault(require("lodash.isnil"));
6
- const lodash_omitby_1 = tslib_1.__importDefault(require("lodash.omitby"));
7
- const common_1 = require("@opra/common");
8
- class JsonSingletonService {
9
- constructor(dataType, options) {
10
- this.dataType = dataType;
11
- this._data = options?.data;
12
- }
13
- async processRequest(ctx) {
14
- const prepared = this._prepare(ctx.query);
15
- const fn = this[prepared.method];
16
- if (!fn)
17
- throw new TypeError(`Unimplemented method (${prepared.method})`);
18
- // @ts-ignore
19
- return fn.apply(this, prepared.args);
20
- }
21
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
22
- get(options) {
23
- return this._data;
24
- }
25
- _prepare(query) {
26
- if (query.resource instanceof common_1.SingletonResourceInfo) {
27
- if (query.dataType !== this.dataType)
28
- throw new TypeError(`Query data type (${query.dataType.name}) ` +
29
- `differs from JsonCollectionService data type (${this.dataType.name})`);
30
- }
31
- switch (query.method) {
32
- case 'create': {
33
- const options = (0, lodash_omitby_1.default)({
34
- pick: query.pick,
35
- omit: query.omit,
36
- include: query.include
37
- }, lodash_isnil_1.default);
38
- const { data } = query;
39
- return {
40
- method: query.method,
41
- values: data,
42
- options,
43
- args: [data, options]
44
- };
45
- }
46
- case 'get': {
47
- if (query.kind === 'CollectionGetQuery') {
48
- const options = (0, lodash_omitby_1.default)({
49
- pick: query.pick,
50
- omit: query.omit,
51
- include: query.include
52
- }, lodash_isnil_1.default);
53
- const keyValue = query.keyValue;
54
- return {
55
- method: query.method,
56
- keyValue,
57
- options,
58
- args: [keyValue, options]
59
- };
60
- }
61
- if (query.kind === 'FieldGetQuery') {
62
- // todo
63
- }
64
- break;
65
- }
66
- case 'update': {
67
- const options = (0, lodash_omitby_1.default)({
68
- pick: query.pick,
69
- omit: query.omit,
70
- include: query.include
71
- }, lodash_isnil_1.default);
72
- const { data } = query;
73
- const keyValue = query.keyValue;
74
- return {
75
- method: query.method,
76
- keyValue: query.keyValue,
77
- values: data,
78
- options,
79
- args: [keyValue, data, options]
80
- };
81
- }
82
- case 'delete': {
83
- const options = {};
84
- const keyValue = query.keyValue;
85
- return {
86
- method: query.method,
87
- keyValue,
88
- options,
89
- args: [keyValue, options]
90
- };
91
- }
92
- }
93
- throw new Error(`Unimplemented query type "${query.method}"`);
94
- }
95
- }
96
- exports.JsonSingletonService = JsonSingletonService;
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createI18n = void 0;
4
- const tslib_1 = require("tslib");
5
- const path_1 = tslib_1.__importDefault(require("path"));
6
- const common_1 = require("@opra/common");
7
- const get_caller_file_util_js_1 = require("./get-caller-file.util.js");
8
- async function createI18n(options) {
9
- const opts = {
10
- ...options,
11
- };
12
- delete opts.resourceDirs;
13
- const instance = common_1.I18n.createInstance(opts);
14
- await instance.init();
15
- await instance.loadResourceDir(path_1.default.resolve((0, get_caller_file_util_js_1.getCallerFile)(), '../../../i18n'));
16
- if (options?.resourceDirs)
17
- for (const dir of options.resourceDirs)
18
- await instance.loadResourceDir(dir);
19
- return instance;
20
- }
21
- exports.createI18n = createI18n;
@@ -1,24 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getCallerFile = void 0;
4
- const PATH_PATTERN = /^(?:file:\/\/)?(.+)$/;
5
- function getCallerFile(position = 1) {
6
- if (position >= Error.stackTraceLimit) {
7
- throw new TypeError('getCallerFile(position) requires position be less then Error.stackTraceLimit but position was: `' +
8
- position + '` and Error.stackTraceLimit was: `' + Error.stackTraceLimit + '`');
9
- }
10
- const oldPrepareStackTrace = Error.prepareStackTrace;
11
- Error.prepareStackTrace = (_, stack) => stack;
12
- const stack = new Error().stack;
13
- Error.prepareStackTrace = oldPrepareStackTrace;
14
- if (stack !== null && typeof stack === 'object') {
15
- // stack[0] holds this file
16
- // stack[1] holds where this function was called
17
- const s = stack[position] ?
18
- stack[position].getFileName() : undefined;
19
- const m = s ? PATH_PATTERN.exec(s) : undefined;
20
- return m ? m[1] : '';
21
- }
22
- return '';
23
- }
24
- exports.getCallerFile = getCallerFile;
@@ -1,10 +0,0 @@
1
- import { AsyncEventEmitter } from 'strict-typed-events';
2
- import { ContextType, IExecutionContext, IHttpExecutionContext } from '../../interfaces/execution-context.interface.js';
3
- export declare abstract class ExecutionContextHost extends AsyncEventEmitter implements IExecutionContext {
4
- userContext: any;
5
- protected constructor();
6
- abstract getType(): ContextType;
7
- abstract getPlatform(): string;
8
- switchToHttp(): IHttpExecutionContext;
9
- onFinish(fn: (...args: any[]) => (void | Promise<void>)): void;
10
- }