@opra/nestjs-http 1.11.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/LICENSE +21 -0
- package/README.md +3 -0
- package/cjs/constants.js +4 -0
- package/cjs/index.js +13 -0
- package/cjs/opra-exception-filter.js +27 -0
- package/cjs/opra-http-core.module.js +119 -0
- package/cjs/opra-http-nestjs-adapter.js +167 -0
- package/cjs/opra-http.module.js +33 -0
- package/cjs/opra-middleware.js +33 -0
- package/cjs/package.json +3 -0
- package/esm/constants.js +1 -0
- package/esm/index.js +7 -0
- package/esm/opra-exception-filter.js +24 -0
- package/esm/opra-http-core.module.js +116 -0
- package/esm/opra-http-nestjs-adapter.js +163 -0
- package/esm/opra-http.module.js +30 -0
- package/esm/opra-middleware.js +30 -0
- package/esm/package.json +3 -0
- package/package.json +62 -0
- package/types/constants.d.ts +1 -0
- package/types/index.d.cts +7 -0
- package/types/index.d.ts +7 -0
- package/types/opra-exception-filter.d.ts +7 -0
- package/types/opra-http-core.module.d.ts +12 -0
- package/types/opra-http-nestjs-adapter.d.ts +12 -0
- package/types/opra-http.module.d.ts +36 -0
- package/types/opra-middleware.d.ts +8 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Panates
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/cjs/constants.js
ADDED
package/cjs/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Public = exports.OpraNestUtils = exports.IS_PUBLIC_KEY = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const nestjs_1 = require("@opra/nestjs");
|
|
6
|
+
Object.defineProperty(exports, "IS_PUBLIC_KEY", { enumerable: true, get: function () { return nestjs_1.IS_PUBLIC_KEY; } });
|
|
7
|
+
Object.defineProperty(exports, "OpraNestUtils", { enumerable: true, get: function () { return nestjs_1.OpraNestUtils; } });
|
|
8
|
+
Object.defineProperty(exports, "Public", { enumerable: true, get: function () { return nestjs_1.Public; } });
|
|
9
|
+
tslib_1.__exportStar(require("./constants.js"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./opra-exception-filter.js"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./opra-http.module.js"), exports);
|
|
12
|
+
tslib_1.__exportStar(require("./opra-http-nestjs-adapter.js"), exports);
|
|
13
|
+
tslib_1.__exportStar(require("./opra-middleware.js"), exports);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OpraExceptionFilter = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const common_1 = require("@nestjs/common");
|
|
6
|
+
const core_1 = require("@nestjs/core");
|
|
7
|
+
const opra_http_nestjs_adapter_js_1 = require("./opra-http-nestjs-adapter.js");
|
|
8
|
+
let OpraExceptionFilter = class OpraExceptionFilter extends core_1.BaseExceptionFilter {
|
|
9
|
+
constructor(moduleRef) {
|
|
10
|
+
super();
|
|
11
|
+
this.moduleRef = moduleRef;
|
|
12
|
+
}
|
|
13
|
+
catch(exception, host) {
|
|
14
|
+
const ctx = host.switchToHttp().getRequest().opraContext;
|
|
15
|
+
if (ctx) {
|
|
16
|
+
const adapter = this.moduleRef.get(opra_http_nestjs_adapter_js_1.OpraHttpNestjsAdapter);
|
|
17
|
+
ctx.errors.push(exception);
|
|
18
|
+
return adapter.handler.sendResponse(ctx);
|
|
19
|
+
}
|
|
20
|
+
super.catch(exception, host);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
exports.OpraExceptionFilter = OpraExceptionFilter;
|
|
24
|
+
exports.OpraExceptionFilter = OpraExceptionFilter = tslib_1.__decorate([
|
|
25
|
+
(0, common_1.Catch)(),
|
|
26
|
+
tslib_1.__metadata("design:paramtypes", [core_1.ModuleRef])
|
|
27
|
+
], OpraExceptionFilter);
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var OpraHttpCoreModule_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.OpraHttpCoreModule = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const objects_1 = require("@jsopen/objects");
|
|
7
|
+
const common_1 = require("@nestjs/common");
|
|
8
|
+
const core_1 = require("@nestjs/core");
|
|
9
|
+
const common_2 = require("@opra/common");
|
|
10
|
+
const constants_js_1 = require("./constants.js");
|
|
11
|
+
const opra_exception_filter_js_1 = require("./opra-exception-filter.js");
|
|
12
|
+
const opra_http_nestjs_adapter_js_1 = require("./opra-http-nestjs-adapter.js");
|
|
13
|
+
const opra_middleware_js_1 = require("./opra-middleware.js");
|
|
14
|
+
const opraHttpNestjsAdapterToken = Symbol('OpraHttpNestjsAdapter');
|
|
15
|
+
let OpraHttpCoreModule = OpraHttpCoreModule_1 = class OpraHttpCoreModule {
|
|
16
|
+
constructor(opraAdapter) {
|
|
17
|
+
this.opraAdapter = opraAdapter;
|
|
18
|
+
}
|
|
19
|
+
configure(consumer) {
|
|
20
|
+
consumer.apply(opra_middleware_js_1.OpraMiddleware).forRoutes({
|
|
21
|
+
path: '*',
|
|
22
|
+
method: common_1.RequestMethod.ALL,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
static forRoot(moduleOptions) {
|
|
26
|
+
return this._getDynamicModule({
|
|
27
|
+
...moduleOptions,
|
|
28
|
+
providers: [
|
|
29
|
+
...(moduleOptions?.providers || []),
|
|
30
|
+
{
|
|
31
|
+
provide: constants_js_1.OPRA_HTTP_API_CONFIG,
|
|
32
|
+
useValue: { ...moduleOptions },
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
static forRootAsync(moduleOptions) {
|
|
38
|
+
if (!moduleOptions.useFactory)
|
|
39
|
+
throw new Error('Invalid configuration. Must provide "useFactory"');
|
|
40
|
+
return this._getDynamicModule({
|
|
41
|
+
...moduleOptions,
|
|
42
|
+
providers: [
|
|
43
|
+
...(moduleOptions?.providers || []),
|
|
44
|
+
{
|
|
45
|
+
provide: constants_js_1.OPRA_HTTP_API_CONFIG,
|
|
46
|
+
inject: moduleOptions.inject,
|
|
47
|
+
useFactory: moduleOptions.useFactory,
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
static _getDynamicModule(moduleOptions) {
|
|
53
|
+
const token = moduleOptions?.token || opra_http_nestjs_adapter_js_1.OpraHttpNestjsAdapter;
|
|
54
|
+
const opraNestAdapter = new opra_http_nestjs_adapter_js_1.OpraHttpNestjsAdapter({
|
|
55
|
+
...moduleOptions,
|
|
56
|
+
interceptors: undefined,
|
|
57
|
+
});
|
|
58
|
+
const adapterProvider = {
|
|
59
|
+
provide: token,
|
|
60
|
+
inject: [core_1.ModuleRef, constants_js_1.OPRA_HTTP_API_CONFIG],
|
|
61
|
+
useFactory: async (moduleRef, apiConfig) => {
|
|
62
|
+
opraNestAdapter.scope = apiConfig.scope;
|
|
63
|
+
opraNestAdapter.logger =
|
|
64
|
+
opraNestAdapter.logger || new common_1.Logger(apiConfig.name);
|
|
65
|
+
opraNestAdapter._document =
|
|
66
|
+
await common_2.ApiDocumentFactory.createDocument({
|
|
67
|
+
...apiConfig,
|
|
68
|
+
api: {
|
|
69
|
+
transport: 'http',
|
|
70
|
+
name: apiConfig.name,
|
|
71
|
+
description: apiConfig.description,
|
|
72
|
+
controllers: moduleOptions.controllers,
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
if (moduleOptions.interceptors) {
|
|
76
|
+
opraNestAdapter.interceptors = moduleOptions.interceptors.map(x => {
|
|
77
|
+
if ((0, objects_1.isConstructor)(x)) {
|
|
78
|
+
return async (ctx, next) => {
|
|
79
|
+
const interceptor = moduleRef.get(x);
|
|
80
|
+
if (typeof interceptor.intercept === 'function')
|
|
81
|
+
return interceptor.intercept(ctx, next);
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
return x;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return opraNestAdapter;
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
return {
|
|
91
|
+
global: moduleOptions.global,
|
|
92
|
+
module: OpraHttpCoreModule_1,
|
|
93
|
+
controllers: opraNestAdapter.nestControllers,
|
|
94
|
+
providers: [
|
|
95
|
+
...(moduleOptions?.providers || []),
|
|
96
|
+
adapterProvider,
|
|
97
|
+
{
|
|
98
|
+
provide: opraHttpNestjsAdapterToken,
|
|
99
|
+
useExisting: token,
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
provide: core_1.APP_FILTER,
|
|
103
|
+
useClass: opra_exception_filter_js_1.OpraExceptionFilter,
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
imports: [...(moduleOptions?.imports || [])],
|
|
107
|
+
exports: [...(moduleOptions?.exports || []), adapterProvider],
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
async onModuleDestroy() {
|
|
111
|
+
await this.opraAdapter.close();
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
exports.OpraHttpCoreModule = OpraHttpCoreModule;
|
|
115
|
+
exports.OpraHttpCoreModule = OpraHttpCoreModule = OpraHttpCoreModule_1 = tslib_1.__decorate([
|
|
116
|
+
(0, common_1.Module)({}),
|
|
117
|
+
(0, common_1.Global)(),
|
|
118
|
+
tslib_1.__metadata("design:paramtypes", [opra_http_nestjs_adapter_js_1.OpraHttpNestjsAdapter])
|
|
119
|
+
], OpraHttpCoreModule);
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OpraHttpNestjsAdapter = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const node_path_1 = tslib_1.__importDefault(require("node:path"));
|
|
6
|
+
const objects_1 = require("@jsopen/objects");
|
|
7
|
+
const common_1 = require("@nestjs/common");
|
|
8
|
+
const common_2 = require("@opra/common");
|
|
9
|
+
const http_1 = require("@opra/http");
|
|
10
|
+
const nestjs_1 = require("@opra/nestjs");
|
|
11
|
+
const ts_gems_1 = require("ts-gems");
|
|
12
|
+
class OpraHttpNestjsAdapter extends http_1.HttpAdapter {
|
|
13
|
+
constructor(options) {
|
|
14
|
+
super(options);
|
|
15
|
+
this.nestControllers = [];
|
|
16
|
+
this._addRootController(options.schemaIsPublic);
|
|
17
|
+
if (options.controllers) {
|
|
18
|
+
for (const c of options.controllers) {
|
|
19
|
+
this._addToNestControllers(c, this.basePath, []);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
async close() {
|
|
24
|
+
//
|
|
25
|
+
}
|
|
26
|
+
_addRootController(isPublic) {
|
|
27
|
+
const _this = this;
|
|
28
|
+
let RootController = class RootController {
|
|
29
|
+
schema(_req, next) {
|
|
30
|
+
_this.handler.sendDocumentSchema(_req.opraContext).catch(() => next());
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
tslib_1.__decorate([
|
|
34
|
+
(0, common_1.Get)('/\\$schema'),
|
|
35
|
+
tslib_1.__param(0, (0, common_1.Req)()),
|
|
36
|
+
tslib_1.__param(1, (0, common_1.Next)()),
|
|
37
|
+
tslib_1.__metadata("design:type", Function),
|
|
38
|
+
tslib_1.__metadata("design:paramtypes", [Object, Function]),
|
|
39
|
+
tslib_1.__metadata("design:returntype", void 0)
|
|
40
|
+
], RootController.prototype, "schema", null);
|
|
41
|
+
RootController = tslib_1.__decorate([
|
|
42
|
+
(0, common_1.Controller)({
|
|
43
|
+
path: this.basePath,
|
|
44
|
+
})
|
|
45
|
+
], RootController);
|
|
46
|
+
if (isPublic) {
|
|
47
|
+
(0, nestjs_1.Public)()(RootController.prototype, 'schema', Object.getOwnPropertyDescriptor(RootController.prototype, 'schema'));
|
|
48
|
+
}
|
|
49
|
+
this.nestControllers.push(RootController);
|
|
50
|
+
}
|
|
51
|
+
_addToNestControllers(sourceClass, currentPath, parentTree) {
|
|
52
|
+
const metadata = Reflect.getMetadata(common_2.HTTP_CONTROLLER_METADATA, sourceClass);
|
|
53
|
+
if (!metadata)
|
|
54
|
+
return;
|
|
55
|
+
/** Create a new controller class */
|
|
56
|
+
const newClass = {
|
|
57
|
+
[sourceClass.name]: class extends sourceClass {
|
|
58
|
+
},
|
|
59
|
+
}[sourceClass.name];
|
|
60
|
+
/** Copy metadata keys from source class to new one */
|
|
61
|
+
nestjs_1.OpraNestUtils.copyDecoratorMetadata(newClass, ...parentTree);
|
|
62
|
+
(0, common_1.Controller)()(newClass);
|
|
63
|
+
const newPath = metadata.path
|
|
64
|
+
? node_path_1.default.join(currentPath, metadata.path)
|
|
65
|
+
: currentPath;
|
|
66
|
+
const adapter = this;
|
|
67
|
+
// adapter.logger =
|
|
68
|
+
/** Disable default error handler. Errors will be handled by OpraExceptionFilter */
|
|
69
|
+
adapter.handler.onError = (context, error) => {
|
|
70
|
+
throw error;
|
|
71
|
+
};
|
|
72
|
+
this.nestControllers.push(newClass);
|
|
73
|
+
let metadataKeys;
|
|
74
|
+
if (metadata.operations) {
|
|
75
|
+
for (const [k, v] of Object.entries(metadata.operations)) {
|
|
76
|
+
const operationHandler = sourceClass.prototype[k];
|
|
77
|
+
Object.defineProperty(newClass.prototype, k, {
|
|
78
|
+
writable: true,
|
|
79
|
+
/** NestJS handler method */
|
|
80
|
+
async value(_req, _res) {
|
|
81
|
+
_res.statusCode = 200;
|
|
82
|
+
const api = adapter.document.api;
|
|
83
|
+
const controller = api.findController(sourceClass);
|
|
84
|
+
const operation = controller?.operations.get(k);
|
|
85
|
+
const context = (0, ts_gems_1.asMutable)(_req.opraContext);
|
|
86
|
+
if (!(context && operation && typeof operationHandler === 'function')) {
|
|
87
|
+
throw new common_2.NotFoundError({
|
|
88
|
+
message: `No endpoint found for [${_req.method}]${_req.baseUrl}`,
|
|
89
|
+
details: {
|
|
90
|
+
path: _req.baseUrl,
|
|
91
|
+
method: _req.method,
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/** Configure the HttpContext */
|
|
96
|
+
context.operation = operation;
|
|
97
|
+
context.controller = operation.owner;
|
|
98
|
+
context.controllerInstance = this;
|
|
99
|
+
context.operationHandler = operationHandler;
|
|
100
|
+
/** Handle request */
|
|
101
|
+
await adapter.handler.handleRequest(context);
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
/** Copy metadata keys from source function to new one */
|
|
105
|
+
metadataKeys = Reflect.getOwnMetadataKeys(operationHandler);
|
|
106
|
+
const newFn = newClass.prototype[k];
|
|
107
|
+
for (const key of metadataKeys) {
|
|
108
|
+
const m = Reflect.getMetadata(key, operationHandler);
|
|
109
|
+
Reflect.defineMetadata(key, m, newFn);
|
|
110
|
+
}
|
|
111
|
+
(0, common_1.Req)()(newClass.prototype, k, 0);
|
|
112
|
+
(0, common_1.Res)()(newClass.prototype, k, 1);
|
|
113
|
+
const descriptor = Object.getOwnPropertyDescriptor(newClass.prototype, k);
|
|
114
|
+
const operationPath = v.mergePath
|
|
115
|
+
? newPath + (v.path || '')
|
|
116
|
+
: node_path_1.default.posix.join(newPath, v.path || '');
|
|
117
|
+
switch (v.method || 'GET') {
|
|
118
|
+
case 'DELETE':
|
|
119
|
+
/** Call @Delete decorator over new property */
|
|
120
|
+
(0, common_1.Delete)(operationPath)(newClass.prototype, k, descriptor);
|
|
121
|
+
break;
|
|
122
|
+
case 'GET':
|
|
123
|
+
/** Call @Get decorator over new property */
|
|
124
|
+
(0, common_1.Get)(operationPath)(newClass.prototype, k, descriptor);
|
|
125
|
+
break;
|
|
126
|
+
case 'HEAD':
|
|
127
|
+
/** Call @Head decorator over new property */
|
|
128
|
+
(0, common_1.Head)(operationPath)(newClass.prototype, k, descriptor);
|
|
129
|
+
break;
|
|
130
|
+
case 'OPTIONS':
|
|
131
|
+
/** Call @Options decorator over new property */
|
|
132
|
+
(0, common_1.Options)(operationPath)(newClass.prototype, k, descriptor);
|
|
133
|
+
break;
|
|
134
|
+
case 'PATCH':
|
|
135
|
+
/** Call @Patch decorator over new property */
|
|
136
|
+
(0, common_1.Patch)(operationPath)(newClass.prototype, k, descriptor);
|
|
137
|
+
break;
|
|
138
|
+
case 'POST':
|
|
139
|
+
/** Call @Post decorator over new property */
|
|
140
|
+
(0, common_1.Post)(operationPath)(newClass.prototype, k, descriptor);
|
|
141
|
+
break;
|
|
142
|
+
case 'PUT':
|
|
143
|
+
/** Call @Put decorator over new property */
|
|
144
|
+
(0, common_1.Put)(operationPath)(newClass.prototype, k, descriptor);
|
|
145
|
+
break;
|
|
146
|
+
case 'SEARCH':
|
|
147
|
+
/** Call @Search decorator over new property */
|
|
148
|
+
(0, common_1.Search)(operationPath)(newClass.prototype, k, descriptor);
|
|
149
|
+
break;
|
|
150
|
+
default:
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (metadata.controllers) {
|
|
156
|
+
for (const child of metadata.controllers) {
|
|
157
|
+
if (!(0, objects_1.isConstructor)(child))
|
|
158
|
+
throw new TypeError('Controllers should be injectable a class');
|
|
159
|
+
this._addToNestControllers(child, newPath, [
|
|
160
|
+
...parentTree,
|
|
161
|
+
sourceClass,
|
|
162
|
+
]);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
exports.OpraHttpNestjsAdapter = OpraHttpNestjsAdapter;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var OpraHttpModule_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.OpraHttpModule = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const common_1 = require("@nestjs/common");
|
|
7
|
+
const opra_http_core_module_js_1 = require("./opra-http-core.module.js");
|
|
8
|
+
let OpraHttpModule = OpraHttpModule_1 = class OpraHttpModule {
|
|
9
|
+
/**
|
|
10
|
+
*
|
|
11
|
+
* @param init
|
|
12
|
+
*/
|
|
13
|
+
static forRoot(init) {
|
|
14
|
+
return {
|
|
15
|
+
module: OpraHttpModule_1,
|
|
16
|
+
imports: [opra_http_core_module_js_1.OpraHttpCoreModule.forRoot(init)],
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
*
|
|
21
|
+
* @param options
|
|
22
|
+
*/
|
|
23
|
+
static forRootAsync(options) {
|
|
24
|
+
return {
|
|
25
|
+
module: OpraHttpModule_1,
|
|
26
|
+
imports: [opra_http_core_module_js_1.OpraHttpCoreModule.forRootAsync(options)],
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
exports.OpraHttpModule = OpraHttpModule;
|
|
31
|
+
exports.OpraHttpModule = OpraHttpModule = OpraHttpModule_1 = tslib_1.__decorate([
|
|
32
|
+
(0, common_1.Module)({})
|
|
33
|
+
], OpraHttpModule);
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OpraMiddleware = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const common_1 = require("@nestjs/common");
|
|
6
|
+
const http_1 = require("@opra/http");
|
|
7
|
+
const opra_http_nestjs_adapter_js_1 = require("./opra-http-nestjs-adapter.js");
|
|
8
|
+
let OpraMiddleware = class OpraMiddleware {
|
|
9
|
+
constructor(opraAdapter) {
|
|
10
|
+
this.opraAdapter = opraAdapter;
|
|
11
|
+
}
|
|
12
|
+
use(req, res, next) {
|
|
13
|
+
const request = http_1.HttpIncoming.from(req);
|
|
14
|
+
const response = http_1.HttpOutgoing.from(res);
|
|
15
|
+
/** Create the HttpContext */
|
|
16
|
+
const context = new http_1.HttpContext({
|
|
17
|
+
adapter: this.opraAdapter,
|
|
18
|
+
platform: req.route ? 'express' : 'fastify',
|
|
19
|
+
request,
|
|
20
|
+
response,
|
|
21
|
+
});
|
|
22
|
+
req.opraContext = context;
|
|
23
|
+
this.opraAdapter
|
|
24
|
+
.emitAsync('createContext', context)
|
|
25
|
+
.then(() => next())
|
|
26
|
+
.catch(next);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
exports.OpraMiddleware = OpraMiddleware;
|
|
30
|
+
exports.OpraMiddleware = OpraMiddleware = tslib_1.__decorate([
|
|
31
|
+
(0, common_1.Injectable)(),
|
|
32
|
+
tslib_1.__metadata("design:paramtypes", [opra_http_nestjs_adapter_js_1.OpraHttpNestjsAdapter])
|
|
33
|
+
], OpraMiddleware);
|
package/cjs/package.json
ADDED
package/esm/constants.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const OPRA_HTTP_API_CONFIG = 'OPRA_HTTP_API_CONFIG';
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { IS_PUBLIC_KEY, OpraNestUtils, Public } from '@opra/nestjs';
|
|
2
|
+
export * from './constants.js';
|
|
3
|
+
export * from './opra-exception-filter.js';
|
|
4
|
+
export * from './opra-http.module.js';
|
|
5
|
+
export * from './opra-http-nestjs-adapter.js';
|
|
6
|
+
export * from './opra-middleware.js';
|
|
7
|
+
export { IS_PUBLIC_KEY, OpraNestUtils, Public };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { __decorate, __metadata } from "tslib";
|
|
2
|
+
import { Catch } from '@nestjs/common';
|
|
3
|
+
import { BaseExceptionFilter, ModuleRef } from '@nestjs/core';
|
|
4
|
+
import { OpraHttpNestjsAdapter } from './opra-http-nestjs-adapter.js';
|
|
5
|
+
let OpraExceptionFilter = class OpraExceptionFilter extends BaseExceptionFilter {
|
|
6
|
+
constructor(moduleRef) {
|
|
7
|
+
super();
|
|
8
|
+
this.moduleRef = moduleRef;
|
|
9
|
+
}
|
|
10
|
+
catch(exception, host) {
|
|
11
|
+
const ctx = host.switchToHttp().getRequest().opraContext;
|
|
12
|
+
if (ctx) {
|
|
13
|
+
const adapter = this.moduleRef.get(OpraHttpNestjsAdapter);
|
|
14
|
+
ctx.errors.push(exception);
|
|
15
|
+
return adapter.handler.sendResponse(ctx);
|
|
16
|
+
}
|
|
17
|
+
super.catch(exception, host);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
OpraExceptionFilter = __decorate([
|
|
21
|
+
Catch(),
|
|
22
|
+
__metadata("design:paramtypes", [ModuleRef])
|
|
23
|
+
], OpraExceptionFilter);
|
|
24
|
+
export { OpraExceptionFilter };
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
var OpraHttpCoreModule_1;
|
|
2
|
+
import { __decorate, __metadata } from "tslib";
|
|
3
|
+
import { isConstructor } from '@jsopen/objects';
|
|
4
|
+
import { Global, Logger, Module, RequestMethod, } from '@nestjs/common';
|
|
5
|
+
import { APP_FILTER, ModuleRef } from '@nestjs/core';
|
|
6
|
+
import { ApiDocumentFactory } from '@opra/common';
|
|
7
|
+
import { OPRA_HTTP_API_CONFIG } from './constants.js';
|
|
8
|
+
import { OpraExceptionFilter } from './opra-exception-filter.js';
|
|
9
|
+
import { OpraHttpNestjsAdapter } from './opra-http-nestjs-adapter.js';
|
|
10
|
+
import { OpraMiddleware } from './opra-middleware.js';
|
|
11
|
+
const opraHttpNestjsAdapterToken = Symbol('OpraHttpNestjsAdapter');
|
|
12
|
+
let OpraHttpCoreModule = OpraHttpCoreModule_1 = class OpraHttpCoreModule {
|
|
13
|
+
constructor(opraAdapter) {
|
|
14
|
+
this.opraAdapter = opraAdapter;
|
|
15
|
+
}
|
|
16
|
+
configure(consumer) {
|
|
17
|
+
consumer.apply(OpraMiddleware).forRoutes({
|
|
18
|
+
path: '*',
|
|
19
|
+
method: RequestMethod.ALL,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
static forRoot(moduleOptions) {
|
|
23
|
+
return this._getDynamicModule({
|
|
24
|
+
...moduleOptions,
|
|
25
|
+
providers: [
|
|
26
|
+
...(moduleOptions?.providers || []),
|
|
27
|
+
{
|
|
28
|
+
provide: OPRA_HTTP_API_CONFIG,
|
|
29
|
+
useValue: { ...moduleOptions },
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
static forRootAsync(moduleOptions) {
|
|
35
|
+
if (!moduleOptions.useFactory)
|
|
36
|
+
throw new Error('Invalid configuration. Must provide "useFactory"');
|
|
37
|
+
return this._getDynamicModule({
|
|
38
|
+
...moduleOptions,
|
|
39
|
+
providers: [
|
|
40
|
+
...(moduleOptions?.providers || []),
|
|
41
|
+
{
|
|
42
|
+
provide: OPRA_HTTP_API_CONFIG,
|
|
43
|
+
inject: moduleOptions.inject,
|
|
44
|
+
useFactory: moduleOptions.useFactory,
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
static _getDynamicModule(moduleOptions) {
|
|
50
|
+
const token = moduleOptions?.token || OpraHttpNestjsAdapter;
|
|
51
|
+
const opraNestAdapter = new OpraHttpNestjsAdapter({
|
|
52
|
+
...moduleOptions,
|
|
53
|
+
interceptors: undefined,
|
|
54
|
+
});
|
|
55
|
+
const adapterProvider = {
|
|
56
|
+
provide: token,
|
|
57
|
+
inject: [ModuleRef, OPRA_HTTP_API_CONFIG],
|
|
58
|
+
useFactory: async (moduleRef, apiConfig) => {
|
|
59
|
+
opraNestAdapter.scope = apiConfig.scope;
|
|
60
|
+
opraNestAdapter.logger =
|
|
61
|
+
opraNestAdapter.logger || new Logger(apiConfig.name);
|
|
62
|
+
opraNestAdapter._document =
|
|
63
|
+
await ApiDocumentFactory.createDocument({
|
|
64
|
+
...apiConfig,
|
|
65
|
+
api: {
|
|
66
|
+
transport: 'http',
|
|
67
|
+
name: apiConfig.name,
|
|
68
|
+
description: apiConfig.description,
|
|
69
|
+
controllers: moduleOptions.controllers,
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
if (moduleOptions.interceptors) {
|
|
73
|
+
opraNestAdapter.interceptors = moduleOptions.interceptors.map(x => {
|
|
74
|
+
if (isConstructor(x)) {
|
|
75
|
+
return async (ctx, next) => {
|
|
76
|
+
const interceptor = moduleRef.get(x);
|
|
77
|
+
if (typeof interceptor.intercept === 'function')
|
|
78
|
+
return interceptor.intercept(ctx, next);
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
return x;
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
return opraNestAdapter;
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
return {
|
|
88
|
+
global: moduleOptions.global,
|
|
89
|
+
module: OpraHttpCoreModule_1,
|
|
90
|
+
controllers: opraNestAdapter.nestControllers,
|
|
91
|
+
providers: [
|
|
92
|
+
...(moduleOptions?.providers || []),
|
|
93
|
+
adapterProvider,
|
|
94
|
+
{
|
|
95
|
+
provide: opraHttpNestjsAdapterToken,
|
|
96
|
+
useExisting: token,
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
provide: APP_FILTER,
|
|
100
|
+
useClass: OpraExceptionFilter,
|
|
101
|
+
},
|
|
102
|
+
],
|
|
103
|
+
imports: [...(moduleOptions?.imports || [])],
|
|
104
|
+
exports: [...(moduleOptions?.exports || []), adapterProvider],
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
async onModuleDestroy() {
|
|
108
|
+
await this.opraAdapter.close();
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
OpraHttpCoreModule = OpraHttpCoreModule_1 = __decorate([
|
|
112
|
+
Module({}),
|
|
113
|
+
Global(),
|
|
114
|
+
__metadata("design:paramtypes", [OpraHttpNestjsAdapter])
|
|
115
|
+
], OpraHttpCoreModule);
|
|
116
|
+
export { OpraHttpCoreModule };
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { __decorate, __metadata, __param } from "tslib";
|
|
2
|
+
import nodePath from 'node:path';
|
|
3
|
+
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, NotFoundError, } from '@opra/common';
|
|
6
|
+
import { HttpAdapter } from '@opra/http';
|
|
7
|
+
import { OpraNestUtils, Public } from '@opra/nestjs';
|
|
8
|
+
import { asMutable } from 'ts-gems';
|
|
9
|
+
export class OpraHttpNestjsAdapter extends HttpAdapter {
|
|
10
|
+
constructor(options) {
|
|
11
|
+
super(options);
|
|
12
|
+
this.nestControllers = [];
|
|
13
|
+
this._addRootController(options.schemaIsPublic);
|
|
14
|
+
if (options.controllers) {
|
|
15
|
+
for (const c of options.controllers) {
|
|
16
|
+
this._addToNestControllers(c, this.basePath, []);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
async close() {
|
|
21
|
+
//
|
|
22
|
+
}
|
|
23
|
+
_addRootController(isPublic) {
|
|
24
|
+
const _this = this;
|
|
25
|
+
let RootController = class RootController {
|
|
26
|
+
schema(_req, next) {
|
|
27
|
+
_this.handler.sendDocumentSchema(_req.opraContext).catch(() => next());
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
__decorate([
|
|
31
|
+
Get('/\\$schema'),
|
|
32
|
+
__param(0, Req()),
|
|
33
|
+
__param(1, Next()),
|
|
34
|
+
__metadata("design:type", Function),
|
|
35
|
+
__metadata("design:paramtypes", [Object, Function]),
|
|
36
|
+
__metadata("design:returntype", void 0)
|
|
37
|
+
], RootController.prototype, "schema", null);
|
|
38
|
+
RootController = __decorate([
|
|
39
|
+
Controller({
|
|
40
|
+
path: this.basePath,
|
|
41
|
+
})
|
|
42
|
+
], RootController);
|
|
43
|
+
if (isPublic) {
|
|
44
|
+
Public()(RootController.prototype, 'schema', Object.getOwnPropertyDescriptor(RootController.prototype, 'schema'));
|
|
45
|
+
}
|
|
46
|
+
this.nestControllers.push(RootController);
|
|
47
|
+
}
|
|
48
|
+
_addToNestControllers(sourceClass, currentPath, parentTree) {
|
|
49
|
+
const metadata = Reflect.getMetadata(HTTP_CONTROLLER_METADATA, sourceClass);
|
|
50
|
+
if (!metadata)
|
|
51
|
+
return;
|
|
52
|
+
/** Create a new controller class */
|
|
53
|
+
const newClass = {
|
|
54
|
+
[sourceClass.name]: class extends sourceClass {
|
|
55
|
+
},
|
|
56
|
+
}[sourceClass.name];
|
|
57
|
+
/** Copy metadata keys from source class to new one */
|
|
58
|
+
OpraNestUtils.copyDecoratorMetadata(newClass, ...parentTree);
|
|
59
|
+
Controller()(newClass);
|
|
60
|
+
const newPath = metadata.path
|
|
61
|
+
? nodePath.join(currentPath, metadata.path)
|
|
62
|
+
: currentPath;
|
|
63
|
+
const adapter = this;
|
|
64
|
+
// adapter.logger =
|
|
65
|
+
/** Disable default error handler. Errors will be handled by OpraExceptionFilter */
|
|
66
|
+
adapter.handler.onError = (context, error) => {
|
|
67
|
+
throw error;
|
|
68
|
+
};
|
|
69
|
+
this.nestControllers.push(newClass);
|
|
70
|
+
let metadataKeys;
|
|
71
|
+
if (metadata.operations) {
|
|
72
|
+
for (const [k, v] of Object.entries(metadata.operations)) {
|
|
73
|
+
const operationHandler = sourceClass.prototype[k];
|
|
74
|
+
Object.defineProperty(newClass.prototype, k, {
|
|
75
|
+
writable: true,
|
|
76
|
+
/** NestJS handler method */
|
|
77
|
+
async value(_req, _res) {
|
|
78
|
+
_res.statusCode = 200;
|
|
79
|
+
const api = adapter.document.api;
|
|
80
|
+
const controller = api.findController(sourceClass);
|
|
81
|
+
const operation = controller?.operations.get(k);
|
|
82
|
+
const context = asMutable(_req.opraContext);
|
|
83
|
+
if (!(context && operation && typeof operationHandler === 'function')) {
|
|
84
|
+
throw new NotFoundError({
|
|
85
|
+
message: `No endpoint found for [${_req.method}]${_req.baseUrl}`,
|
|
86
|
+
details: {
|
|
87
|
+
path: _req.baseUrl,
|
|
88
|
+
method: _req.method,
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
/** Configure the HttpContext */
|
|
93
|
+
context.operation = operation;
|
|
94
|
+
context.controller = operation.owner;
|
|
95
|
+
context.controllerInstance = this;
|
|
96
|
+
context.operationHandler = operationHandler;
|
|
97
|
+
/** Handle request */
|
|
98
|
+
await adapter.handler.handleRequest(context);
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
/** Copy metadata keys from source function to new one */
|
|
102
|
+
metadataKeys = Reflect.getOwnMetadataKeys(operationHandler);
|
|
103
|
+
const newFn = newClass.prototype[k];
|
|
104
|
+
for (const key of metadataKeys) {
|
|
105
|
+
const m = Reflect.getMetadata(key, operationHandler);
|
|
106
|
+
Reflect.defineMetadata(key, m, newFn);
|
|
107
|
+
}
|
|
108
|
+
Req()(newClass.prototype, k, 0);
|
|
109
|
+
Res()(newClass.prototype, k, 1);
|
|
110
|
+
const descriptor = Object.getOwnPropertyDescriptor(newClass.prototype, k);
|
|
111
|
+
const operationPath = v.mergePath
|
|
112
|
+
? newPath + (v.path || '')
|
|
113
|
+
: nodePath.posix.join(newPath, v.path || '');
|
|
114
|
+
switch (v.method || 'GET') {
|
|
115
|
+
case 'DELETE':
|
|
116
|
+
/** Call @Delete decorator over new property */
|
|
117
|
+
Delete(operationPath)(newClass.prototype, k, descriptor);
|
|
118
|
+
break;
|
|
119
|
+
case 'GET':
|
|
120
|
+
/** Call @Get decorator over new property */
|
|
121
|
+
Get(operationPath)(newClass.prototype, k, descriptor);
|
|
122
|
+
break;
|
|
123
|
+
case 'HEAD':
|
|
124
|
+
/** Call @Head decorator over new property */
|
|
125
|
+
Head(operationPath)(newClass.prototype, k, descriptor);
|
|
126
|
+
break;
|
|
127
|
+
case 'OPTIONS':
|
|
128
|
+
/** Call @Options decorator over new property */
|
|
129
|
+
Options(operationPath)(newClass.prototype, k, descriptor);
|
|
130
|
+
break;
|
|
131
|
+
case 'PATCH':
|
|
132
|
+
/** Call @Patch decorator over new property */
|
|
133
|
+
Patch(operationPath)(newClass.prototype, k, descriptor);
|
|
134
|
+
break;
|
|
135
|
+
case 'POST':
|
|
136
|
+
/** Call @Post decorator over new property */
|
|
137
|
+
Post(operationPath)(newClass.prototype, k, descriptor);
|
|
138
|
+
break;
|
|
139
|
+
case 'PUT':
|
|
140
|
+
/** Call @Put decorator over new property */
|
|
141
|
+
Put(operationPath)(newClass.prototype, k, descriptor);
|
|
142
|
+
break;
|
|
143
|
+
case 'SEARCH':
|
|
144
|
+
/** Call @Search decorator over new property */
|
|
145
|
+
Search(operationPath)(newClass.prototype, k, descriptor);
|
|
146
|
+
break;
|
|
147
|
+
default:
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (metadata.controllers) {
|
|
153
|
+
for (const child of metadata.controllers) {
|
|
154
|
+
if (!isConstructor(child))
|
|
155
|
+
throw new TypeError('Controllers should be injectable a class');
|
|
156
|
+
this._addToNestControllers(child, newPath, [
|
|
157
|
+
...parentTree,
|
|
158
|
+
sourceClass,
|
|
159
|
+
]);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
var OpraHttpModule_1;
|
|
2
|
+
import { __decorate } from "tslib";
|
|
3
|
+
import { Module } from '@nestjs/common';
|
|
4
|
+
import { OpraHttpCoreModule } from './opra-http-core.module.js';
|
|
5
|
+
let OpraHttpModule = OpraHttpModule_1 = class OpraHttpModule {
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* @param init
|
|
9
|
+
*/
|
|
10
|
+
static forRoot(init) {
|
|
11
|
+
return {
|
|
12
|
+
module: OpraHttpModule_1,
|
|
13
|
+
imports: [OpraHttpCoreModule.forRoot(init)],
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
*
|
|
18
|
+
* @param options
|
|
19
|
+
*/
|
|
20
|
+
static forRootAsync(options) {
|
|
21
|
+
return {
|
|
22
|
+
module: OpraHttpModule_1,
|
|
23
|
+
imports: [OpraHttpCoreModule.forRootAsync(options)],
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
OpraHttpModule = OpraHttpModule_1 = __decorate([
|
|
28
|
+
Module({})
|
|
29
|
+
], OpraHttpModule);
|
|
30
|
+
export { OpraHttpModule };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { __decorate, __metadata } from "tslib";
|
|
2
|
+
import { Injectable } from '@nestjs/common';
|
|
3
|
+
import { HttpContext, HttpIncoming, HttpOutgoing } from '@opra/http';
|
|
4
|
+
import { OpraHttpNestjsAdapter } from './opra-http-nestjs-adapter.js';
|
|
5
|
+
let OpraMiddleware = class OpraMiddleware {
|
|
6
|
+
constructor(opraAdapter) {
|
|
7
|
+
this.opraAdapter = opraAdapter;
|
|
8
|
+
}
|
|
9
|
+
use(req, res, next) {
|
|
10
|
+
const request = HttpIncoming.from(req);
|
|
11
|
+
const response = HttpOutgoing.from(res);
|
|
12
|
+
/** Create the HttpContext */
|
|
13
|
+
const context = new HttpContext({
|
|
14
|
+
adapter: this.opraAdapter,
|
|
15
|
+
platform: req.route ? 'express' : 'fastify',
|
|
16
|
+
request,
|
|
17
|
+
response,
|
|
18
|
+
});
|
|
19
|
+
req.opraContext = context;
|
|
20
|
+
this.opraAdapter
|
|
21
|
+
.emitAsync('createContext', context)
|
|
22
|
+
.then(() => next())
|
|
23
|
+
.catch(next);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
OpraMiddleware = __decorate([
|
|
27
|
+
Injectable(),
|
|
28
|
+
__metadata("design:paramtypes", [OpraHttpNestjsAdapter])
|
|
29
|
+
], OpraMiddleware);
|
|
30
|
+
export { OpraMiddleware };
|
package/esm/package.json
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@opra/nestjs-http",
|
|
3
|
+
"version": "1.11.0",
|
|
4
|
+
"description": "Opra NestJS Http Module",
|
|
5
|
+
"author": "Panates",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"@jsopen/objects": "^1.5.0",
|
|
9
|
+
"tslib": "^2.8.1"
|
|
10
|
+
},
|
|
11
|
+
"peerDependencies": {
|
|
12
|
+
"@opra/common": "^1.11.0",
|
|
13
|
+
"@opra/core": "^1.11.0",
|
|
14
|
+
"@opra/nestjs": "^1.11.0",
|
|
15
|
+
"@opra/http": "^1.11.0",
|
|
16
|
+
"@nestjs/common": "^10.0.0 || ^11.0.0",
|
|
17
|
+
"@nestjs/core": "^10.0.0 || ^11.0.0"
|
|
18
|
+
},
|
|
19
|
+
"type": "module",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"import": {
|
|
23
|
+
"types": "./types/index.d.ts",
|
|
24
|
+
"default": "./esm/index.js"
|
|
25
|
+
},
|
|
26
|
+
"require": {
|
|
27
|
+
"types": "./types/index.d.cts",
|
|
28
|
+
"default": "./cjs/index.js"
|
|
29
|
+
},
|
|
30
|
+
"default": "./esm/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./package.json": "./package.json"
|
|
33
|
+
},
|
|
34
|
+
"main": "./cjs/index.js",
|
|
35
|
+
"module": "./esm/index.js",
|
|
36
|
+
"types": "./types/index.d.ts",
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "https://github.com/panates/opra.git",
|
|
40
|
+
"directory": "packages/nestjs-http"
|
|
41
|
+
},
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=16.0",
|
|
44
|
+
"npm": ">=7.0.0"
|
|
45
|
+
},
|
|
46
|
+
"files": [
|
|
47
|
+
"bin/",
|
|
48
|
+
"cjs/",
|
|
49
|
+
"esm/",
|
|
50
|
+
"types/",
|
|
51
|
+
"LICENSE",
|
|
52
|
+
"README.md"
|
|
53
|
+
],
|
|
54
|
+
"keywords": [
|
|
55
|
+
"opra",
|
|
56
|
+
"nestjs",
|
|
57
|
+
"http"
|
|
58
|
+
],
|
|
59
|
+
"publishConfig": {
|
|
60
|
+
"access": "public"
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const OPRA_HTTP_API_CONFIG = "OPRA_HTTP_API_CONFIG";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { IS_PUBLIC_KEY, OpraNestUtils, Public } from '@opra/nestjs';
|
|
2
|
+
export * from './constants.js';
|
|
3
|
+
export * from './opra-exception-filter.js';
|
|
4
|
+
export * from './opra-http.module.js';
|
|
5
|
+
export * from './opra-http-nestjs-adapter.js';
|
|
6
|
+
export * from './opra-middleware.js';
|
|
7
|
+
export { IS_PUBLIC_KEY, OpraNestUtils, Public };
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { IS_PUBLIC_KEY, OpraNestUtils, Public } from '@opra/nestjs';
|
|
2
|
+
export * from './constants.js';
|
|
3
|
+
export * from './opra-exception-filter.js';
|
|
4
|
+
export * from './opra-http.module.js';
|
|
5
|
+
export * from './opra-http-nestjs-adapter.js';
|
|
6
|
+
export * from './opra-middleware.js';
|
|
7
|
+
export { IS_PUBLIC_KEY, OpraNestUtils, Public };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type ArgumentsHost } from '@nestjs/common';
|
|
2
|
+
import { BaseExceptionFilter, ModuleRef } from '@nestjs/core';
|
|
3
|
+
export declare class OpraExceptionFilter extends BaseExceptionFilter {
|
|
4
|
+
private moduleRef;
|
|
5
|
+
constructor(moduleRef: ModuleRef);
|
|
6
|
+
catch(exception: any, host: ArgumentsHost): Promise<void> | undefined;
|
|
7
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type DynamicModule, type MiddlewareConsumer, type NestModule, type OnModuleDestroy } from '@nestjs/common';
|
|
2
|
+
import type { OpraHttpModule } from './opra-http.module.js';
|
|
3
|
+
import { OpraHttpNestjsAdapter } from './opra-http-nestjs-adapter.js';
|
|
4
|
+
export declare class OpraHttpCoreModule implements OnModuleDestroy, NestModule {
|
|
5
|
+
protected opraAdapter: OpraHttpNestjsAdapter;
|
|
6
|
+
constructor(opraAdapter: OpraHttpNestjsAdapter);
|
|
7
|
+
configure(consumer: MiddlewareConsumer): void;
|
|
8
|
+
static forRoot(moduleOptions: OpraHttpModule.ModuleOptions): DynamicModule;
|
|
9
|
+
static forRootAsync(moduleOptions: OpraHttpModule.AsyncModuleOptions): DynamicModule;
|
|
10
|
+
protected static _getDynamicModule(moduleOptions: OpraHttpModule.ModuleOptions | OpraHttpModule.AsyncModuleOptions): DynamicModule;
|
|
11
|
+
onModuleDestroy(): Promise<void>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type Type } from '@nestjs/common';
|
|
2
|
+
import { HttpAdapter } from '@opra/http';
|
|
3
|
+
export declare class OpraHttpNestjsAdapter extends HttpAdapter {
|
|
4
|
+
readonly nestControllers: Type[];
|
|
5
|
+
constructor(options: HttpAdapter.Options & {
|
|
6
|
+
schemaIsPublic?: boolean;
|
|
7
|
+
controllers?: Type[];
|
|
8
|
+
});
|
|
9
|
+
close(): Promise<void>;
|
|
10
|
+
protected _addRootController(isPublic?: boolean): void;
|
|
11
|
+
protected _addToNestControllers(sourceClass: Type, currentPath: string, parentTree: Type[]): void;
|
|
12
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type DynamicModule, Logger, type Type } from '@nestjs/common';
|
|
2
|
+
import type { ApiDocumentFactory } from '@opra/common';
|
|
3
|
+
import type { HttpAdapter } from '@opra/http';
|
|
4
|
+
export declare namespace OpraHttpModule {
|
|
5
|
+
export interface ModuleOptions extends BaseModuleOptions, ApiConfig {
|
|
6
|
+
}
|
|
7
|
+
export interface AsyncModuleOptions extends BaseModuleOptions {
|
|
8
|
+
inject?: any[];
|
|
9
|
+
useFactory?: (...args: any[]) => Promise<ApiConfig> | ApiConfig;
|
|
10
|
+
}
|
|
11
|
+
interface BaseModuleOptions extends Pick<DynamicModule, 'imports' | 'providers' | 'exports' | 'controllers' | 'global'> {
|
|
12
|
+
token?: any;
|
|
13
|
+
basePath?: string;
|
|
14
|
+
schemaIsPublic?: boolean;
|
|
15
|
+
interceptors?: (HttpAdapter.InterceptorFunction | HttpAdapter.IHttpInterceptor | Type<HttpAdapter.IHttpInterceptor>)[];
|
|
16
|
+
}
|
|
17
|
+
export interface ApiConfig extends Pick<ApiDocumentFactory.InitArguments, 'types' | 'references' | 'info'> {
|
|
18
|
+
name: string;
|
|
19
|
+
description?: string;
|
|
20
|
+
scope?: string;
|
|
21
|
+
logger?: Logger;
|
|
22
|
+
}
|
|
23
|
+
export {};
|
|
24
|
+
}
|
|
25
|
+
export declare class OpraHttpModule {
|
|
26
|
+
/**
|
|
27
|
+
*
|
|
28
|
+
* @param init
|
|
29
|
+
*/
|
|
30
|
+
static forRoot(init: OpraHttpModule.ModuleOptions): DynamicModule;
|
|
31
|
+
/**
|
|
32
|
+
*
|
|
33
|
+
* @param options
|
|
34
|
+
*/
|
|
35
|
+
static forRootAsync(options: OpraHttpModule.AsyncModuleOptions): DynamicModule;
|
|
36
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type NestMiddleware } from '@nestjs/common';
|
|
2
|
+
import type { NextFunction, Request, Response } from 'express';
|
|
3
|
+
import { OpraHttpNestjsAdapter } from './opra-http-nestjs-adapter.js';
|
|
4
|
+
export declare class OpraMiddleware implements NestMiddleware {
|
|
5
|
+
protected opraAdapter: OpraHttpNestjsAdapter;
|
|
6
|
+
constructor(opraAdapter: OpraHttpNestjsAdapter);
|
|
7
|
+
use(req: Request, res: Response, next: NextFunction): void;
|
|
8
|
+
}
|