-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #317 from atls/feat/nestjs-kafka
feat(nestjs-kafka): init package
- Loading branch information
Showing
11 changed files
with
232 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './kafka-log.creator.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from 'kafkajs' | ||
|
||
export * from './module/index.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters