From 9935b87d450eae430ecf630bd7713e638a32b4a8 Mon Sep 17 00:00:00 2001 From: atassis Date: Sat, 9 Jul 2022 17:40:49 +0800 Subject: [PATCH 1/3] style: applied linter rules for all files --- lib/decorators/scene/scene-enter.decorator.ts | 5 ++-- lib/decorators/scene/scene-leave.decorator.ts | 5 ++-- .../telegraf-arguments-host.ts | 3 ++- .../telegraf-execution-context.ts | 3 ++- lib/interfaces/telegraf-options.interface.ts | 4 +++- lib/services/listeners-explorer.service.ts | 10 ++++---- lib/types/index.ts | 4 ++-- lib/utils/create-listener-decorator.util.ts | 5 ++-- lib/utils/param-decorator.util.ts | 24 ++++++++++--------- package-lock.json | 6 ++--- 10 files changed, 35 insertions(+), 34 deletions(-) diff --git a/lib/decorators/scene/scene-enter.decorator.ts b/lib/decorators/scene/scene-enter.decorator.ts index d198f60..be4c23a 100644 --- a/lib/decorators/scene/scene-enter.decorator.ts +++ b/lib/decorators/scene/scene-enter.decorator.ts @@ -1,6 +1,5 @@ import { createListenerDecorator } from '../../utils'; import { Scenes } from 'telegraf'; -export const SceneEnter = createListenerDecorator>( - 'enter', -); +export const SceneEnter = + createListenerDecorator>('enter'); diff --git a/lib/decorators/scene/scene-leave.decorator.ts b/lib/decorators/scene/scene-leave.decorator.ts index 3f935ca..ad14111 100644 --- a/lib/decorators/scene/scene-leave.decorator.ts +++ b/lib/decorators/scene/scene-leave.decorator.ts @@ -1,6 +1,5 @@ import { createListenerDecorator } from '../../utils'; import { Scenes } from 'telegraf'; -export const SceneLeave = createListenerDecorator>( - 'leave', -); +export const SceneLeave = + createListenerDecorator>('leave'); diff --git a/lib/execution-context/telegraf-arguments-host.ts b/lib/execution-context/telegraf-arguments-host.ts index 0854642..41caef5 100644 --- a/lib/execution-context/telegraf-arguments-host.ts +++ b/lib/execution-context/telegraf-arguments-host.ts @@ -4,7 +4,8 @@ import { TgArgumentsHost } from './tg-arguments-host.interface'; export class TelegrafArgumentsHost extends ExecutionContextHost - implements TgArgumentsHost { + implements TgArgumentsHost +{ static create(context: ArgumentsHost): TelegrafArgumentsHost { const type = context.getType(); const tgContext = new TelegrafArgumentsHost(context.getArgs()); diff --git a/lib/execution-context/telegraf-execution-context.ts b/lib/execution-context/telegraf-execution-context.ts index 44f5795..e209681 100644 --- a/lib/execution-context/telegraf-execution-context.ts +++ b/lib/execution-context/telegraf-execution-context.ts @@ -6,7 +6,8 @@ export type TelegrafContextType = 'telegraf' | ContextType; export class TelegrafExecutionContext extends ExecutionContextHost - implements TgArgumentsHost { + implements TgArgumentsHost +{ static create(context: ExecutionContext): TelegrafExecutionContext { const type = context.getType(); const tgContext = new TelegrafExecutionContext( diff --git a/lib/interfaces/telegraf-options.interface.ts b/lib/interfaces/telegraf-options.interface.ts index f8f6b74..e2dcde2 100644 --- a/lib/interfaces/telegraf-options.interface.ts +++ b/lib/interfaces/telegraf-options.interface.ts @@ -11,7 +11,9 @@ export interface TelegrafModuleOptions { } export interface TelegrafOptionsFactory { - createTelegrafOptions(): Promise | TelegrafModuleOptions; + createTelegrafOptions(): + | Promise + | TelegrafModuleOptions; } export interface TelegrafModuleAsyncOptions diff --git a/lib/services/listeners-explorer.service.ts b/lib/services/listeners-explorer.service.ts index a2348b8..ab3dfbf 100644 --- a/lib/services/listeners-explorer.service.ts +++ b/lib/services/listeners-explorer.service.ts @@ -162,7 +162,7 @@ export class ListenersExplorerService {}, ); - const steps = Object.values(group).map((stepsMetadata) => { + wizard.steps = Object.values(group).map((stepsMetadata) => { const composer = new Composer(); stepsMetadata.forEach((stepMethod) => { this.registerIfListener( @@ -175,8 +175,6 @@ export class ListenersExplorerService }); return composer.middleware(); }); - - wizard.steps = steps; } private registerIfListener( @@ -187,7 +185,8 @@ export class ListenersExplorerService defaultMetadata?: ListenerMetadata[], ): void { const methodRef = prototype[methodName]; - const metadata = this.metadataAccessor.getListenerMetadata(methodRef) || defaultMetadata; + const metadata = + this.metadataAccessor.getListenerMetadata(methodRef) || defaultMetadata; if (!metadata || metadata.length < 1) { return undefined; } @@ -222,7 +221,7 @@ export class ListenersExplorerService methodName: string, ) { const paramsFactory = this.telegrafParamsFactory; - const resolverCallback = this.externalContextCreator.create< + return this.externalContextCreator.create< Record, TelegrafContextType >( @@ -236,6 +235,5 @@ export class ListenersExplorerService undefined, 'telegraf', ); - return resolverCallback; } } diff --git a/lib/types/index.ts b/lib/types/index.ts index 8ce8c2f..c5cdd83 100644 --- a/lib/types/index.ts +++ b/lib/types/index.ts @@ -1,4 +1,4 @@ -import { Composer, Middleware, Telegraf } from 'telegraf'; +import { Composer, Middleware } from 'telegraf'; export type Filter = T extends [] ? [] @@ -14,5 +14,5 @@ export type OnlyFunctionPropertyNames = { export type ComposerMethodArgs< T extends Composer, - U extends OnlyFunctionPropertyNames = OnlyFunctionPropertyNames + U extends OnlyFunctionPropertyNames = OnlyFunctionPropertyNames, > = Filter, Middleware>; diff --git a/lib/utils/create-listener-decorator.util.ts b/lib/utils/create-listener-decorator.util.ts index 1b3cb9a..e42d7c6 100644 --- a/lib/utils/create-listener-decorator.util.ts +++ b/lib/utils/create-listener-decorator.util.ts @@ -19,8 +19,9 @@ export function createListenerDecorator< args, } as ListenerMetadata, ]; - - const previousValue = Reflect.getMetadata(LISTENERS_METADATA, descriptor.value) || []; + + const previousValue = + Reflect.getMetadata(LISTENERS_METADATA, descriptor.value) || []; const value = [...previousValue, ...metadata]; Reflect.defineMetadata(LISTENERS_METADATA, value, descriptor.value); return descriptor; diff --git a/lib/utils/param-decorator.util.ts b/lib/utils/param-decorator.util.ts index 6a2d261..e852d2c 100644 --- a/lib/utils/param-decorator.util.ts +++ b/lib/utils/param-decorator.util.ts @@ -5,8 +5,10 @@ import { PARAM_ARGS_METADATA } from '../telegraf.constants'; export type ParamData = object | string | number; -export const createTelegrafParamDecorator = (paramtype: TelegrafParamtype) => { - return (data?: ParamData): ParameterDecorator => (target, key, index) => { +export const createTelegrafParamDecorator = + (paramtype: TelegrafParamtype) => + (data?: ParamData): ParameterDecorator => + (target, key, index) => { const args = Reflect.getMetadata(PARAM_ARGS_METADATA, target.constructor, key) || {}; Reflect.defineMetadata( @@ -16,16 +18,16 @@ export const createTelegrafParamDecorator = (paramtype: TelegrafParamtype) => { key, ); }; -}; -export const createTelegrafPipesParamDecorator = ( - paramtype: TelegrafParamtype, -) => ( - data?: any, - ...pipes: (Type | PipeTransform)[] -): ParameterDecorator => (target, key, index) => { - addPipesMetadata(paramtype, data, pipes, target, key, index); -}; +export const createTelegrafPipesParamDecorator = + (paramtype: TelegrafParamtype) => + ( + data?: any, + ...pipes: (Type | PipeTransform)[] + ): ParameterDecorator => + (target, key, index) => { + addPipesMetadata(paramtype, data, pipes, target, key, index); + }; export const addPipesMetadata = ( paramtype: TelegrafParamtype, diff --git a/package-lock.json b/package-lock.json index 747dbe9..ece3b56 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5062,8 +5062,7 @@ "version": "5.3.2", "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz", "integrity": "sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==", - "dev": true, - "requires": {} + "dev": true }, "acorn-walk": { "version": "8.2.0", @@ -5620,8 +5619,7 @@ "version": "8.5.0", "resolved": "https://registry.npmjs.org/eslint-config-prettier/-/eslint-config-prettier-8.5.0.tgz", "integrity": "sha512-obmWKLUNCnhtQRKc+tmnYuQl0pFU1ibYJQ5BGhTVB08bHe9wC8qUeG7c08dj9XX+AuPj1YSGSQIHl1pnDHZR0Q==", - "dev": true, - "requires": {} + "dev": true }, "eslint-plugin-prettier": { "version": "4.2.1", From 92ff5164a67b6c9398c8aea1e3c28ff567626889 Mon Sep 17 00:00:00 2001 From: atassis Date: Sat, 9 Jul 2022 18:15:13 +0800 Subject: [PATCH 2/3] feat: create inject all bots decorator --- lib/decorators/core/index.ts | 1 + .../core/inject-all-bots.decorator.ts | 9 ++++++++ lib/services/base-explorer.service.ts | 3 +-- lib/telegraf-all-bots.provider.ts | 12 +++++++++++ lib/telegraf-core.module.ts | 21 ++++++++++++++++--- lib/telegraf.constants.ts | 2 ++ lib/utils/get-all-bots-token.util.ts | 3 +++ 7 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 lib/decorators/core/inject-all-bots.decorator.ts create mode 100644 lib/telegraf-all-bots.provider.ts create mode 100644 lib/utils/get-all-bots-token.util.ts diff --git a/lib/decorators/core/index.ts b/lib/decorators/core/index.ts index a982c9d..cd0c9b0 100644 --- a/lib/decorators/core/index.ts +++ b/lib/decorators/core/index.ts @@ -2,3 +2,4 @@ export * from './update.decorator'; export * from './scene.decorator'; export * from './wizard.decorator'; export * from './inject-bot.decorator'; +export * from './inject-all-bots.decorator'; diff --git a/lib/decorators/core/inject-all-bots.decorator.ts b/lib/decorators/core/inject-all-bots.decorator.ts new file mode 100644 index 0000000..2b6eb6f --- /dev/null +++ b/lib/decorators/core/inject-all-bots.decorator.ts @@ -0,0 +1,9 @@ +import { Inject } from '@nestjs/common'; +import { Telegraf } from 'telegraf'; + +import { getAllBotsToken } from '../../utils/get-all-bots-token.util'; + +export type AllBotsMap = Map>; + +export const InjectAllBots = (): ParameterDecorator => + Inject(getAllBotsToken()); diff --git a/lib/services/base-explorer.service.ts b/lib/services/base-explorer.service.ts index 2bfb50a..2c0ca17 100644 --- a/lib/services/base-explorer.service.ts +++ b/lib/services/base-explorer.service.ts @@ -10,8 +10,7 @@ export class BaseExplorerService { if (!include || isEmpty(include)) { return [...modulesContainer.values()]; } - const whitelisted = this.includeWhitelisted(modulesContainer, include); - return whitelisted; + return this.includeWhitelisted(modulesContainer, include); } includeWhitelisted( diff --git a/lib/telegraf-all-bots.provider.ts b/lib/telegraf-all-bots.provider.ts new file mode 100644 index 0000000..43df2e2 --- /dev/null +++ b/lib/telegraf-all-bots.provider.ts @@ -0,0 +1,12 @@ +import { Provider } from '@nestjs/common'; +import { Telegraf } from 'telegraf'; +import { TELEGRAF_ALL_BOTS } from './telegraf.constants'; + +export const allBotsMap = new Map>(); + +setTimeout(() => console.log(allBotsMap), 2000); + +export const telegrafAllBotsProvider: Provider = { + provide: TELEGRAF_ALL_BOTS, + useValue: allBotsMap, +}; diff --git a/lib/telegraf-core.module.ts b/lib/telegraf-core.module.ts index 9b4ab34..1a740e6 100644 --- a/lib/telegraf-core.module.ts +++ b/lib/telegraf-core.module.ts @@ -19,6 +19,10 @@ import { } from './telegraf.constants'; import { ListenersExplorerService, MetadataAccessorService } from './services'; import { telegrafStageProvider } from './stage.provider'; +import { + allBotsMap, + telegrafAllBotsProvider, +} from './telegraf-all-bots.provider'; import { createBotFactory, getBotToken } from './utils'; @Global() @@ -43,7 +47,11 @@ export class TelegrafCoreModule implements OnApplicationShutdown { const telegrafBotProvider: Provider = { provide: telegrafBotName, - useFactory: async () => await createBotFactory(options), + useFactory: async () => { + const bot = await createBotFactory(options); + allBotsMap.set(telegrafBotName, bot); + return bot; + }, }; return { @@ -56,11 +64,13 @@ export class TelegrafCoreModule implements OnApplicationShutdown { telegrafStageProvider, telegrafBotNameProvider, telegrafBotProvider, + telegrafAllBotsProvider, ], exports: [ telegrafStageProvider, telegrafBotNameProvider, telegrafBotProvider, + telegrafAllBotsProvider, ], }; } @@ -77,8 +87,11 @@ export class TelegrafCoreModule implements OnApplicationShutdown { const telegrafBotProvider: Provider = { provide: telegrafBotName, - useFactory: async (options: TelegrafModuleOptions) => - await createBotFactory(options), + useFactory: async (options: TelegrafModuleOptions) => { + const bot = await createBotFactory(options); + allBotsMap.set(telegrafBotName, bot); + return bot; + }, inject: [TELEGRAF_MODULE_OPTIONS], }; @@ -91,11 +104,13 @@ export class TelegrafCoreModule implements OnApplicationShutdown { telegrafStageProvider, telegrafBotNameProvider, telegrafBotProvider, + telegrafAllBotsProvider, ], exports: [ telegrafStageProvider, telegrafBotNameProvider, telegrafBotProvider, + telegrafAllBotsProvider, ], }; } diff --git a/lib/telegraf.constants.ts b/lib/telegraf.constants.ts index 26e4cd1..e5f824e 100644 --- a/lib/telegraf.constants.ts +++ b/lib/telegraf.constants.ts @@ -12,3 +12,5 @@ export const WIZARD_STEP_METADATA = 'WIZARD_STEP_METADATA'; export const PARAM_ARGS_METADATA = ROUTE_ARGS_METADATA; export const TELEGRAF_STAGE = 'TelegrafStage'; + +export const TELEGRAF_ALL_BOTS = 'TELEGRAF_ALL_BOTS'; diff --git a/lib/utils/get-all-bots-token.util.ts b/lib/utils/get-all-bots-token.util.ts new file mode 100644 index 0000000..cb17b0b --- /dev/null +++ b/lib/utils/get-all-bots-token.util.ts @@ -0,0 +1,3 @@ +import { TELEGRAF_ALL_BOTS } from '../telegraf.constants'; + +export const getAllBotsToken = (): string => TELEGRAF_ALL_BOTS; From 295ca4ffdc72621a3a98e009ba2efd9c78e59571 Mon Sep 17 00:00:00 2001 From: Taymuraz Kaytmazov Date: Sat, 9 Jul 2022 18:53:22 +0800 Subject: [PATCH 3/3] Update telegraf-all-bots.provider.ts --- lib/telegraf-all-bots.provider.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/telegraf-all-bots.provider.ts b/lib/telegraf-all-bots.provider.ts index 43df2e2..b60c242 100644 --- a/lib/telegraf-all-bots.provider.ts +++ b/lib/telegraf-all-bots.provider.ts @@ -4,8 +4,6 @@ import { TELEGRAF_ALL_BOTS } from './telegraf.constants'; export const allBotsMap = new Map>(); -setTimeout(() => console.log(allBotsMap), 2000); - export const telegrafAllBotsProvider: Provider = { provide: TELEGRAF_ALL_BOTS, useValue: allBotsMap,