@opra/nestjs 1.9.4 → 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 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';
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);
@@ -68,7 +68,7 @@ let OpraKafkaCoreModule = OpraKafkaCoreModule_1 = class OpraKafkaCoreModule {
68
68
  name: config.name,
69
69
  description: config.description,
70
70
  transport: 'rpc',
71
- platform: 'kafka',
71
+ platform: kafka_1.KafkaAdapter.PlatformName,
72
72
  controllers,
73
73
  },
74
74
  });
@@ -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,3 +1,4 @@
1
1
  export const IS_PUBLIC_KEY = 'opra:isPublic';
2
2
  export const OPRA_HTTP_API_CONFIG = 'OPRA_HTTP_API_CONFIG';
3
3
  export const OPRA_KAFKA_MODULE_CONFIG = 'OPRA_KAFKA_MODULE_CONFIG';
4
+ export const OPRA_RMQ_MODULE_CONFIG = 'OPRA_RMQ_MODULE_CONFIG';
package/esm/index.js CHANGED
@@ -5,3 +5,4 @@ export * from './decorators/public.decorator.js';
5
5
  export * from './http/opra-http.module.js';
6
6
  export * from './http/opra-http-nestjs-adapter.js';
7
7
  export * from './kafka/opra-kafka.module.js';
8
+ export * from './rabbitmq/opra-rabbitmq.module.js';
@@ -65,7 +65,7 @@ let OpraKafkaCoreModule = OpraKafkaCoreModule_1 = class OpraKafkaCoreModule {
65
65
  name: config.name,
66
66
  description: config.description,
67
67
  transport: 'rpc',
68
- platform: 'kafka',
68
+ platform: KafkaAdapter.PlatformName,
69
69
  controllers,
70
70
  },
71
71
  });
@@ -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.9.4",
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.9.4",
10
- "@opra/core": "^1.9.4",
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.9.4",
23
- "@opra/kafka": "^1.9.4"
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": {
@@ -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";
package/types/index.d.cts CHANGED
@@ -5,3 +5,4 @@ export * from './decorators/public.decorator.js';
5
5
  export * from './http/opra-http.module.js';
6
6
  export * from './http/opra-http-nestjs-adapter.js';
7
7
  export * from './kafka/opra-kafka.module.js';
8
+ export * from './rabbitmq/opra-rabbitmq.module.js';
package/types/index.d.ts CHANGED
@@ -5,3 +5,4 @@ export * from './decorators/public.decorator.js';
5
5
  export * from './http/opra-http.module.js';
6
6
  export * from './http/opra-http-nestjs-adapter.js';
7
7
  export * from './kafka/opra-kafka.module.js';
8
+ export * from './rabbitmq/opra-rabbitmq.module.js';
@@ -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
+ }