@rsdk/kafka.producer 2.0.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/CHANGELOG.md ADDED
@@ -0,0 +1,24 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ # [2.0.0](https://github.com/R-Vision/rsdk/compare/v1.0.12...v2.0.0) (2023-06-06)
7
+
8
+ **Note:** Version bump only for package @rsdk/kafka.producer
9
+
10
+ ## 1.0.12 (2023-05-23)
11
+
12
+ ### Bug Fixes
13
+
14
+ * remove private dependencies from `peerDependencies` section ([0bce6ff](https://github.com/R-Vision/rsdk/commit/0bce6ffb5b699e2ed1dc9aca77cbdbf085267ff7))
15
+
16
+ ## 1.0.11 (2023-05-23)
17
+
18
+ **Note:** Version bump only for package @rsdk/kafka.producer
19
+
20
+ ## 1.0.10 (2023-05-22)
21
+
22
+ ### Bug Fixes
23
+
24
+ * add "access: public" to new packages ([04d5983](https://github.com/R-Vision/rsdk/commit/04d5983cfa3e97c5d8219e07208a0499a8c6bc83))
@@ -0,0 +1,9 @@
1
+ import type { EventType } from '@rsdk/kafka.common';
2
+ import type { KafkaProducer, OutboxTable, PublishOptions } from './types';
3
+ export declare abstract class BaseProducer<T extends EventType> implements KafkaProducer<T> {
4
+ readonly eventType: T;
5
+ constructor(eventType: T);
6
+ protected getPartitionKey(payload: T, partitionKeyField: string | null): string;
7
+ protected parsePayload(payload: T, options?: PublishOptions): Partial<OutboxTable>;
8
+ abstract publish(payload: T, options?: PublishOptions | undefined): Promise<void>;
9
+ }
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.BaseProducer = void 0;
7
+ const crypto_1 = require("crypto");
8
+ const get_1 = __importDefault(require("lodash/get"));
9
+ class BaseProducer {
10
+ eventType;
11
+ constructor(eventType) {
12
+ this.eventType = eventType;
13
+ }
14
+ getPartitionKey(payload, partitionKeyField) {
15
+ const partitionKey = typeof partitionKeyField === 'string' && partitionKeyField.length > 0
16
+ ? (0, get_1.default)(payload, partitionKeyField, (0, crypto_1.randomUUID)())
17
+ : (0, get_1.default)(payload, 'id') ||
18
+ (0, get_1.default)(payload, 'uuid') ||
19
+ (0, get_1.default)(payload, 'guid', (0, crypto_1.randomUUID)());
20
+ return String(partitionKey);
21
+ }
22
+ parsePayload(payload, options) {
23
+ return {
24
+ group: this.eventType.$group,
25
+ metadata: options?.metadata ? options.metadata : null,
26
+ partition_key: options?.partitionKey
27
+ ? String(options.partitionKey)
28
+ : this.getPartitionKey(payload, this.eventType.$partitionKeyField),
29
+ payload: Buffer.from(this.eventType.encode(payload).finish()),
30
+ type: this.eventType.$type,
31
+ };
32
+ }
33
+ }
34
+ exports.BaseProducer = BaseProducer;
35
+ //# sourceMappingURL=base.producer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.producer.js","sourceRoot":"","sources":["../src/base.producer.ts"],"names":[],"mappings":";;;;;;AAAA,mCAAoC;AAGpC,qDAA6B;AAI7B,MAAsB,YAAY;IAGX;IAArB,YAAqB,SAAY;QAAZ,cAAS,GAAT,SAAS,CAAG;IAAG,CAAC;IAE3B,eAAe,CACvB,OAAU,EACV,iBAAgC;QAEhC,MAAM,YAAY,GAChB,OAAO,iBAAiB,KAAK,QAAQ,IAAI,iBAAiB,CAAC,MAAM,GAAG,CAAC;YACnE,CAAC,CAAC,IAAA,aAAG,EAAC,OAAO,EAAE,iBAAiB,EAAE,IAAA,mBAAU,GAAE,CAAC;YAC/C,CAAC,CAAC,IAAA,aAAG,EAAC,OAAO,EAAE,IAAI,CAAC;gBAClB,IAAA,aAAG,EAAC,OAAO,EAAE,MAAM,CAAC;gBACpB,IAAA,aAAG,EAAC,OAAO,EAAE,MAAM,EAAE,IAAA,mBAAU,GAAE,CAAC,CAAC;QAEzC,OAAO,MAAM,CAAC,YAAY,CAAC,CAAC;IAC9B,CAAC;IAES,YAAY,CACpB,OAAU,EACV,OAAwB;QAExB,OAAO;YACL,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM;YAC5B,QAAQ,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI;YACrD,aAAa,EAAE,OAAO,EAAE,YAAY;gBAClC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC;gBAC9B,CAAC,CAAC,IAAI,CAAC,eAAe,CAAC,OAAO,EAAE,IAAI,CAAC,SAAS,CAAC,kBAAkB,CAAC;YACpE,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,CAAC;YAC7D,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK;SAC3B,CAAC;IACJ,CAAC;CAMF;AAtCD,oCAsCC"}
@@ -0,0 +1,3 @@
1
+ export { BaseProducer } from './base.producer';
2
+ export { InjectProducer } from './inject-producer.decorator';
3
+ export { KafkaProducer, OutboxTable, PublishOptions } from './types';
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InjectProducer = exports.BaseProducer = void 0;
4
+ var base_producer_1 = require("./base.producer");
5
+ Object.defineProperty(exports, "BaseProducer", { enumerable: true, get: function () { return base_producer_1.BaseProducer; } });
6
+ var inject_producer_decorator_1 = require("./inject-producer.decorator");
7
+ Object.defineProperty(exports, "InjectProducer", { enumerable: true, get: function () { return inject_producer_decorator_1.InjectProducer; } });
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,iDAA+C;AAAtC,6GAAA,YAAY,OAAA;AACrB,yEAA6D;AAApD,2HAAA,cAAc,OAAA"}
@@ -0,0 +1,3 @@
1
+ import { Inject } from '@nestjs/common';
2
+ import type { EventType } from '@rsdk/kafka.common';
3
+ export declare const InjectProducer: <T>(eventType: EventType<T>) => ReturnType<typeof Inject>;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InjectProducer = void 0;
4
+ const common_1 = require("@nestjs/common");
5
+ const InjectProducer = (eventType) => (0, common_1.Inject)(eventType.$type);
6
+ exports.InjectProducer = InjectProducer;
7
+ //# sourceMappingURL=inject-producer.decorator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inject-producer.decorator.js","sourceRoot":"","sources":["../src/inject-producer.decorator.ts"],"names":[],"mappings":";;;AAAA,2CAAwC;AAGjC,MAAM,cAAc,GAAG,CAC5B,SAAuB,EACI,EAAE,CAAC,IAAA,eAAM,EAAC,SAAS,CAAC,KAAK,CAAC,CAAC;AAF3C,QAAA,cAAc,kBAE6B"}
@@ -0,0 +1,16 @@
1
+ /// <reference types="node" />
2
+ export interface PublishOptions {
3
+ metadata?: Record<string, any>;
4
+ partitionKey?: string | number;
5
+ }
6
+ export interface OutboxTable {
7
+ id: string;
8
+ type: string;
9
+ group: string;
10
+ partition_key: string;
11
+ metadata: Record<string, any> | null;
12
+ payload: Buffer;
13
+ }
14
+ export interface KafkaProducer<T> {
15
+ publish(payload: T, options?: PublishOptions | undefined): Promise<void>;
16
+ }
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@rsdk/kafka.producer",
3
+ "version": "2.0.0",
4
+ "description": "Publishers for kafka (different direct and outbox strategies)",
5
+ "license": "Apache License 2.0",
6
+ "publishConfig": {
7
+ "access": "public"
8
+ },
9
+ "repository": {
10
+ "url": "https://github.com/R-Vision/rsdk"
11
+ },
12
+ "main": "dist/index.js",
13
+ "peerDependencies": {
14
+ "@nestjs/common": "^9.0.0",
15
+ "@nestjs/microservices": "^9.0.0",
16
+ "@nestjs/typeorm": "^9.0.0",
17
+ "@rsdk/common": "^2.0.0",
18
+ "@rsdk/core": "^2.0.0",
19
+ "@rsdk/kafka.common": "^2.0.0",
20
+ "@rsdk/logging": "^2.0.0",
21
+ "lodash": "^4.17.21"
22
+ },
23
+ "gitHead": "9fe1395b8e38e1c7b9578dd5eed12e0c57a9087f"
24
+ }
@@ -0,0 +1,46 @@
1
+ import { randomUUID } from 'crypto';
2
+
3
+ import type { EventType } from '@rsdk/kafka.common';
4
+ import get from 'lodash/get';
5
+
6
+ import type { KafkaProducer, OutboxTable, PublishOptions } from './types';
7
+
8
+ export abstract class BaseProducer<T extends EventType>
9
+ implements KafkaProducer<T>
10
+ {
11
+ constructor(readonly eventType: T) {}
12
+
13
+ protected getPartitionKey(
14
+ payload: T,
15
+ partitionKeyField: string | null,
16
+ ): string {
17
+ const partitionKey =
18
+ typeof partitionKeyField === 'string' && partitionKeyField.length > 0
19
+ ? get(payload, partitionKeyField, randomUUID())
20
+ : get(payload, 'id') ||
21
+ get(payload, 'uuid') ||
22
+ get(payload, 'guid', randomUUID());
23
+
24
+ return String(partitionKey);
25
+ }
26
+
27
+ protected parsePayload(
28
+ payload: T,
29
+ options?: PublishOptions,
30
+ ): Partial<OutboxTable> {
31
+ return {
32
+ group: this.eventType.$group,
33
+ metadata: options?.metadata ? options.metadata : null,
34
+ partition_key: options?.partitionKey
35
+ ? String(options.partitionKey)
36
+ : this.getPartitionKey(payload, this.eventType.$partitionKeyField),
37
+ payload: Buffer.from(this.eventType.encode(payload).finish()),
38
+ type: this.eventType.$type,
39
+ };
40
+ }
41
+
42
+ abstract publish(
43
+ payload: T,
44
+ options?: PublishOptions | undefined,
45
+ ): Promise<void>;
46
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { BaseProducer } from './base.producer';
2
+ export { InjectProducer } from './inject-producer.decorator';
3
+ export { KafkaProducer, OutboxTable, PublishOptions } from './types';
@@ -0,0 +1,6 @@
1
+ import { Inject } from '@nestjs/common';
2
+ import type { EventType } from '@rsdk/kafka.common';
3
+
4
+ export const InjectProducer = <T>(
5
+ eventType: EventType<T>,
6
+ ): ReturnType<typeof Inject> => Inject(eventType.$type);
package/src/types.ts ADDED
@@ -0,0 +1,20 @@
1
+ export interface PublishOptions {
2
+ // Metadata must be a plain object. Object will be stringified.
3
+ metadata?: Record<string, any>;
4
+ partitionKey?: string | number;
5
+ }
6
+
7
+ export interface OutboxTable {
8
+ id: string;
9
+ type: string;
10
+ group: string;
11
+ partition_key: string;
12
+ metadata: Record<string, any> | null;
13
+ payload: Buffer;
14
+ }
15
+
16
+ // TODO: Добавить типизацию T extends EventType. Но с текущим генерируемым кодом это не работает
17
+ // т. к. интерфейс события по факту не экстендит EventType
18
+ export interface KafkaProducer<T> {
19
+ publish(payload: T, options?: PublishOptions | undefined): Promise<void>;
20
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "@rsdk/tsconfig/base.json",
3
+ "compilerOptions": {
4
+ "declaration": true,
5
+ "outDir": "dist"
6
+ },
7
+ "include": ["src/**/*"],
8
+ "exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
9
+ }