From 0bcb4d57a99833e31bae6de918cf8b25507c2abe Mon Sep 17 00:00:00 2001 From: houssenedao Date: Wed, 22 Dec 2021 14:56:33 +0000 Subject: [PATCH] fix(): rename notify to notification --- .commitlintrc.json | 2 +- README.md | 8 +- lib/constants/index.ts | 2 +- .../nestjs-notification-provider.constant.ts | 1 + .../nestjs-notify-provider.constant.ts | 1 - lib/index.ts | 4 +- lib/interfaces/index.ts | 6 +- .../nestjs-notification-channel.interface.ts | 14 +++ .../nestjs-notification-module.interface.ts | 35 ++++++ ...ce.ts => nestjs-notification.interface.ts} | 10 +- .../nestjs-notify-channel.interface.ts | 14 --- .../nestjs-notify-module.interface.ts | 31 ----- lib/nestjs-notification.module.ts | 112 ++++++++++++++++++ ...vice.ts => nestjs-notification.service.ts} | 21 ++-- lib/nestjs-notify.module.ts | 110 ----------------- 15 files changed, 189 insertions(+), 182 deletions(-) create mode 100644 lib/constants/nestjs-notification-provider.constant.ts delete mode 100644 lib/constants/nestjs-notify-provider.constant.ts create mode 100644 lib/interfaces/nestjs-notification-channel.interface.ts create mode 100644 lib/interfaces/nestjs-notification-module.interface.ts rename lib/interfaces/{nestjs-notify.interface.ts => nestjs-notification.interface.ts} (54%) delete mode 100644 lib/interfaces/nestjs-notify-channel.interface.ts delete mode 100644 lib/interfaces/nestjs-notify-module.interface.ts create mode 100644 lib/nestjs-notification.module.ts rename lib/{nestjs-notify.service.ts => nestjs-notification.service.ts} (54%) delete mode 100644 lib/nestjs-notify.module.ts diff --git a/.commitlintrc.json b/.commitlintrc.json index 1f9f917e..e39932e9 100644 --- a/.commitlintrc.json +++ b/.commitlintrc.json @@ -9,7 +9,7 @@ "type-enum": [ 2, "always", - ["build", "chore", "ci", "docs", "feat", "fix", "perf", "refactor", "style", "test"] + ["build", "chore", "ci", "docs", "feat", "fix", "perf", "refactor", "style", "test", "revert"] ] } } diff --git a/README.md b/README.md index 64dabba6..41a0c91c 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,10 @@ $ npm i @sinuos/nestjs-notification ### Declare module ```typescript -import { NestjsNotifyModule } from '@sinuos/nestjs-notify.module'; +import { NestjsNotificationModule } from '@sinuos/nestjs-notify.module'; @Module({ - imports: [NestjsNotifyModule.register()], + imports: [NestjsNotificationModule.register()], }) export class AppModule {} ``` @@ -68,11 +68,11 @@ export class InvoicPaidNotification implements NestJsNotify { ```typescript import { Injectable } from '@nestjs/common'; -import { NestjsNotifyService } from '@sinuos/nestjs-notify.service'; +import { NestjsNotificationService } from '@sinuos/nestjs-notify.service'; @Injectable() export class AppService { - constructor(private notify: NestjsNotifyService) {} + constructor(private notify: NestjsNotificationService) {} notify() { const notification = new InvoicePaidNotification(); diff --git a/lib/constants/index.ts b/lib/constants/index.ts index 40bfe0c3..1ea8a740 100644 --- a/lib/constants/index.ts +++ b/lib/constants/index.ts @@ -1 +1 @@ -export * from './nestjs-notify-provider.constant'; +export * from './nestjs-notification-provider.constant'; diff --git a/lib/constants/nestjs-notification-provider.constant.ts b/lib/constants/nestjs-notification-provider.constant.ts new file mode 100644 index 00000000..d0101e3b --- /dev/null +++ b/lib/constants/nestjs-notification-provider.constant.ts @@ -0,0 +1 @@ +export const NESTJS_NOTIFICATION_OPTIONS = 'NESTJS_NOTIFICATION_OPTIONS'; diff --git a/lib/constants/nestjs-notify-provider.constant.ts b/lib/constants/nestjs-notify-provider.constant.ts deleted file mode 100644 index 5e3c28b3..00000000 --- a/lib/constants/nestjs-notify-provider.constant.ts +++ /dev/null @@ -1 +0,0 @@ -export const NESTJS_NOTIFY_OPTIONS = 'NESTJS_NOTIFY_OPTIONS'; diff --git a/lib/index.ts b/lib/index.ts index 9f10d2e6..f18ce0f1 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,3 +1,3 @@ export * from './interfaces'; -export * from './nestjs-notify.module'; -export * from './nestjs-notify.service'; +export * from './nestjs-notification.module'; +export * from './nestjs-notification.service'; diff --git a/lib/interfaces/index.ts b/lib/interfaces/index.ts index 483c9db7..e7169a54 100644 --- a/lib/interfaces/index.ts +++ b/lib/interfaces/index.ts @@ -1,3 +1,3 @@ -export * from './nestjs-notify.interface'; -export * from './nestjs-notify-module.interface'; -export * from './nestjs-notify-channel.interface'; +export * from './nestjs-notification.interface'; +export * from './nestjs-notification-module.interface'; +export * from './nestjs-notification-channel.interface'; diff --git a/lib/interfaces/nestjs-notification-channel.interface.ts b/lib/interfaces/nestjs-notification-channel.interface.ts new file mode 100644 index 00000000..aa3eba6c --- /dev/null +++ b/lib/interfaces/nestjs-notification-channel.interface.ts @@ -0,0 +1,14 @@ +import { NestJsNotification } from './nestjs-notification.interface'; + +/** + * @interface INestjsNotificationChannel + * @property send() + */ +export interface INestjsNotificationChannel { + /** + * Send the given notification + * @param {NestJsNotification} notification + * @return Promise + */ + send(notification: NestJsNotification): Promise; +} diff --git a/lib/interfaces/nestjs-notification-module.interface.ts b/lib/interfaces/nestjs-notification-module.interface.ts new file mode 100644 index 00000000..d5fa51a7 --- /dev/null +++ b/lib/interfaces/nestjs-notification-module.interface.ts @@ -0,0 +1,35 @@ +import { ModuleMetadata, Provider, Type } from '@nestjs/common'; + +/** + * @interface NestjsNotificationModuleOptionsFactory + * @property createNestjsNotificationOptions() + */ +export interface NestjsNotificationModuleOptionsFactory { + createNestjsNotificationOptions(): + | Promise + | NestjsNotificationModuleOptions; +} + +/** + * @interface NestjsNotificationModuleAsyncOptions + * @extends {Pick} + * @property useExisting + * @property useClass + * @property useFactory + * @property inject + * @property extraProviders + */ +export interface NestjsNotificationModuleAsyncOptions extends Pick { + useExisting?: Type; + useClass?: Type; + useFactory?: ( + ...args: any[] + ) => Promise | NestjsNotificationModuleOptions; + inject?: any[]; + extraProviders?: Provider[]; +} + +/** + * @interface NestjsNotificationModuleOptions + */ +export type NestjsNotificationModuleOptions = null; diff --git a/lib/interfaces/nestjs-notify.interface.ts b/lib/interfaces/nestjs-notification.interface.ts similarity index 54% rename from lib/interfaces/nestjs-notify.interface.ts rename to lib/interfaces/nestjs-notification.interface.ts index 3d57527c..7a3230e5 100644 --- a/lib/interfaces/nestjs-notify.interface.ts +++ b/lib/interfaces/nestjs-notification.interface.ts @@ -1,17 +1,17 @@ import { Type } from '@nestjs/common'; -import { INestjsNotifyChannel } from './nestjs-notify-channel.interface'; +import { INestjsNotificationChannel } from './nestjs-notification-channel.interface'; /** - * @interface NestJsNotify + * @interface NestJsNotification * @property sendToChannels() * @property toPayload?() */ -export interface NestJsNotify { +export interface NestJsNotification { /** * Get the channels the notification should broadcast on. - * @returns {Type[]} array + * @returns {Type[]} array */ - sendToChannels(): Type[]; + sendToChannels(): Type[]; /** * Get the json representation of the notification. diff --git a/lib/interfaces/nestjs-notify-channel.interface.ts b/lib/interfaces/nestjs-notify-channel.interface.ts deleted file mode 100644 index 475b03de..00000000 --- a/lib/interfaces/nestjs-notify-channel.interface.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { NestJsNotify } from './nestjs-notify.interface'; - -/** - * @interface INestjsNotifyChannel - * @property send() - */ -export interface INestjsNotifyChannel { - /** - * Send the given notification - * @param {NestJsNotify} notification - * @return Promise - */ - send(notification: NestJsNotify): Promise; -} diff --git a/lib/interfaces/nestjs-notify-module.interface.ts b/lib/interfaces/nestjs-notify-module.interface.ts deleted file mode 100644 index 070821c2..00000000 --- a/lib/interfaces/nestjs-notify-module.interface.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { ModuleMetadata, Provider, Type } from '@nestjs/common'; - -/** - * @interface NestjsNotifyModuleOptionsFactory - * @property createNestjsNotifyOptions() - */ -export interface NestjsNotifyModuleOptionsFactory { - createNestjsNotifyOptions(): Promise | NestjsNotifyModuleOptions; -} - -/** - * @interface NestjsNotifyModuleAsyncOptions - * @extends {Pick} - * @property useExisting - * @property useClass - * @property useFactory - * @property inject - * @property extraProviders - */ -export interface NestjsNotifyModuleAsyncOptions extends Pick { - useExisting?: Type; - useClass?: Type; - useFactory?: (...args: any[]) => Promise | NestjsNotifyModuleOptions; - inject?: any[]; - extraProviders?: Provider[]; -} - -/** - * @interface NestjsNotifyModuleOptions - */ -export type NestjsNotifyModuleOptions = null; diff --git a/lib/nestjs-notification.module.ts b/lib/nestjs-notification.module.ts new file mode 100644 index 00000000..029ae018 --- /dev/null +++ b/lib/nestjs-notification.module.ts @@ -0,0 +1,112 @@ +import { DynamicModule, Module, Provider, ValueProvider } from '@nestjs/common'; +import { NestjsNotificationService } from './nestjs-notification.service'; +import { + NestjsNotificationModuleAsyncOptions, + NestjsNotificationModuleOptions, + NestjsNotificationModuleOptionsFactory, +} from './interfaces'; +import { NESTJS_NOTIFICATION_OPTIONS } from './constants'; + +@Module({}) +export class NestjsNotificationModule { + /** + * Register module + * @static + * @param {NestjsNotificationModuleOptions} options + * @return DynamicModule + */ + static register(options: NestjsNotificationModuleOptions): DynamicModule { + const notifyProvider: ValueProvider = { + provide: NESTJS_NOTIFICATION_OPTIONS, + useValue: options, + }; + + return { + module: NestjsNotificationModule, + providers: [NestjsNotificationService, notifyProvider], + exports: [NestjsNotificationService, notifyProvider], + }; + } + + /** + * Register async + * @static + * @param {NestjsNotificationModuleAsyncOptions} options + * @return DynamicModule + */ + static registerAsync(options: NestjsNotificationModuleAsyncOptions): DynamicModule { + const notifyProvider: ValueProvider = { + provide: NESTJS_NOTIFICATION_OPTIONS, + useValue: options, + }; + + return { + module: NestjsNotificationModule, + imports: options.imports || [], + providers: [NestjsNotificationService, notifyProvider, ...this.createAsyncProviders(options)], + exports: [NestjsNotificationService, notifyProvider], + }; + } + + /** + * Create async providers + * @private + * @param {NestjsNotificationModuleAsyncOptions} options + * @return Provider[] + */ + private static createAsyncProviders(options: NestjsNotificationModuleAsyncOptions): Provider[] { + if (options.useExisting || options.useFactory) { + return [this.createAsyncConfigProvider(options)]; + } else if (!options.useClass) { + return [ + { + provide: NESTJS_NOTIFICATION_OPTIONS, + useValue: {}, + inject: options.inject || [], + }, + ]; + } + + return [ + this.createAsyncConfigProvider(options), + { + provide: options.useClass, + useClass: options.useClass, + }, + ]; + } + + /** + * Create async config provider + * @private + * @param {NestjsNotificationModuleAsyncOptions} options + * @return Provider + */ + private static createAsyncConfigProvider( + options: NestjsNotificationModuleAsyncOptions, + ): Provider { + if (options.useFactory) { + return { + provide: NESTJS_NOTIFICATION_OPTIONS, + useFactory: options.useFactory, + inject: options.inject || [], + }; + } + + const inject = options.useClass || options.useExisting; + + if (!inject) { + throw new Error('Invalid configuration. Must provide useFactory, useClass or useExisting'); + } + + return { + provide: NESTJS_NOTIFICATION_OPTIONS, + async useFactory( + optionsFactory: NestjsNotificationModuleOptionsFactory, + ): Promise { + return optionsFactory.createNestjsNotificationOptions(); + }, + inject: [inject], + }; + } +} diff --git a/lib/nestjs-notify.service.ts b/lib/nestjs-notification.service.ts similarity index 54% rename from lib/nestjs-notify.service.ts rename to lib/nestjs-notification.service.ts index 21d6adcc..f4974b89 100644 --- a/lib/nestjs-notify.service.ts +++ b/lib/nestjs-notification.service.ts @@ -1,14 +1,15 @@ import { Injectable, Type } from '@nestjs/common'; import { ModuleRef } from '@nestjs/core'; -import { INestjsNotifyChannel, NestJsNotify } from './interfaces'; +import { INestjsNotificationChannel, NestJsNotification } from './interfaces'; @Injectable() -export class NestjsNotifyService { +export class NestjsNotificationService { /** * Resolve the channel needed to send the Notification * @param channel */ - private resolveChannel = (channel: Type) => this.moduleRef.create(channel); + private resolveChannel = (channel: Type) => + this.moduleRef.create(channel); /** * @constructor @@ -18,13 +19,13 @@ export class NestjsNotifyService { /** * Process a notification and send via designated channel - * @param {NestJsNotify} notification + * @param {NestJsNotification} notification */ - public send(notification: NestJsNotify): Promise { + public send(notification: NestJsNotification): Promise { const channels = notification.sendToChannels(); return Promise.all( - channels.map((channel: Type) => + channels.map((channel: Type) => this.sendOnChannel(notification, channel), ), ); @@ -32,12 +33,12 @@ export class NestjsNotifyService { /** * Send notification on designated channel - * @param {NestJsNotify} notification - * @param {Type} clientChannel + * @param {NestJsNotification} notification + * @param {Type} clientChannel */ private async sendOnChannel( - notification: NestJsNotify, - clientChannel: Type, + notification: NestJsNotification, + clientChannel: Type, ): Promise { return (await this.resolveChannel(clientChannel)).send(notification); } diff --git a/lib/nestjs-notify.module.ts b/lib/nestjs-notify.module.ts deleted file mode 100644 index 2c92658f..00000000 --- a/lib/nestjs-notify.module.ts +++ /dev/null @@ -1,110 +0,0 @@ -import { DynamicModule, Module, Provider, ValueProvider } from '@nestjs/common'; -import { NestjsNotifyService } from './nestjs-notify.service'; -import { - NestjsNotifyModuleAsyncOptions, - NestjsNotifyModuleOptions, - NestjsNotifyModuleOptionsFactory, -} from './interfaces'; -import { NESTJS_NOTIFY_OPTIONS } from './constants'; - -@Module({}) -export class NestjsNotifyModule { - /** - * Register module - * @static - * @param {NestjsNotifyModuleOptions} options - * @return DynamicModule - */ - static register(options: NestjsNotifyModuleOptions): DynamicModule { - const notifyProvider: ValueProvider = { - provide: NESTJS_NOTIFY_OPTIONS, - useValue: options, - }; - - return { - module: NestjsNotifyModule, - providers: [NestjsNotifyService, notifyProvider], - exports: [NestjsNotifyService, notifyProvider], - }; - } - - /** - * Register async - * @static - * @param {NestjsNotifyModuleAsyncOptions} options - * @return DynamicModule - */ - static registerAsync(options: NestjsNotifyModuleAsyncOptions): DynamicModule { - const notifyProvider: ValueProvider = { - provide: NESTJS_NOTIFY_OPTIONS, - useValue: options, - }; - - return { - module: NestjsNotifyModule, - imports: options.imports || [], - providers: [NestjsNotifyService, notifyProvider, ...this.createAsyncProviders(options)], - exports: [NestjsNotifyService, notifyProvider], - }; - } - - /** - * Create async providers - * @private - * @param {NestjsNotifyModuleAsyncOptions} options - * @return Provider[] - */ - private static createAsyncProviders(options: NestjsNotifyModuleAsyncOptions): Provider[] { - if (options.useExisting || options.useFactory) { - return [this.createAsyncConfigProvider(options)]; - } else if (!options.useClass) { - return [ - { - provide: NESTJS_NOTIFY_OPTIONS, - useValue: {}, - inject: options.inject || [], - }, - ]; - } - - return [ - this.createAsyncConfigProvider(options), - { - provide: options.useClass, - useClass: options.useClass, - }, - ]; - } - - /** - * Create async config provider - * @private - * @param {NestjsNotifyModuleAsyncOptions} options - * @return Provider - */ - private static createAsyncConfigProvider(options: NestjsNotifyModuleAsyncOptions): Provider { - if (options.useFactory) { - return { - provide: NESTJS_NOTIFY_OPTIONS, - useFactory: options.useFactory, - inject: options.inject || [], - }; - } - - const inject = options.useClass || options.useExisting; - - if (!inject) { - throw new Error('Invalid configuration. Must provide useFactory, useClass or useExisting'); - } - - return { - provide: NESTJS_NOTIFY_OPTIONS, - async useFactory( - optionsFactory: NestjsNotifyModuleOptionsFactory, - ): Promise { - return optionsFactory.createNestjsNotifyOptions(); - }, - inject: [inject], - }; - } -}