Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nestjs-kafka): init package #317

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/nestjs-kafka/logger/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './kafka-log.creator.js'
25 changes: 25 additions & 0 deletions packages/nestjs-kafka/logger/kafka-log.creator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { LogEntry } from 'kafkajs'

import { Logger } from '@atls/logger'
import { logLevel } from 'kafkajs'
import camelcase from 'camelcase'

export const kafkaLogCreator = (): ((logEntry: LogEntry) => void) => {
const kafkaLogger = new Logger('kafka')

return ({ namespace, level, log: { message, ...extra } }: LogEntry): void => {
const logger = namespace
? kafkaLogger.child(camelcase(namespace, { pascalCase: true }))
: kafkaLogger

if (level === logLevel.ERROR || level === logLevel.NOTHING) {
logger.error(message, extra)
} else if (level === logLevel.WARN) {
logger.warn(message, extra)
} else if (level === logLevel.INFO) {
logger.info(message, extra)
} else if (level === logLevel.DEBUG) {
logger.debug(message, extra)
}
}
}
48 changes: 48 additions & 0 deletions packages/nestjs-kafka/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{

Check failure on line 1 in packages/nestjs-kafka/package.json

View workflow job for this annotation

GitHub Actions / Release

Error release workspace @atls/nestjs-kafka

Exit code 1
Raw output
➤ YN0000: ┌ Library Build
::group::Library Build
➤ YN0000: │ packages/nestjs-kafka/src/module/kafka.factory.ts
➤ YN0000: │ 
➤ YN0000: │ TS2307: Cannot find module '../logger/index.js' or its corresponding type
➤ YN0000: │       declarations.
➤ YN0000: │ 
➤ YN0000: │ 
::endgroup::
➤ YN0000: packages/nestjs-kafka/src/module/kafka.factory.ts
➤ YN0000: 
➤ YN0000: TS2307: Cannot find module '../logger/index.js' or its corresponding type
➤ YN0000:       declarations.
➤ YN0000: 
➤ YN0000: 
➤ YN0000: └ Completed in 3s 796ms
➤ YN0000: Failed with errors in 3s 799ms
"name": "@atls/nestjs-kafka",
"version": "0.0.0",
"license": "BSD-3-Clause",
"type": "module",
"exports": {
"./package.json": "./package.json",
".": "./src/index.ts"
},
"main": "src/index.ts",
"files": [
"dist"
],
"scripts": {
"build": "yarn library build",
"prepack": "yarn run build",
"postpack": "rm -rf dist"
},
"dependencies": {
"@atls/logger": "^0.0.2",
"camelcase": "^8.0.0",
"kafkajs": "^2.2.4"
},
"devDependencies": {
"@nestjs/common": "^10.0.5",
"@nestjs/core": "^10.0.5",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
},
"peerDependencies": {
"@nestjs/common": "^10",
"@nestjs/core": "^10",
"reflect-metadata": "^0.1",
"rxjs": "^7"
},
"publishConfig": {
"exports": {
"./package.json": "./package.json",
".": {
"import": "./dist/index.js",
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"main": "dist/index.js",
"typings": "dist/index.d.ts"
}
}
3 changes: 3 additions & 0 deletions packages/nestjs-kafka/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from 'kafkajs'

export * from './module/index.js'
4 changes: 4 additions & 0 deletions packages/nestjs-kafka/src/module/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './kafka.module.constants.js'
export * from './kafka.config-factory.js'
export * from './kafka.factory.js'
export * from './kafka.module.js'
26 changes: 26 additions & 0 deletions packages/nestjs-kafka/src/module/kafka.config-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { KafkaConfig } from 'kafkajs'

import { Inject } from '@nestjs/common'
import { Injectable } from '@nestjs/common'

import { KAFKA_MODULE_OPTIONS_CLIENT_ID } from './kafka.module.constants.js'
import { KAFKA_MODULE_OPTIONS_BROKERS } from './kafka.module.constants.js'

@Injectable()
export class KafkaConfigFactory {
constructor(
@Inject(KAFKA_MODULE_OPTIONS_CLIENT_ID)
private readonly clientId: string,
@Inject(KAFKA_MODULE_OPTIONS_BROKERS)
private readonly brokers: Array<string>
) {}

createKafkaOptions(): KafkaConfig {
return {
clientId: this.clientId || process.env.KAFKA_CLIENT_ID,
brokers:
this.brokers ||
(process.env.KAFKA_BROKERS ? process.env.KAFKA_BROKERS.split(',') : ['localhost:29092']),
}
}
}
20 changes: 20 additions & 0 deletions packages/nestjs-kafka/src/module/kafka.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { KafkaConfig } from 'kafkajs'

import { Injectable } from '@nestjs/common'
import { Kafka } from 'kafkajs'

import { KafkaConfigFactory } from './kafka.config-factory.js'
import { kafkaLogCreator } from '../logger/index.js'

@Injectable()
export class KafkaFactory {
constructor(private readonly configFactory: KafkaConfigFactory) {}

create(options: Partial<KafkaConfig> = {}): Kafka {
return new Kafka({
logCreator: kafkaLogCreator,
...this.configFactory.createKafkaOptions(),
...options,
})
}
}
3 changes: 3 additions & 0 deletions packages/nestjs-kafka/src/module/kafka.module.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const KAFKA_MODULE_OPTIONS_GROUP_ID = Symbol('kafka-module-options-group-id')
export const KAFKA_MODULE_OPTIONS_CLIENT_ID = Symbol('kafka-module-options-client-id')
export const KAFKA_MODULE_OPTIONS_BROKERS = Symbol('kafka-module-options-brokers')
31 changes: 31 additions & 0 deletions packages/nestjs-kafka/src/module/kafka.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { DynamicModule } from '@nestjs/common'
import type { KafkaConfig } from 'kafkajs'

import { Module } from '@nestjs/common'

import { KAFKA_MODULE_OPTIONS_CLIENT_ID } from './kafka.module.constants.js'
import { KAFKA_MODULE_OPTIONS_BROKERS } from './kafka.module.constants.js'
import { KafkaConfigFactory } from './kafka.config-factory.js'
import { KafkaFactory } from './kafka.factory.js'

@Module({})
export class KafkaModule {
static register(options: Partial<KafkaConfig> = {}): DynamicModule {
return {
module: KafkaModule,
providers: [
KafkaConfigFactory,
KafkaFactory,
{
provide: KAFKA_MODULE_OPTIONS_BROKERS,
useValue: options.brokers,
},
{
provide: KAFKA_MODULE_OPTIONS_CLIENT_ID,
useValue: options.clientId,
},
],
exports: [KafkaConfigFactory, KafkaFactory],
}
}
}
35 changes: 34 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ __metadata:
languageName: node
linkType: hard

"@atls/logger@npm:0.0.2":
"@atls/logger@npm:0.0.2, @atls/logger@npm:^0.0.2":
version: 0.0.2
resolution: "@atls/logger@npm:0.0.2"
dependencies:
Expand Down Expand Up @@ -621,6 +621,25 @@ __metadata:
languageName: unknown
linkType: soft

"@atls/nestjs-kafka@workspace:packages/nestjs-kafka":
version: 0.0.0-use.local
resolution: "@atls/nestjs-kafka@workspace:packages/nestjs-kafka"
dependencies:
"@atls/logger": "npm:^0.0.2"
"@nestjs/common": "npm:^10.0.5"
"@nestjs/core": "npm:^10.0.5"
camelcase: "npm:^8.0.0"
kafkajs: "npm:^2.2.4"
reflect-metadata: "npm:^0.1.13"
rxjs: "npm:^7.8.1"
peerDependencies:
"@nestjs/common": ^10
"@nestjs/core": ^10
reflect-metadata: ^0.1
rxjs: ^7
languageName: unknown
linkType: soft

"@atls/nestjs-keto@workspace:packages/nestjs-keto":
version: 0.0.0-use.local
resolution: "@atls/nestjs-keto@workspace:packages/nestjs-keto"
Expand Down Expand Up @@ -7833,6 +7852,13 @@ __metadata:
languageName: node
linkType: hard

"camelcase@npm:^8.0.0":
version: 8.0.0
resolution: "camelcase@npm:8.0.0"
checksum: 10c0/56c5fe072f0523c9908cdaac21d4a3b3fb0f608fb2e9ba90a60e792b95dd3bb3d1f3523873ab17d86d146e94171305f73ef619e2f538bd759675bc4a14b4bff3
languageName: node
linkType: hard

"caniuse-lite@npm:^1.0.30001646":
version: 1.0.30001660
resolution: "caniuse-lite@npm:1.0.30001660"
Expand Down Expand Up @@ -12039,6 +12065,13 @@ __metadata:
languageName: node
linkType: hard

"kafkajs@npm:^2.2.4":
version: 2.2.4
resolution: "kafkajs@npm:2.2.4"
checksum: 10c0/6eece1f682a257955f647b6142430e87681d11bf01e9ebe0a73b4bc0653b8f970c48ba72a3b25fdbb4205cfbb36e5ef93d954b8f8c5f7767ae2b4693bdec9d88
languageName: node
linkType: hard

"keyv@npm:^4.5.3":
version: 4.5.4
resolution: "keyv@npm:4.5.4"
Expand Down
Loading