@opra/nestjs 1.0.0-beta.3 → 1.0.0-beta.5
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/augmentation/nestjs.augmentation.js +8 -0
- package/cjs/constants.js +3 -2
- package/cjs/http/opra-http-core.module.js +116 -0
- package/cjs/{opra-nestjs-adapter.js → http/opra-http-nestjs-adapter.js} +14 -21
- package/cjs/{opra-http.module.js → http/opra-http.module.js} +11 -2
- package/cjs/{services → http/services}/opra-exception-filter.js +2 -2
- package/cjs/{services → http/services}/opra-middleware.js +3 -6
- package/cjs/index.js +4 -2
- package/cjs/kafka/opra-kafka-core.module.js +107 -0
- package/cjs/kafka/opra-kafka-nestjs-adapter.js +79 -0
- package/cjs/kafka/opra-kafka.module.js +33 -0
- package/esm/augmentation/nestjs.augmentation.js +8 -0
- package/esm/constants.js +2 -1
- package/esm/http/opra-http-core.module.js +113 -0
- package/esm/{opra-nestjs-adapter.js → http/opra-http-nestjs-adapter.js} +12 -19
- package/esm/{opra-http.module.js → http/opra-http.module.js} +11 -2
- package/esm/{services → http/services}/opra-exception-filter.js +2 -2
- package/esm/{services → http/services}/opra-middleware.js +5 -8
- package/esm/index.js +4 -2
- package/esm/kafka/opra-kafka-core.module.js +104 -0
- package/esm/kafka/opra-kafka-nestjs-adapter.js +75 -0
- package/esm/kafka/opra-kafka.module.js +30 -0
- package/package.json +8 -4
- package/types/constants.d.ts +2 -1
- package/types/http/opra-http-core.module.d.ts +12 -0
- package/types/{opra-nestjs-adapter.d.ts → http/opra-http-nestjs-adapter.d.ts} +6 -5
- package/types/http/opra-http.module.d.ts +35 -0
- package/types/http/services/opra-middleware.d.ts +8 -0
- package/types/index.d.cts +4 -2
- package/types/index.d.ts +4 -2
- package/types/kafka/opra-kafka-core.module.d.ts +14 -0
- package/types/kafka/opra-kafka-nestjs-adapter.d.ts +19 -0
- package/types/kafka/opra-kafka.module.d.ts +33 -0
- package/cjs/opra-http-core.module.js +0 -82
- package/esm/opra-http-core.module.js +0 -79
- package/types/opra-http-core.module.d.ts +0 -10
- package/types/opra-http.module.d.ts +0 -22
- package/types/services/opra-middleware.d.ts +0 -10
- /package/types/{services → http/services}/opra-exception-filter.d.ts +0 -0
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const common_1 = require("@nestjs/common");
|
|
3
4
|
const external_exception_filter_js_1 = require("@nestjs/core/exceptions/external-exception-filter.js");
|
|
5
|
+
const common_2 = require("@opra/common");
|
|
6
|
+
const { RpcControllerDecoratorFactory } = common_2.classes;
|
|
4
7
|
const oldCatchMethod = external_exception_filter_js_1.ExternalExceptionFilter.prototype.catch;
|
|
5
8
|
external_exception_filter_js_1.ExternalExceptionFilter.prototype.catch = function (exception, host) {
|
|
6
9
|
const opraContext = host.getArgByIndex(3);
|
|
@@ -9,3 +12,8 @@ external_exception_filter_js_1.ExternalExceptionFilter.prototype.catch = functio
|
|
|
9
12
|
throw exception;
|
|
10
13
|
oldCatchMethod(exception, host);
|
|
11
14
|
};
|
|
15
|
+
RpcControllerDecoratorFactory.augment((decorator, decoratorChain) => {
|
|
16
|
+
decoratorChain.push((_, target) => {
|
|
17
|
+
(0, common_1.Controller)()(target);
|
|
18
|
+
});
|
|
19
|
+
});
|
package/cjs/constants.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.OPRA_KAFKA_MODULE_CONFIG = exports.OPRA_HTTP_API_CONFIG = exports.IS_PUBLIC_KEY = void 0;
|
|
4
4
|
exports.IS_PUBLIC_KEY = 'opra:isPublic';
|
|
5
|
-
exports.
|
|
5
|
+
exports.OPRA_HTTP_API_CONFIG = 'OPRA_HTTP_API_CONFIG';
|
|
6
|
+
exports.OPRA_KAFKA_MODULE_CONFIG = 'OPRA_KAFKA_MODULE_CONFIG';
|
|
@@ -0,0 +1,116 @@
|
|
|
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 common_1 = require("@nestjs/common");
|
|
7
|
+
const core_1 = require("@nestjs/core");
|
|
8
|
+
const common_2 = require("@opra/common");
|
|
9
|
+
const ts_gems_1 = require("ts-gems");
|
|
10
|
+
const constants_js_1 = require("../constants.js");
|
|
11
|
+
const opra_http_nestjs_adapter_js_1 = require("./opra-http-nestjs-adapter.js");
|
|
12
|
+
const opra_exception_filter_js_1 = require("./services/opra-exception-filter.js");
|
|
13
|
+
const opra_middleware_js_1 = require("./services/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.logger = opraNestAdapter.logger || new common_1.Logger(apiConfig.name);
|
|
63
|
+
(0, ts_gems_1.asMutable)(opraNestAdapter).document = await common_2.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 ((0, common_2.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: core_1.APP_FILTER,
|
|
100
|
+
useClass: opra_exception_filter_js_1.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
|
+
exports.OpraHttpCoreModule = OpraHttpCoreModule;
|
|
112
|
+
exports.OpraHttpCoreModule = OpraHttpCoreModule = OpraHttpCoreModule_1 = tslib_1.__decorate([
|
|
113
|
+
(0, common_1.Module)({}),
|
|
114
|
+
(0, common_1.Global)(),
|
|
115
|
+
tslib_1.__metadata("design:paramtypes", [opra_http_nestjs_adapter_js_1.OpraHttpNestjsAdapter])
|
|
116
|
+
], OpraHttpCoreModule);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.OpraHttpNestjsAdapter = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const node_path_1 = tslib_1.__importDefault(require("node:path"));
|
|
6
6
|
const common_1 = require("@nestjs/common");
|
|
@@ -8,30 +8,23 @@ const constants_js_1 = require("@nestjs/common/constants.js");
|
|
|
8
8
|
const common_2 = require("@opra/common");
|
|
9
9
|
const http_1 = require("@opra/http");
|
|
10
10
|
const ts_gems_1 = require("ts-gems");
|
|
11
|
-
const public_decorator_js_1 = require("
|
|
12
|
-
class
|
|
13
|
-
constructor(
|
|
11
|
+
const public_decorator_js_1 = require("../decorators/public.decorator.js");
|
|
12
|
+
class OpraHttpNestjsAdapter extends http_1.HttpAdapter {
|
|
13
|
+
constructor(options) {
|
|
14
14
|
super((function () {
|
|
15
15
|
const document = new common_2.ApiDocument();
|
|
16
|
-
document.api = new common_2.HttpApi({ owner: document, name:
|
|
16
|
+
document.api = new common_2.HttpApi({ owner: document, name: '', transport: 'http' });
|
|
17
17
|
return document;
|
|
18
|
-
})(),
|
|
19
|
-
...options,
|
|
20
|
-
interceptors: options?.interceptors,
|
|
21
|
-
});
|
|
18
|
+
})(), options);
|
|
22
19
|
this.nestControllers = [];
|
|
23
|
-
this.options
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
basePath = '/' + basePath;
|
|
27
|
-
this._addRootController(basePath);
|
|
28
|
-
if (init.controllers)
|
|
29
|
-
init.controllers.forEach(c => this._addToNestControllers(c, basePath, []));
|
|
20
|
+
this._addRootController(options.schemaIsPublic);
|
|
21
|
+
if (options.controllers)
|
|
22
|
+
options.controllers.forEach(c => this._addToNestControllers(c, this.basePath, []));
|
|
30
23
|
}
|
|
31
24
|
async close() {
|
|
32
25
|
//
|
|
33
26
|
}
|
|
34
|
-
_addRootController(
|
|
27
|
+
_addRootController(isPublic) {
|
|
35
28
|
const _this = this;
|
|
36
29
|
let RootController = class RootController {
|
|
37
30
|
schema(_req, next) {
|
|
@@ -48,10 +41,10 @@ class OpraNestAdapter extends http_1.HttpAdapter {
|
|
|
48
41
|
], RootController.prototype, "schema", null);
|
|
49
42
|
RootController = tslib_1.__decorate([
|
|
50
43
|
(0, common_1.Controller)({
|
|
51
|
-
path: basePath,
|
|
44
|
+
path: this.basePath,
|
|
52
45
|
})
|
|
53
46
|
], RootController);
|
|
54
|
-
if (
|
|
47
|
+
if (isPublic) {
|
|
55
48
|
(0, public_decorator_js_1.Public)()(RootController.prototype, 'schema', Object.getOwnPropertyDescriptor(RootController.prototype, 'schema'));
|
|
56
49
|
}
|
|
57
50
|
this.nestControllers.push(RootController);
|
|
@@ -66,7 +59,7 @@ class OpraNestAdapter extends http_1.HttpAdapter {
|
|
|
66
59
|
}[sourceClass.name];
|
|
67
60
|
/** Copy metadata keys from source class to new one */
|
|
68
61
|
let metadataKeys;
|
|
69
|
-
|
|
62
|
+
OpraHttpNestjsAdapter.copyDecoratorMetadataToChild(newClass, parentTree);
|
|
70
63
|
const newPath = metadata.path ? node_path_1.default.join(currentPath, metadata.path) : currentPath;
|
|
71
64
|
const adapter = this;
|
|
72
65
|
// adapter.logger =
|
|
@@ -187,4 +180,4 @@ class OpraNestAdapter extends http_1.HttpAdapter {
|
|
|
187
180
|
}
|
|
188
181
|
}
|
|
189
182
|
}
|
|
190
|
-
exports.
|
|
183
|
+
exports.OpraHttpNestjsAdapter = OpraHttpNestjsAdapter;
|
|
@@ -9,12 +9,21 @@ let OpraHttpModule = OpraHttpModule_1 = class OpraHttpModule {
|
|
|
9
9
|
/**
|
|
10
10
|
*
|
|
11
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
|
+
*
|
|
12
21
|
* @param options
|
|
13
22
|
*/
|
|
14
|
-
static
|
|
23
|
+
static forRootAsync(options) {
|
|
15
24
|
return {
|
|
16
25
|
module: OpraHttpModule_1,
|
|
17
|
-
imports: [opra_http_core_module_js_1.OpraHttpCoreModule.
|
|
26
|
+
imports: [opra_http_core_module_js_1.OpraHttpCoreModule.forRootAsync(options)],
|
|
18
27
|
};
|
|
19
28
|
}
|
|
20
29
|
};
|
|
@@ -4,7 +4,7 @@ exports.OpraExceptionFilter = void 0;
|
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const common_1 = require("@nestjs/common");
|
|
6
6
|
const core_1 = require("@nestjs/core");
|
|
7
|
-
const
|
|
7
|
+
const opra_http_nestjs_adapter_js_1 = require("../opra-http-nestjs-adapter.js");
|
|
8
8
|
let OpraExceptionFilter = class OpraExceptionFilter extends core_1.BaseExceptionFilter {
|
|
9
9
|
constructor(moduleRef) {
|
|
10
10
|
super();
|
|
@@ -13,7 +13,7 @@ let OpraExceptionFilter = class OpraExceptionFilter extends core_1.BaseException
|
|
|
13
13
|
catch(exception, host) {
|
|
14
14
|
const ctx = host.switchToHttp().getRequest().opraContext;
|
|
15
15
|
if (ctx) {
|
|
16
|
-
const adapter = this.moduleRef.get(
|
|
16
|
+
const adapter = this.moduleRef.get(opra_http_nestjs_adapter_js_1.OpraHttpNestjsAdapter);
|
|
17
17
|
ctx.errors.push(exception);
|
|
18
18
|
return adapter.handler.sendResponse(ctx);
|
|
19
19
|
}
|
|
@@ -4,12 +4,10 @@ exports.OpraMiddleware = void 0;
|
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const common_1 = require("@nestjs/common");
|
|
6
6
|
const http_1 = require("@opra/http");
|
|
7
|
-
const
|
|
8
|
-
const opra_nestjs_adapter_js_1 = require("../opra-nestjs-adapter.js");
|
|
7
|
+
const opra_http_nestjs_adapter_js_1 = require("../opra-http-nestjs-adapter.js");
|
|
9
8
|
let OpraMiddleware = class OpraMiddleware {
|
|
10
|
-
constructor(opraAdapter
|
|
9
|
+
constructor(opraAdapter) {
|
|
11
10
|
this.opraAdapter = opraAdapter;
|
|
12
|
-
this.options = options;
|
|
13
11
|
}
|
|
14
12
|
use(req, res, next) {
|
|
15
13
|
const request = http_1.HttpIncoming.from(req);
|
|
@@ -31,6 +29,5 @@ let OpraMiddleware = class OpraMiddleware {
|
|
|
31
29
|
exports.OpraMiddleware = OpraMiddleware;
|
|
32
30
|
exports.OpraMiddleware = OpraMiddleware = tslib_1.__decorate([
|
|
33
31
|
(0, common_1.Injectable)(),
|
|
34
|
-
tslib_1.
|
|
35
|
-
tslib_1.__metadata("design:paramtypes", [opra_nestjs_adapter_js_1.OpraNestAdapter, Object])
|
|
32
|
+
tslib_1.__metadata("design:paramtypes", [opra_http_nestjs_adapter_js_1.OpraHttpNestjsAdapter])
|
|
36
33
|
], OpraMiddleware);
|
package/cjs/index.js
CHANGED
|
@@ -5,5 +5,7 @@ require("reflect-metadata");
|
|
|
5
5
|
require("./augmentation/nestjs.augmentation.js");
|
|
6
6
|
tslib_1.__exportStar(require("./constants.js"), exports);
|
|
7
7
|
tslib_1.__exportStar(require("./decorators/public.decorator.js"), exports);
|
|
8
|
-
tslib_1.__exportStar(require("./opra-http.module.js"), exports);
|
|
9
|
-
tslib_1.__exportStar(require("./opra-nestjs-adapter.js"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./http/opra-http.module.js"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./http/opra-http-nestjs-adapter.js"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./kafka/opra-kafka.module.js"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./kafka/opra-kafka-nestjs-adapter.js"), exports);
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var OpraKafkaCoreModule_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.OpraKafkaCoreModule = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const common_1 = require("@nestjs/common");
|
|
7
|
+
const core_1 = require("@nestjs/core");
|
|
8
|
+
const common_2 = require("@opra/common");
|
|
9
|
+
const constants_js_1 = require("../constants.js");
|
|
10
|
+
const opra_kafka_nestjs_adapter_js_1 = require("./opra-kafka-nestjs-adapter.js");
|
|
11
|
+
const opraKafkaNestjsAdapterToken = Symbol('OpraKafkaNestjsAdapter');
|
|
12
|
+
let OpraKafkaCoreModule = OpraKafkaCoreModule_1 = class OpraKafkaCoreModule {
|
|
13
|
+
constructor(adapter, init) {
|
|
14
|
+
this.adapter = adapter;
|
|
15
|
+
this.init = init;
|
|
16
|
+
}
|
|
17
|
+
static forRoot(moduleOptions) {
|
|
18
|
+
return this._getDynamicModule({
|
|
19
|
+
...moduleOptions,
|
|
20
|
+
providers: [
|
|
21
|
+
...(moduleOptions?.providers || []),
|
|
22
|
+
{
|
|
23
|
+
provide: constants_js_1.OPRA_KAFKA_MODULE_CONFIG,
|
|
24
|
+
useValue: {
|
|
25
|
+
...moduleOptions,
|
|
26
|
+
logger: moduleOptions.logger || new common_1.Logger(moduleOptions.name),
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
static forRootAsync(moduleOptions) {
|
|
33
|
+
if (!moduleOptions.useFactory)
|
|
34
|
+
throw new Error('Invalid configuration. Must provide "useFactory"');
|
|
35
|
+
return this._getDynamicModule({
|
|
36
|
+
...moduleOptions,
|
|
37
|
+
providers: [
|
|
38
|
+
...(moduleOptions?.providers || []),
|
|
39
|
+
{
|
|
40
|
+
provide: constants_js_1.OPRA_KAFKA_MODULE_CONFIG,
|
|
41
|
+
inject: moduleOptions.inject,
|
|
42
|
+
useFactory: async (...args) => {
|
|
43
|
+
const result = await moduleOptions.useFactory(...args);
|
|
44
|
+
result.logger = result.logger || new common_1.Logger(result.name);
|
|
45
|
+
return result;
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
static _getDynamicModule(moduleOptions) {
|
|
52
|
+
const token = moduleOptions.id || opra_kafka_nestjs_adapter_js_1.OpraKafkaNestjsAdapter;
|
|
53
|
+
const adapterProvider = {
|
|
54
|
+
provide: token,
|
|
55
|
+
inject: [core_1.ModuleRef, constants_js_1.OPRA_KAFKA_MODULE_CONFIG],
|
|
56
|
+
useFactory: async (moduleRef, config) => {
|
|
57
|
+
const adapter = new opra_kafka_nestjs_adapter_js_1.OpraKafkaNestjsAdapter(moduleRef);
|
|
58
|
+
await adapter.initialize(config);
|
|
59
|
+
if (moduleOptions.interceptors) {
|
|
60
|
+
adapter.adapter.interceptors = moduleOptions.interceptors.map(x => {
|
|
61
|
+
if ((0, common_2.isConstructor)(x)) {
|
|
62
|
+
return async (ctx, next) => {
|
|
63
|
+
const interceptor = moduleRef.get(x);
|
|
64
|
+
if (typeof interceptor.intercept === 'function')
|
|
65
|
+
return interceptor.intercept(ctx, next);
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
return x;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return adapter;
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
return {
|
|
75
|
+
global: moduleOptions.global,
|
|
76
|
+
module: OpraKafkaCoreModule_1,
|
|
77
|
+
controllers: moduleOptions.controllers,
|
|
78
|
+
providers: [
|
|
79
|
+
...(moduleOptions?.providers || []),
|
|
80
|
+
adapterProvider,
|
|
81
|
+
{
|
|
82
|
+
provide: opraKafkaNestjsAdapterToken,
|
|
83
|
+
useExisting: token,
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
imports: [...(moduleOptions?.imports || [])],
|
|
87
|
+
exports: [...(moduleOptions?.exports || []), adapterProvider],
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
async onModuleInit() {
|
|
91
|
+
await this.adapter.initialize(this.init);
|
|
92
|
+
}
|
|
93
|
+
async onApplicationBootstrap() {
|
|
94
|
+
await this.adapter.start();
|
|
95
|
+
}
|
|
96
|
+
async onApplicationShutdown() {
|
|
97
|
+
await this.adapter.stop();
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
exports.OpraKafkaCoreModule = OpraKafkaCoreModule;
|
|
101
|
+
exports.OpraKafkaCoreModule = OpraKafkaCoreModule = OpraKafkaCoreModule_1 = tslib_1.__decorate([
|
|
102
|
+
(0, common_1.Module)({}),
|
|
103
|
+
(0, common_1.Global)(),
|
|
104
|
+
tslib_1.__param(0, (0, common_1.Inject)(opraKafkaNestjsAdapterToken)),
|
|
105
|
+
tslib_1.__param(1, (0, common_1.Inject)(constants_js_1.OPRA_KAFKA_MODULE_CONFIG)),
|
|
106
|
+
tslib_1.__metadata("design:paramtypes", [opra_kafka_nestjs_adapter_js_1.OpraKafkaNestjsAdapter, Object])
|
|
107
|
+
], OpraKafkaCoreModule);
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OpraKafkaNestjsAdapter = void 0;
|
|
4
|
+
const index_1 = require("@nestjs/core/errors/exceptions/index");
|
|
5
|
+
const common_1 = require("@opra/common");
|
|
6
|
+
const kafka_1 = require("@opra/kafka");
|
|
7
|
+
/**
|
|
8
|
+
* @class
|
|
9
|
+
*/
|
|
10
|
+
class OpraKafkaNestjsAdapter {
|
|
11
|
+
constructor(moduleRef) {
|
|
12
|
+
this.moduleRef = moduleRef;
|
|
13
|
+
}
|
|
14
|
+
get document() {
|
|
15
|
+
if (!this._document)
|
|
16
|
+
throw new index_1.RuntimeException('Not initialized yet');
|
|
17
|
+
return this._document;
|
|
18
|
+
}
|
|
19
|
+
get adapter() {
|
|
20
|
+
if (!this._adapter)
|
|
21
|
+
throw new index_1.RuntimeException('Not initialized yet');
|
|
22
|
+
return this._adapter;
|
|
23
|
+
}
|
|
24
|
+
async start() {
|
|
25
|
+
if (!this._adapter) {
|
|
26
|
+
throw new index_1.RuntimeException('Not initialized yet');
|
|
27
|
+
}
|
|
28
|
+
return this._adapter.start();
|
|
29
|
+
}
|
|
30
|
+
async stop() {
|
|
31
|
+
return this._adapter?.close();
|
|
32
|
+
}
|
|
33
|
+
async initialize(init) {
|
|
34
|
+
const controllers = this._scanControllers();
|
|
35
|
+
this._document = await common_1.ApiDocumentFactory.createDocument({
|
|
36
|
+
info: init.info,
|
|
37
|
+
types: init.types,
|
|
38
|
+
references: init.references,
|
|
39
|
+
api: {
|
|
40
|
+
name: init.name,
|
|
41
|
+
description: init.description,
|
|
42
|
+
transport: 'rpc',
|
|
43
|
+
platform: 'kafka',
|
|
44
|
+
controllers,
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
this._adapter = new kafka_1.KafkaAdapter({
|
|
48
|
+
client: init.client,
|
|
49
|
+
logger: init.logger,
|
|
50
|
+
logExtra: init.logExtra,
|
|
51
|
+
consumers: init.consumers,
|
|
52
|
+
document: this._document,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
_scanControllers() {
|
|
56
|
+
const container = this.moduleRef.container;
|
|
57
|
+
const modules = container.getModules();
|
|
58
|
+
const controllerInstances = [];
|
|
59
|
+
modules.forEach(({ controllers }) => {
|
|
60
|
+
controllers.forEach(wrapper => {
|
|
61
|
+
const ctor = Object.getPrototypeOf(wrapper.instance).constructor;
|
|
62
|
+
const metadata = Reflect.getMetadata(common_1.RPC_CONTROLLER_METADATA, ctor);
|
|
63
|
+
if (metadata) {
|
|
64
|
+
const instance = {};
|
|
65
|
+
Object.setPrototypeOf(instance, wrapper.instance);
|
|
66
|
+
controllerInstances.push(instance);
|
|
67
|
+
if (metadata.operations) {
|
|
68
|
+
// for (const [k, _] of Object.keys(metadata.operations)) {
|
|
69
|
+
// const fn = instance[k];
|
|
70
|
+
// instance[k] = fn;
|
|
71
|
+
// }
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
return controllerInstances;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.OpraKafkaNestjsAdapter = OpraKafkaNestjsAdapter;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var OpraKafkaModule_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.OpraKafkaModule = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const common_1 = require("@nestjs/common");
|
|
7
|
+
const opra_kafka_core_module_js_1 = require("./opra-kafka-core.module.js");
|
|
8
|
+
let OpraKafkaModule = OpraKafkaModule_1 = class OpraKafkaModule {
|
|
9
|
+
/**
|
|
10
|
+
*
|
|
11
|
+
* @param options
|
|
12
|
+
*/
|
|
13
|
+
static forRoot(options) {
|
|
14
|
+
return {
|
|
15
|
+
module: OpraKafkaModule_1,
|
|
16
|
+
imports: [opra_kafka_core_module_js_1.OpraKafkaCoreModule.forRoot(options)],
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
*
|
|
21
|
+
* @param options
|
|
22
|
+
*/
|
|
23
|
+
static forRootAsync(options) {
|
|
24
|
+
return {
|
|
25
|
+
module: OpraKafkaModule_1,
|
|
26
|
+
imports: [opra_kafka_core_module_js_1.OpraKafkaCoreModule.forRootAsync(options)],
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
exports.OpraKafkaModule = OpraKafkaModule;
|
|
31
|
+
exports.OpraKafkaModule = OpraKafkaModule = OpraKafkaModule_1 = tslib_1.__decorate([
|
|
32
|
+
(0, common_1.Module)({})
|
|
33
|
+
], OpraKafkaModule);
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import { Controller } from '@nestjs/common';
|
|
1
2
|
import { ExternalExceptionFilter } from '@nestjs/core/exceptions/external-exception-filter.js';
|
|
3
|
+
import { classes } from '@opra/common';
|
|
4
|
+
const { RpcControllerDecoratorFactory } = classes;
|
|
2
5
|
const oldCatchMethod = ExternalExceptionFilter.prototype.catch;
|
|
3
6
|
ExternalExceptionFilter.prototype.catch = function (exception, host) {
|
|
4
7
|
const opraContext = host.getArgByIndex(3);
|
|
@@ -7,3 +10,8 @@ ExternalExceptionFilter.prototype.catch = function (exception, host) {
|
|
|
7
10
|
throw exception;
|
|
8
11
|
oldCatchMethod(exception, host);
|
|
9
12
|
};
|
|
13
|
+
RpcControllerDecoratorFactory.augment((decorator, decoratorChain) => {
|
|
14
|
+
decoratorChain.push((_, target) => {
|
|
15
|
+
Controller()(target);
|
|
16
|
+
});
|
|
17
|
+
});
|
package/esm/constants.js
CHANGED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
var OpraHttpCoreModule_1;
|
|
2
|
+
import { __decorate, __metadata } from "tslib";
|
|
3
|
+
import { Global, Logger, Module, RequestMethod, } from '@nestjs/common';
|
|
4
|
+
import { APP_FILTER, ModuleRef } from '@nestjs/core';
|
|
5
|
+
import { ApiDocumentFactory, isConstructor } from '@opra/common';
|
|
6
|
+
import { asMutable } from 'ts-gems';
|
|
7
|
+
import { OPRA_HTTP_API_CONFIG } from '../constants.js';
|
|
8
|
+
import { OpraHttpNestjsAdapter } from './opra-http-nestjs-adapter.js';
|
|
9
|
+
import { OpraExceptionFilter } from './services/opra-exception-filter.js';
|
|
10
|
+
import { OpraMiddleware } from './services/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.logger = opraNestAdapter.logger || new Logger(apiConfig.name);
|
|
60
|
+
asMutable(opraNestAdapter).document = await ApiDocumentFactory.createDocument({
|
|
61
|
+
...apiConfig,
|
|
62
|
+
api: {
|
|
63
|
+
transport: 'http',
|
|
64
|
+
name: apiConfig.name,
|
|
65
|
+
description: apiConfig.description,
|
|
66
|
+
controllers: moduleOptions.controllers,
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
if (moduleOptions.interceptors) {
|
|
70
|
+
opraNestAdapter.interceptors = moduleOptions.interceptors.map(x => {
|
|
71
|
+
if (isConstructor(x)) {
|
|
72
|
+
return async (ctx, next) => {
|
|
73
|
+
const interceptor = moduleRef.get(x);
|
|
74
|
+
if (typeof interceptor.intercept === 'function')
|
|
75
|
+
return interceptor.intercept(ctx, next);
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
return x;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
return opraNestAdapter;
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
return {
|
|
85
|
+
global: moduleOptions.global,
|
|
86
|
+
module: OpraHttpCoreModule_1,
|
|
87
|
+
controllers: opraNestAdapter.nestControllers,
|
|
88
|
+
providers: [
|
|
89
|
+
...(moduleOptions?.providers || []),
|
|
90
|
+
adapterProvider,
|
|
91
|
+
{
|
|
92
|
+
provide: opraHttpNestjsAdapterToken,
|
|
93
|
+
useExisting: token,
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
provide: APP_FILTER,
|
|
97
|
+
useClass: OpraExceptionFilter,
|
|
98
|
+
},
|
|
99
|
+
],
|
|
100
|
+
imports: [...(moduleOptions?.imports || [])],
|
|
101
|
+
exports: [...(moduleOptions?.exports || []), adapterProvider],
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
async onModuleDestroy() {
|
|
105
|
+
await this.opraAdapter.close();
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
OpraHttpCoreModule = OpraHttpCoreModule_1 = __decorate([
|
|
109
|
+
Module({}),
|
|
110
|
+
Global(),
|
|
111
|
+
__metadata("design:paramtypes", [OpraHttpNestjsAdapter])
|
|
112
|
+
], OpraHttpCoreModule);
|
|
113
|
+
export { OpraHttpCoreModule };
|