@opra/core 1.0.0-alpha.26 → 1.0.0-alpha.28
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.
|
@@ -64,10 +64,10 @@ class ExpressAdapter extends http_adapter_js_1.HttpAdapter {
|
|
|
64
64
|
}
|
|
65
65
|
else
|
|
66
66
|
this.app.use(router);
|
|
67
|
-
const createContext = (_req, _res, args) => {
|
|
67
|
+
const createContext = async (_req, _res, args) => {
|
|
68
68
|
const request = http_incoming_interface_js_1.HttpIncoming.from(_req);
|
|
69
69
|
const response = http_outgoing_interface_js_1.HttpOutgoing.from(_res);
|
|
70
|
-
|
|
70
|
+
const ctx = new http_context_js_1.HttpContext({
|
|
71
71
|
adapter: this,
|
|
72
72
|
platform: this.platform,
|
|
73
73
|
request,
|
|
@@ -77,11 +77,14 @@ class ExpressAdapter extends http_adapter_js_1.HttpAdapter {
|
|
|
77
77
|
operation: args?.operation,
|
|
78
78
|
operationHandler: args?.operationHandler,
|
|
79
79
|
});
|
|
80
|
+
await this.emitAsync('createContext', ctx);
|
|
81
|
+
return ctx;
|
|
80
82
|
};
|
|
81
83
|
/** Add an endpoint that returns document schema */
|
|
82
84
|
router.get('/\\$schema', (_req, _res, next) => {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
+
createContext(_req, _res)
|
|
86
|
+
.then(ctx => this.handler.sendDocumentSchema(ctx).catch(next))
|
|
87
|
+
.catch(next);
|
|
85
88
|
});
|
|
86
89
|
/** Add operation endpoints */
|
|
87
90
|
if (this.api.controllers.size) {
|
|
@@ -95,14 +98,13 @@ class ExpressAdapter extends http_adapter_js_1.HttpAdapter {
|
|
|
95
98
|
continue;
|
|
96
99
|
/** Define router callback */
|
|
97
100
|
router[operation.method.toLowerCase()](routePath, (_req, _res, _next) => {
|
|
98
|
-
|
|
101
|
+
createContext(_req, _res, {
|
|
99
102
|
controller,
|
|
100
103
|
controllerInstance,
|
|
101
104
|
operation,
|
|
102
105
|
operationHandler,
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
.handleRequest(context)
|
|
106
|
+
})
|
|
107
|
+
.then(ctx => this.handler.handleRequest(ctx))
|
|
106
108
|
.then(() => {
|
|
107
109
|
if (!_res.headersSent)
|
|
108
110
|
_next();
|
|
@@ -120,15 +122,18 @@ class ExpressAdapter extends http_adapter_js_1.HttpAdapter {
|
|
|
120
122
|
}
|
|
121
123
|
/** Add an endpoint that returns 404 error at last */
|
|
122
124
|
router.use('*', (_req, _res, next) => {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
125
|
+
createContext(_req, _res)
|
|
126
|
+
.then(ctx => {
|
|
127
|
+
ctx.errors.push(new common_1.NotFoundError({
|
|
128
|
+
message: `No endpoint found at [${_req.method}]${_req.baseUrl}`,
|
|
129
|
+
details: {
|
|
130
|
+
path: _req.baseUrl,
|
|
131
|
+
method: _req.method,
|
|
132
|
+
},
|
|
133
|
+
}));
|
|
134
|
+
this.handler.sendResponse(ctx).catch(next);
|
|
135
|
+
})
|
|
136
|
+
.catch(next);
|
|
132
137
|
});
|
|
133
138
|
}
|
|
134
139
|
_createControllers(controller) {
|
package/cjs/http/http-handler.js
CHANGED
|
@@ -52,11 +52,14 @@ class HttpHandler {
|
|
|
52
52
|
await this.adapter.emitAsync('request', context);
|
|
53
53
|
// Call interceptors than execute request
|
|
54
54
|
if (this.adapter.interceptors) {
|
|
55
|
+
const interceptors = this.adapter.interceptors;
|
|
55
56
|
let i = 0;
|
|
56
57
|
const next = async () => {
|
|
57
|
-
const interceptor =
|
|
58
|
-
if (interceptor)
|
|
58
|
+
const interceptor = interceptors[i++];
|
|
59
|
+
if (typeof interceptor === 'function')
|
|
59
60
|
await interceptor(context, next);
|
|
61
|
+
else if (typeof interceptor?.intercept === 'function')
|
|
62
|
+
await interceptor.intercept(context, next);
|
|
60
63
|
await this._executeRequest(context);
|
|
61
64
|
};
|
|
62
65
|
await next();
|
|
@@ -380,7 +383,7 @@ class HttpHandler {
|
|
|
380
383
|
async _sendErrorResponse(context) {
|
|
381
384
|
context.errors = this._wrapExceptions(context.errors);
|
|
382
385
|
try {
|
|
383
|
-
await this.adapter.emitAsync('error', context
|
|
386
|
+
await this.adapter.emitAsync('error', context);
|
|
384
387
|
context.errors = this._wrapExceptions(context.errors);
|
|
385
388
|
}
|
|
386
389
|
catch (e) {
|
|
@@ -60,10 +60,10 @@ export class ExpressAdapter extends HttpAdapter {
|
|
|
60
60
|
}
|
|
61
61
|
else
|
|
62
62
|
this.app.use(router);
|
|
63
|
-
const createContext = (_req, _res, args) => {
|
|
63
|
+
const createContext = async (_req, _res, args) => {
|
|
64
64
|
const request = HttpIncoming.from(_req);
|
|
65
65
|
const response = HttpOutgoing.from(_res);
|
|
66
|
-
|
|
66
|
+
const ctx = new HttpContext({
|
|
67
67
|
adapter: this,
|
|
68
68
|
platform: this.platform,
|
|
69
69
|
request,
|
|
@@ -73,11 +73,14 @@ export class ExpressAdapter extends HttpAdapter {
|
|
|
73
73
|
operation: args?.operation,
|
|
74
74
|
operationHandler: args?.operationHandler,
|
|
75
75
|
});
|
|
76
|
+
await this.emitAsync('createContext', ctx);
|
|
77
|
+
return ctx;
|
|
76
78
|
};
|
|
77
79
|
/** Add an endpoint that returns document schema */
|
|
78
80
|
router.get('/\\$schema', (_req, _res, next) => {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
+
createContext(_req, _res)
|
|
82
|
+
.then(ctx => this.handler.sendDocumentSchema(ctx).catch(next))
|
|
83
|
+
.catch(next);
|
|
81
84
|
});
|
|
82
85
|
/** Add operation endpoints */
|
|
83
86
|
if (this.api.controllers.size) {
|
|
@@ -91,14 +94,13 @@ export class ExpressAdapter extends HttpAdapter {
|
|
|
91
94
|
continue;
|
|
92
95
|
/** Define router callback */
|
|
93
96
|
router[operation.method.toLowerCase()](routePath, (_req, _res, _next) => {
|
|
94
|
-
|
|
97
|
+
createContext(_req, _res, {
|
|
95
98
|
controller,
|
|
96
99
|
controllerInstance,
|
|
97
100
|
operation,
|
|
98
101
|
operationHandler,
|
|
99
|
-
})
|
|
100
|
-
|
|
101
|
-
.handleRequest(context)
|
|
102
|
+
})
|
|
103
|
+
.then(ctx => this.handler.handleRequest(ctx))
|
|
102
104
|
.then(() => {
|
|
103
105
|
if (!_res.headersSent)
|
|
104
106
|
_next();
|
|
@@ -116,15 +118,18 @@ export class ExpressAdapter extends HttpAdapter {
|
|
|
116
118
|
}
|
|
117
119
|
/** Add an endpoint that returns 404 error at last */
|
|
118
120
|
router.use('*', (_req, _res, next) => {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
121
|
+
createContext(_req, _res)
|
|
122
|
+
.then(ctx => {
|
|
123
|
+
ctx.errors.push(new NotFoundError({
|
|
124
|
+
message: `No endpoint found at [${_req.method}]${_req.baseUrl}`,
|
|
125
|
+
details: {
|
|
126
|
+
path: _req.baseUrl,
|
|
127
|
+
method: _req.method,
|
|
128
|
+
},
|
|
129
|
+
}));
|
|
130
|
+
this.handler.sendResponse(ctx).catch(next);
|
|
131
|
+
})
|
|
132
|
+
.catch(next);
|
|
128
133
|
});
|
|
129
134
|
}
|
|
130
135
|
_createControllers(controller) {
|
package/esm/http/http-handler.js
CHANGED
|
@@ -48,11 +48,14 @@ export class HttpHandler {
|
|
|
48
48
|
await this.adapter.emitAsync('request', context);
|
|
49
49
|
// Call interceptors than execute request
|
|
50
50
|
if (this.adapter.interceptors) {
|
|
51
|
+
const interceptors = this.adapter.interceptors;
|
|
51
52
|
let i = 0;
|
|
52
53
|
const next = async () => {
|
|
53
|
-
const interceptor =
|
|
54
|
-
if (interceptor)
|
|
54
|
+
const interceptor = interceptors[i++];
|
|
55
|
+
if (typeof interceptor === 'function')
|
|
55
56
|
await interceptor(context, next);
|
|
57
|
+
else if (typeof interceptor?.intercept === 'function')
|
|
58
|
+
await interceptor.intercept(context, next);
|
|
56
59
|
await this._executeRequest(context);
|
|
57
60
|
};
|
|
58
61
|
await next();
|
|
@@ -376,7 +379,7 @@ export class HttpHandler {
|
|
|
376
379
|
async _sendErrorResponse(context) {
|
|
377
380
|
context.errors = this._wrapExceptions(context.errors);
|
|
378
381
|
try {
|
|
379
|
-
await this.adapter.emitAsync('error', context
|
|
382
|
+
await this.adapter.emitAsync('error', context);
|
|
380
383
|
context.errors = this._wrapExceptions(context.errors);
|
|
381
384
|
}
|
|
382
385
|
catch (e) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/core",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.28",
|
|
4
4
|
"description": "Opra schema package",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@browsery/http-parser": "^0.5.8",
|
|
35
35
|
"@browsery/type-is": "^1.6.18-r2",
|
|
36
|
-
"@opra/common": "^1.0.0-alpha.
|
|
36
|
+
"@opra/common": "^1.0.0-alpha.28",
|
|
37
37
|
"accepts": "^1.3.8",
|
|
38
38
|
"base64-stream": "^1.0.0",
|
|
39
39
|
"busboy": "^1.6.0",
|
|
@@ -1,19 +1,28 @@
|
|
|
1
|
-
import { ApiDocument, HttpApi,
|
|
1
|
+
import { ApiDocument, HttpApi, OpraSchema } from '@opra/common';
|
|
2
2
|
import { PlatformAdapter } from '../platform-adapter.js';
|
|
3
3
|
import { HttpContext } from './http-context.js';
|
|
4
4
|
import { HttpHandler } from './http-handler.js';
|
|
5
5
|
export declare namespace HttpAdapter {
|
|
6
|
+
type NextCallback = () => Promise<void>;
|
|
6
7
|
/**
|
|
7
|
-
* @type
|
|
8
|
+
* @type InterceptorFunction
|
|
8
9
|
*/
|
|
9
|
-
type
|
|
10
|
+
type InterceptorFunction = IHttpInterceptor['intercept'];
|
|
11
|
+
/**
|
|
12
|
+
* @interface IHttpInterceptor
|
|
13
|
+
*/
|
|
14
|
+
type IHttpInterceptor = {
|
|
15
|
+
intercept(context: HttpContext, next: NextCallback): Promise<void>;
|
|
16
|
+
};
|
|
10
17
|
interface Options extends PlatformAdapter.Options {
|
|
11
18
|
basePath?: string;
|
|
12
|
-
interceptors?:
|
|
19
|
+
interceptors?: (InterceptorFunction | IHttpInterceptor)[];
|
|
13
20
|
}
|
|
21
|
+
type EventFunction = (context: HttpContext) => void | Promise<void>;
|
|
14
22
|
interface Events {
|
|
15
|
-
|
|
16
|
-
|
|
23
|
+
createContext: EventFunction;
|
|
24
|
+
error: EventFunction;
|
|
25
|
+
request: EventFunction;
|
|
17
26
|
}
|
|
18
27
|
}
|
|
19
28
|
export interface HttpAdapter {
|
|
@@ -39,7 +48,7 @@ export interface HttpAdapter {
|
|
|
39
48
|
export declare abstract class HttpAdapter extends PlatformAdapter {
|
|
40
49
|
readonly handler: HttpHandler;
|
|
41
50
|
readonly protocol: OpraSchema.Protocol;
|
|
42
|
-
interceptors: HttpAdapter.
|
|
51
|
+
interceptors: (HttpAdapter.InterceptorFunction | HttpAdapter.IHttpInterceptor)[];
|
|
43
52
|
protected constructor(document: ApiDocument, options?: HttpAdapter.Options);
|
|
44
53
|
get api(): HttpApi;
|
|
45
54
|
}
|