@opra/core 0.14.0 → 0.16.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 +89 -293
- package/cjs/adapter/http/express-adapter.js +27 -0
- package/cjs/adapter/http/http-adapter.js +467 -0
- package/cjs/adapter/http/http-request-context.host.js +28 -0
- package/cjs/adapter/http/http-request.host.js +14 -0
- package/cjs/adapter/http/http-response.host.js +14 -0
- package/cjs/adapter/internal/metadata.resource.js +27 -0
- package/cjs/adapter/request-context.host.js +30 -0
- package/cjs/adapter/request.host.js +19 -0
- package/cjs/adapter/response.host.js +20 -0
- package/cjs/augmentation/resource.augmentation.js +26 -0
- package/cjs/index.js +7 -8
- package/esm/adapter/adapter.js +97 -297
- package/esm/adapter/http/express-adapter.js +27 -0
- package/esm/adapter/http/http-adapter.js +467 -0
- package/esm/adapter/http/http-request-context.host.js +28 -0
- package/esm/adapter/http/http-request.host.js +14 -0
- package/esm/adapter/http/http-response.host.js +14 -0
- package/esm/adapter/interfaces/request-context.interface.js +2 -0
- package/esm/adapter/interfaces/request.interface.js +2 -0
- package/esm/adapter/interfaces/response.interface.js +2 -0
- package/esm/adapter/internal/metadata.resource.js +27 -0
- package/esm/adapter/request-context.host.js +30 -0
- package/esm/adapter/request.host.js +19 -0
- package/esm/adapter/response.host.js +20 -0
- package/esm/augmentation/resource.augmentation.js +26 -0
- package/esm/index.js +13 -11
- package/esm/types.js +2 -1
- package/i18n/en/error.json +2 -2
- package/package.json +11 -8
- package/types/adapter/adapter.d.ts +63 -0
- package/types/adapter/http/express-adapter.d.ts +11 -0
- package/types/adapter/http/http-adapter.d.ts +37 -0
- package/types/adapter/http/http-request-context.host.d.ts +16 -0
- package/types/adapter/http/http-request.host.d.ts +7 -0
- package/types/adapter/http/http-response.host.d.ts +7 -0
- package/{esm → types/adapter}/interfaces/logger.interface.d.ts +1 -0
- package/types/adapter/interfaces/request-context.interface.d.ts +25 -0
- package/types/adapter/interfaces/request.interface.d.ts +13 -0
- package/types/adapter/interfaces/response.interface.d.ts +22 -0
- package/types/adapter/internal/metadata.resource.d.ts +7 -0
- package/types/adapter/request-context.host.d.ts +19 -0
- package/types/adapter/request.host.d.ts +26 -0
- package/types/adapter/response.host.d.ts +20 -0
- package/types/augmentation/resource.augmentation.d.ts +33 -0
- package/types/index.d.ts +10 -0
- package/cjs/adapter/classes/execution-context.host.js +0 -16
- package/cjs/adapter/classes/express-request-wrapper.host.js +0 -36
- package/cjs/adapter/classes/express-response-wrapper.host.js +0 -55
- package/cjs/adapter/classes/http-execution-context.host.js +0 -28
- package/cjs/adapter/classes/metadata.resource.js +0 -22
- package/cjs/adapter/express-adapter.js +0 -26
- package/cjs/adapter/http-adapter.js +0 -443
- package/cjs/adapter/request-contexts/batch-request-context.js +0 -11
- package/cjs/adapter/request-contexts/request-context.js +0 -25
- package/cjs/adapter/request-contexts/single-request-context.js +0 -14
- package/cjs/services/data-service.js +0 -9
- package/cjs/services/json-singleton-service.js +0 -96
- package/cjs/utils/create-i18n.js +0 -21
- package/cjs/utils/get-caller-file.util.js +0 -24
- package/esm/adapter/adapter.d.ts +0 -38
- package/esm/adapter/classes/execution-context.host.d.ts +0 -10
- package/esm/adapter/classes/execution-context.host.js +0 -12
- package/esm/adapter/classes/express-request-wrapper.host.d.ts +0 -19
- package/esm/adapter/classes/express-request-wrapper.host.js +0 -32
- package/esm/adapter/classes/express-response-wrapper.host.d.ts +0 -22
- package/esm/adapter/classes/express-response-wrapper.host.js +0 -51
- package/esm/adapter/classes/http-execution-context.host.d.ts +0 -13
- package/esm/adapter/classes/http-execution-context.host.js +0 -24
- package/esm/adapter/classes/metadata.resource.d.ts +0 -8
- package/esm/adapter/classes/metadata.resource.js +0 -19
- package/esm/adapter/express-adapter.d.ts +0 -11
- package/esm/adapter/express-adapter.js +0 -21
- package/esm/adapter/http-adapter.d.ts +0 -37
- package/esm/adapter/http-adapter.js +0 -439
- package/esm/adapter/request-contexts/batch-request-context.d.ts +0 -7
- package/esm/adapter/request-contexts/batch-request-context.js +0 -7
- package/esm/adapter/request-contexts/request-context.d.ts +0 -22
- package/esm/adapter/request-contexts/request-context.js +0 -21
- package/esm/adapter/request-contexts/single-request-context.d.ts +0 -10
- package/esm/adapter/request-contexts/single-request-context.js +0 -10
- package/esm/enums/issue-severity.enum.d.ts +0 -1
- package/esm/enums/issue-severity.enum.js +0 -1
- package/esm/index.d.ts +0 -11
- package/esm/interfaces/execution-context.interface.d.ts +0 -47
- package/esm/interfaces/execution-context.interface.js +0 -1
- package/esm/interfaces/i18n-options.interface.d.ts +0 -28
- package/esm/interfaces/i18n-options.interface.js +0 -1
- package/esm/interfaces/logger.interface.js +0 -1
- package/esm/interfaces/resource.interface.d.ts +0 -23
- package/esm/interfaces/resource.interface.js +0 -1
- package/esm/services/data-service.d.ts +0 -2
- package/esm/services/data-service.js +0 -5
- package/esm/services/json-singleton-service.d.ts +0 -39
- package/esm/services/json-singleton-service.js +0 -91
- package/esm/utils/create-i18n.d.ts +0 -3
- package/esm/utils/create-i18n.js +0 -16
- package/esm/utils/get-caller-file.util.d.ts +0 -1
- package/esm/utils/get-caller-file.util.js +0 -20
- /package/cjs/{interfaces → adapter/interfaces}/logger.interface.js +0 -0
- /package/cjs/{enums/issue-severity.enum.js → adapter/interfaces/request-context.interface.js} +0 -0
- /package/cjs/{interfaces/execution-context.interface.js → adapter/interfaces/request.interface.js} +0 -0
- /package/cjs/{interfaces/i18n-options.interface.js → adapter/interfaces/response.interface.js} +0 -0
- /package/{cjs/interfaces/resource.interface.js → esm/adapter/interfaces/logger.interface.js} +0 -0
- /package/{esm → types}/types.d.ts +0 -0
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OpraHttpAdapter = void 0;
|
|
4
|
+
const power_tasks_1 = require("power-tasks");
|
|
5
|
+
const common_1 = require("@opra/common");
|
|
6
|
+
const adapter_js_1 = require("../adapter.js");
|
|
7
|
+
const http_request_host_js_1 = require("./http-request.host.js");
|
|
8
|
+
const http_request_context_host_js_1 = require("./http-request-context.host.js");
|
|
9
|
+
const http_response_host_js_1 = require("./http-response.host.js");
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
* @class OpraHttpAdapter
|
|
13
|
+
*/
|
|
14
|
+
class OpraHttpAdapter extends adapter_js_1.OpraAdapter {
|
|
15
|
+
/**
|
|
16
|
+
* Main http request handler
|
|
17
|
+
* @param incoming
|
|
18
|
+
* @param outgoing
|
|
19
|
+
* @protected
|
|
20
|
+
*/
|
|
21
|
+
async handler(incoming, outgoing) {
|
|
22
|
+
try {
|
|
23
|
+
// Batch
|
|
24
|
+
if (incoming.is('multipart/mixed')) {
|
|
25
|
+
throw new common_1.BadRequestError({ message: 'Not implemented yet' });
|
|
26
|
+
}
|
|
27
|
+
if (!(incoming.method === 'POST' || incoming.method === 'PATCH') || incoming.is('json')) {
|
|
28
|
+
const request = await this.parseRequest(incoming);
|
|
29
|
+
const response = new http_response_host_js_1.HttpResponseHost({}, outgoing);
|
|
30
|
+
const context = new http_request_context_host_js_1.HttpRequestContextHost(this.platform, this.api, request, response);
|
|
31
|
+
const task = new power_tasks_1.Task(async () => {
|
|
32
|
+
try {
|
|
33
|
+
await this.executeRequest(context);
|
|
34
|
+
if (request.operation === 'findMany' && request.args.count && response.count != null) {
|
|
35
|
+
response.switchToHttp().header(common_1.HttpHeaderCodes.X_Opra_Total_Matches, String(response.count));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
return this.errorHandler(incoming, outgoing, [error]);
|
|
40
|
+
}
|
|
41
|
+
await this.sendResponse(context);
|
|
42
|
+
}, {
|
|
43
|
+
id: incoming.get('content-id'),
|
|
44
|
+
exclusive: request.crud !== 'read'
|
|
45
|
+
});
|
|
46
|
+
await task.toPromise().catch(e => {
|
|
47
|
+
this.logger?.error?.(e);
|
|
48
|
+
outgoing.sendStatus(500);
|
|
49
|
+
});
|
|
50
|
+
await this.emitAsync('request-finish', context);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
throw new common_1.BadRequestError({ message: 'Unsupported Content-Type' });
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
await this.errorHandler(incoming, outgoing, [error]);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async errorHandler(incoming, outgoing, errors) {
|
|
60
|
+
errors.forEach(e => {
|
|
61
|
+
this.log((e instanceof common_1.OpraException) ? 'error' : 'fatal', incoming, e); // todo. implement a better logger
|
|
62
|
+
});
|
|
63
|
+
errors = errors.map(common_1.wrapException);
|
|
64
|
+
let status = outgoing.statusCode || 0;
|
|
65
|
+
// Sort errors from fatal to info
|
|
66
|
+
errors.sort((a, b) => {
|
|
67
|
+
const i = common_1.IssueSeverity.Keys.indexOf(a.issue.severity) - common_1.IssueSeverity.Keys.indexOf(b.issue.severity);
|
|
68
|
+
if (i === 0)
|
|
69
|
+
return b.status - a.status;
|
|
70
|
+
return i;
|
|
71
|
+
});
|
|
72
|
+
if (!status || status < common_1.HttpStatusCodes.BAD_REQUEST) {
|
|
73
|
+
status = errors[0].status;
|
|
74
|
+
if (status < common_1.HttpStatusCodes.BAD_REQUEST)
|
|
75
|
+
status = common_1.HttpStatusCodes.INTERNAL_SERVER_ERROR;
|
|
76
|
+
}
|
|
77
|
+
const body = this.i18n.deep({
|
|
78
|
+
errors: errors.map(e => e.issue)
|
|
79
|
+
});
|
|
80
|
+
outgoing.set(common_1.HttpHeaderCodes.Content_Type, 'application/json; charset=utf-8');
|
|
81
|
+
outgoing.set(common_1.HttpHeaderCodes.Cache_Control, 'no-cache');
|
|
82
|
+
outgoing.set(common_1.HttpHeaderCodes.Pragma, 'no-cache');
|
|
83
|
+
outgoing.set(common_1.HttpHeaderCodes.Expires, '-1');
|
|
84
|
+
outgoing.set(common_1.HttpHeaderCodes.X_Opra_Version, common_1.OpraSchema.SpecVersion);
|
|
85
|
+
outgoing.status(status);
|
|
86
|
+
outgoing.send(JSON.stringify(body));
|
|
87
|
+
outgoing.end();
|
|
88
|
+
}
|
|
89
|
+
log(logType, incoming, message, ...optionalParams) {
|
|
90
|
+
const logFn = logType === 'fatal'
|
|
91
|
+
? this.logger?.fatal || this.logger?.error
|
|
92
|
+
: this.logger?.[logType];
|
|
93
|
+
if (!logFn)
|
|
94
|
+
return;
|
|
95
|
+
logFn.apply(this.logger, [String(message), ...optionalParams]);
|
|
96
|
+
}
|
|
97
|
+
async executeRequest(context) {
|
|
98
|
+
await super.executeRequest(context);
|
|
99
|
+
const { request } = context;
|
|
100
|
+
const response = context.response;
|
|
101
|
+
const { crud } = request;
|
|
102
|
+
const httpResponse = response.switchToHttp();
|
|
103
|
+
if (request.resource instanceof common_1.Singleton || request.resource instanceof common_1.Collection) {
|
|
104
|
+
httpResponse.set(common_1.HttpHeaderCodes.X_Opra_Data_Type, request.resource.type.name);
|
|
105
|
+
httpResponse.set(common_1.HttpHeaderCodes.X_Opra_Operation, request.operation);
|
|
106
|
+
}
|
|
107
|
+
if (crud === 'create') {
|
|
108
|
+
if (!response.value)
|
|
109
|
+
throw new common_1.InternalServerError();
|
|
110
|
+
// todo validate
|
|
111
|
+
httpResponse.status(201);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
*
|
|
116
|
+
* @param incoming
|
|
117
|
+
* @protected
|
|
118
|
+
*/
|
|
119
|
+
async parseRequest(incoming) {
|
|
120
|
+
try {
|
|
121
|
+
const url = new common_1.OpraURL();
|
|
122
|
+
url.searchParams.define({
|
|
123
|
+
'$pick': { codec: 'string', array: true },
|
|
124
|
+
'$omit': { codec: 'string', array: true },
|
|
125
|
+
'$include': { codec: 'string', array: true },
|
|
126
|
+
'$sort': { codec: 'string', array: true },
|
|
127
|
+
'$filter': { codec: 'filter' },
|
|
128
|
+
'$limit': { codec: 'number' },
|
|
129
|
+
'$skip': { codec: 'number' },
|
|
130
|
+
'$distinct': { codec: 'boolean' },
|
|
131
|
+
'$count': { codec: 'boolean' },
|
|
132
|
+
});
|
|
133
|
+
url.parse(incoming.url);
|
|
134
|
+
// const {context, url, method, headers, body, contentId} = args;
|
|
135
|
+
if (!url.path.size)
|
|
136
|
+
throw new common_1.BadRequestError();
|
|
137
|
+
const method = incoming.method;
|
|
138
|
+
if (method !== 'GET' && url.path.size > 1)
|
|
139
|
+
throw new common_1.BadRequestError();
|
|
140
|
+
// const pathLen = url.path.size;
|
|
141
|
+
// let pathIndex = 0;
|
|
142
|
+
const params = url.searchParams;
|
|
143
|
+
const p = url.path.get(0);
|
|
144
|
+
const resource = this._internalDoc.getResource(p.resource);
|
|
145
|
+
// let container: IResourceContainer | undefined;
|
|
146
|
+
// while (resource && resource instanceof ContainerResourceInfo) {
|
|
147
|
+
// container = resource;
|
|
148
|
+
// p = url.path.get(++pathIndex);
|
|
149
|
+
// resource = container.getResource(p.resource);
|
|
150
|
+
// }
|
|
151
|
+
// const headers = incoming.headers;
|
|
152
|
+
/*
|
|
153
|
+
* Collection
|
|
154
|
+
*/
|
|
155
|
+
if (resource instanceof common_1.Collection) {
|
|
156
|
+
switch (method) {
|
|
157
|
+
case 'POST': {
|
|
158
|
+
if (!p.key) {
|
|
159
|
+
return new http_request_host_js_1.HttpRequestHost({
|
|
160
|
+
kind: 'CollectionCreateRequest',
|
|
161
|
+
resource,
|
|
162
|
+
operation: 'create',
|
|
163
|
+
crud: 'create',
|
|
164
|
+
many: false,
|
|
165
|
+
args: {
|
|
166
|
+
data: incoming.body,
|
|
167
|
+
pick: resource.normalizeFieldNames(params.get('$pick')),
|
|
168
|
+
omit: resource.normalizeFieldNames(params.get('$omit')),
|
|
169
|
+
include: resource.normalizeFieldNames(params.get('$include'))
|
|
170
|
+
}
|
|
171
|
+
}, incoming);
|
|
172
|
+
}
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
case 'DELETE': {
|
|
176
|
+
if (p.key) {
|
|
177
|
+
return new http_request_host_js_1.HttpRequestHost({
|
|
178
|
+
kind: 'CollectionDeleteRequest',
|
|
179
|
+
resource,
|
|
180
|
+
operation: 'delete',
|
|
181
|
+
crud: 'delete',
|
|
182
|
+
many: false,
|
|
183
|
+
args: {
|
|
184
|
+
key: resource.parseKeyValue(p.key)
|
|
185
|
+
}
|
|
186
|
+
}, incoming);
|
|
187
|
+
}
|
|
188
|
+
return new http_request_host_js_1.HttpRequestHost({
|
|
189
|
+
kind: 'CollectionDeleteManyRequest',
|
|
190
|
+
resource,
|
|
191
|
+
operation: 'deleteMany',
|
|
192
|
+
crud: 'delete',
|
|
193
|
+
many: true,
|
|
194
|
+
args: {
|
|
195
|
+
filter: resource.normalizeFilterFields(params.get('$filter'))
|
|
196
|
+
}
|
|
197
|
+
}, incoming);
|
|
198
|
+
}
|
|
199
|
+
case 'GET': {
|
|
200
|
+
if (p.key) {
|
|
201
|
+
return new http_request_host_js_1.HttpRequestHost({
|
|
202
|
+
kind: 'CollectionGetRequest',
|
|
203
|
+
resource,
|
|
204
|
+
operation: 'get',
|
|
205
|
+
crud: 'read',
|
|
206
|
+
many: false,
|
|
207
|
+
args: {
|
|
208
|
+
key: resource.parseKeyValue(p.key),
|
|
209
|
+
pick: resource.normalizeFieldNames(params.get('$pick')),
|
|
210
|
+
omit: resource.normalizeFieldNames(params.get('$omit')),
|
|
211
|
+
include: resource.normalizeFieldNames(params.get('$include'))
|
|
212
|
+
}
|
|
213
|
+
}, incoming);
|
|
214
|
+
}
|
|
215
|
+
return new http_request_host_js_1.HttpRequestHost({
|
|
216
|
+
kind: 'CollectionFindManyRequest',
|
|
217
|
+
resource,
|
|
218
|
+
operation: 'findMany',
|
|
219
|
+
crud: 'read',
|
|
220
|
+
many: true,
|
|
221
|
+
args: {
|
|
222
|
+
sort: resource.normalizeSortFields(params.get('$sort')),
|
|
223
|
+
pick: resource.normalizeFieldNames(params.get('$pick')),
|
|
224
|
+
omit: resource.normalizeFieldNames(params.get('$omit')),
|
|
225
|
+
include: resource.normalizeFieldNames(params.get('$include')),
|
|
226
|
+
filter: resource.normalizeFilterFields(params.get('$filter')),
|
|
227
|
+
limit: params.get('$limit'),
|
|
228
|
+
skip: params.get('$skip'),
|
|
229
|
+
distinct: params.get('$distinct'),
|
|
230
|
+
count: params.get('$count'),
|
|
231
|
+
}
|
|
232
|
+
}, incoming);
|
|
233
|
+
}
|
|
234
|
+
case 'PATCH': {
|
|
235
|
+
if (p.key) {
|
|
236
|
+
return new http_request_host_js_1.HttpRequestHost({
|
|
237
|
+
kind: 'CollectionUpdateRequest',
|
|
238
|
+
resource,
|
|
239
|
+
operation: 'update',
|
|
240
|
+
crud: 'update',
|
|
241
|
+
many: false,
|
|
242
|
+
args: {
|
|
243
|
+
key: resource.parseKeyValue(p.key),
|
|
244
|
+
data: incoming.body,
|
|
245
|
+
pick: resource.normalizeFieldNames(params.get('$pick')),
|
|
246
|
+
omit: resource.normalizeFieldNames(params.get('$omit')),
|
|
247
|
+
include: resource.normalizeFieldNames(params.get('$include'))
|
|
248
|
+
}
|
|
249
|
+
}, incoming);
|
|
250
|
+
}
|
|
251
|
+
return new http_request_host_js_1.HttpRequestHost({
|
|
252
|
+
kind: 'CollectionUpdateManyRequest',
|
|
253
|
+
resource,
|
|
254
|
+
operation: 'updateMany',
|
|
255
|
+
crud: 'update',
|
|
256
|
+
many: true,
|
|
257
|
+
args: {
|
|
258
|
+
data: incoming.body,
|
|
259
|
+
filter: resource.normalizeFilterFields(params.get('$filter'))
|
|
260
|
+
}
|
|
261
|
+
}, incoming);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
else
|
|
266
|
+
/*
|
|
267
|
+
* Singleton
|
|
268
|
+
*/
|
|
269
|
+
if (resource instanceof common_1.Singleton && !p.key) {
|
|
270
|
+
switch (method) {
|
|
271
|
+
case 'POST': {
|
|
272
|
+
return new http_request_host_js_1.HttpRequestHost({
|
|
273
|
+
kind: 'SingletonCreateRequest',
|
|
274
|
+
resource,
|
|
275
|
+
operation: 'create',
|
|
276
|
+
crud: 'create',
|
|
277
|
+
many: false,
|
|
278
|
+
args: {
|
|
279
|
+
data: incoming.body,
|
|
280
|
+
pick: resource.normalizeFieldNames(params.get('$pick')),
|
|
281
|
+
omit: resource.normalizeFieldNames(params.get('$omit')),
|
|
282
|
+
include: resource.normalizeFieldNames(params.get('$include'))
|
|
283
|
+
}
|
|
284
|
+
}, incoming);
|
|
285
|
+
}
|
|
286
|
+
case 'DELETE': {
|
|
287
|
+
return new http_request_host_js_1.HttpRequestHost({
|
|
288
|
+
kind: 'SingletonDeleteRequest',
|
|
289
|
+
resource,
|
|
290
|
+
operation: 'delete',
|
|
291
|
+
crud: 'delete',
|
|
292
|
+
many: false,
|
|
293
|
+
args: {}
|
|
294
|
+
}, incoming);
|
|
295
|
+
}
|
|
296
|
+
case 'GET': {
|
|
297
|
+
return new http_request_host_js_1.HttpRequestHost({
|
|
298
|
+
kind: 'SingletonGetRequest',
|
|
299
|
+
resource,
|
|
300
|
+
operation: 'get',
|
|
301
|
+
crud: 'read',
|
|
302
|
+
many: false,
|
|
303
|
+
args: {
|
|
304
|
+
pick: resource.normalizeFieldNames(params.get('$pick')),
|
|
305
|
+
omit: resource.normalizeFieldNames(params.get('$omit')),
|
|
306
|
+
include: resource.normalizeFieldNames(params.get('$include'))
|
|
307
|
+
}
|
|
308
|
+
}, incoming);
|
|
309
|
+
}
|
|
310
|
+
case 'PATCH': {
|
|
311
|
+
return new http_request_host_js_1.HttpRequestHost({
|
|
312
|
+
kind: 'SingletonUpdateRequest',
|
|
313
|
+
resource,
|
|
314
|
+
operation: 'update',
|
|
315
|
+
crud: 'update',
|
|
316
|
+
many: false,
|
|
317
|
+
args: {
|
|
318
|
+
data: incoming.body,
|
|
319
|
+
pick: resource.normalizeFieldNames(params.get('$pick')),
|
|
320
|
+
omit: resource.normalizeFieldNames(params.get('$omit')),
|
|
321
|
+
include: resource.normalizeFieldNames(params.get('$include'))
|
|
322
|
+
}
|
|
323
|
+
}, incoming);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
else
|
|
328
|
+
throw new common_1.InternalServerError();
|
|
329
|
+
// if (query instanceof SingletonGetQuery || query instanceof CollectionGetQuery || query instanceof ElementReadQuery) {
|
|
330
|
+
// // Move through properties
|
|
331
|
+
// let parentType: DataType;
|
|
332
|
+
// const curPath: string[] = [];
|
|
333
|
+
// let parent: SingletonGetQuery | CollectionGetQuery | ElementReadQuery = query;
|
|
334
|
+
// while (++pathIndex < pathLen) {
|
|
335
|
+
// p = url.path.get(pathIndex);
|
|
336
|
+
// parentType = parent.type;
|
|
337
|
+
// if (parent.type instanceof UnionType) {
|
|
338
|
+
// if (parent.type.name === 'any')
|
|
339
|
+
// parentType = this.document.getComplexType('object');
|
|
340
|
+
// else
|
|
341
|
+
// throw new TypeError(`"${resource.name}.${curPath.join()}" is a UnionType and needs type casting.`);
|
|
342
|
+
// }
|
|
343
|
+
// if (!(parentType instanceof ComplexType))
|
|
344
|
+
// throw new TypeError(`"${resource.name}.${curPath.join()}" is not a ComplexType and has no fields.`);
|
|
345
|
+
// curPath.push(p.resource);
|
|
346
|
+
// parent.child = new ElementReadQuery(parent, p.resource, {castingType: parentType});
|
|
347
|
+
// parent = parent.child;
|
|
348
|
+
// }
|
|
349
|
+
// }
|
|
350
|
+
throw new common_1.MethodNotAllowedError({
|
|
351
|
+
message: `Method "${method}" is not allowed by target endpoint`
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
catch (e) {
|
|
355
|
+
if (e instanceof common_1.OpraException)
|
|
356
|
+
throw e;
|
|
357
|
+
throw new common_1.BadRequestError(e);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
// async parseMultiPart(
|
|
361
|
+
// context: TExecutionContext,
|
|
362
|
+
// url: OpraURL,
|
|
363
|
+
// headers: IncomingHttpHeaders,
|
|
364
|
+
// input: Readable,
|
|
365
|
+
// boundary: string
|
|
366
|
+
// ): Promise<BatchRequestContext> {
|
|
367
|
+
// return await new Promise((resolve, reject) => {
|
|
368
|
+
// let _resolved = false;
|
|
369
|
+
// const dicer = new Dicer({boundary});
|
|
370
|
+
// const doReject = (e) => {
|
|
371
|
+
// if (_resolved) return;
|
|
372
|
+
// _resolved = true;
|
|
373
|
+
// reject(e);
|
|
374
|
+
// taskQueue.clearQueue();
|
|
375
|
+
// dicer.destroy();
|
|
376
|
+
// }
|
|
377
|
+
// const taskQueue = new TaskQueue({concurrency: 1});
|
|
378
|
+
// taskQueue.on('error', doReject);
|
|
379
|
+
//
|
|
380
|
+
// const queries: SingleRequestContext[] = [];
|
|
381
|
+
// let partCounter = 0;
|
|
382
|
+
// dicer.on('error', doReject);
|
|
383
|
+
// dicer.on('part', part => {
|
|
384
|
+
// const partIndex = partCounter++;
|
|
385
|
+
// let header: any;
|
|
386
|
+
// const chunks: Buffer[] = [];
|
|
387
|
+
// part.on('error', doReject);
|
|
388
|
+
// part.on('header', (_header) => header = normalizeHeaders(_header));
|
|
389
|
+
// part.on('data', (chunk: Buffer) => chunks.push(chunk));
|
|
390
|
+
// part.on('end', () => {
|
|
391
|
+
// if (_resolved || !(header || chunks.length))
|
|
392
|
+
// return;
|
|
393
|
+
// const ct = header['content-type'];
|
|
394
|
+
// if (ct === 'application/http') {
|
|
395
|
+
// taskQueue.enqueue(async () => {
|
|
396
|
+
// const data = Buffer.concat(chunks);
|
|
397
|
+
// if (!(data && data.length))
|
|
398
|
+
// return;
|
|
399
|
+
// const r = HttpRequest.parse(data);
|
|
400
|
+
// await callMiddlewares(r, [jsonBodyParser]);
|
|
401
|
+
// const subUrl = new OpraURL(r.url);
|
|
402
|
+
// const contentId = header && header['content-id'];
|
|
403
|
+
// queries.push(this.parseSingleQuery({
|
|
404
|
+
// context,
|
|
405
|
+
// url: subUrl,
|
|
406
|
+
// method: r.method,
|
|
407
|
+
// headers: r.headers,
|
|
408
|
+
// body: r.body,
|
|
409
|
+
// contentId
|
|
410
|
+
// }));
|
|
411
|
+
// });
|
|
412
|
+
// } else doReject(new BadRequestError({
|
|
413
|
+
// message: 'Unaccepted "content-type" header in multipart data',
|
|
414
|
+
// details: {
|
|
415
|
+
// position: `${boundary}[${partIndex}]`
|
|
416
|
+
// }
|
|
417
|
+
// }))
|
|
418
|
+
// });
|
|
419
|
+
// });
|
|
420
|
+
// dicer.on('finish', () => {
|
|
421
|
+
// taskQueue.enqueue(() => {
|
|
422
|
+
// if (_resolved) return;
|
|
423
|
+
// _resolved = true;
|
|
424
|
+
// const batch = new BatchRequestContext({
|
|
425
|
+
// service: this.document,
|
|
426
|
+
// context,
|
|
427
|
+
// headers,
|
|
428
|
+
// queries,
|
|
429
|
+
// params: url.searchParams,
|
|
430
|
+
// continueOnError: false
|
|
431
|
+
// });
|
|
432
|
+
// resolve(batch);
|
|
433
|
+
// });
|
|
434
|
+
// });
|
|
435
|
+
// input.pipe(dicer);
|
|
436
|
+
// });
|
|
437
|
+
// }
|
|
438
|
+
async sendResponse(context) {
|
|
439
|
+
const { request, response } = context;
|
|
440
|
+
const outgoing = response.switchToHttp();
|
|
441
|
+
const errors = response.errors?.map(e => (0, common_1.wrapException)(e));
|
|
442
|
+
if (errors && errors.length) {
|
|
443
|
+
await this.errorHandler(request.switchToHttp(), outgoing, errors);
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
outgoing.set(common_1.HttpHeaderCodes.Cache_Control, 'no-cache');
|
|
447
|
+
outgoing.set(common_1.HttpHeaderCodes.Pragma, 'no-cache');
|
|
448
|
+
outgoing.set(common_1.HttpHeaderCodes.Expires, '-1');
|
|
449
|
+
outgoing.set(common_1.HttpHeaderCodes.X_Opra_Version, common_1.OpraSchema.SpecVersion);
|
|
450
|
+
outgoing.status(outgoing.statusCode || common_1.HttpStatusCodes.OK);
|
|
451
|
+
if (response.value) {
|
|
452
|
+
if (typeof response.value === 'object') {
|
|
453
|
+
if ((0, common_1.isReadable)(response.value) || Buffer.isBuffer(response.value))
|
|
454
|
+
outgoing.send(response.value);
|
|
455
|
+
else {
|
|
456
|
+
const body = this.i18n.deep(response.value);
|
|
457
|
+
outgoing.set(common_1.HttpHeaderCodes.Content_Type, 'application/json; charset=utf-8');
|
|
458
|
+
outgoing.send(JSON.stringify(body));
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
else
|
|
462
|
+
outgoing.send(JSON.stringify(response.value));
|
|
463
|
+
}
|
|
464
|
+
outgoing.end();
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
exports.OpraHttpAdapter = OpraHttpAdapter;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpRequestContextHost = void 0;
|
|
4
|
+
const request_context_host_js_1 = require("../request-context.host.js");
|
|
5
|
+
class HttpRequestContextHost extends request_context_host_js_1.RequestContextHost {
|
|
6
|
+
constructor(platform, api, _request, _response) {
|
|
7
|
+
super('http', platform, api, _request, _response);
|
|
8
|
+
this.platform = platform;
|
|
9
|
+
this.api = api;
|
|
10
|
+
this._request = _request;
|
|
11
|
+
this._response = _response;
|
|
12
|
+
}
|
|
13
|
+
switchToHttp() {
|
|
14
|
+
const obj = {
|
|
15
|
+
request: this._request.switchToHttp(),
|
|
16
|
+
response: this._response.switchToHttp()
|
|
17
|
+
};
|
|
18
|
+
Object.setPrototypeOf(obj, this);
|
|
19
|
+
return obj;
|
|
20
|
+
}
|
|
21
|
+
switchToWs() {
|
|
22
|
+
throw new TypeError('Not executing in an "WebSocket" protocol');
|
|
23
|
+
}
|
|
24
|
+
switchToRpc() {
|
|
25
|
+
throw new TypeError('Not executing in an "RPC" protocol');
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.HttpRequestContextHost = HttpRequestContextHost;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpRequestHost = void 0;
|
|
4
|
+
const request_host_js_1 = require("../request.host.js");
|
|
5
|
+
class HttpRequestHost extends request_host_js_1.RequestHost {
|
|
6
|
+
constructor(init, _incoming) {
|
|
7
|
+
super(init);
|
|
8
|
+
this._incoming = _incoming;
|
|
9
|
+
}
|
|
10
|
+
switchToHttp() {
|
|
11
|
+
return this._incoming;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.HttpRequestHost = HttpRequestHost;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpResponseHost = void 0;
|
|
4
|
+
const response_host_js_1 = require("../response.host.js");
|
|
5
|
+
class HttpResponseHost extends response_host_js_1.ResponseHost {
|
|
6
|
+
constructor(init, _outgoing) {
|
|
7
|
+
super(init);
|
|
8
|
+
this._outgoing = _outgoing;
|
|
9
|
+
}
|
|
10
|
+
switchToHttp() {
|
|
11
|
+
return this._outgoing;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.HttpResponseHost = HttpResponseHost;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MetadataResource = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const common_1 = require("@opra/common");
|
|
6
|
+
let MetadataResource = class MetadataResource {
|
|
7
|
+
constructor(document) {
|
|
8
|
+
this.document = document;
|
|
9
|
+
this._schema = document.exportSchema();
|
|
10
|
+
}
|
|
11
|
+
get() {
|
|
12
|
+
return (0, common_1.cloneObject)(this.document.exportSchema(), true);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
tslib_1.__decorate([
|
|
16
|
+
common_1.Singleton.Get(),
|
|
17
|
+
tslib_1.__metadata("design:type", Function),
|
|
18
|
+
tslib_1.__metadata("design:paramtypes", []),
|
|
19
|
+
tslib_1.__metadata("design:returntype", void 0)
|
|
20
|
+
], MetadataResource.prototype, "get", null);
|
|
21
|
+
MetadataResource = tslib_1.__decorate([
|
|
22
|
+
(0, common_1.Singleton)(Object, {
|
|
23
|
+
name: '$metadata',
|
|
24
|
+
}),
|
|
25
|
+
tslib_1.__metadata("design:paramtypes", [common_1.ApiDocument])
|
|
26
|
+
], MetadataResource);
|
|
27
|
+
exports.MetadataResource = MetadataResource;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RequestContextHost = void 0;
|
|
4
|
+
const strict_typed_events_1 = require("strict-typed-events");
|
|
5
|
+
class RequestContextHost extends strict_typed_events_1.AsyncEventEmitter {
|
|
6
|
+
constructor(protocol, platform, api, _request, _response) {
|
|
7
|
+
super();
|
|
8
|
+
this.protocol = protocol;
|
|
9
|
+
this.platform = platform;
|
|
10
|
+
this.api = api;
|
|
11
|
+
this._request = _request;
|
|
12
|
+
this._response = _response;
|
|
13
|
+
}
|
|
14
|
+
get request() {
|
|
15
|
+
return this._request;
|
|
16
|
+
}
|
|
17
|
+
get response() {
|
|
18
|
+
return this._response;
|
|
19
|
+
}
|
|
20
|
+
switchToHttp() {
|
|
21
|
+
throw new TypeError('Not executing in an "Http" protocol');
|
|
22
|
+
}
|
|
23
|
+
switchToWs() {
|
|
24
|
+
throw new TypeError('Not executing in an "WebSocket" protocol');
|
|
25
|
+
}
|
|
26
|
+
switchToRpc() {
|
|
27
|
+
throw new TypeError('Not executing in an "RPC" protocol');
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.RequestContextHost = RequestContextHost;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RequestHost = void 0;
|
|
4
|
+
class RequestHost {
|
|
5
|
+
constructor(init) {
|
|
6
|
+
Object.assign(this, init);
|
|
7
|
+
this.resourceKind = this.resource.kind;
|
|
8
|
+
}
|
|
9
|
+
switchToHttp() {
|
|
10
|
+
throw new TypeError('Not executing in an "Http" protocol');
|
|
11
|
+
}
|
|
12
|
+
switchToWs() {
|
|
13
|
+
throw new TypeError('Not executing in an "WebSocket" protocol');
|
|
14
|
+
}
|
|
15
|
+
switchToRpc() {
|
|
16
|
+
throw new TypeError('Not executing in an "RPC" protocol');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.RequestHost = RequestHost;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ResponseHost = void 0;
|
|
4
|
+
class ResponseHost {
|
|
5
|
+
constructor(init) {
|
|
6
|
+
if (init)
|
|
7
|
+
Object.assign(this, init);
|
|
8
|
+
this.errors = this.errors || [];
|
|
9
|
+
}
|
|
10
|
+
switchToHttp() {
|
|
11
|
+
throw new TypeError('Not executing in an "Http" protocol');
|
|
12
|
+
}
|
|
13
|
+
switchToWs() {
|
|
14
|
+
throw new TypeError('Not executing in an "WebSocket" protocol');
|
|
15
|
+
}
|
|
16
|
+
switchToRpc() {
|
|
17
|
+
throw new TypeError('Not executing in an "RPC" protocol');
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.ResponseHost = ResponseHost;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const common_1 = require("@opra/common");
|
|
4
|
+
// @ts-ignore
|
|
5
|
+
const oldConstruct = common_1.Resource.prototype._construct;
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
common_1.Resource.prototype._construct = function (init) {
|
|
8
|
+
oldConstruct.call(this, init);
|
|
9
|
+
const _this = this;
|
|
10
|
+
_this.onInit = init.onInit;
|
|
11
|
+
_this.onShutdown = init.onShutdown;
|
|
12
|
+
};
|
|
13
|
+
common_1.Collection.OnInit = common_1.Singleton.OnInit = function () {
|
|
14
|
+
return (target, propertyKey) => {
|
|
15
|
+
const resourceMetadata = (Reflect.getOwnMetadata(common_1.METADATA_KEY, target.constructor) || {});
|
|
16
|
+
resourceMetadata.onInit = target[propertyKey];
|
|
17
|
+
Reflect.defineMetadata(common_1.METADATA_KEY, target.constructor, resourceMetadata);
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
common_1.Collection.OnShutdown = common_1.Singleton.OnShutdown = function () {
|
|
21
|
+
return (target, propertyKey) => {
|
|
22
|
+
const resourceMetadata = (Reflect.getOwnMetadata(common_1.METADATA_KEY, target.constructor) || {});
|
|
23
|
+
resourceMetadata.onShutdown = target[propertyKey];
|
|
24
|
+
Reflect.defineMetadata(common_1.METADATA_KEY, target.constructor, resourceMetadata);
|
|
25
|
+
};
|
|
26
|
+
};
|
package/cjs/index.js
CHANGED
|
@@ -2,13 +2,12 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
require("reflect-metadata");
|
|
5
|
+
require("./augmentation/resource.augmentation.js");
|
|
5
6
|
tslib_1.__exportStar(require("./types.js"), exports);
|
|
6
|
-
tslib_1.__exportStar(require("./interfaces/execution-context.interface.js"), exports);
|
|
7
|
-
tslib_1.__exportStar(require("./interfaces/resource.interface.js"), exports);
|
|
8
|
-
tslib_1.__exportStar(require("./interfaces/i18n-options.interface.js"), exports);
|
|
9
|
-
tslib_1.__exportStar(require("./adapter/request-contexts/single-request-context.js"), exports);
|
|
10
7
|
tslib_1.__exportStar(require("./adapter/adapter.js"), exports);
|
|
11
|
-
tslib_1.__exportStar(require("./adapter/http-adapter.js"), exports);
|
|
12
|
-
tslib_1.__exportStar(require("./adapter/
|
|
13
|
-
tslib_1.__exportStar(require("./
|
|
14
|
-
tslib_1.__exportStar(require("./
|
|
8
|
+
tslib_1.__exportStar(require("./adapter/http/express-adapter.js"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./adapter/http/http-adapter.js"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./adapter/interfaces/request-context.interface.js"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./adapter/interfaces/logger.interface.js"), exports);
|
|
12
|
+
tslib_1.__exportStar(require("./adapter/interfaces/request.interface.js"), exports);
|
|
13
|
+
tslib_1.__exportStar(require("./adapter/interfaces/response.interface.js"), exports);
|