@opra/core 0.5.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 -122
- 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 +270 -68
- package/cjs/adapter/request-contexts/batch-request-context.js +12 -0
- package/cjs/adapter/{query-context.js → request-contexts/request-context.js} +8 -12
- package/cjs/adapter/request-contexts/single-request-context.js +15 -0
- package/cjs/index.js +2 -1
- 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 -108
- 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 -46
- 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 -1
- package/esm/index.js +2 -1
- 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/utils/path-to-tree.js +0 -28
- package/esm/adapter/metadata-resource.d.ts +0 -8
- package/esm/adapter/query-context.d.ts +0 -24
- 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,149 +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
7
|
const create_i18n_js_1 = require("../utils/create-i18n.js");
|
|
10
|
-
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;
|
|
11
12
|
class OpraAdapter {
|
|
12
13
|
document;
|
|
13
|
-
i18n;
|
|
14
14
|
userContextResolver;
|
|
15
|
-
// protected _metadataResource: SingletonResourceInfo;
|
|
16
15
|
_internalResources = new common_1.ResponsiveMap();
|
|
16
|
+
i18n;
|
|
17
17
|
constructor(document) {
|
|
18
18
|
this.document = document;
|
|
19
19
|
}
|
|
20
20
|
async handler(executionContext) {
|
|
21
|
-
let
|
|
22
|
-
let userContext;
|
|
21
|
+
let requestContext;
|
|
23
22
|
let failed = false;
|
|
24
23
|
try {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
exclusive = exclusive || context.query.operation !== 'read';
|
|
32
|
-
// Wait previous read requests before executing update request
|
|
33
|
-
if (exclusive && promises) {
|
|
34
|
-
await Promise.allSettled(promises);
|
|
35
|
-
promises = undefined;
|
|
36
|
-
}
|
|
37
|
-
// If previous request in bucket had an error and executed an update
|
|
38
|
-
// we do not execute next requests
|
|
39
|
-
if (stop) {
|
|
40
|
-
context.errors.push(new exception_1.FailedDependencyError());
|
|
41
|
-
continue;
|
|
42
|
-
}
|
|
43
|
-
try {
|
|
44
|
-
const promise = (async () => {
|
|
45
|
-
// if (context.query.method === 'metadata') {
|
|
46
|
-
// await this._getSchemaExecute(context); //todo
|
|
47
|
-
// return;
|
|
48
|
-
// }
|
|
49
|
-
const resource = context.query.resource;
|
|
50
|
-
await this._resourcePrepare(resource, context);
|
|
51
|
-
if (this.userContextResolver && !userContext)
|
|
52
|
-
userContext = this.userContextResolver({
|
|
53
|
-
executionContext,
|
|
54
|
-
isBatch: this.isBatch(executionContext)
|
|
55
|
-
});
|
|
56
|
-
context.userContext = userContext;
|
|
57
|
-
await this._resourceExecute(this.document, resource, context);
|
|
58
|
-
})().catch(e => {
|
|
59
|
-
context.errors.push(e);
|
|
60
|
-
});
|
|
61
|
-
if (exclusive)
|
|
62
|
-
await promise;
|
|
63
|
-
else {
|
|
64
|
-
promises = promises || [];
|
|
65
|
-
promises.push(promise);
|
|
66
|
-
}
|
|
67
|
-
// todo execute sub property queries
|
|
68
|
-
}
|
|
69
|
-
catch (e) {
|
|
70
|
-
context.errors.unshift(e);
|
|
71
|
-
}
|
|
72
|
-
if (context.errors.length) {
|
|
73
|
-
// noinspection SuspiciousTypeOfGuard
|
|
74
|
-
context.errors = context.errors.map(e => (0, exception_1.wrapException)(e));
|
|
75
|
-
if (exclusive)
|
|
76
|
-
stop = stop || !!context.errors.find(e => !(e.issue.severity === 'warning' || e.issue.severity === 'info'));
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
if (promises)
|
|
80
|
-
await Promise.allSettled(promises);
|
|
81
|
-
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);
|
|
82
30
|
}
|
|
83
31
|
catch (e) {
|
|
84
32
|
failed = true;
|
|
85
|
-
const error = (0,
|
|
33
|
+
const error = (0, common_1.wrapException)(e);
|
|
86
34
|
await this.sendError(executionContext, error);
|
|
87
35
|
}
|
|
88
36
|
finally {
|
|
89
37
|
if (executionContext instanceof strict_typed_events_1.AsyncEventEmitter) {
|
|
90
38
|
await executionContext
|
|
91
39
|
.emitAsyncSerial('finish', {
|
|
92
|
-
|
|
40
|
+
context: executionContext,
|
|
93
41
|
failed
|
|
94
42
|
}).catch();
|
|
95
43
|
}
|
|
96
44
|
}
|
|
97
45
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
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 });
|
|
103
50
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
};
|
|
118
|
-
Object.setPrototypeOf(ctx, context);
|
|
119
|
-
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;
|
|
120
64
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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;
|
|
131
80
|
}
|
|
132
|
-
|
|
81
|
+
/* istanbul ignore next */
|
|
82
|
+
throw new TypeError('Invalid request context instance');
|
|
133
83
|
}
|
|
134
84
|
async _init(options) {
|
|
135
|
-
if (options?.i18n instanceof
|
|
85
|
+
if (options?.i18n instanceof common_1.I18n)
|
|
136
86
|
this.i18n = options.i18n;
|
|
137
87
|
else if (typeof options?.i18n === 'function')
|
|
138
88
|
this.i18n = await options.i18n();
|
|
139
89
|
else
|
|
140
90
|
this.i18n = await (0, create_i18n_js_1.createI18n)(options?.i18n);
|
|
141
|
-
this.i18n = this.i18n ||
|
|
91
|
+
this.i18n = this.i18n || common_1.I18n.defaultInstance;
|
|
142
92
|
if (!this.i18n.isInitialized)
|
|
143
93
|
await this.i18n.init();
|
|
144
94
|
this.userContextResolver = options?.userContext;
|
|
145
95
|
const metadataResource = new metadata_resource_js_1.MetadataResource();
|
|
146
|
-
const metadataResourceInfo = new
|
|
96
|
+
const metadataResourceInfo = new common_1.SingletonResourceInfo(this.document, '$metadata', this.document.getComplexDataType('object'), {
|
|
147
97
|
kind: 'SingletonResource',
|
|
148
98
|
type: 'object',
|
|
149
99
|
instance: metadataResource,
|
|
@@ -161,12 +111,44 @@ class OpraAdapter {
|
|
|
161
111
|
}
|
|
162
112
|
}
|
|
163
113
|
}
|
|
164
|
-
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) {
|
|
165
147
|
const method = context.query.method;
|
|
166
148
|
const resolverInfo = resource.metadata[method];
|
|
167
149
|
if (!(resolverInfo && resolverInfo.handler))
|
|
168
|
-
throw new
|
|
169
|
-
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`),
|
|
170
152
|
severity: 'error',
|
|
171
153
|
code: 'RESOLVER_FORBIDDEN'
|
|
172
154
|
});
|
|
@@ -178,13 +160,12 @@ class OpraAdapter {
|
|
|
178
160
|
result = Array.isArray(result) ? result[0] : result;
|
|
179
161
|
if (result)
|
|
180
162
|
context.status = 201;
|
|
181
|
-
context.responseHeaders
|
|
163
|
+
context.responseHeaders[common_1.HttpHeaders.X_Opra_DataType] = resource.dataType.name;
|
|
182
164
|
return result;
|
|
183
165
|
}
|
|
184
166
|
case 'count': {
|
|
185
167
|
const query = context.query;
|
|
186
168
|
result = await resolverInfo.handler(context, query);
|
|
187
|
-
context.responseHeaders.set(common_1.HttpHeaders.X_Opra_Count, result);
|
|
188
169
|
return result;
|
|
189
170
|
}
|
|
190
171
|
case 'get': {
|
|
@@ -192,19 +173,19 @@ class OpraAdapter {
|
|
|
192
173
|
result = await resolverInfo.handler(context, query.keyValue, query);
|
|
193
174
|
result = Array.isArray(result) ? result[0] : result;
|
|
194
175
|
if (!result)
|
|
195
|
-
throw new
|
|
176
|
+
throw new common_1.ResourceNotFoundError(resource.name, query.keyValue);
|
|
196
177
|
const v = await this._pathWalkThrough(query, query.dataType, result, resource.name);
|
|
197
178
|
if (v.value === undefined)
|
|
198
|
-
throw new
|
|
179
|
+
throw new common_1.ResourceNotFoundError(v.path);
|
|
199
180
|
if (v.dataType)
|
|
200
|
-
context.responseHeaders
|
|
181
|
+
context.responseHeaders[common_1.HttpHeaders.X_Opra_DataType] = v.dataType.name;
|
|
201
182
|
return v.value;
|
|
202
183
|
}
|
|
203
184
|
case 'search': {
|
|
204
185
|
const query = context.query;
|
|
205
186
|
result = await resolverInfo.handler(context, query);
|
|
206
187
|
const items = Array.isArray(result) ? result : (context.response ? [result] : []);
|
|
207
|
-
context.responseHeaders
|
|
188
|
+
context.responseHeaders[common_1.HttpHeaders.X_Opra_DataType] = resource.dataType.name;
|
|
208
189
|
return items;
|
|
209
190
|
}
|
|
210
191
|
case 'update': {
|
|
@@ -212,8 +193,8 @@ class OpraAdapter {
|
|
|
212
193
|
result = await resolverInfo.handler(context, query.keyValue, query.data, query);
|
|
213
194
|
result = Array.isArray(result) ? result[0] : result;
|
|
214
195
|
if (!result)
|
|
215
|
-
throw new
|
|
216
|
-
context.responseHeaders
|
|
196
|
+
throw new common_1.ResourceNotFoundError(resource.name, query.keyValue);
|
|
197
|
+
context.responseHeaders[common_1.HttpHeaders.X_Opra_DataType] = resource.dataType.name;
|
|
217
198
|
return result;
|
|
218
199
|
}
|
|
219
200
|
case 'delete':
|
|
@@ -250,12 +231,12 @@ class OpraAdapter {
|
|
|
250
231
|
}
|
|
251
232
|
}
|
|
252
233
|
}
|
|
253
|
-
async
|
|
234
|
+
async _executeSingletonResource(document, resource, context) {
|
|
254
235
|
const method = context.query.method;
|
|
255
236
|
const resolverInfo = resource.metadata[method];
|
|
256
237
|
if (!(resolverInfo && resolverInfo.handler))
|
|
257
|
-
throw new
|
|
258
|
-
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`),
|
|
259
240
|
severity: 'error',
|
|
260
241
|
code: 'RESOLVER_FORBIDDEN'
|
|
261
242
|
});
|
|
@@ -266,12 +247,12 @@ class OpraAdapter {
|
|
|
266
247
|
result = await resolverInfo.handler(context, query);
|
|
267
248
|
result = Array.isArray(result) ? result[0] : result;
|
|
268
249
|
if (!result)
|
|
269
|
-
throw new
|
|
250
|
+
throw new common_1.ResourceNotFoundError(resource.name);
|
|
270
251
|
const v = await this._pathWalkThrough(query, query.dataType, result, resource.name);
|
|
271
252
|
if (v.value === undefined)
|
|
272
|
-
throw new
|
|
253
|
+
throw new common_1.ResourceNotFoundError(v.path);
|
|
273
254
|
if (v.dataType)
|
|
274
|
-
context.responseHeaders
|
|
255
|
+
context.responseHeaders[common_1.HttpHeaders.X_Opra_DataType] = v.dataType.name;
|
|
275
256
|
return v.value;
|
|
276
257
|
}
|
|
277
258
|
}
|
|
@@ -282,14 +263,14 @@ class OpraAdapter {
|
|
|
282
263
|
if (context.resultPath) {
|
|
283
264
|
const pathArray = context.resultPath.split('.');
|
|
284
265
|
for (const field of pathArray) {
|
|
285
|
-
const prop = dataType instanceof
|
|
266
|
+
const prop = dataType instanceof common_1.ComplexType ? dataType.fields.get(field) : undefined;
|
|
286
267
|
dataType = prop && prop.type ? this.document.types.get(prop.type) : undefined;
|
|
287
268
|
result = result && typeof result === 'object' && result[field];
|
|
288
269
|
}
|
|
289
270
|
}
|
|
290
271
|
if (method === 'create')
|
|
291
272
|
context.status = 201;
|
|
292
|
-
context.responseHeaders
|
|
273
|
+
context.responseHeaders[common_1.HttpHeaders.X_Opra_DataType] = resource.dataType.name;
|
|
293
274
|
return result;
|
|
294
275
|
}
|
|
295
276
|
async _pathWalkThrough(query, dataType, value, parentPath) {
|
|
@@ -304,7 +285,7 @@ class OpraAdapter {
|
|
|
304
285
|
let v = value[key];
|
|
305
286
|
if (v == null)
|
|
306
287
|
return { path };
|
|
307
|
-
if (child.child && child.dataType instanceof
|
|
288
|
+
if (child.child && child.dataType instanceof common_1.ComplexType) {
|
|
308
289
|
if (Array.isArray(v))
|
|
309
290
|
v = v[0];
|
|
310
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
|
-
}
|