@opra/nestjs 1.0.0-alpha.1 → 1.0.0-alpha.10
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/constants.js +4 -0
- package/cjs/decorators/public.decorator.js +7 -0
- package/cjs/index.js +2 -0
- package/cjs/opra-http-core.module.js +19 -11
- package/cjs/opra-http.module.js +3 -2
- package/cjs/opra-nestjs-adapter.js +61 -39
- package/cjs/services/opra-middleware.js +24 -0
- package/esm/constants.js +1 -0
- package/esm/decorators/public.decorator.js +3 -0
- package/esm/index.js +2 -0
- package/esm/opra-http-core.module.js +20 -12
- package/esm/opra-http.module.js +3 -2
- package/esm/opra-nestjs-adapter.js +64 -41
- package/esm/services/opra-middleware.js +21 -0
- package/package.json +11 -9
- package/types/constants.d.ts +1 -0
- package/types/decorators/public.decorator.d.ts +1 -0
- package/types/index.d.ts +2 -0
- package/types/opra-http-core.module.d.ts +4 -3
- package/types/opra-http.module.d.ts +6 -2
- package/types/opra-nestjs-adapter.d.ts +5 -3
- package/types/services/opra-middleware.d.ts +5 -0
package/cjs/constants.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Public = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const constants_js_1 = require("../constants.js");
|
|
6
|
+
const Public = () => (0, common_1.SetMetadata)(constants_js_1.IS_PUBLIC_KEY, true);
|
|
7
|
+
exports.Public = Public;
|
package/cjs/index.js
CHANGED
|
@@ -3,5 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
require("reflect-metadata");
|
|
5
5
|
require("./augmentation/nestjs.augmentation.js");
|
|
6
|
+
tslib_1.__exportStar(require("./constants.js"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./decorators/public.decorator.js"), exports);
|
|
6
8
|
tslib_1.__exportStar(require("./opra-http.module.js"), exports);
|
|
7
9
|
tslib_1.__exportStar(require("./opra-nestjs-adapter.js"), exports);
|
|
@@ -3,40 +3,48 @@ var OpraHttpCoreModule_1;
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.OpraHttpCoreModule = void 0;
|
|
5
5
|
const tslib_1 = require("tslib");
|
|
6
|
-
const ts_gems_1 = require("ts-gems");
|
|
7
6
|
const common_1 = require("@nestjs/common");
|
|
8
7
|
const common_2 = require("@opra/common");
|
|
8
|
+
const ts_gems_1 = require("ts-gems");
|
|
9
9
|
const opra_nestjs_adapter_js_1 = require("./opra-nestjs-adapter.js");
|
|
10
|
+
const opra_middleware_js_1 = require("./services/opra-middleware.js");
|
|
10
11
|
let OpraHttpCoreModule = OpraHttpCoreModule_1 = class OpraHttpCoreModule {
|
|
11
12
|
constructor(opraAdapter) {
|
|
12
13
|
this.opraAdapter = opraAdapter;
|
|
13
14
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
configure(consumer) {
|
|
16
|
+
consumer.apply(opra_middleware_js_1.OpraMiddleware).forRoutes({
|
|
17
|
+
path: '*',
|
|
18
|
+
method: common_1.RequestMethod.ALL,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
static forRoot(init, options) {
|
|
22
|
+
const opraAdapter = new opra_nestjs_adapter_js_1.OpraNestAdapter(init, options);
|
|
23
|
+
const token = init?.id || opra_nestjs_adapter_js_1.OpraNestAdapter;
|
|
17
24
|
const providers = [
|
|
18
|
-
...(
|
|
25
|
+
...(init?.providers || []),
|
|
19
26
|
{
|
|
20
27
|
provide: opra_nestjs_adapter_js_1.OpraNestAdapter,
|
|
21
28
|
useFactory: async () => {
|
|
22
29
|
(0, ts_gems_1.asMutable)(opraAdapter).document = await common_2.ApiDocumentFactory.createDocument({
|
|
23
|
-
...
|
|
24
|
-
api: { protocol: 'http', name:
|
|
30
|
+
...init,
|
|
31
|
+
api: { protocol: 'http', name: init.name, controllers: init.controllers },
|
|
25
32
|
});
|
|
26
33
|
return opraAdapter;
|
|
27
34
|
},
|
|
28
35
|
},
|
|
29
36
|
];
|
|
30
|
-
if (token !== opra_nestjs_adapter_js_1.OpraNestAdapter)
|
|
37
|
+
if (token !== opra_nestjs_adapter_js_1.OpraNestAdapter) {
|
|
31
38
|
providers.push({
|
|
32
39
|
provide: token,
|
|
33
40
|
useValue: opraAdapter,
|
|
34
41
|
});
|
|
42
|
+
}
|
|
35
43
|
return {
|
|
36
44
|
module: OpraHttpCoreModule_1,
|
|
37
|
-
controllers: opraAdapter.
|
|
38
|
-
imports: [...(
|
|
39
|
-
exports: [...(
|
|
45
|
+
controllers: opraAdapter.nestControllers,
|
|
46
|
+
imports: [...(init?.imports || [])],
|
|
47
|
+
exports: [...(init?.exports || []), token],
|
|
40
48
|
providers,
|
|
41
49
|
};
|
|
42
50
|
}
|
package/cjs/opra-http.module.js
CHANGED
|
@@ -8,12 +8,13 @@ const opra_http_core_module_js_1 = require("./opra-http-core.module.js");
|
|
|
8
8
|
let OpraHttpModule = OpraHttpModule_1 = class OpraHttpModule {
|
|
9
9
|
/**
|
|
10
10
|
*
|
|
11
|
+
* @param init
|
|
11
12
|
* @param options
|
|
12
13
|
*/
|
|
13
|
-
static forRoot(options) {
|
|
14
|
+
static forRoot(init, options) {
|
|
14
15
|
return {
|
|
15
16
|
module: OpraHttpModule_1,
|
|
16
|
-
imports: [opra_http_core_module_js_1.OpraHttpCoreModule.forRoot(options)],
|
|
17
|
+
imports: [opra_http_core_module_js_1.OpraHttpCoreModule.forRoot(init, options)],
|
|
17
18
|
};
|
|
18
19
|
}
|
|
19
20
|
};
|
|
@@ -2,85 +2,103 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OpraNestAdapter = exports.kHandler = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
-
const path_1 = tslib_1.__importDefault(require("path"));
|
|
6
5
|
const common_1 = require("@nestjs/common");
|
|
7
6
|
const common_2 = require("@opra/common");
|
|
8
7
|
const core_1 = require("@opra/core");
|
|
8
|
+
const path_1 = tslib_1.__importDefault(require("path"));
|
|
9
|
+
const ts_gems_1 = require("ts-gems");
|
|
10
|
+
const public_decorator_1 = require("./decorators/public.decorator");
|
|
9
11
|
const opra_exception_filter_js_1 = require("./services/opra-exception-filter.js");
|
|
10
12
|
exports.kHandler = Symbol.for('kHandler');
|
|
11
13
|
class OpraNestAdapter extends core_1.HttpAdapter {
|
|
12
|
-
constructor(options) {
|
|
14
|
+
constructor(init, options) {
|
|
13
15
|
super((function () {
|
|
14
16
|
const document = new common_2.ApiDocument();
|
|
15
17
|
document.api = new common_2.HttpApi(document);
|
|
16
18
|
return document;
|
|
17
19
|
})(), options);
|
|
18
|
-
|
|
19
|
-
this.
|
|
20
|
-
let basePath = options
|
|
20
|
+
this.nestControllers = [];
|
|
21
|
+
this.options = options;
|
|
22
|
+
let basePath = options?.basePath || '/';
|
|
21
23
|
if (!basePath.startsWith('/'))
|
|
22
24
|
basePath = '/' + basePath;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
this._addRootController(basePath);
|
|
26
|
+
if (init.controllers)
|
|
27
|
+
init.controllers.forEach(c => this._addToNestControllers(c, basePath));
|
|
25
28
|
}
|
|
26
29
|
async close() {
|
|
27
30
|
//
|
|
28
31
|
}
|
|
29
|
-
|
|
30
|
-
const
|
|
32
|
+
_addRootController(basePath) {
|
|
33
|
+
const _this = this;
|
|
34
|
+
let RootController = class RootController {
|
|
35
|
+
schema(_req, next) {
|
|
36
|
+
_this[exports.kHandler].sendDocumentSchema(_req.opraContext).catch(next);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
tslib_1.__decorate([
|
|
40
|
+
(0, common_1.Get)('/\\$schema'),
|
|
41
|
+
tslib_1.__param(0, (0, common_1.Req)()),
|
|
42
|
+
tslib_1.__param(1, (0, common_1.Next)()),
|
|
43
|
+
tslib_1.__metadata("design:type", Function),
|
|
44
|
+
tslib_1.__metadata("design:paramtypes", [Object, Function]),
|
|
45
|
+
tslib_1.__metadata("design:returntype", void 0)
|
|
46
|
+
], RootController.prototype, "schema", null);
|
|
47
|
+
RootController = tslib_1.__decorate([
|
|
48
|
+
(0, common_1.Controller)({
|
|
49
|
+
path: basePath,
|
|
50
|
+
})
|
|
51
|
+
], RootController);
|
|
52
|
+
if (this.options?.schemaRouteIsPublic) {
|
|
53
|
+
(0, public_decorator_1.Public)()(RootController.prototype, 'schema', Object.getOwnPropertyDescriptor(RootController.prototype, 'schema'));
|
|
54
|
+
}
|
|
55
|
+
this.nestControllers.push(RootController);
|
|
56
|
+
}
|
|
57
|
+
_addToNestControllers(sourceClass, currentPath) {
|
|
58
|
+
const metadata = Reflect.getMetadata(common_2.HTTP_CONTROLLER_METADATA, sourceClass);
|
|
31
59
|
if (!metadata)
|
|
32
60
|
return;
|
|
33
61
|
const newClass = {
|
|
34
|
-
[
|
|
62
|
+
[sourceClass.name]: class extends sourceClass {
|
|
35
63
|
},
|
|
36
|
-
}[
|
|
64
|
+
}[sourceClass.name];
|
|
37
65
|
const newPath = metadata.path ? path_1.default.join(currentPath, metadata.path) : currentPath;
|
|
38
66
|
const adapter = this;
|
|
39
67
|
/** Inject exception filter */
|
|
40
68
|
(0, common_1.UseFilters)(new opra_exception_filter_js_1.OpraExceptionFilter(adapter))(newClass);
|
|
41
69
|
(0, common_1.Controller)()(newClass);
|
|
42
|
-
this.
|
|
70
|
+
this.nestControllers.push(newClass);
|
|
43
71
|
if (metadata.operations) {
|
|
44
72
|
for (const [k, v] of Object.entries(metadata.operations)) {
|
|
45
|
-
const operationHandler =
|
|
73
|
+
const operationHandler = sourceClass.prototype[k];
|
|
46
74
|
Object.defineProperty(newClass.prototype, k, {
|
|
47
75
|
writable: true,
|
|
48
76
|
/** NestJS handler method */
|
|
49
|
-
async value(_req
|
|
50
|
-
const request = core_1.HttpIncoming.from(_req);
|
|
51
|
-
const response = core_1.HttpOutgoing.from(_res);
|
|
77
|
+
async value(_req) {
|
|
52
78
|
const api = adapter.document.api;
|
|
53
|
-
const controller = api.findController(
|
|
79
|
+
const controller = api.findController(sourceClass);
|
|
54
80
|
const operation = controller?.operations.get(k);
|
|
55
|
-
|
|
81
|
+
const context = (0, ts_gems_1.asMutable)(_req.opraContext);
|
|
82
|
+
if (!(context && operation && typeof operationHandler === 'function')) {
|
|
56
83
|
throw new common_2.NotFoundError({
|
|
57
|
-
message: `No endpoint found for [${
|
|
84
|
+
message: `No endpoint found for [${_req.method}]${_req.baseUrl}`,
|
|
58
85
|
details: {
|
|
59
|
-
path:
|
|
60
|
-
method:
|
|
86
|
+
path: _req.baseUrl,
|
|
87
|
+
method: _req.method,
|
|
61
88
|
},
|
|
62
89
|
});
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
request,
|
|
72
|
-
response,
|
|
73
|
-
operation,
|
|
74
|
-
controller: operation.owner,
|
|
75
|
-
controllerInstance: this,
|
|
76
|
-
operationHandler,
|
|
77
|
-
});
|
|
90
|
+
}
|
|
91
|
+
/** Configure the HttpContext */
|
|
92
|
+
context.adapter = adapter;
|
|
93
|
+
context.document = adapter.document;
|
|
94
|
+
context.operation = operation;
|
|
95
|
+
context.controller = operation.owner;
|
|
96
|
+
context.controllerInstance = this;
|
|
97
|
+
context.operationHandler = operationHandler;
|
|
78
98
|
/** Handle request */
|
|
79
99
|
await adapter[exports.kHandler].handleRequest(context);
|
|
80
100
|
},
|
|
81
101
|
});
|
|
82
|
-
(0, common_1.Req)()(newClass.prototype, k, 0);
|
|
83
|
-
(0, common_1.Res)()(newClass.prototype, k, 1);
|
|
84
102
|
/** Copy metadata keys from source function to new one */
|
|
85
103
|
const metadataKeys = Reflect.getOwnMetadataKeys(operationHandler);
|
|
86
104
|
const newFn = newClass.prototype[k];
|
|
@@ -88,6 +106,8 @@ class OpraNestAdapter extends core_1.HttpAdapter {
|
|
|
88
106
|
const m = Reflect.getMetadata(key, operationHandler);
|
|
89
107
|
Reflect.defineMetadata(key, m, newFn);
|
|
90
108
|
}
|
|
109
|
+
(0, common_1.Req)()(newClass.prototype, k, 0);
|
|
110
|
+
(0, common_1.Res)()(newClass.prototype, k, 1);
|
|
91
111
|
const descriptor = Object.getOwnPropertyDescriptor(newClass.prototype, k);
|
|
92
112
|
const operationPath = newPath + (v.path || '');
|
|
93
113
|
switch (v.method || 'GET') {
|
|
@@ -123,6 +143,8 @@ class OpraNestAdapter extends core_1.HttpAdapter {
|
|
|
123
143
|
/** Call @Search decorator over new property */
|
|
124
144
|
(0, common_1.Search)(operationPath)(newClass.prototype, k, descriptor);
|
|
125
145
|
break;
|
|
146
|
+
default:
|
|
147
|
+
break;
|
|
126
148
|
}
|
|
127
149
|
}
|
|
128
150
|
}
|
|
@@ -130,7 +152,7 @@ class OpraNestAdapter extends core_1.HttpAdapter {
|
|
|
130
152
|
for (const child of metadata.controllers) {
|
|
131
153
|
if (!(0, common_2.isConstructor)(child))
|
|
132
154
|
throw new TypeError('Controllers should be injectable a class');
|
|
133
|
-
this.
|
|
155
|
+
this._addToNestControllers(child, newPath);
|
|
134
156
|
}
|
|
135
157
|
}
|
|
136
158
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
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 core_1 = require("@opra/core");
|
|
7
|
+
let OpraMiddleware = class OpraMiddleware {
|
|
8
|
+
use(req, res, next) {
|
|
9
|
+
const request = core_1.HttpIncoming.from(req);
|
|
10
|
+
const response = core_1.HttpOutgoing.from(res);
|
|
11
|
+
/** Create the HttpContext */
|
|
12
|
+
req.opraContext = new core_1.HttpContext({
|
|
13
|
+
adapter: {},
|
|
14
|
+
platform: req.route ? 'express' : 'fastify',
|
|
15
|
+
request,
|
|
16
|
+
response,
|
|
17
|
+
});
|
|
18
|
+
next();
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
exports.OpraMiddleware = OpraMiddleware;
|
|
22
|
+
exports.OpraMiddleware = OpraMiddleware = tslib_1.__decorate([
|
|
23
|
+
(0, common_1.Injectable)()
|
|
24
|
+
], OpraMiddleware);
|
package/esm/constants.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const IS_PUBLIC_KEY = 'isPublic';
|
package/esm/index.js
CHANGED
|
@@ -1,39 +1,47 @@
|
|
|
1
1
|
var OpraHttpCoreModule_1;
|
|
2
2
|
import { __decorate, __metadata } from "tslib";
|
|
3
|
-
import {
|
|
4
|
-
import { Global, Module } from '@nestjs/common';
|
|
3
|
+
import { Global, Module, RequestMethod, } from '@nestjs/common';
|
|
5
4
|
import { ApiDocumentFactory } from '@opra/common';
|
|
5
|
+
import { asMutable } from 'ts-gems';
|
|
6
6
|
import { OpraNestAdapter } from './opra-nestjs-adapter.js';
|
|
7
|
+
import { OpraMiddleware } from './services/opra-middleware.js';
|
|
7
8
|
let OpraHttpCoreModule = OpraHttpCoreModule_1 = class OpraHttpCoreModule {
|
|
8
9
|
constructor(opraAdapter) {
|
|
9
10
|
this.opraAdapter = opraAdapter;
|
|
10
11
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
configure(consumer) {
|
|
13
|
+
consumer.apply(OpraMiddleware).forRoutes({
|
|
14
|
+
path: '*',
|
|
15
|
+
method: RequestMethod.ALL,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
static forRoot(init, options) {
|
|
19
|
+
const opraAdapter = new OpraNestAdapter(init, options);
|
|
20
|
+
const token = init?.id || OpraNestAdapter;
|
|
14
21
|
const providers = [
|
|
15
|
-
...(
|
|
22
|
+
...(init?.providers || []),
|
|
16
23
|
{
|
|
17
24
|
provide: OpraNestAdapter,
|
|
18
25
|
useFactory: async () => {
|
|
19
26
|
asMutable(opraAdapter).document = await ApiDocumentFactory.createDocument({
|
|
20
|
-
...
|
|
21
|
-
api: { protocol: 'http', name:
|
|
27
|
+
...init,
|
|
28
|
+
api: { protocol: 'http', name: init.name, controllers: init.controllers },
|
|
22
29
|
});
|
|
23
30
|
return opraAdapter;
|
|
24
31
|
},
|
|
25
32
|
},
|
|
26
33
|
];
|
|
27
|
-
if (token !== OpraNestAdapter)
|
|
34
|
+
if (token !== OpraNestAdapter) {
|
|
28
35
|
providers.push({
|
|
29
36
|
provide: token,
|
|
30
37
|
useValue: opraAdapter,
|
|
31
38
|
});
|
|
39
|
+
}
|
|
32
40
|
return {
|
|
33
41
|
module: OpraHttpCoreModule_1,
|
|
34
|
-
controllers: opraAdapter.
|
|
35
|
-
imports: [...(
|
|
36
|
-
exports: [...(
|
|
42
|
+
controllers: opraAdapter.nestControllers,
|
|
43
|
+
imports: [...(init?.imports || [])],
|
|
44
|
+
exports: [...(init?.exports || []), token],
|
|
37
45
|
providers,
|
|
38
46
|
};
|
|
39
47
|
}
|
package/esm/opra-http.module.js
CHANGED
|
@@ -5,12 +5,13 @@ import { OpraHttpCoreModule } from './opra-http-core.module.js';
|
|
|
5
5
|
let OpraHttpModule = OpraHttpModule_1 = class OpraHttpModule {
|
|
6
6
|
/**
|
|
7
7
|
*
|
|
8
|
+
* @param init
|
|
8
9
|
* @param options
|
|
9
10
|
*/
|
|
10
|
-
static forRoot(options) {
|
|
11
|
+
static forRoot(init, options) {
|
|
11
12
|
return {
|
|
12
13
|
module: OpraHttpModule_1,
|
|
13
|
-
imports: [OpraHttpCoreModule.forRoot(options)],
|
|
14
|
+
imports: [OpraHttpCoreModule.forRoot(init, options)],
|
|
14
15
|
};
|
|
15
16
|
}
|
|
16
17
|
};
|
|
@@ -1,82 +1,101 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { Controller, Delete, Get, Head, Options, Patch, Post, Put, Req, Res, Search, UseFilters, } from '@nestjs/common';
|
|
1
|
+
import { __decorate, __metadata, __param } from "tslib";
|
|
2
|
+
import { Controller, Delete, Get, Head, Next, Options, Patch, Post, Put, Req, Res, Search, UseFilters, } from '@nestjs/common';
|
|
3
3
|
import { ApiDocument, HTTP_CONTROLLER_METADATA, HttpApi, isConstructor, NotFoundError, } from '@opra/common';
|
|
4
|
-
import { HttpAdapter
|
|
4
|
+
import { HttpAdapter } from '@opra/core';
|
|
5
|
+
import nodePath from 'path';
|
|
6
|
+
import { asMutable } from 'ts-gems';
|
|
7
|
+
import { Public } from './decorators/public.decorator';
|
|
5
8
|
import { OpraExceptionFilter } from './services/opra-exception-filter.js';
|
|
6
9
|
export const kHandler = Symbol.for('kHandler');
|
|
7
10
|
export class OpraNestAdapter extends HttpAdapter {
|
|
8
|
-
constructor(options) {
|
|
11
|
+
constructor(init, options) {
|
|
9
12
|
super((function () {
|
|
10
13
|
const document = new ApiDocument();
|
|
11
14
|
document.api = new HttpApi(document);
|
|
12
15
|
return document;
|
|
13
16
|
})(), options);
|
|
14
|
-
|
|
15
|
-
this.
|
|
16
|
-
let basePath = options
|
|
17
|
+
this.nestControllers = [];
|
|
18
|
+
this.options = options;
|
|
19
|
+
let basePath = options?.basePath || '/';
|
|
17
20
|
if (!basePath.startsWith('/'))
|
|
18
21
|
basePath = '/' + basePath;
|
|
19
|
-
|
|
20
|
-
|
|
22
|
+
this._addRootController(basePath);
|
|
23
|
+
if (init.controllers)
|
|
24
|
+
init.controllers.forEach(c => this._addToNestControllers(c, basePath));
|
|
21
25
|
}
|
|
22
26
|
async close() {
|
|
23
27
|
//
|
|
24
28
|
}
|
|
25
|
-
|
|
26
|
-
const
|
|
29
|
+
_addRootController(basePath) {
|
|
30
|
+
const _this = this;
|
|
31
|
+
let RootController = class RootController {
|
|
32
|
+
schema(_req, next) {
|
|
33
|
+
_this[kHandler].sendDocumentSchema(_req.opraContext).catch(next);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
__decorate([
|
|
37
|
+
Get('/\\$schema'),
|
|
38
|
+
__param(0, Req()),
|
|
39
|
+
__param(1, Next()),
|
|
40
|
+
__metadata("design:type", Function),
|
|
41
|
+
__metadata("design:paramtypes", [Object, Function]),
|
|
42
|
+
__metadata("design:returntype", void 0)
|
|
43
|
+
], RootController.prototype, "schema", null);
|
|
44
|
+
RootController = __decorate([
|
|
45
|
+
Controller({
|
|
46
|
+
path: basePath,
|
|
47
|
+
})
|
|
48
|
+
], RootController);
|
|
49
|
+
if (this.options?.schemaRouteIsPublic) {
|
|
50
|
+
Public()(RootController.prototype, 'schema', Object.getOwnPropertyDescriptor(RootController.prototype, 'schema'));
|
|
51
|
+
}
|
|
52
|
+
this.nestControllers.push(RootController);
|
|
53
|
+
}
|
|
54
|
+
_addToNestControllers(sourceClass, currentPath) {
|
|
55
|
+
const metadata = Reflect.getMetadata(HTTP_CONTROLLER_METADATA, sourceClass);
|
|
27
56
|
if (!metadata)
|
|
28
57
|
return;
|
|
29
58
|
const newClass = {
|
|
30
|
-
[
|
|
59
|
+
[sourceClass.name]: class extends sourceClass {
|
|
31
60
|
},
|
|
32
|
-
}[
|
|
61
|
+
}[sourceClass.name];
|
|
33
62
|
const newPath = metadata.path ? nodePath.join(currentPath, metadata.path) : currentPath;
|
|
34
63
|
const adapter = this;
|
|
35
64
|
/** Inject exception filter */
|
|
36
65
|
UseFilters(new OpraExceptionFilter(adapter))(newClass);
|
|
37
66
|
Controller()(newClass);
|
|
38
|
-
this.
|
|
67
|
+
this.nestControllers.push(newClass);
|
|
39
68
|
if (metadata.operations) {
|
|
40
69
|
for (const [k, v] of Object.entries(metadata.operations)) {
|
|
41
|
-
const operationHandler =
|
|
70
|
+
const operationHandler = sourceClass.prototype[k];
|
|
42
71
|
Object.defineProperty(newClass.prototype, k, {
|
|
43
72
|
writable: true,
|
|
44
73
|
/** NestJS handler method */
|
|
45
|
-
async value(_req
|
|
46
|
-
const request = HttpIncoming.from(_req);
|
|
47
|
-
const response = HttpOutgoing.from(_res);
|
|
74
|
+
async value(_req) {
|
|
48
75
|
const api = adapter.document.api;
|
|
49
|
-
const controller = api.findController(
|
|
76
|
+
const controller = api.findController(sourceClass);
|
|
50
77
|
const operation = controller?.operations.get(k);
|
|
51
|
-
|
|
78
|
+
const context = asMutable(_req.opraContext);
|
|
79
|
+
if (!(context && operation && typeof operationHandler === 'function')) {
|
|
52
80
|
throw new NotFoundError({
|
|
53
|
-
message: `No endpoint found for [${
|
|
81
|
+
message: `No endpoint found for [${_req.method}]${_req.baseUrl}`,
|
|
54
82
|
details: {
|
|
55
|
-
path:
|
|
56
|
-
method:
|
|
83
|
+
path: _req.baseUrl,
|
|
84
|
+
method: _req.method,
|
|
57
85
|
},
|
|
58
86
|
});
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
request,
|
|
68
|
-
response,
|
|
69
|
-
operation,
|
|
70
|
-
controller: operation.owner,
|
|
71
|
-
controllerInstance: this,
|
|
72
|
-
operationHandler,
|
|
73
|
-
});
|
|
87
|
+
}
|
|
88
|
+
/** Configure the HttpContext */
|
|
89
|
+
context.adapter = adapter;
|
|
90
|
+
context.document = adapter.document;
|
|
91
|
+
context.operation = operation;
|
|
92
|
+
context.controller = operation.owner;
|
|
93
|
+
context.controllerInstance = this;
|
|
94
|
+
context.operationHandler = operationHandler;
|
|
74
95
|
/** Handle request */
|
|
75
96
|
await adapter[kHandler].handleRequest(context);
|
|
76
97
|
},
|
|
77
98
|
});
|
|
78
|
-
Req()(newClass.prototype, k, 0);
|
|
79
|
-
Res()(newClass.prototype, k, 1);
|
|
80
99
|
/** Copy metadata keys from source function to new one */
|
|
81
100
|
const metadataKeys = Reflect.getOwnMetadataKeys(operationHandler);
|
|
82
101
|
const newFn = newClass.prototype[k];
|
|
@@ -84,6 +103,8 @@ export class OpraNestAdapter extends HttpAdapter {
|
|
|
84
103
|
const m = Reflect.getMetadata(key, operationHandler);
|
|
85
104
|
Reflect.defineMetadata(key, m, newFn);
|
|
86
105
|
}
|
|
106
|
+
Req()(newClass.prototype, k, 0);
|
|
107
|
+
Res()(newClass.prototype, k, 1);
|
|
87
108
|
const descriptor = Object.getOwnPropertyDescriptor(newClass.prototype, k);
|
|
88
109
|
const operationPath = newPath + (v.path || '');
|
|
89
110
|
switch (v.method || 'GET') {
|
|
@@ -119,6 +140,8 @@ export class OpraNestAdapter extends HttpAdapter {
|
|
|
119
140
|
/** Call @Search decorator over new property */
|
|
120
141
|
Search(operationPath)(newClass.prototype, k, descriptor);
|
|
121
142
|
break;
|
|
143
|
+
default:
|
|
144
|
+
break;
|
|
122
145
|
}
|
|
123
146
|
}
|
|
124
147
|
}
|
|
@@ -126,7 +149,7 @@ export class OpraNestAdapter extends HttpAdapter {
|
|
|
126
149
|
for (const child of metadata.controllers) {
|
|
127
150
|
if (!isConstructor(child))
|
|
128
151
|
throw new TypeError('Controllers should be injectable a class');
|
|
129
|
-
this.
|
|
152
|
+
this._addToNestControllers(child, newPath);
|
|
130
153
|
}
|
|
131
154
|
}
|
|
132
155
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { __decorate } from "tslib";
|
|
2
|
+
import { Injectable } from '@nestjs/common';
|
|
3
|
+
import { HttpContext, HttpIncoming, HttpOutgoing } from '@opra/core';
|
|
4
|
+
let OpraMiddleware = class OpraMiddleware {
|
|
5
|
+
use(req, res, next) {
|
|
6
|
+
const request = HttpIncoming.from(req);
|
|
7
|
+
const response = HttpOutgoing.from(res);
|
|
8
|
+
/** Create the HttpContext */
|
|
9
|
+
req.opraContext = new HttpContext({
|
|
10
|
+
adapter: {},
|
|
11
|
+
platform: req.route ? 'express' : 'fastify',
|
|
12
|
+
request,
|
|
13
|
+
response,
|
|
14
|
+
});
|
|
15
|
+
next();
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
OpraMiddleware = __decorate([
|
|
19
|
+
Injectable()
|
|
20
|
+
], OpraMiddleware);
|
|
21
|
+
export { OpraMiddleware };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/nestjs",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.10",
|
|
4
4
|
"description": "Opra NestJS module",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,8 +18,8 @@
|
|
|
18
18
|
"postbuild": "cp README.md package.json ../../LICENSE ../../build/nestjs && cp ../../package.cjs.json ../../build/nestjs/cjs/package.json",
|
|
19
19
|
"lint": "eslint . --max-warnings=0",
|
|
20
20
|
"format": "prettier . --write --log-level=warn",
|
|
21
|
-
"test": "jest",
|
|
22
|
-
"cover": "jest --collect-coverage",
|
|
21
|
+
"test": "jest --passWithNoTests",
|
|
22
|
+
"cover": "jest --passWithNoTests --collect-coverage",
|
|
23
23
|
"clean": "npm run clean:src && npm run clean:test && npm run clean:dist && npm run clean:cover",
|
|
24
24
|
"clean:src": "ts-cleanup -s src --all",
|
|
25
25
|
"clean:test": "ts-cleanup -s test --all",
|
|
@@ -27,21 +27,23 @@
|
|
|
27
27
|
"clean:cover": "rimraf ../../coverage/client"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@opra/common": "^0.
|
|
31
|
-
"@opra/core": "^0.
|
|
30
|
+
"@opra/common": "^1.0.0-alpha.10",
|
|
31
|
+
"@opra/core": "^1.0.0-alpha.10",
|
|
32
32
|
"fast-tokenizer": "^1.3.0",
|
|
33
33
|
"lodash.head": "^4.0.1",
|
|
34
34
|
"reflect-metadata": "^0.2.2"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"@nestjs/common": "^10.3.
|
|
38
|
-
"@nestjs/core": "^10.3.
|
|
37
|
+
"@nestjs/common": "^10.3.10",
|
|
38
|
+
"@nestjs/core": "^10.3.10"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@nestjs/platform-express": "^10.3.
|
|
42
|
-
"@nestjs/testing": "^10.3.
|
|
41
|
+
"@nestjs/platform-express": "^10.3.10",
|
|
42
|
+
"@nestjs/testing": "^10.3.10",
|
|
43
43
|
"@types/lodash.head": "^4.0.9",
|
|
44
44
|
"filedirname": "^3.4.0",
|
|
45
|
+
"rxjs": "^7.8.1",
|
|
46
|
+
"supertest": "^7.0.0",
|
|
45
47
|
"ts-gems": "^3.4.0"
|
|
46
48
|
},
|
|
47
49
|
"type": "module",
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const IS_PUBLIC_KEY = "isPublic";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const Public: () => import("@nestjs/common").CustomDecorator<string>;
|
package/types/index.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { DynamicModule, OnModuleDestroy } from '@nestjs/common';
|
|
1
|
+
import { DynamicModule, MiddlewareConsumer, NestModule, OnModuleDestroy } from '@nestjs/common';
|
|
2
2
|
import type { OpraHttpModule } from './opra-http.module';
|
|
3
3
|
import { OpraNestAdapter } from './opra-nestjs-adapter.js';
|
|
4
|
-
export declare class OpraHttpCoreModule implements OnModuleDestroy {
|
|
4
|
+
export declare class OpraHttpCoreModule implements OnModuleDestroy, NestModule {
|
|
5
5
|
protected opraAdapter: OpraNestAdapter;
|
|
6
6
|
constructor(opraAdapter: OpraNestAdapter);
|
|
7
|
-
|
|
7
|
+
configure(consumer: MiddlewareConsumer): void;
|
|
8
|
+
static forRoot(init: OpraHttpModule.Initiator, options?: OpraHttpModule.Options): DynamicModule;
|
|
8
9
|
onModuleDestroy(): Promise<void>;
|
|
9
10
|
}
|
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
import { DynamicModule } from '@nestjs/common';
|
|
2
2
|
import { ApiDocumentFactory } from '@opra/common';
|
|
3
3
|
export declare namespace OpraHttpModule {
|
|
4
|
-
interface
|
|
4
|
+
interface Initiator extends Pick<DynamicModule, 'imports' | 'providers' | 'exports' | 'controllers'>, Pick<ApiDocumentFactory.InitArguments, 'types' | 'references' | 'info'> {
|
|
5
5
|
id?: any;
|
|
6
6
|
name: string;
|
|
7
|
+
}
|
|
8
|
+
interface Options {
|
|
7
9
|
basePath?: string;
|
|
10
|
+
schemaRouteIsPublic?: boolean;
|
|
8
11
|
}
|
|
9
12
|
}
|
|
10
13
|
export declare class OpraHttpModule {
|
|
11
14
|
/**
|
|
12
15
|
*
|
|
16
|
+
* @param init
|
|
13
17
|
* @param options
|
|
14
18
|
*/
|
|
15
|
-
static forRoot(
|
|
19
|
+
static forRoot(init: OpraHttpModule.Initiator, options?: OpraHttpModule.Options): DynamicModule;
|
|
16
20
|
}
|
|
@@ -3,8 +3,10 @@ import { HttpAdapter } from '@opra/core';
|
|
|
3
3
|
import type { OpraHttpModule } from './opra-http.module';
|
|
4
4
|
export declare const kHandler: unique symbol;
|
|
5
5
|
export declare class OpraNestAdapter extends HttpAdapter {
|
|
6
|
-
readonly
|
|
7
|
-
|
|
6
|
+
readonly nestControllers: Type[];
|
|
7
|
+
readonly options?: OpraHttpModule.Options;
|
|
8
|
+
constructor(init: OpraHttpModule.Initiator, options?: OpraHttpModule.Options);
|
|
8
9
|
close(): Promise<void>;
|
|
9
|
-
protected
|
|
10
|
+
protected _addRootController(basePath: string): void;
|
|
11
|
+
protected _addToNestControllers(sourceClass: Type, currentPath: string): void;
|
|
10
12
|
}
|