@opra/core 0.4.0 → 0.6.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/adapter/adapter.js +103 -123
- package/cjs/adapter/classes/execution-context.host.js +17 -0
- package/cjs/adapter/classes/express-request-wrapper.host.js +37 -0
- package/cjs/adapter/classes/express-response-wrapper.host.js +56 -0
- package/cjs/adapter/classes/http-execution-context.host.js +31 -0
- package/cjs/adapter/{metadata-resource.js → classes/metadata.resource.js} +3 -3
- package/cjs/adapter/express-adapter.js +6 -104
- package/cjs/adapter/http-adapter.js +282 -81
- package/cjs/adapter/request-contexts/batch-request-context.js +12 -0
- package/cjs/adapter/{query-context.js → request-contexts/request-context.js} +9 -13
- package/cjs/adapter/request-contexts/single-request-context.js +15 -0
- package/cjs/index.js +2 -2
- package/cjs/interfaces/i18n-options.interface.js +2 -0
- package/cjs/services/json-collection-service.js +36 -39
- package/cjs/services/json-singleton-service.js +9 -10
- package/cjs/utils/create-i18n.js +2 -2
- package/esm/adapter/adapter.d.ts +13 -44
- package/esm/adapter/adapter.js +89 -109
- package/esm/adapter/classes/execution-context.host.d.ts +10 -0
- package/esm/adapter/classes/execution-context.host.js +13 -0
- package/esm/adapter/classes/express-request-wrapper.host.d.ts +19 -0
- package/esm/adapter/classes/express-request-wrapper.host.js +33 -0
- package/esm/adapter/classes/express-response-wrapper.host.d.ts +22 -0
- package/esm/adapter/classes/express-response-wrapper.host.js +52 -0
- package/esm/adapter/classes/http-execution-context.host.d.ts +13 -0
- package/esm/adapter/classes/http-execution-context.host.js +27 -0
- package/esm/adapter/classes/metadata.resource.d.ts +8 -0
- package/esm/adapter/{metadata-resource.js → classes/metadata.resource.js} +2 -2
- package/esm/adapter/express-adapter.d.ts +1 -1
- package/esm/adapter/express-adapter.js +5 -103
- package/esm/adapter/http-adapter.d.ts +23 -12
- package/esm/adapter/http-adapter.js +248 -47
- package/esm/adapter/request-contexts/batch-request-context.d.ts +7 -0
- package/esm/adapter/request-contexts/batch-request-context.js +8 -0
- package/esm/adapter/request-contexts/request-context.d.ts +22 -0
- package/esm/adapter/{query-context.js → request-contexts/request-context.js} +6 -10
- package/esm/adapter/request-contexts/single-request-context.d.ts +10 -0
- package/esm/adapter/request-contexts/single-request-context.js +11 -0
- package/esm/index.d.ts +2 -2
- package/esm/index.js +2 -2
- package/esm/interfaces/execution-context.interface.d.ts +19 -11
- package/esm/interfaces/i18n-options.interface.d.ts +28 -0
- package/esm/interfaces/i18n-options.interface.js +1 -0
- package/esm/interfaces/resource.interface.d.ts +1 -1
- package/esm/services/json-collection-service.d.ts +3 -4
- package/esm/services/json-collection-service.js +17 -20
- package/esm/services/json-singleton-service.d.ts +3 -3
- package/esm/services/json-singleton-service.js +8 -8
- package/esm/utils/create-i18n.d.ts +3 -3
- package/esm/utils/create-i18n.js +1 -1
- package/package.json +7 -8
- package/cjs/enums/http-headers.enum.js +0 -395
- package/cjs/enums/http-status.enum.js +0 -300
- package/cjs/enums/index.js +0 -5
- package/cjs/helpers/headers-map.js +0 -18
- package/cjs/utils/path-to-tree.js +0 -28
- package/esm/adapter/metadata-resource.d.ts +0 -8
- package/esm/adapter/query-context.d.ts +0 -25
- package/esm/enums/http-headers.enum.d.ts +0 -370
- package/esm/enums/http-headers.enum.js +0 -392
- package/esm/enums/http-status.enum.d.ts +0 -290
- package/esm/enums/http-status.enum.js +0 -297
- package/esm/enums/index.d.ts +0 -2
- package/esm/enums/index.js +0 -2
- package/esm/helpers/headers-map.d.ts +0 -5
- package/esm/helpers/headers-map.js +0 -14
- package/esm/utils/path-to-tree.d.ts +0 -4
- package/esm/utils/path-to-tree.js +0 -24
package/cjs/adapter/adapter.js
CHANGED
|
@@ -1,150 +1,99 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OpraAdapter = void 0;
|
|
4
|
+
const power_tasks_1 = require("power-tasks");
|
|
4
5
|
const strict_typed_events_1 = require("strict-typed-events");
|
|
5
6
|
const common_1 = require("@opra/common");
|
|
6
|
-
const exception_1 = require("@opra/exception");
|
|
7
|
-
const i18n_1 = require("@opra/i18n");
|
|
8
|
-
const schema_1 = require("@opra/schema");
|
|
9
|
-
const index_js_1 = require("../enums/index.js");
|
|
10
7
|
const create_i18n_js_1 = require("../utils/create-i18n.js");
|
|
11
|
-
const metadata_resource_js_1 = require("./metadata
|
|
8
|
+
const metadata_resource_js_1 = require("./classes/metadata.resource.js");
|
|
9
|
+
const batch_request_context_js_1 = require("./request-contexts/batch-request-context.js");
|
|
10
|
+
const single_request_context_js_1 = require("./request-contexts/single-request-context.js");
|
|
11
|
+
const noOp = () => void 0;
|
|
12
12
|
class OpraAdapter {
|
|
13
13
|
document;
|
|
14
|
-
i18n;
|
|
15
14
|
userContextResolver;
|
|
16
|
-
// protected _metadataResource: SingletonResourceInfo;
|
|
17
15
|
_internalResources = new common_1.ResponsiveMap();
|
|
16
|
+
i18n;
|
|
18
17
|
constructor(document) {
|
|
19
18
|
this.document = document;
|
|
20
19
|
}
|
|
21
20
|
async handler(executionContext) {
|
|
22
|
-
let
|
|
23
|
-
let userContext;
|
|
21
|
+
let requestContext;
|
|
24
22
|
let failed = false;
|
|
25
23
|
try {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
exclusive = exclusive || context.query.operation !== 'read';
|
|
33
|
-
// Wait previous read requests before executing update request
|
|
34
|
-
if (exclusive && promises) {
|
|
35
|
-
await Promise.allSettled(promises);
|
|
36
|
-
promises = undefined;
|
|
37
|
-
}
|
|
38
|
-
// If previous request in bucket had an error and executed an update
|
|
39
|
-
// we do not execute next requests
|
|
40
|
-
if (stop) {
|
|
41
|
-
context.errors.push(new exception_1.FailedDependencyError());
|
|
42
|
-
continue;
|
|
43
|
-
}
|
|
44
|
-
try {
|
|
45
|
-
const promise = (async () => {
|
|
46
|
-
// if (context.query.method === 'metadata') {
|
|
47
|
-
// await this._getSchemaExecute(context); //todo
|
|
48
|
-
// return;
|
|
49
|
-
// }
|
|
50
|
-
const resource = context.query.resource;
|
|
51
|
-
await this._resourcePrepare(resource, context);
|
|
52
|
-
if (this.userContextResolver && !userContext)
|
|
53
|
-
userContext = this.userContextResolver({
|
|
54
|
-
executionContext,
|
|
55
|
-
isBatch: this.isBatch(executionContext)
|
|
56
|
-
});
|
|
57
|
-
context.userContext = userContext;
|
|
58
|
-
await this._resourceExecute(this.document, resource, context);
|
|
59
|
-
})().catch(e => {
|
|
60
|
-
context.errors.push(e);
|
|
61
|
-
});
|
|
62
|
-
if (exclusive)
|
|
63
|
-
await promise;
|
|
64
|
-
else {
|
|
65
|
-
promises = promises || [];
|
|
66
|
-
promises.push(promise);
|
|
67
|
-
}
|
|
68
|
-
// todo execute sub property queries
|
|
69
|
-
}
|
|
70
|
-
catch (e) {
|
|
71
|
-
context.errors.unshift(e);
|
|
72
|
-
}
|
|
73
|
-
if (context.errors.length) {
|
|
74
|
-
// noinspection SuspiciousTypeOfGuard
|
|
75
|
-
context.errors = context.errors.map(e => (0, exception_1.wrapException)(e));
|
|
76
|
-
if (exclusive)
|
|
77
|
-
stop = stop || !!context.errors.find(e => !(e.issue.severity === 'warning' || e.issue.severity === 'info'));
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
if (promises)
|
|
81
|
-
await Promise.allSettled(promises);
|
|
82
|
-
await this.sendResponse(executionContext, queryContexts);
|
|
24
|
+
if (this.userContextResolver)
|
|
25
|
+
executionContext.userContext = this.userContextResolver(executionContext);
|
|
26
|
+
requestContext = await this.parse(executionContext);
|
|
27
|
+
const task = this._requestContextToTask(executionContext, requestContext);
|
|
28
|
+
await task.toPromise().catch(noOp);
|
|
29
|
+
await this.sendResponse(executionContext, requestContext);
|
|
83
30
|
}
|
|
84
31
|
catch (e) {
|
|
85
32
|
failed = true;
|
|
86
|
-
const error = (0,
|
|
33
|
+
const error = (0, common_1.wrapException)(e);
|
|
87
34
|
await this.sendError(executionContext, error);
|
|
88
35
|
}
|
|
89
36
|
finally {
|
|
90
37
|
if (executionContext instanceof strict_typed_events_1.AsyncEventEmitter) {
|
|
91
38
|
await executionContext
|
|
92
39
|
.emitAsyncSerial('finish', {
|
|
93
|
-
|
|
40
|
+
context: executionContext,
|
|
94
41
|
failed
|
|
95
42
|
}).catch();
|
|
96
43
|
}
|
|
97
44
|
}
|
|
98
45
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
await fn(context);
|
|
46
|
+
_requestContextToTask(executionContext, requestContext) {
|
|
47
|
+
if (requestContext instanceof batch_request_context_js_1.BatchRequestContext) {
|
|
48
|
+
const children = requestContext.queries.map(q => this._requestContextToTask(executionContext, q));
|
|
49
|
+
return new power_tasks_1.Task(children, { bail: true });
|
|
104
50
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
};
|
|
119
|
-
Object.setPrototypeOf(ctx, context);
|
|
120
|
-
promises.push(this._collectionResourceExecute(document, resource, ctx));
|
|
51
|
+
else if (requestContext instanceof single_request_context_js_1.SingleRequestContext) {
|
|
52
|
+
const { query } = requestContext;
|
|
53
|
+
const task = new power_tasks_1.Task(async () => {
|
|
54
|
+
if (query.resource) {
|
|
55
|
+
const { resource } = query;
|
|
56
|
+
// call pre_xxx method
|
|
57
|
+
const fn = resource.metadata['pre_' + query.method];
|
|
58
|
+
if (fn && typeof fn === 'function') {
|
|
59
|
+
await fn(requestContext);
|
|
60
|
+
}
|
|
61
|
+
await this._executeResourceQuery(this.document, resource, requestContext);
|
|
62
|
+
// todo execute sub property queries
|
|
63
|
+
return;
|
|
121
64
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
65
|
+
throw new common_1.InternalServerError('Not implemented yet');
|
|
66
|
+
}, {
|
|
67
|
+
id: requestContext.contentId,
|
|
68
|
+
exclusive: query.operation !== 'read'
|
|
69
|
+
});
|
|
70
|
+
task.on('finish', () => {
|
|
71
|
+
if (task.error) {
|
|
72
|
+
if (task.failedDependencies)
|
|
73
|
+
requestContext.errors.push(new common_1.FailedDependencyError());
|
|
74
|
+
else
|
|
75
|
+
requestContext.errors.push((0, common_1.wrapException)(task.error));
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
return task;
|
|
132
80
|
}
|
|
133
|
-
|
|
81
|
+
/* istanbul ignore next */
|
|
82
|
+
throw new TypeError('Invalid request context instance');
|
|
134
83
|
}
|
|
135
84
|
async _init(options) {
|
|
136
|
-
if (options?.i18n instanceof
|
|
85
|
+
if (options?.i18n instanceof common_1.I18n)
|
|
137
86
|
this.i18n = options.i18n;
|
|
138
87
|
else if (typeof options?.i18n === 'function')
|
|
139
88
|
this.i18n = await options.i18n();
|
|
140
89
|
else
|
|
141
90
|
this.i18n = await (0, create_i18n_js_1.createI18n)(options?.i18n);
|
|
142
|
-
this.i18n = this.i18n ||
|
|
91
|
+
this.i18n = this.i18n || common_1.I18n.defaultInstance;
|
|
143
92
|
if (!this.i18n.isInitialized)
|
|
144
93
|
await this.i18n.init();
|
|
145
94
|
this.userContextResolver = options?.userContext;
|
|
146
95
|
const metadataResource = new metadata_resource_js_1.MetadataResource();
|
|
147
|
-
const metadataResourceInfo = new
|
|
96
|
+
const metadataResourceInfo = new common_1.SingletonResourceInfo(this.document, '$metadata', this.document.getComplexDataType('object'), {
|
|
148
97
|
kind: 'SingletonResource',
|
|
149
98
|
type: 'object',
|
|
150
99
|
instance: metadataResource,
|
|
@@ -162,12 +111,44 @@ class OpraAdapter {
|
|
|
162
111
|
}
|
|
163
112
|
}
|
|
164
113
|
}
|
|
165
|
-
async
|
|
114
|
+
async _executeResourceQuery(document, resource, context) {
|
|
115
|
+
if (resource instanceof common_1.CollectionResourceInfo) {
|
|
116
|
+
const { query } = context;
|
|
117
|
+
if (query instanceof common_1.CollectionSearchQuery) {
|
|
118
|
+
const promises = [];
|
|
119
|
+
let search;
|
|
120
|
+
promises.push(this._executeCollectionResource(document, resource, context)
|
|
121
|
+
.then(v => search = v));
|
|
122
|
+
if (query.count && resource.metadata.count) {
|
|
123
|
+
const ctx = {
|
|
124
|
+
query: new common_1.CollectionCountQuery(query.resource, { filter: query.filter }),
|
|
125
|
+
resultPath: ''
|
|
126
|
+
};
|
|
127
|
+
Object.setPrototypeOf(ctx, context);
|
|
128
|
+
promises.push(this._executeCollectionResource(document, resource, ctx)
|
|
129
|
+
.then(r => {
|
|
130
|
+
context.responseHeaders[common_1.HttpHeaders.X_Opra_Count] = r;
|
|
131
|
+
}));
|
|
132
|
+
}
|
|
133
|
+
await Promise.all(promises);
|
|
134
|
+
context.response = search;
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
context.response = await this._executeCollectionResource(document, resource, context);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
else if (resource instanceof common_1.SingletonResourceInfo) {
|
|
141
|
+
context.response = await this._executeSingletonResource(document, resource, context);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
throw new Error(`Executing "${resource.kind}" has not been implemented yet`);
|
|
145
|
+
}
|
|
146
|
+
async _executeCollectionResource(document, resource, context) {
|
|
166
147
|
const method = context.query.method;
|
|
167
148
|
const resolverInfo = resource.metadata[method];
|
|
168
149
|
if (!(resolverInfo && resolverInfo.handler))
|
|
169
|
-
throw new
|
|
170
|
-
message: (0,
|
|
150
|
+
throw new common_1.ForbiddenError({
|
|
151
|
+
message: (0, common_1.translate)('RESOLVER_FORBIDDEN', { method }, `The resource endpoint does not accept '{{method}}' operations`),
|
|
171
152
|
severity: 'error',
|
|
172
153
|
code: 'RESOLVER_FORBIDDEN'
|
|
173
154
|
});
|
|
@@ -179,13 +160,12 @@ class OpraAdapter {
|
|
|
179
160
|
result = Array.isArray(result) ? result[0] : result;
|
|
180
161
|
if (result)
|
|
181
162
|
context.status = 201;
|
|
182
|
-
context.responseHeaders.
|
|
163
|
+
context.responseHeaders[common_1.HttpHeaders.X_Opra_DataType] = resource.dataType.name;
|
|
183
164
|
return result;
|
|
184
165
|
}
|
|
185
166
|
case 'count': {
|
|
186
167
|
const query = context.query;
|
|
187
168
|
result = await resolverInfo.handler(context, query);
|
|
188
|
-
context.responseHeaders.set(index_js_1.HttpHeaders.X_Opra_Count, result);
|
|
189
169
|
return result;
|
|
190
170
|
}
|
|
191
171
|
case 'get': {
|
|
@@ -193,19 +173,19 @@ class OpraAdapter {
|
|
|
193
173
|
result = await resolverInfo.handler(context, query.keyValue, query);
|
|
194
174
|
result = Array.isArray(result) ? result[0] : result;
|
|
195
175
|
if (!result)
|
|
196
|
-
throw new
|
|
176
|
+
throw new common_1.ResourceNotFoundError(resource.name, query.keyValue);
|
|
197
177
|
const v = await this._pathWalkThrough(query, query.dataType, result, resource.name);
|
|
198
178
|
if (v.value === undefined)
|
|
199
|
-
throw new
|
|
179
|
+
throw new common_1.ResourceNotFoundError(v.path);
|
|
200
180
|
if (v.dataType)
|
|
201
|
-
context.responseHeaders.
|
|
181
|
+
context.responseHeaders[common_1.HttpHeaders.X_Opra_DataType] = v.dataType.name;
|
|
202
182
|
return v.value;
|
|
203
183
|
}
|
|
204
184
|
case 'search': {
|
|
205
185
|
const query = context.query;
|
|
206
186
|
result = await resolverInfo.handler(context, query);
|
|
207
187
|
const items = Array.isArray(result) ? result : (context.response ? [result] : []);
|
|
208
|
-
context.responseHeaders.
|
|
188
|
+
context.responseHeaders[common_1.HttpHeaders.X_Opra_DataType] = resource.dataType.name;
|
|
209
189
|
return items;
|
|
210
190
|
}
|
|
211
191
|
case 'update': {
|
|
@@ -213,8 +193,8 @@ class OpraAdapter {
|
|
|
213
193
|
result = await resolverInfo.handler(context, query.keyValue, query.data, query);
|
|
214
194
|
result = Array.isArray(result) ? result[0] : result;
|
|
215
195
|
if (!result)
|
|
216
|
-
throw new
|
|
217
|
-
context.responseHeaders.
|
|
196
|
+
throw new common_1.ResourceNotFoundError(resource.name, query.keyValue);
|
|
197
|
+
context.responseHeaders[common_1.HttpHeaders.X_Opra_DataType] = resource.dataType.name;
|
|
218
198
|
return result;
|
|
219
199
|
}
|
|
220
200
|
case 'delete':
|
|
@@ -251,12 +231,12 @@ class OpraAdapter {
|
|
|
251
231
|
}
|
|
252
232
|
}
|
|
253
233
|
}
|
|
254
|
-
async
|
|
234
|
+
async _executeSingletonResource(document, resource, context) {
|
|
255
235
|
const method = context.query.method;
|
|
256
236
|
const resolverInfo = resource.metadata[method];
|
|
257
237
|
if (!(resolverInfo && resolverInfo.handler))
|
|
258
|
-
throw new
|
|
259
|
-
message: (0,
|
|
238
|
+
throw new common_1.ForbiddenError({
|
|
239
|
+
message: (0, common_1.translate)('RESOLVER_FORBIDDEN', { method }, `The resource endpoint does not accept '{{method}}' operations`),
|
|
260
240
|
severity: 'error',
|
|
261
241
|
code: 'RESOLVER_FORBIDDEN'
|
|
262
242
|
});
|
|
@@ -267,12 +247,12 @@ class OpraAdapter {
|
|
|
267
247
|
result = await resolverInfo.handler(context, query);
|
|
268
248
|
result = Array.isArray(result) ? result[0] : result;
|
|
269
249
|
if (!result)
|
|
270
|
-
throw new
|
|
250
|
+
throw new common_1.ResourceNotFoundError(resource.name);
|
|
271
251
|
const v = await this._pathWalkThrough(query, query.dataType, result, resource.name);
|
|
272
252
|
if (v.value === undefined)
|
|
273
|
-
throw new
|
|
253
|
+
throw new common_1.ResourceNotFoundError(v.path);
|
|
274
254
|
if (v.dataType)
|
|
275
|
-
context.responseHeaders.
|
|
255
|
+
context.responseHeaders[common_1.HttpHeaders.X_Opra_DataType] = v.dataType.name;
|
|
276
256
|
return v.value;
|
|
277
257
|
}
|
|
278
258
|
}
|
|
@@ -283,14 +263,14 @@ class OpraAdapter {
|
|
|
283
263
|
if (context.resultPath) {
|
|
284
264
|
const pathArray = context.resultPath.split('.');
|
|
285
265
|
for (const field of pathArray) {
|
|
286
|
-
const prop = dataType instanceof
|
|
266
|
+
const prop = dataType instanceof common_1.ComplexType ? dataType.fields.get(field) : undefined;
|
|
287
267
|
dataType = prop && prop.type ? this.document.types.get(prop.type) : undefined;
|
|
288
268
|
result = result && typeof result === 'object' && result[field];
|
|
289
269
|
}
|
|
290
270
|
}
|
|
291
271
|
if (method === 'create')
|
|
292
272
|
context.status = 201;
|
|
293
|
-
context.responseHeaders.
|
|
273
|
+
context.responseHeaders[common_1.HttpHeaders.X_Opra_DataType] = resource.dataType.name;
|
|
294
274
|
return result;
|
|
295
275
|
}
|
|
296
276
|
async _pathWalkThrough(query, dataType, value, parentPath) {
|
|
@@ -305,7 +285,7 @@ class OpraAdapter {
|
|
|
305
285
|
let v = value[key];
|
|
306
286
|
if (v == null)
|
|
307
287
|
return { path };
|
|
308
|
-
if (child.child && child.dataType instanceof
|
|
288
|
+
if (child.child && child.dataType instanceof common_1.ComplexType) {
|
|
309
289
|
if (Array.isArray(v))
|
|
310
290
|
v = v[0];
|
|
311
291
|
return this._pathWalkThrough(child, child.dataType, v, path);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExecutionContextHost = void 0;
|
|
4
|
+
const strict_typed_events_1 = require("strict-typed-events");
|
|
5
|
+
class ExecutionContextHost extends strict_typed_events_1.AsyncEventEmitter {
|
|
6
|
+
userContext;
|
|
7
|
+
constructor() {
|
|
8
|
+
super();
|
|
9
|
+
}
|
|
10
|
+
switchToHttp() {
|
|
11
|
+
throw new Error(`This is not an http context`);
|
|
12
|
+
}
|
|
13
|
+
onFinish(fn) {
|
|
14
|
+
this.on('finish', fn);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.ExecutionContextHost = ExecutionContextHost;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExpressRequestWrapperHost = void 0;
|
|
4
|
+
class ExpressRequestWrapperHost {
|
|
5
|
+
instance;
|
|
6
|
+
constructor(instance) {
|
|
7
|
+
this.instance = instance;
|
|
8
|
+
}
|
|
9
|
+
getInstance() {
|
|
10
|
+
return this.instance;
|
|
11
|
+
}
|
|
12
|
+
getMethod() {
|
|
13
|
+
return this.instance.method;
|
|
14
|
+
}
|
|
15
|
+
getUrl() {
|
|
16
|
+
return this.instance.url;
|
|
17
|
+
}
|
|
18
|
+
getHeaderNames() {
|
|
19
|
+
return Object.keys(this.instance.headers);
|
|
20
|
+
}
|
|
21
|
+
getHeader(name) {
|
|
22
|
+
return this.instance.get(name);
|
|
23
|
+
}
|
|
24
|
+
getHeaders() {
|
|
25
|
+
return this.instance.headers;
|
|
26
|
+
}
|
|
27
|
+
getBody() {
|
|
28
|
+
return this.instance.body;
|
|
29
|
+
}
|
|
30
|
+
isCompleted() {
|
|
31
|
+
return this.instance.complete;
|
|
32
|
+
}
|
|
33
|
+
getStream() {
|
|
34
|
+
return this.instance;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.ExpressRequestWrapperHost = ExpressRequestWrapperHost;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExpressResponseWrapperHost = void 0;
|
|
4
|
+
class ExpressResponseWrapperHost {
|
|
5
|
+
instance;
|
|
6
|
+
constructor(instance) {
|
|
7
|
+
this.instance = instance;
|
|
8
|
+
}
|
|
9
|
+
getInstance() {
|
|
10
|
+
return this.instance;
|
|
11
|
+
}
|
|
12
|
+
getHeader(name) {
|
|
13
|
+
return this.instance.get(name);
|
|
14
|
+
}
|
|
15
|
+
getHeaders() {
|
|
16
|
+
return this.instance.getHeaders();
|
|
17
|
+
}
|
|
18
|
+
getHeaderNames() {
|
|
19
|
+
return this.instance.getHeaderNames();
|
|
20
|
+
}
|
|
21
|
+
hasHeader(name) {
|
|
22
|
+
return this.instance.hasHeader(name);
|
|
23
|
+
}
|
|
24
|
+
removeHeader(name) {
|
|
25
|
+
return this.instance.removeHeader(name);
|
|
26
|
+
}
|
|
27
|
+
setHeader(name, value) {
|
|
28
|
+
this.instance.setHeader(name, value);
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
getStatus() {
|
|
32
|
+
return this.instance.statusCode;
|
|
33
|
+
}
|
|
34
|
+
setStatus(value) {
|
|
35
|
+
// noinspection SuspiciousTypeOfGuard
|
|
36
|
+
this.instance.status(typeof value === 'number'
|
|
37
|
+
? value
|
|
38
|
+
: parseInt(value, 10) || 500);
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
getStream() {
|
|
42
|
+
return this.instance;
|
|
43
|
+
}
|
|
44
|
+
send(body) {
|
|
45
|
+
if (typeof body === 'string' || Buffer.isBuffer(body))
|
|
46
|
+
this.instance.send(body);
|
|
47
|
+
else
|
|
48
|
+
this.instance.json(body);
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
end() {
|
|
52
|
+
this.instance.end();
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.ExpressResponseWrapperHost = ExpressResponseWrapperHost;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpExecutionContextHost = void 0;
|
|
4
|
+
const execution_context_host_js_1 = require("./execution-context.host.js");
|
|
5
|
+
class HttpExecutionContextHost extends execution_context_host_js_1.ExecutionContextHost {
|
|
6
|
+
_platform;
|
|
7
|
+
_request;
|
|
8
|
+
_response;
|
|
9
|
+
constructor(_platform, _request, _response) {
|
|
10
|
+
super();
|
|
11
|
+
this._platform = _platform;
|
|
12
|
+
this._request = _request;
|
|
13
|
+
this._response = _response;
|
|
14
|
+
}
|
|
15
|
+
getType() {
|
|
16
|
+
return 'http';
|
|
17
|
+
}
|
|
18
|
+
getPlatform() {
|
|
19
|
+
return this._platform;
|
|
20
|
+
}
|
|
21
|
+
getRequest() {
|
|
22
|
+
return this._request;
|
|
23
|
+
}
|
|
24
|
+
getResponse() {
|
|
25
|
+
return this._response;
|
|
26
|
+
}
|
|
27
|
+
switchToHttp() {
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.HttpExecutionContextHost = HttpExecutionContextHost;
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MetadataResource = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
-
const
|
|
6
|
-
const json_singleton_service_js_1 = require("
|
|
5
|
+
const common_1 = require("@opra/common");
|
|
6
|
+
const json_singleton_service_js_1 = require("../../services/json-singleton-service.js");
|
|
7
7
|
let MetadataResource = class MetadataResource {
|
|
8
8
|
service;
|
|
9
9
|
init(resource) {
|
|
@@ -16,7 +16,7 @@ let MetadataResource = class MetadataResource {
|
|
|
16
16
|
}
|
|
17
17
|
};
|
|
18
18
|
MetadataResource = tslib_1.__decorate([
|
|
19
|
-
(0,
|
|
19
|
+
(0, common_1.OprSingletonResource)(Object, {
|
|
20
20
|
name: '$metadata'
|
|
21
21
|
})
|
|
22
22
|
], MetadataResource);
|
|
@@ -3,18 +3,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.OpraExpressAdapter = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const body_parser_1 = tslib_1.__importDefault(require("body-parser"));
|
|
6
|
-
const
|
|
7
|
-
const
|
|
6
|
+
const common_1 = require("@opra/common");
|
|
7
|
+
const express_request_wrapper_host_js_1 = require("./classes/express-request-wrapper.host.js");
|
|
8
|
+
const express_response_wrapper_host_js_1 = require("./classes/express-response-wrapper.host.js");
|
|
9
|
+
const http_execution_context_host_js_1 = require("./classes/http-execution-context.host.js");
|
|
8
10
|
const http_adapter_js_1 = require("./http-adapter.js");
|
|
9
11
|
class OpraExpressAdapter extends http_adapter_js_1.OpraHttpAdapter {
|
|
10
12
|
static async init(app, document, options) {
|
|
11
13
|
const adapter = new OpraExpressAdapter(document);
|
|
12
14
|
await adapter._init(options);
|
|
13
|
-
const prefix = '/' + (0,
|
|
15
|
+
const prefix = '/' + (0, common_1.normalizePath)(options?.prefix, true);
|
|
14
16
|
app.use(prefix, body_parser_1.default.json());
|
|
15
17
|
app.use(prefix, (request, response, next) => {
|
|
16
18
|
(async () => {
|
|
17
|
-
const executionContext = new
|
|
19
|
+
const executionContext = new http_execution_context_host_js_1.HttpExecutionContextHost('express', new express_request_wrapper_host_js_1.ExpressRequestWrapperHost(request), new express_response_wrapper_host_js_1.ExpressResponseWrapperHost(response));
|
|
18
20
|
await adapter.handler(executionContext);
|
|
19
21
|
})().catch(e => next(e));
|
|
20
22
|
});
|
|
@@ -22,103 +24,3 @@ class OpraExpressAdapter extends http_adapter_js_1.OpraHttpAdapter {
|
|
|
22
24
|
}
|
|
23
25
|
}
|
|
24
26
|
exports.OpraExpressAdapter = OpraExpressAdapter;
|
|
25
|
-
class ExpressExecutionContext extends strict_typed_events_1.AsyncEventEmitter {
|
|
26
|
-
_request;
|
|
27
|
-
_response;
|
|
28
|
-
constructor(request, response) {
|
|
29
|
-
super();
|
|
30
|
-
this._request = new ExpressRequestWrapper(request);
|
|
31
|
-
this._response = new ExpressResponseWrapper(response);
|
|
32
|
-
}
|
|
33
|
-
getType() {
|
|
34
|
-
return 'http';
|
|
35
|
-
}
|
|
36
|
-
getPlatform() {
|
|
37
|
-
return 'express';
|
|
38
|
-
}
|
|
39
|
-
switchToHttp() {
|
|
40
|
-
return this;
|
|
41
|
-
}
|
|
42
|
-
getRequest() {
|
|
43
|
-
return this._request.getInstance();
|
|
44
|
-
}
|
|
45
|
-
getResponse() {
|
|
46
|
-
return this._response.getInstance();
|
|
47
|
-
}
|
|
48
|
-
getRequestWrapper() {
|
|
49
|
-
return this._request;
|
|
50
|
-
}
|
|
51
|
-
getResponseWrapper() {
|
|
52
|
-
return this._response;
|
|
53
|
-
}
|
|
54
|
-
onFinish(fn) {
|
|
55
|
-
this.on('finish', fn);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
class ExpressRequestWrapper {
|
|
59
|
-
instance;
|
|
60
|
-
constructor(instance) {
|
|
61
|
-
this.instance = instance;
|
|
62
|
-
}
|
|
63
|
-
getInstance() {
|
|
64
|
-
return this.instance;
|
|
65
|
-
}
|
|
66
|
-
getMethod() {
|
|
67
|
-
return this.instance.method;
|
|
68
|
-
}
|
|
69
|
-
getUrl() {
|
|
70
|
-
return this.instance.url;
|
|
71
|
-
}
|
|
72
|
-
getHeaderNames() {
|
|
73
|
-
return Object.keys(this.instance.headers);
|
|
74
|
-
}
|
|
75
|
-
getHeader(name) {
|
|
76
|
-
return this.instance.get(name);
|
|
77
|
-
}
|
|
78
|
-
getHeaders() {
|
|
79
|
-
return this.instance.headers;
|
|
80
|
-
}
|
|
81
|
-
getBody() {
|
|
82
|
-
return this.instance.body;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
class ExpressResponseWrapper {
|
|
86
|
-
instance;
|
|
87
|
-
constructor(instance) {
|
|
88
|
-
this.instance = instance;
|
|
89
|
-
}
|
|
90
|
-
getInstance() {
|
|
91
|
-
return this.instance;
|
|
92
|
-
}
|
|
93
|
-
getHeaderNames() {
|
|
94
|
-
return this.instance.getHeaderNames();
|
|
95
|
-
}
|
|
96
|
-
getHeader(name) {
|
|
97
|
-
return this.instance.get(name);
|
|
98
|
-
}
|
|
99
|
-
setHeader(name, value) {
|
|
100
|
-
this.instance.setHeader(name, value);
|
|
101
|
-
return this;
|
|
102
|
-
}
|
|
103
|
-
getStatus() {
|
|
104
|
-
return this.instance.statusCode;
|
|
105
|
-
}
|
|
106
|
-
setStatus(value) {
|
|
107
|
-
// noinspection SuspiciousTypeOfGuard
|
|
108
|
-
this.instance.status(typeof value === 'number'
|
|
109
|
-
? value
|
|
110
|
-
: parseInt(value, 10) || 500);
|
|
111
|
-
return this;
|
|
112
|
-
}
|
|
113
|
-
send(body) {
|
|
114
|
-
if (typeof body === 'string' || Buffer.isBuffer(body))
|
|
115
|
-
this.instance.send(body);
|
|
116
|
-
else
|
|
117
|
-
this.instance.json(body);
|
|
118
|
-
return this;
|
|
119
|
-
}
|
|
120
|
-
end() {
|
|
121
|
-
this.instance.end();
|
|
122
|
-
return this;
|
|
123
|
-
}
|
|
124
|
-
}
|