@opra/nestjs 1.9.3 → 1.10.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/cjs/constants.js +2 -1
- package/cjs/helpers/base-opra-nest-factory.js +4 -4
- package/cjs/helpers/rpc-controller-factory.service.js +12 -12
- package/cjs/index.js +1 -0
- package/cjs/kafka/opra-kafka-core.module.js +1 -1
- package/cjs/rabbitmq/opra-rabbitmq-core.module.js +137 -0
- package/cjs/rabbitmq/opra-rabbitmq.module.js +33 -0
- package/esm/constants.js +1 -0
- package/esm/helpers/base-opra-nest-factory.js +1 -1
- package/esm/helpers/rpc-controller-factory.service.js +5 -5
- package/esm/index.js +1 -0
- package/esm/kafka/opra-kafka-core.module.js +1 -1
- package/esm/rabbitmq/opra-rabbitmq-core.module.js +134 -0
- package/esm/rabbitmq/opra-rabbitmq.module.js +30 -0
- package/package.json +7 -5
- package/types/constants.d.ts +1 -0
- package/types/helpers/rpc-controller-factory.service.d.ts +2 -2
- package/types/index.d.cts +1 -0
- package/types/index.d.ts +1 -0
- package/types/kafka/opra-kafka.module.d.ts +1 -1
- package/types/rabbitmq/opra-rabbitmq-core.module.d.ts +16 -0
- package/types/rabbitmq/opra-rabbitmq.module.d.ts +38 -0
package/cjs/constants.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.OPRA_KAFKA_MODULE_CONFIG = exports.OPRA_HTTP_API_CONFIG = exports.IS_PUBLIC_KEY = void 0;
|
|
3
|
+
exports.OPRA_RMQ_MODULE_CONFIG = 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
5
|
exports.OPRA_HTTP_API_CONFIG = 'OPRA_HTTP_API_CONFIG';
|
|
6
6
|
exports.OPRA_KAFKA_MODULE_CONFIG = 'OPRA_KAFKA_MODULE_CONFIG';
|
|
7
|
+
exports.OPRA_RMQ_MODULE_CONFIG = 'OPRA_RMQ_MODULE_CONFIG';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BaseOpraNestFactory = void 0;
|
|
4
|
-
const
|
|
4
|
+
const constants_js_1 = require("@nestjs/common/constants.js");
|
|
5
5
|
class BaseOpraNestFactory {
|
|
6
6
|
static copyDecoratorMetadata(target, ...source) {
|
|
7
7
|
for (const parent of source) {
|
|
@@ -14,9 +14,9 @@ class BaseOpraNestFactory {
|
|
|
14
14
|
Reflect.defineMetadata(key, metadata, target);
|
|
15
15
|
continue;
|
|
16
16
|
}
|
|
17
|
-
if (key ===
|
|
18
|
-
key ===
|
|
19
|
-
key ===
|
|
17
|
+
if (key === constants_js_1.GUARDS_METADATA ||
|
|
18
|
+
key === constants_js_1.INTERCEPTORS_METADATA ||
|
|
19
|
+
key === constants_js_1.EXCEPTION_FILTERS_METADATA) {
|
|
20
20
|
const m1 = Reflect.getMetadata(key, target) || [];
|
|
21
21
|
const metadata = [...m1];
|
|
22
22
|
const m2 = Reflect.getOwnMetadata(key, parent) || [];
|
|
@@ -5,11 +5,11 @@ exports.RpcControllerFactory = void 0;
|
|
|
5
5
|
const tslib_1 = require("tslib");
|
|
6
6
|
const common_1 = require("@nestjs/common");
|
|
7
7
|
const core_1 = require("@nestjs/core");
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
8
|
+
const external_context_creator_js_1 = require("@nestjs/core/helpers/external-context-creator.js");
|
|
9
|
+
const injector_js_1 = require("@nestjs/core/injector/injector.js");
|
|
10
|
+
const index_js_1 = require("@nestjs/core/injector/internal-core-module/index.js");
|
|
11
|
+
const request_constants_js_1 = require("@nestjs/core/router/request/request-constants.js");
|
|
12
|
+
const constants_js_1 = require("@nestjs/microservices/constants.js");
|
|
13
13
|
const common_2 = require("@opra/common");
|
|
14
14
|
const base_opra_nest_factory_js_1 = require("./base-opra-nest-factory.js");
|
|
15
15
|
const rpc_params_factory_js_1 = require("./rpc-params.factory.js");
|
|
@@ -19,7 +19,7 @@ let RpcControllerFactory = RpcControllerFactory_1 = class RpcControllerFactory e
|
|
|
19
19
|
this.modulesContainer = modulesContainer;
|
|
20
20
|
this.externalContextCreator = externalContextCreator;
|
|
21
21
|
this.paramsFactory = new rpc_params_factory_js_1.RpcParamsFactory();
|
|
22
|
-
this.injector = new
|
|
22
|
+
this.injector = new injector_js_1.Injector();
|
|
23
23
|
}
|
|
24
24
|
wrapControllers() {
|
|
25
25
|
const out = [];
|
|
@@ -41,7 +41,7 @@ let RpcControllerFactory = RpcControllerFactory_1 = class RpcControllerFactory e
|
|
|
41
41
|
for (const operationName of Object.keys(metadata.operations)) {
|
|
42
42
|
// const orgFn: Function = sourceClass.prototype[operationName];
|
|
43
43
|
newClass.prototype[operationName] = this._createContextCallback(instance, wrapper, module, operationName, isRequestScoped, 'rpc');
|
|
44
|
-
Reflect.defineMetadata(
|
|
44
|
+
Reflect.defineMetadata(constants_js_1.PARAM_ARGS_METADATA, [core_1.REQUEST], instance.constructor, operationName);
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
}
|
|
@@ -52,7 +52,7 @@ let RpcControllerFactory = RpcControllerFactory_1 = class RpcControllerFactory e
|
|
|
52
52
|
if (isRequestScoped) {
|
|
53
53
|
return async (opraContext) => {
|
|
54
54
|
const contextId = (0, core_1.createContextId)();
|
|
55
|
-
Object.defineProperty(opraContext,
|
|
55
|
+
Object.defineProperty(opraContext, request_constants_js_1.REQUEST_CONTEXT_ID, {
|
|
56
56
|
value: contextId,
|
|
57
57
|
enumerable: false,
|
|
58
58
|
configurable: false,
|
|
@@ -60,18 +60,18 @@ let RpcControllerFactory = RpcControllerFactory_1 = class RpcControllerFactory e
|
|
|
60
60
|
});
|
|
61
61
|
this.registerContextProvider(opraContext, contextId);
|
|
62
62
|
const contextInstance = await this.injector.loadPerContext(instance, moduleRef, moduleRef.providers, contextId);
|
|
63
|
-
const contextCallback = this.externalContextCreator.create(contextInstance, contextInstance[methodName], methodName,
|
|
63
|
+
const contextCallback = this.externalContextCreator.create(contextInstance, contextInstance[methodName], methodName, constants_js_1.PARAM_ARGS_METADATA, paramsFactory, contextId, wrapper.id, options, opraContext.protocol);
|
|
64
64
|
return contextCallback(opraContext);
|
|
65
65
|
};
|
|
66
66
|
}
|
|
67
|
-
return this.externalContextCreator.create(instance, instance[methodName], methodName,
|
|
67
|
+
return this.externalContextCreator.create(instance, instance[methodName], methodName, constants_js_1.PARAM_ARGS_METADATA, paramsFactory, undefined, undefined, options, contextType);
|
|
68
68
|
}
|
|
69
69
|
registerContextProvider(request, contextId) {
|
|
70
70
|
if (!this._coreModuleRef) {
|
|
71
71
|
const coreModuleArray = [...this.modulesContainer.entries()]
|
|
72
72
|
.filter(
|
|
73
73
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
74
|
-
([_, { metatype }]) => metatype && metatype.name ===
|
|
74
|
+
([_, { metatype }]) => metatype && metatype.name === index_js_1.InternalCoreModule.name)
|
|
75
75
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
76
76
|
.map(([_, value]) => value);
|
|
77
77
|
this._coreModuleRef = coreModuleArray[0];
|
|
@@ -119,5 +119,5 @@ exports.RpcControllerFactory = RpcControllerFactory = RpcControllerFactory_1 = t
|
|
|
119
119
|
tslib_1.__param(0, (0, common_1.Inject)()),
|
|
120
120
|
tslib_1.__param(1, (0, common_1.Inject)()),
|
|
121
121
|
tslib_1.__metadata("design:paramtypes", [core_1.ModulesContainer,
|
|
122
|
-
|
|
122
|
+
external_context_creator_js_1.ExternalContextCreator])
|
|
123
123
|
], RpcControllerFactory);
|
package/cjs/index.js
CHANGED
|
@@ -8,3 +8,4 @@ tslib_1.__exportStar(require("./decorators/public.decorator.js"), exports);
|
|
|
8
8
|
tslib_1.__exportStar(require("./http/opra-http.module.js"), exports);
|
|
9
9
|
tslib_1.__exportStar(require("./http/opra-http-nestjs-adapter.js"), exports);
|
|
10
10
|
tslib_1.__exportStar(require("./kafka/opra-kafka.module.js"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./rabbitmq/opra-rabbitmq.module.js"), exports);
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var OpraRabbitmqCoreModule_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.OpraRabbitmqCoreModule = 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 rabbitmq_1 = require("@opra/rabbitmq");
|
|
11
|
+
const constants_js_1 = require("../constants.js");
|
|
12
|
+
const rpc_controller_factory_service_js_1 = require("../helpers/rpc-controller-factory.service.js");
|
|
13
|
+
const opraRabbitmqNestjsAdapterToken = Symbol('OpraRabbitmqNestjsAdapter');
|
|
14
|
+
let OpraRabbitmqCoreModule = OpraRabbitmqCoreModule_1 = class OpraRabbitmqCoreModule {
|
|
15
|
+
constructor(controllerFactory, adapter, config) {
|
|
16
|
+
this.controllerFactory = controllerFactory;
|
|
17
|
+
this.adapter = adapter;
|
|
18
|
+
this.config = config;
|
|
19
|
+
}
|
|
20
|
+
static forRoot(moduleOptions) {
|
|
21
|
+
return this._getDynamicModule({
|
|
22
|
+
...moduleOptions,
|
|
23
|
+
providers: [
|
|
24
|
+
...(moduleOptions?.providers || []),
|
|
25
|
+
{
|
|
26
|
+
provide: constants_js_1.OPRA_RMQ_MODULE_CONFIG,
|
|
27
|
+
useValue: {
|
|
28
|
+
...moduleOptions,
|
|
29
|
+
logger: moduleOptions.logger || new common_1.Logger(moduleOptions.name),
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
static forRootAsync(moduleOptions) {
|
|
36
|
+
if (!moduleOptions.useFactory)
|
|
37
|
+
throw new Error('Invalid configuration. Must provide "useFactory"');
|
|
38
|
+
return this._getDynamicModule({
|
|
39
|
+
...moduleOptions,
|
|
40
|
+
providers: [
|
|
41
|
+
...(moduleOptions?.providers || []),
|
|
42
|
+
{
|
|
43
|
+
provide: constants_js_1.OPRA_RMQ_MODULE_CONFIG,
|
|
44
|
+
inject: moduleOptions.inject,
|
|
45
|
+
useFactory: async (...args) => {
|
|
46
|
+
const result = await moduleOptions.useFactory(...args);
|
|
47
|
+
result.logger = result.logger || new common_1.Logger(result.name);
|
|
48
|
+
return result;
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
static _getDynamicModule(moduleOptions) {
|
|
55
|
+
const token = moduleOptions.id || rabbitmq_1.RabbitmqAdapter;
|
|
56
|
+
const adapterProvider = {
|
|
57
|
+
provide: token,
|
|
58
|
+
inject: [rpc_controller_factory_service_js_1.RpcControllerFactory, core_1.ModuleRef, constants_js_1.OPRA_RMQ_MODULE_CONFIG],
|
|
59
|
+
useFactory: async (controllerFactory, moduleRef, config) => {
|
|
60
|
+
const controllers = controllerFactory
|
|
61
|
+
.exploreControllers()
|
|
62
|
+
.map(x => x.wrapper.instance.constructor);
|
|
63
|
+
const document = await common_2.ApiDocumentFactory.createDocument({
|
|
64
|
+
info: config.info,
|
|
65
|
+
types: config.types,
|
|
66
|
+
references: config.references,
|
|
67
|
+
api: {
|
|
68
|
+
name: config.name,
|
|
69
|
+
description: config.description,
|
|
70
|
+
transport: 'rpc',
|
|
71
|
+
platform: rabbitmq_1.RabbitmqAdapter.PlatformName,
|
|
72
|
+
controllers,
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
const interceptors = moduleOptions.interceptors
|
|
76
|
+
? 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
|
+
: undefined;
|
|
87
|
+
return new rabbitmq_1.RabbitmqAdapter(document, { ...config, interceptors });
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
return {
|
|
91
|
+
global: moduleOptions.global,
|
|
92
|
+
module: OpraRabbitmqCoreModule_1,
|
|
93
|
+
controllers: moduleOptions.controllers,
|
|
94
|
+
providers: [
|
|
95
|
+
...(moduleOptions?.providers || []),
|
|
96
|
+
rpc_controller_factory_service_js_1.RpcControllerFactory,
|
|
97
|
+
adapterProvider,
|
|
98
|
+
{
|
|
99
|
+
provide: opraRabbitmqNestjsAdapterToken,
|
|
100
|
+
useExisting: token,
|
|
101
|
+
},
|
|
102
|
+
],
|
|
103
|
+
imports: [...(moduleOptions?.imports || [])],
|
|
104
|
+
exports: [...(moduleOptions?.exports || []), adapterProvider],
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
onModuleInit() {
|
|
108
|
+
/** NestJS initialize controller instances on init stage.
|
|
109
|
+
* So we should update instance properties */
|
|
110
|
+
const rpcApi = this.adapter.document.rpcApi;
|
|
111
|
+
const controllers = Array.from(rpcApi.controllers.values());
|
|
112
|
+
for (const { wrapper } of this.controllerFactory
|
|
113
|
+
.exploreControllers()
|
|
114
|
+
.values()) {
|
|
115
|
+
const ctor = wrapper.instance.constructor;
|
|
116
|
+
const controller = controllers.find(x => x.ctor === ctor);
|
|
117
|
+
if (controller) {
|
|
118
|
+
controller.instance = wrapper.instance;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
async onApplicationBootstrap() {
|
|
123
|
+
await this.adapter.start();
|
|
124
|
+
}
|
|
125
|
+
async onApplicationShutdown() {
|
|
126
|
+
await this.adapter.close();
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
exports.OpraRabbitmqCoreModule = OpraRabbitmqCoreModule;
|
|
130
|
+
exports.OpraRabbitmqCoreModule = OpraRabbitmqCoreModule = OpraRabbitmqCoreModule_1 = tslib_1.__decorate([
|
|
131
|
+
(0, common_1.Module)({}),
|
|
132
|
+
(0, common_1.Global)(),
|
|
133
|
+
tslib_1.__param(1, (0, common_1.Inject)(opraRabbitmqNestjsAdapterToken)),
|
|
134
|
+
tslib_1.__param(2, (0, common_1.Inject)(constants_js_1.OPRA_RMQ_MODULE_CONFIG)),
|
|
135
|
+
tslib_1.__metadata("design:paramtypes", [rpc_controller_factory_service_js_1.RpcControllerFactory,
|
|
136
|
+
rabbitmq_1.RabbitmqAdapter, Object])
|
|
137
|
+
], OpraRabbitmqCoreModule);
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var OpraRabbitmqModule_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.OpraRabbitmqModule = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const common_1 = require("@nestjs/common");
|
|
7
|
+
const opra_rabbitmq_core_module_js_1 = require("./opra-rabbitmq-core.module.js");
|
|
8
|
+
let OpraRabbitmqModule = OpraRabbitmqModule_1 = class OpraRabbitmqModule {
|
|
9
|
+
/**
|
|
10
|
+
*
|
|
11
|
+
* @param options
|
|
12
|
+
*/
|
|
13
|
+
static forRoot(options) {
|
|
14
|
+
return {
|
|
15
|
+
module: OpraRabbitmqModule_1,
|
|
16
|
+
imports: [opra_rabbitmq_core_module_js_1.OpraRabbitmqCoreModule.forRoot(options)],
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
*
|
|
21
|
+
* @param options
|
|
22
|
+
*/
|
|
23
|
+
static forRootAsync(options) {
|
|
24
|
+
return {
|
|
25
|
+
module: OpraRabbitmqModule_1,
|
|
26
|
+
imports: [opra_rabbitmq_core_module_js_1.OpraRabbitmqCoreModule.forRootAsync(options)],
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
exports.OpraRabbitmqModule = OpraRabbitmqModule;
|
|
31
|
+
exports.OpraRabbitmqModule = OpraRabbitmqModule = OpraRabbitmqModule_1 = tslib_1.__decorate([
|
|
32
|
+
(0, common_1.Module)({})
|
|
33
|
+
], OpraRabbitmqModule);
|
package/esm/constants.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EXCEPTION_FILTERS_METADATA, GUARDS_METADATA, INTERCEPTORS_METADATA, } from '@nestjs/common/constants';
|
|
1
|
+
import { EXCEPTION_FILTERS_METADATA, GUARDS_METADATA, INTERCEPTORS_METADATA, } from '@nestjs/common/constants.js';
|
|
2
2
|
export class BaseOpraNestFactory {
|
|
3
3
|
static copyDecoratorMetadata(target, ...source) {
|
|
4
4
|
for (const parent of source) {
|
|
@@ -2,11 +2,11 @@ var RpcControllerFactory_1;
|
|
|
2
2
|
import { __decorate, __metadata, __param } from "tslib";
|
|
3
3
|
import { Controller, Inject, Injectable, } from '@nestjs/common';
|
|
4
4
|
import { createContextId, ModulesContainer, REQUEST } from '@nestjs/core';
|
|
5
|
-
import { ExternalContextCreator, } from '@nestjs/core/helpers/external-context-creator';
|
|
6
|
-
import { Injector } from '@nestjs/core/injector/injector';
|
|
7
|
-
import { InternalCoreModule } from '@nestjs/core/injector/internal-core-module';
|
|
8
|
-
import { REQUEST_CONTEXT_ID } from '@nestjs/core/router/request/request-constants';
|
|
9
|
-
import { PARAM_ARGS_METADATA } from '@nestjs/microservices/constants';
|
|
5
|
+
import { ExternalContextCreator, } from '@nestjs/core/helpers/external-context-creator.js';
|
|
6
|
+
import { Injector } from '@nestjs/core/injector/injector.js';
|
|
7
|
+
import { InternalCoreModule } from '@nestjs/core/injector/internal-core-module/index.js';
|
|
8
|
+
import { REQUEST_CONTEXT_ID } from '@nestjs/core/router/request/request-constants.js';
|
|
9
|
+
import { PARAM_ARGS_METADATA } from '@nestjs/microservices/constants.js';
|
|
10
10
|
import { RPC_CONTROLLER_METADATA } from '@opra/common';
|
|
11
11
|
import { BaseOpraNestFactory } from './base-opra-nest-factory.js';
|
|
12
12
|
import { RpcParamsFactory } from './rpc-params.factory.js';
|
package/esm/index.js
CHANGED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
var OpraRabbitmqCoreModule_1;
|
|
2
|
+
import { __decorate, __metadata, __param } from "tslib";
|
|
3
|
+
import { isConstructor } from '@jsopen/objects';
|
|
4
|
+
import { Global, Inject, Logger, Module, } from '@nestjs/common';
|
|
5
|
+
import { ModuleRef } from '@nestjs/core';
|
|
6
|
+
import { ApiDocumentFactory } from '@opra/common';
|
|
7
|
+
import { RabbitmqAdapter } from '@opra/rabbitmq';
|
|
8
|
+
import { OPRA_RMQ_MODULE_CONFIG } from '../constants.js';
|
|
9
|
+
import { RpcControllerFactory } from '../helpers/rpc-controller-factory.service.js';
|
|
10
|
+
const opraRabbitmqNestjsAdapterToken = Symbol('OpraRabbitmqNestjsAdapter');
|
|
11
|
+
let OpraRabbitmqCoreModule = OpraRabbitmqCoreModule_1 = class OpraRabbitmqCoreModule {
|
|
12
|
+
constructor(controllerFactory, adapter, config) {
|
|
13
|
+
this.controllerFactory = controllerFactory;
|
|
14
|
+
this.adapter = adapter;
|
|
15
|
+
this.config = config;
|
|
16
|
+
}
|
|
17
|
+
static forRoot(moduleOptions) {
|
|
18
|
+
return this._getDynamicModule({
|
|
19
|
+
...moduleOptions,
|
|
20
|
+
providers: [
|
|
21
|
+
...(moduleOptions?.providers || []),
|
|
22
|
+
{
|
|
23
|
+
provide: OPRA_RMQ_MODULE_CONFIG,
|
|
24
|
+
useValue: {
|
|
25
|
+
...moduleOptions,
|
|
26
|
+
logger: moduleOptions.logger || new 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: OPRA_RMQ_MODULE_CONFIG,
|
|
41
|
+
inject: moduleOptions.inject,
|
|
42
|
+
useFactory: async (...args) => {
|
|
43
|
+
const result = await moduleOptions.useFactory(...args);
|
|
44
|
+
result.logger = result.logger || new Logger(result.name);
|
|
45
|
+
return result;
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
static _getDynamicModule(moduleOptions) {
|
|
52
|
+
const token = moduleOptions.id || RabbitmqAdapter;
|
|
53
|
+
const adapterProvider = {
|
|
54
|
+
provide: token,
|
|
55
|
+
inject: [RpcControllerFactory, ModuleRef, OPRA_RMQ_MODULE_CONFIG],
|
|
56
|
+
useFactory: async (controllerFactory, moduleRef, config) => {
|
|
57
|
+
const controllers = controllerFactory
|
|
58
|
+
.exploreControllers()
|
|
59
|
+
.map(x => x.wrapper.instance.constructor);
|
|
60
|
+
const document = await ApiDocumentFactory.createDocument({
|
|
61
|
+
info: config.info,
|
|
62
|
+
types: config.types,
|
|
63
|
+
references: config.references,
|
|
64
|
+
api: {
|
|
65
|
+
name: config.name,
|
|
66
|
+
description: config.description,
|
|
67
|
+
transport: 'rpc',
|
|
68
|
+
platform: RabbitmqAdapter.PlatformName,
|
|
69
|
+
controllers,
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
const interceptors = moduleOptions.interceptors
|
|
73
|
+
? 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
|
+
: undefined;
|
|
84
|
+
return new RabbitmqAdapter(document, { ...config, interceptors });
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
return {
|
|
88
|
+
global: moduleOptions.global,
|
|
89
|
+
module: OpraRabbitmqCoreModule_1,
|
|
90
|
+
controllers: moduleOptions.controllers,
|
|
91
|
+
providers: [
|
|
92
|
+
...(moduleOptions?.providers || []),
|
|
93
|
+
RpcControllerFactory,
|
|
94
|
+
adapterProvider,
|
|
95
|
+
{
|
|
96
|
+
provide: opraRabbitmqNestjsAdapterToken,
|
|
97
|
+
useExisting: token,
|
|
98
|
+
},
|
|
99
|
+
],
|
|
100
|
+
imports: [...(moduleOptions?.imports || [])],
|
|
101
|
+
exports: [...(moduleOptions?.exports || []), adapterProvider],
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
onModuleInit() {
|
|
105
|
+
/** NestJS initialize controller instances on init stage.
|
|
106
|
+
* So we should update instance properties */
|
|
107
|
+
const rpcApi = this.adapter.document.rpcApi;
|
|
108
|
+
const controllers = Array.from(rpcApi.controllers.values());
|
|
109
|
+
for (const { wrapper } of this.controllerFactory
|
|
110
|
+
.exploreControllers()
|
|
111
|
+
.values()) {
|
|
112
|
+
const ctor = wrapper.instance.constructor;
|
|
113
|
+
const controller = controllers.find(x => x.ctor === ctor);
|
|
114
|
+
if (controller) {
|
|
115
|
+
controller.instance = wrapper.instance;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
async onApplicationBootstrap() {
|
|
120
|
+
await this.adapter.start();
|
|
121
|
+
}
|
|
122
|
+
async onApplicationShutdown() {
|
|
123
|
+
await this.adapter.close();
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
OpraRabbitmqCoreModule = OpraRabbitmqCoreModule_1 = __decorate([
|
|
127
|
+
Module({}),
|
|
128
|
+
Global(),
|
|
129
|
+
__param(1, Inject(opraRabbitmqNestjsAdapterToken)),
|
|
130
|
+
__param(2, Inject(OPRA_RMQ_MODULE_CONFIG)),
|
|
131
|
+
__metadata("design:paramtypes", [RpcControllerFactory,
|
|
132
|
+
RabbitmqAdapter, Object])
|
|
133
|
+
], OpraRabbitmqCoreModule);
|
|
134
|
+
export { OpraRabbitmqCoreModule };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
var OpraRabbitmqModule_1;
|
|
2
|
+
import { __decorate } from "tslib";
|
|
3
|
+
import { Module } from '@nestjs/common';
|
|
4
|
+
import { OpraRabbitmqCoreModule } from './opra-rabbitmq-core.module.js';
|
|
5
|
+
let OpraRabbitmqModule = OpraRabbitmqModule_1 = class OpraRabbitmqModule {
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* @param options
|
|
9
|
+
*/
|
|
10
|
+
static forRoot(options) {
|
|
11
|
+
return {
|
|
12
|
+
module: OpraRabbitmqModule_1,
|
|
13
|
+
imports: [OpraRabbitmqCoreModule.forRoot(options)],
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
*
|
|
18
|
+
* @param options
|
|
19
|
+
*/
|
|
20
|
+
static forRootAsync(options) {
|
|
21
|
+
return {
|
|
22
|
+
module: OpraRabbitmqModule_1,
|
|
23
|
+
imports: [OpraRabbitmqCoreModule.forRootAsync(options)],
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
OpraRabbitmqModule = OpraRabbitmqModule_1 = __decorate([
|
|
28
|
+
Module({})
|
|
29
|
+
], OpraRabbitmqModule);
|
|
30
|
+
export { OpraRabbitmqModule };
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/nestjs",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.0",
|
|
4
4
|
"description": "Opra NestJS module",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"@jsopen/objects": "^1.5.0",
|
|
9
|
-
"@opra/common": "^1.
|
|
10
|
-
"@opra/core": "^1.
|
|
9
|
+
"@opra/common": "^1.10.0",
|
|
10
|
+
"@opra/core": "^1.10.0",
|
|
11
11
|
"fast-tokenizer": "^1.7.0",
|
|
12
12
|
"putil-promisify": "^1.10.1",
|
|
13
13
|
"reflect-metadata": "^0.2.2",
|
|
@@ -19,8 +19,10 @@
|
|
|
19
19
|
},
|
|
20
20
|
"optionalDependencies": {
|
|
21
21
|
"@nestjs/microservices": "^10.0.0 || ^11.0.0",
|
|
22
|
-
"@opra/http": "^1.
|
|
23
|
-
"@opra/kafka": "^1.
|
|
22
|
+
"@opra/http": "^1.10.0",
|
|
23
|
+
"@opra/kafka": "^1.10.0",
|
|
24
|
+
"amqplib": ">=0.10.0 <1.0.0",
|
|
25
|
+
"kafkajs": "^2.2.4"
|
|
24
26
|
},
|
|
25
27
|
"type": "module",
|
|
26
28
|
"exports": {
|
package/types/constants.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export declare const IS_PUBLIC_KEY = "opra:isPublic";
|
|
2
2
|
export declare const OPRA_HTTP_API_CONFIG = "OPRA_HTTP_API_CONFIG";
|
|
3
3
|
export declare const OPRA_KAFKA_MODULE_CONFIG = "OPRA_KAFKA_MODULE_CONFIG";
|
|
4
|
+
export declare const OPRA_RMQ_MODULE_CONFIG = "OPRA_RMQ_MODULE_CONFIG";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type Type } from '@nestjs/common';
|
|
2
2
|
import { ModulesContainer } from '@nestjs/core';
|
|
3
|
-
import { ExternalContextCreator } from '@nestjs/core/helpers/external-context-creator';
|
|
4
|
-
import type { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
|
|
3
|
+
import { ExternalContextCreator } from '@nestjs/core/helpers/external-context-creator.js';
|
|
4
|
+
import type { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper.js';
|
|
5
5
|
import type { Module } from '@nestjs/core/injector/module.js';
|
|
6
6
|
import { BaseOpraNestFactory } from './base-opra-nest-factory.js';
|
|
7
7
|
export declare class RpcControllerFactory extends BaseOpraNestFactory {
|
package/types/index.d.cts
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type DynamicModule, Logger, type Type } from '@nestjs/common';
|
|
2
2
|
import { ApiDocumentFactory } from '@opra/common';
|
|
3
|
-
import { KafkaAdapter } from '@opra/kafka';
|
|
3
|
+
import type { KafkaAdapter } from '@opra/kafka';
|
|
4
4
|
export declare namespace OpraKafkaModule {
|
|
5
5
|
export interface ModuleOptions extends BaseModuleOptions, ApiConfig {
|
|
6
6
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type DynamicModule, OnApplicationBootstrap, OnApplicationShutdown, OnModuleInit } from '@nestjs/common';
|
|
2
|
+
import { RabbitmqAdapter } from '@opra/rabbitmq';
|
|
3
|
+
import { RpcControllerFactory } from '../helpers/rpc-controller-factory.service.js';
|
|
4
|
+
import type { OpraRabbitmqModule } from './opra-rabbitmq.module.js';
|
|
5
|
+
export declare class OpraRabbitmqCoreModule implements OnModuleInit, OnApplicationBootstrap, OnApplicationShutdown {
|
|
6
|
+
private controllerFactory;
|
|
7
|
+
protected adapter: RabbitmqAdapter;
|
|
8
|
+
protected config: OpraRabbitmqModule.ApiConfig;
|
|
9
|
+
constructor(controllerFactory: RpcControllerFactory, adapter: RabbitmqAdapter, config: OpraRabbitmqModule.ApiConfig);
|
|
10
|
+
static forRoot(moduleOptions: OpraRabbitmqModule.ModuleOptions): DynamicModule;
|
|
11
|
+
static forRootAsync(moduleOptions: OpraRabbitmqModule.AsyncModuleOptions): DynamicModule;
|
|
12
|
+
protected static _getDynamicModule(moduleOptions: OpraRabbitmqModule.ModuleOptions | OpraRabbitmqModule.AsyncModuleOptions): DynamicModule;
|
|
13
|
+
onModuleInit(): any;
|
|
14
|
+
onApplicationBootstrap(): Promise<void>;
|
|
15
|
+
onApplicationShutdown(): Promise<void>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type DynamicModule, Logger, type Type } from '@nestjs/common';
|
|
2
|
+
import { ApiDocumentFactory } from '@opra/common';
|
|
3
|
+
import type { RabbitmqAdapter } from '@opra/rabbitmq';
|
|
4
|
+
export declare namespace OpraRabbitmqModule {
|
|
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
|
+
id?: any;
|
|
13
|
+
interceptors?: (RabbitmqAdapter.InterceptorFunction | RabbitmqAdapter.IRabbitmqInterceptor | Type<RabbitmqAdapter.IRabbitmqInterceptor>)[];
|
|
14
|
+
}
|
|
15
|
+
export interface ApiConfig extends Pick<ApiDocumentFactory.InitArguments, 'types' | 'references' | 'info'> {
|
|
16
|
+
connection: RabbitmqAdapter.Config['connection'];
|
|
17
|
+
queues?: RabbitmqAdapter.Config['queues'];
|
|
18
|
+
logExtra?: RabbitmqAdapter.Config['logExtra'];
|
|
19
|
+
defaults?: RabbitmqAdapter.Config['defaults'];
|
|
20
|
+
name: string;
|
|
21
|
+
description?: string;
|
|
22
|
+
scope?: string;
|
|
23
|
+
logger?: Logger;
|
|
24
|
+
}
|
|
25
|
+
export {};
|
|
26
|
+
}
|
|
27
|
+
export declare class OpraRabbitmqModule {
|
|
28
|
+
/**
|
|
29
|
+
*
|
|
30
|
+
* @param options
|
|
31
|
+
*/
|
|
32
|
+
static forRoot(options: OpraRabbitmqModule.ModuleOptions): DynamicModule;
|
|
33
|
+
/**
|
|
34
|
+
*
|
|
35
|
+
* @param options
|
|
36
|
+
*/
|
|
37
|
+
static forRootAsync(options: OpraRabbitmqModule.AsyncModuleOptions): DynamicModule;
|
|
38
|
+
}
|