@opra/nestjs-kafka 1.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Panates
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # OPRA
2
+
3
+ Open Protocol for Restful Api
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OPRA_KAFKA_MODULE_CONFIG = void 0;
4
+ exports.OPRA_KAFKA_MODULE_CONFIG = 'OPRA_KAFKA_MODULE_CONFIG';
package/cjs/index.js ADDED
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Public = exports.OpraNestUtils = exports.IS_PUBLIC_KEY = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const nestjs_1 = require("@opra/nestjs");
6
+ Object.defineProperty(exports, "IS_PUBLIC_KEY", { enumerable: true, get: function () { return nestjs_1.IS_PUBLIC_KEY; } });
7
+ Object.defineProperty(exports, "OpraNestUtils", { enumerable: true, get: function () { return nestjs_1.OpraNestUtils; } });
8
+ Object.defineProperty(exports, "Public", { enumerable: true, get: function () { return nestjs_1.Public; } });
9
+ tslib_1.__exportStar(require("./constants.js"), exports);
10
+ tslib_1.__exportStar(require("./opra-kafka.module.js"), exports);
@@ -0,0 +1,137 @@
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 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 kafka_1 = require("@opra/kafka");
11
+ const nestjs_1 = require("@opra/nestjs");
12
+ const constants_js_1 = require("./constants.js");
13
+ const opraKafkaNestjsAdapterToken = Symbol('OpraKafkaNestjsAdapter');
14
+ let OpraKafkaCoreModule = OpraKafkaCoreModule_1 = class OpraKafkaCoreModule {
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_KAFKA_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_KAFKA_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 || kafka_1.KafkaAdapter;
56
+ const adapterProvider = {
57
+ provide: token,
58
+ inject: [nestjs_1.RpcControllerFactory, core_1.ModuleRef, constants_js_1.OPRA_KAFKA_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: kafka_1.KafkaAdapter.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 kafka_1.KafkaAdapter(document, { ...config, interceptors });
88
+ },
89
+ };
90
+ return {
91
+ global: moduleOptions.global,
92
+ module: OpraKafkaCoreModule_1,
93
+ controllers: moduleOptions.controllers,
94
+ providers: [
95
+ ...(moduleOptions?.providers || []),
96
+ nestjs_1.RpcControllerFactory,
97
+ adapterProvider,
98
+ {
99
+ provide: opraKafkaNestjsAdapterToken,
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.OpraKafkaCoreModule = OpraKafkaCoreModule;
130
+ exports.OpraKafkaCoreModule = OpraKafkaCoreModule = OpraKafkaCoreModule_1 = tslib_1.__decorate([
131
+ (0, common_1.Module)({}),
132
+ (0, common_1.Global)(),
133
+ tslib_1.__param(1, (0, common_1.Inject)(opraKafkaNestjsAdapterToken)),
134
+ tslib_1.__param(2, (0, common_1.Inject)(constants_js_1.OPRA_KAFKA_MODULE_CONFIG)),
135
+ tslib_1.__metadata("design:paramtypes", [nestjs_1.RpcControllerFactory,
136
+ kafka_1.KafkaAdapter, Object])
137
+ ], OpraKafkaCoreModule);
@@ -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);
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1 @@
1
+ export const OPRA_KAFKA_MODULE_CONFIG = 'OPRA_KAFKA_MODULE_CONFIG';
package/esm/index.js ADDED
@@ -0,0 +1,4 @@
1
+ import { IS_PUBLIC_KEY, OpraNestUtils, Public } from '@opra/nestjs';
2
+ export * from './constants.js';
3
+ export * from './opra-kafka.module.js';
4
+ export { IS_PUBLIC_KEY, OpraNestUtils, Public };
@@ -0,0 +1,134 @@
1
+ var OpraKafkaCoreModule_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 { KafkaAdapter } from '@opra/kafka';
8
+ import { RpcControllerFactory } from '@opra/nestjs';
9
+ import { OPRA_KAFKA_MODULE_CONFIG } from './constants.js';
10
+ const opraKafkaNestjsAdapterToken = Symbol('OpraKafkaNestjsAdapter');
11
+ let OpraKafkaCoreModule = OpraKafkaCoreModule_1 = class OpraKafkaCoreModule {
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_KAFKA_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_KAFKA_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 || KafkaAdapter;
53
+ const adapterProvider = {
54
+ provide: token,
55
+ inject: [RpcControllerFactory, ModuleRef, OPRA_KAFKA_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: KafkaAdapter.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 KafkaAdapter(document, { ...config, interceptors });
85
+ },
86
+ };
87
+ return {
88
+ global: moduleOptions.global,
89
+ module: OpraKafkaCoreModule_1,
90
+ controllers: moduleOptions.controllers,
91
+ providers: [
92
+ ...(moduleOptions?.providers || []),
93
+ RpcControllerFactory,
94
+ adapterProvider,
95
+ {
96
+ provide: opraKafkaNestjsAdapterToken,
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
+ OpraKafkaCoreModule = OpraKafkaCoreModule_1 = __decorate([
127
+ Module({}),
128
+ Global(),
129
+ __param(1, Inject(opraKafkaNestjsAdapterToken)),
130
+ __param(2, Inject(OPRA_KAFKA_MODULE_CONFIG)),
131
+ __metadata("design:paramtypes", [RpcControllerFactory,
132
+ KafkaAdapter, Object])
133
+ ], OpraKafkaCoreModule);
134
+ export { OpraKafkaCoreModule };
@@ -0,0 +1,30 @@
1
+ var OpraKafkaModule_1;
2
+ import { __decorate } from "tslib";
3
+ import { Module } from '@nestjs/common';
4
+ import { OpraKafkaCoreModule } from './opra-kafka-core.module.js';
5
+ let OpraKafkaModule = OpraKafkaModule_1 = class OpraKafkaModule {
6
+ /**
7
+ *
8
+ * @param options
9
+ */
10
+ static forRoot(options) {
11
+ return {
12
+ module: OpraKafkaModule_1,
13
+ imports: [OpraKafkaCoreModule.forRoot(options)],
14
+ };
15
+ }
16
+ /**
17
+ *
18
+ * @param options
19
+ */
20
+ static forRootAsync(options) {
21
+ return {
22
+ module: OpraKafkaModule_1,
23
+ imports: [OpraKafkaCoreModule.forRootAsync(options)],
24
+ };
25
+ }
26
+ };
27
+ OpraKafkaModule = OpraKafkaModule_1 = __decorate([
28
+ Module({})
29
+ ], OpraKafkaModule);
30
+ export { OpraKafkaModule };
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
package/package.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "@opra/nestjs-kafka",
3
+ "version": "1.11.0",
4
+ "description": "Opra NestJS Kafka Module",
5
+ "author": "Panates",
6
+ "license": "MIT",
7
+ "dependencies": {
8
+ "@jsopen/objects": "^1.5.0",
9
+ "fast-tokenizer": "^1.7.0",
10
+ "putil-promisify": "^1.10.1",
11
+ "reflect-metadata": "^0.2.2",
12
+ "tslib": "^2.8.1"
13
+ },
14
+ "peerDependencies": {
15
+ "@opra/common": "^1.11.0",
16
+ "@opra/core": "^1.11.0",
17
+ "@opra/nestjs": "^1.11.0",
18
+ "@opra/kafka": "^1.11.0",
19
+ "@nestjs/common": "^10.0.0 || ^11.0.0",
20
+ "@nestjs/core": "^10.0.0 || ^11.0.0",
21
+ "@nestjs/microservices": "^10.0.0 || ^11.0.0",
22
+ "kafkajs": "^2.2.4"
23
+ },
24
+ "type": "module",
25
+ "exports": {
26
+ ".": {
27
+ "import": {
28
+ "types": "./types/index.d.ts",
29
+ "default": "./esm/index.js"
30
+ },
31
+ "require": {
32
+ "types": "./types/index.d.cts",
33
+ "default": "./cjs/index.js"
34
+ },
35
+ "default": "./esm/index.js"
36
+ },
37
+ "./package.json": "./package.json"
38
+ },
39
+ "main": "./cjs/index.js",
40
+ "module": "./esm/index.js",
41
+ "types": "./types/index.d.ts",
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "https://github.com/panates/opra.git",
45
+ "directory": "packages/nestjs-kafka"
46
+ },
47
+ "engines": {
48
+ "node": ">=16.0",
49
+ "npm": ">=7.0.0"
50
+ },
51
+ "files": [
52
+ "bin/",
53
+ "cjs/",
54
+ "esm/",
55
+ "types/",
56
+ "LICENSE",
57
+ "README.md"
58
+ ],
59
+ "keywords": [
60
+ "opra",
61
+ "nestjs",
62
+ "http"
63
+ ],
64
+ "publishConfig": {
65
+ "access": "public"
66
+ }
67
+ }
@@ -0,0 +1 @@
1
+ export declare const OPRA_KAFKA_MODULE_CONFIG = "OPRA_KAFKA_MODULE_CONFIG";
@@ -0,0 +1,4 @@
1
+ import { IS_PUBLIC_KEY, OpraNestUtils, Public } from '@opra/nestjs';
2
+ export * from './constants.js';
3
+ export * from './opra-kafka.module.js';
4
+ export { IS_PUBLIC_KEY, OpraNestUtils, Public };
@@ -0,0 +1,4 @@
1
+ import { IS_PUBLIC_KEY, OpraNestUtils, Public } from '@opra/nestjs';
2
+ export * from './constants.js';
3
+ export * from './opra-kafka.module.js';
4
+ export { IS_PUBLIC_KEY, OpraNestUtils, Public };
@@ -0,0 +1,16 @@
1
+ import { type DynamicModule, OnApplicationBootstrap, OnApplicationShutdown, OnModuleInit } from '@nestjs/common';
2
+ import { KafkaAdapter } from '@opra/kafka';
3
+ import { RpcControllerFactory } from '@opra/nestjs';
4
+ import type { OpraKafkaModule } from './opra-kafka.module.js';
5
+ export declare class OpraKafkaCoreModule implements OnModuleInit, OnApplicationBootstrap, OnApplicationShutdown {
6
+ private controllerFactory;
7
+ protected adapter: KafkaAdapter;
8
+ protected config: OpraKafkaModule.ApiConfig;
9
+ constructor(controllerFactory: RpcControllerFactory, adapter: KafkaAdapter, config: OpraKafkaModule.ApiConfig);
10
+ static forRoot(moduleOptions: OpraKafkaModule.ModuleOptions): DynamicModule;
11
+ static forRootAsync(moduleOptions: OpraKafkaModule.AsyncModuleOptions): DynamicModule;
12
+ protected static _getDynamicModule(moduleOptions: OpraKafkaModule.ModuleOptions | OpraKafkaModule.AsyncModuleOptions): DynamicModule;
13
+ onModuleInit(): any;
14
+ onApplicationBootstrap(): Promise<void>;
15
+ onApplicationShutdown(): Promise<void>;
16
+ }
@@ -0,0 +1,34 @@
1
+ import { type DynamicModule, Logger, type Type } from '@nestjs/common';
2
+ import { ApiDocumentFactory } from '@opra/common';
3
+ import type { KafkaAdapter } from '@opra/kafka';
4
+ export declare namespace OpraKafkaModule {
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?: (KafkaAdapter.InterceptorFunction | KafkaAdapter.IKafkaInterceptor | Type<KafkaAdapter.IKafkaInterceptor>)[];
14
+ }
15
+ export interface ApiConfig extends Pick<ApiDocumentFactory.InitArguments, 'types' | 'references' | 'info'>, Pick<KafkaAdapter.Config, 'client' | 'consumers' | 'logExtra' | 'defaults'> {
16
+ name: string;
17
+ description?: string;
18
+ scope?: string;
19
+ logger?: Logger;
20
+ }
21
+ export {};
22
+ }
23
+ export declare class OpraKafkaModule {
24
+ /**
25
+ *
26
+ * @param options
27
+ */
28
+ static forRoot(options: OpraKafkaModule.ModuleOptions): DynamicModule;
29
+ /**
30
+ *
31
+ * @param options
32
+ */
33
+ static forRootAsync(options: OpraKafkaModule.AsyncModuleOptions): DynamicModule;
34
+ }