@opra/nestjs-http 1.28.5 → 1.29.1
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/README.md +1 -1
- package/opra-exception-filter.js +1 -1
- package/opra-http-core.module.js +8 -3
- package/opra-http-nestjs-adapter.d.ts +13 -1
- package/opra-http-nestjs-adapter.js +57 -11
- package/opra-middleware.js +12 -13
- package/package.json +6 -6
package/README.md
CHANGED
package/opra-exception-filter.js
CHANGED
|
@@ -27,7 +27,7 @@ let OpraExceptionFilter = class OpraExceptionFilter extends BaseExceptionFilter
|
|
|
27
27
|
if (ctx) {
|
|
28
28
|
const adapter = this.moduleRef.get(OpraHttpNestjsAdapter);
|
|
29
29
|
ctx.errors.push(exception);
|
|
30
|
-
return adapter.
|
|
30
|
+
return adapter.sendResponse(ctx);
|
|
31
31
|
}
|
|
32
32
|
super.catch(exception, host);
|
|
33
33
|
}
|
package/opra-http-core.module.js
CHANGED
|
@@ -2,7 +2,7 @@ var OpraHttpCoreModule_1;
|
|
|
2
2
|
import { __decorate, __metadata } from "tslib";
|
|
3
3
|
import { isConstructor } from '@jsopen/objects';
|
|
4
4
|
import { Global, Logger, Module, RequestMethod, } from '@nestjs/common';
|
|
5
|
-
import { APP_FILTER, ModuleRef } from '@nestjs/core';
|
|
5
|
+
import { APP_FILTER, HttpAdapterHost, ModuleRef } from '@nestjs/core';
|
|
6
6
|
import { ApiDocumentFactory } from '@opra/common';
|
|
7
7
|
import { HttpAdapter, HttpContext } from '@opra/http';
|
|
8
8
|
import { OPRA_HTTP_API_CONFIG } from './constants.js';
|
|
@@ -56,8 +56,8 @@ let OpraHttpCoreModule = OpraHttpCoreModule_1 = class OpraHttpCoreModule {
|
|
|
56
56
|
});
|
|
57
57
|
const adapterProvider = {
|
|
58
58
|
provide: token,
|
|
59
|
-
inject: [ModuleRef, OPRA_HTTP_API_CONFIG],
|
|
60
|
-
useFactory: async (moduleRef, apiConfig) => {
|
|
59
|
+
inject: [ModuleRef, HttpAdapterHost, OPRA_HTTP_API_CONFIG],
|
|
60
|
+
useFactory: async (moduleRef, httpAdapterHost, apiConfig) => {
|
|
61
61
|
opraNestAdapter.scope = apiConfig.scope;
|
|
62
62
|
opraNestAdapter.logger =
|
|
63
63
|
opraNestAdapter.logger || new Logger(apiConfig.name);
|
|
@@ -83,6 +83,11 @@ let OpraHttpCoreModule = OpraHttpCoreModule_1 = class OpraHttpCoreModule {
|
|
|
83
83
|
return x;
|
|
84
84
|
});
|
|
85
85
|
}
|
|
86
|
+
opraNestAdapter.setHttpHandler((req, res) => {
|
|
87
|
+
const httpInstance = httpAdapterHost.httpAdapter?.getInstance();
|
|
88
|
+
if (httpInstance)
|
|
89
|
+
httpInstance(req, res);
|
|
90
|
+
});
|
|
86
91
|
return opraNestAdapter;
|
|
87
92
|
},
|
|
88
93
|
};
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import * as http from 'node:http';
|
|
2
|
+
import { type IncomingMessage, type ServerResponse } from 'node:http';
|
|
1
3
|
import { type Type } from '@nestjs/common';
|
|
2
|
-
import {
|
|
4
|
+
import { HttpController, HttpOperation } from '@opra/common';
|
|
5
|
+
import { HttpAdapter, HttpContext } from '@opra/http';
|
|
3
6
|
/**
|
|
4
7
|
* OpraHttpNestjsAdapter
|
|
5
8
|
*
|
|
@@ -28,6 +31,15 @@ export declare class OpraHttpNestjsAdapter extends HttpAdapter {
|
|
|
28
31
|
* @returns {Promise<void>}
|
|
29
32
|
*/
|
|
30
33
|
close(): Promise<void>;
|
|
34
|
+
createContext(_req: any, _res: any, args?: {
|
|
35
|
+
controller?: HttpController;
|
|
36
|
+
controllerInstance?: any;
|
|
37
|
+
operation?: HttpOperation;
|
|
38
|
+
operationHandler: Function;
|
|
39
|
+
}): Promise<HttpContext>;
|
|
40
|
+
protected _httpHandler?: (req: IncomingMessage, res: ServerResponse) => void;
|
|
41
|
+
setHttpHandler(handler: (req: IncomingMessage, res: ServerResponse) => void): void;
|
|
42
|
+
handleRawRequest(req: http.IncomingMessage, res: http.ServerResponse): Promise<void>;
|
|
31
43
|
/**
|
|
32
44
|
* Adds the root controller that serves the OPRA schema.
|
|
33
45
|
*
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { __decorate, __metadata, __param } from "tslib";
|
|
2
|
+
import * as http from 'node:http';
|
|
3
|
+
import {} from 'node:http';
|
|
2
4
|
import nodePath from 'node:path';
|
|
3
5
|
import { isConstructor } from '@jsopen/objects';
|
|
4
|
-
import { Controller, Delete, Get, Head, Next, Options, Patch, Post, Put, Req, Res, Search, } from '@nestjs/common';
|
|
5
|
-
import { HTTP_CONTROLLER_METADATA, HttpApi, HttpController, NotFoundError, } from '@opra/common';
|
|
6
|
-
import { HttpAdapter, HttpContext } from '@opra/http';
|
|
6
|
+
import { Controller, Delete, Get, Head, HttpCode, Next, Options, Patch, Post, Put, Req, Res, Search, } from '@nestjs/common';
|
|
7
|
+
import { HTTP_CONTROLLER_METADATA, HttpApi, HttpController, HttpOperation, NotFoundError, } from '@opra/common';
|
|
8
|
+
import { HttpAdapter, HttpBundle, HttpContext, HttpRequest, HttpResponse, } from '@opra/http';
|
|
7
9
|
import { OpraNestUtils, Public } from '@opra/nestjs';
|
|
8
10
|
import { asMutable } from 'ts-gems';
|
|
9
11
|
/**
|
|
@@ -26,6 +28,10 @@ export class OpraHttpNestjsAdapter extends HttpAdapter {
|
|
|
26
28
|
constructor(options) {
|
|
27
29
|
super(options);
|
|
28
30
|
this._addRootController(options.schemaIsPublic);
|
|
31
|
+
/* Disable default error handler. Errors will be handled by OpraExceptionFilter */
|
|
32
|
+
this.on('error', (error) => {
|
|
33
|
+
throw error;
|
|
34
|
+
});
|
|
29
35
|
if (options.controllers) {
|
|
30
36
|
for (const c of options.controllers) {
|
|
31
37
|
this._addToNestControllers(c, this.basePath, []);
|
|
@@ -39,6 +45,25 @@ export class OpraHttpNestjsAdapter extends HttpAdapter {
|
|
|
39
45
|
async close() {
|
|
40
46
|
//
|
|
41
47
|
}
|
|
48
|
+
async createContext(_req, _res, args) {
|
|
49
|
+
const ctx = await super.createContext(_req, _res, args);
|
|
50
|
+
// @ts-ignore
|
|
51
|
+
ctx.platform = _req.route ? 'express' : 'fastify';
|
|
52
|
+
return ctx;
|
|
53
|
+
}
|
|
54
|
+
_httpHandler;
|
|
55
|
+
setHttpHandler(handler) {
|
|
56
|
+
this._httpHandler = handler;
|
|
57
|
+
}
|
|
58
|
+
async handleRawRequest(req, res) {
|
|
59
|
+
if (!this._httpHandler)
|
|
60
|
+
throw new Error('HTTP handler is not initialized. Call setHttpHandler() first.');
|
|
61
|
+
return new Promise((resolve, reject) => {
|
|
62
|
+
res.once('finish', resolve);
|
|
63
|
+
res.once('error', reject);
|
|
64
|
+
this._httpHandler(req, res);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
42
67
|
/**
|
|
43
68
|
* Adds the root controller that serves the OPRA schema.
|
|
44
69
|
*
|
|
@@ -49,7 +74,21 @@ export class OpraHttpNestjsAdapter extends HttpAdapter {
|
|
|
49
74
|
const _this = this;
|
|
50
75
|
let RootController = class RootController {
|
|
51
76
|
schema(_req, next) {
|
|
52
|
-
_this.
|
|
77
|
+
_this.sendDocumentSchema(_req.opraContext).catch(() => next());
|
|
78
|
+
}
|
|
79
|
+
bundle(_req, _res, next) {
|
|
80
|
+
Promise.resolve()
|
|
81
|
+
.then(async () => {
|
|
82
|
+
const bundle = new HttpBundle({
|
|
83
|
+
__adapter: _this,
|
|
84
|
+
platform: _req.route ? 'express' : 'fastify',
|
|
85
|
+
request: HttpRequest.create(_req),
|
|
86
|
+
response: HttpResponse.create(_res),
|
|
87
|
+
});
|
|
88
|
+
await _this.emitAsync('create-bundle', bundle);
|
|
89
|
+
await _this.handleBundle(bundle);
|
|
90
|
+
})
|
|
91
|
+
.catch(() => next());
|
|
53
92
|
}
|
|
54
93
|
};
|
|
55
94
|
__decorate([
|
|
@@ -60,6 +99,16 @@ export class OpraHttpNestjsAdapter extends HttpAdapter {
|
|
|
60
99
|
__metadata("design:paramtypes", [Object, Function]),
|
|
61
100
|
__metadata("design:returntype", void 0)
|
|
62
101
|
], RootController.prototype, "schema", null);
|
|
102
|
+
__decorate([
|
|
103
|
+
Post('/\\$bundle'),
|
|
104
|
+
HttpCode(200),
|
|
105
|
+
__param(0, Req()),
|
|
106
|
+
__param(1, Res()),
|
|
107
|
+
__param(2, Next()),
|
|
108
|
+
__metadata("design:type", Function),
|
|
109
|
+
__metadata("design:paramtypes", [Object, Object, Function]),
|
|
110
|
+
__metadata("design:returntype", void 0)
|
|
111
|
+
], RootController.prototype, "bundle", null);
|
|
63
112
|
RootController = __decorate([
|
|
64
113
|
Controller({
|
|
65
114
|
path: this.basePath,
|
|
@@ -96,11 +145,6 @@ export class OpraHttpNestjsAdapter extends HttpAdapter {
|
|
|
96
145
|
? nodePath.posix.join(currentPath, metadata.path)
|
|
97
146
|
: currentPath;
|
|
98
147
|
const adapter = this;
|
|
99
|
-
// adapter.logger =
|
|
100
|
-
/* Disable default error handler. Errors will be handled by OpraExceptionFilter */
|
|
101
|
-
adapter.handler.onError = (context, error) => {
|
|
102
|
-
throw error;
|
|
103
|
-
};
|
|
104
148
|
this.nestControllers.push(newClass);
|
|
105
149
|
let metadataKeys;
|
|
106
150
|
if (metadata.operations) {
|
|
@@ -115,7 +159,9 @@ export class OpraHttpNestjsAdapter extends HttpAdapter {
|
|
|
115
159
|
const controller = api.findController(sourceClass);
|
|
116
160
|
const operation = controller?.operations.get(k);
|
|
117
161
|
const context = asMutable(_req.opraContext);
|
|
118
|
-
if (!(context &&
|
|
162
|
+
if (!(context &&
|
|
163
|
+
operation &&
|
|
164
|
+
typeof operationHandler === 'function')) {
|
|
119
165
|
throw new NotFoundError({
|
|
120
166
|
message: `No endpoint found for [${_req.method}]${_req.baseUrl}`,
|
|
121
167
|
details: {
|
|
@@ -131,7 +177,7 @@ export class OpraHttpNestjsAdapter extends HttpAdapter {
|
|
|
131
177
|
context.__controller = this;
|
|
132
178
|
context.__handler = operationHandler;
|
|
133
179
|
/* Handle request */
|
|
134
|
-
await adapter.
|
|
180
|
+
await adapter.handleRequest(context);
|
|
135
181
|
},
|
|
136
182
|
});
|
|
137
183
|
/* Copy metadata keys from source function to new one */
|
package/opra-middleware.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { __decorate, __metadata } from "tslib";
|
|
2
2
|
import { Injectable } from '@nestjs/common';
|
|
3
|
-
import {
|
|
3
|
+
import { HttpRequest, HttpResponse } from '@opra/http';
|
|
4
4
|
import { OpraHttpNestjsAdapter } from './opra-http-nestjs-adapter.js';
|
|
5
5
|
/**
|
|
6
6
|
* OpraMiddleware
|
|
@@ -21,19 +21,18 @@ let OpraMiddleware = class OpraMiddleware {
|
|
|
21
21
|
* @param next - Function that calls the next middleware.
|
|
22
22
|
*/
|
|
23
23
|
use(req, res, next) {
|
|
24
|
-
const request =
|
|
25
|
-
const response =
|
|
26
|
-
/* Create the HttpContext */
|
|
27
|
-
const context = new HttpContext({
|
|
28
|
-
__adapter: this.opraAdapter,
|
|
29
|
-
platform: req.route ? 'express' : 'fastify',
|
|
30
|
-
request,
|
|
31
|
-
response,
|
|
32
|
-
});
|
|
33
|
-
req.opraContext = context;
|
|
24
|
+
const request = HttpRequest.create(req);
|
|
25
|
+
const response = HttpResponse.create(res);
|
|
34
26
|
this.opraAdapter
|
|
35
|
-
.
|
|
36
|
-
.then(() =>
|
|
27
|
+
.createContext(request, response)
|
|
28
|
+
.then(async (context) => {
|
|
29
|
+
// @ts-ignore
|
|
30
|
+
context.platform = req.route ? 'express' : 'fastify';
|
|
31
|
+
req.opraContext = context;
|
|
32
|
+
await this.opraAdapter
|
|
33
|
+
.emitAsync('createContext', context)
|
|
34
|
+
.then(() => next());
|
|
35
|
+
})
|
|
37
36
|
.catch(next);
|
|
38
37
|
}
|
|
39
38
|
};
|
package/package.json
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/nestjs-http",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.29.1",
|
|
4
4
|
"description": "Opra NestJS Http Module",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://www.oprajs.com",
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@jsopen/objects": "^2.2.
|
|
9
|
+
"@jsopen/objects": "^2.2.3",
|
|
10
10
|
"tslib": "^2.8.1"
|
|
11
11
|
},
|
|
12
12
|
"peerDependencies": {
|
|
13
|
-
"@opra/common": "^1.
|
|
14
|
-
"@opra/core": "^1.
|
|
15
|
-
"@opra/nestjs": "^1.
|
|
16
|
-
"@opra/http": "^1.
|
|
13
|
+
"@opra/common": "^1.29.1",
|
|
14
|
+
"@opra/core": "^1.29.1",
|
|
15
|
+
"@opra/nestjs": "^1.29.1",
|
|
16
|
+
"@opra/http": "^1.29.1",
|
|
17
17
|
"@nestjs/common": "^10.0.0 || ^11.0.0",
|
|
18
18
|
"@nestjs/core": "^10.0.0 || ^11.0.0"
|
|
19
19
|
},
|