refactor(wip): remove core module & change tokens

This commit is contained in:
unknown 2020-12-26 14:54:10 +03:00
parent c85460dc10
commit 80853be80c
26 changed files with 155 additions and 185 deletions

View File

@ -1,7 +1,7 @@
import { applyDecorators, SetMetadata } from '@nestjs/common'; import { applyDecorators, SetMetadata } from '@nestjs/common';
import { import {
TELEGRAF_LISTENER_OPTIONS, LISTENER_OPTIONS_METADATA,
TELEGRAF_LISTENER_TYPE, LISTENER_TYPE_METADATA,
} from '../../telegraf.constants'; } from '../../telegraf.constants';
import { ListenerType } from '../../enums/listener-type.enum'; import { ListenerType } from '../../enums/listener-type.enum';
import { TelegrafUpdateType } from '../../telegraf.types'; import { TelegrafUpdateType } from '../../telegraf.types';
@ -17,8 +17,8 @@ export interface OnOptions {
*/ */
export const On = (updateTypes: TelegrafUpdateType): MethodDecorator => { export const On = (updateTypes: TelegrafUpdateType): MethodDecorator => {
return applyDecorators( return applyDecorators(
SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.On), SetMetadata(LISTENER_TYPE_METADATA, ListenerType.On),
SetMetadata(TELEGRAF_LISTENER_OPTIONS, { SetMetadata(LISTENER_OPTIONS_METADATA, {
updateTypes, updateTypes,
} as OnOptions), } as OnOptions),
); );

View File

@ -1,8 +1,9 @@
import { SetMetadata } from '@nestjs/common'; import { SetMetadata } from '@nestjs/common';
import { TELEGRAF_UPDATE } from '../../telegraf.constants'; import { TELEGRAF_UPDATE_METADATA } from '../../telegraf.constants';
/** /**
* `@Update` decorator, it's like NestJS `@Controller` decorator, * `@Update` decorator, it's like NestJS `@Controller` decorator,
* but for Telegram Bot API updates. * but for Telegram Bot API updates.
*/ */
export const Update = (): ClassDecorator => SetMetadata(TELEGRAF_UPDATE, true); export const Update = (): ClassDecorator =>
SetMetadata(TELEGRAF_UPDATE_METADATA, true);

View File

@ -1,5 +1,5 @@
import { SetMetadata } from '@nestjs/common'; import { SetMetadata } from '@nestjs/common';
import { TELEGRAF_LISTENER_TYPE } from '../../telegraf.constants'; import { LISTENER_TYPE_METADATA } from '../../telegraf.constants';
import { ListenerType } from '../../enums/listener-type.enum'; import { ListenerType } from '../../enums/listener-type.enum';
/** /**
@ -8,5 +8,5 @@ import { ListenerType } from '../../enums/listener-type.enum';
* @see https://telegraf.js.org/#/?id=use * @see https://telegraf.js.org/#/?id=use
*/ */
export const Use = (): MethodDecorator => { export const Use = (): MethodDecorator => {
return SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Use); return SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Use);
}; };

View File

@ -1,4 +1,4 @@
import { Inject } from '@nestjs/common'; import { Inject } from '@nestjs/common';
import { TELEGRAF_PROVIDER } from '../telegraf.constants'; import { TelegrafProvider } from '../telegraf.provider';
export const InjectBot = (): ParameterDecorator => Inject(TELEGRAF_PROVIDER); export const InjectBot = (): ParameterDecorator => Inject(TelegrafProvider);

View File

@ -1,7 +1,7 @@
import { applyDecorators, SetMetadata } from '@nestjs/common'; import { applyDecorators, SetMetadata } from '@nestjs/common';
import { import {
TELEGRAF_LISTENER_OPTIONS, LISTENER_OPTIONS_METADATA,
TELEGRAF_LISTENER_TYPE, LISTENER_TYPE_METADATA,
} from '../../telegraf.constants'; } from '../../telegraf.constants';
import { TelegrafActionTriggers } from '../../telegraf.types'; import { TelegrafActionTriggers } from '../../telegraf.types';
import { ListenerType } from '../../enums/listener-type.enum'; import { ListenerType } from '../../enums/listener-type.enum';
@ -17,8 +17,8 @@ export interface ActionOptions {
*/ */
export const Action = (triggers: TelegrafActionTriggers): MethodDecorator => { export const Action = (triggers: TelegrafActionTriggers): MethodDecorator => {
return applyDecorators( return applyDecorators(
SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Action), SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Action),
SetMetadata(TELEGRAF_LISTENER_OPTIONS, { SetMetadata(LISTENER_OPTIONS_METADATA, {
triggers, triggers,
} as ActionOptions), } as ActionOptions),
); );

View File

@ -1,7 +1,7 @@
import { applyDecorators, SetMetadata } from '@nestjs/common'; import { applyDecorators, SetMetadata } from '@nestjs/common';
import { import {
TELEGRAF_LISTENER_OPTIONS, LISTENER_OPTIONS_METADATA,
TELEGRAF_LISTENER_TYPE, LISTENER_TYPE_METADATA,
} from '../../telegraf.constants'; } from '../../telegraf.constants';
import { TelegrafCashtag } from '../../telegraf.types'; import { TelegrafCashtag } from '../../telegraf.types';
import { ListenerType } from '../../enums/listener-type.enum'; import { ListenerType } from '../../enums/listener-type.enum';
@ -17,8 +17,8 @@ export interface CashtagOptions {
*/ */
export const Cashtag = (cashtag: TelegrafCashtag): MethodDecorator => { export const Cashtag = (cashtag: TelegrafCashtag): MethodDecorator => {
return applyDecorators( return applyDecorators(
SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Cashtag), SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Cashtag),
SetMetadata(TELEGRAF_LISTENER_OPTIONS, { SetMetadata(LISTENER_OPTIONS_METADATA, {
cashtag, cashtag,
} as CashtagOptions), } as CashtagOptions),
); );

View File

@ -1,7 +1,7 @@
import { applyDecorators, SetMetadata } from '@nestjs/common'; import { applyDecorators, SetMetadata } from '@nestjs/common';
import { import {
TELEGRAF_LISTENER_OPTIONS, LISTENER_OPTIONS_METADATA,
TELEGRAF_LISTENER_TYPE, LISTENER_TYPE_METADATA,
} from '../../telegraf.constants'; } from '../../telegraf.constants';
import { ListenerType } from '../../enums/listener-type.enum'; import { ListenerType } from '../../enums/listener-type.enum';
import { TelegrafCommand } from '../../telegraf.types'; import { TelegrafCommand } from '../../telegraf.types';
@ -17,8 +17,8 @@ export interface CommandOptions {
*/ */
export const Command = (command: TelegrafCommand): MethodDecorator => { export const Command = (command: TelegrafCommand): MethodDecorator => {
return applyDecorators( return applyDecorators(
SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Command), SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Command),
SetMetadata(TELEGRAF_LISTENER_OPTIONS, { SetMetadata(LISTENER_OPTIONS_METADATA, {
command: command, command: command,
} as CommandOptions), } as CommandOptions),
); );

View File

@ -1,7 +1,7 @@
import { applyDecorators, SetMetadata } from '@nestjs/common'; import { applyDecorators, SetMetadata } from '@nestjs/common';
import { import {
TELEGRAF_LISTENER_OPTIONS, LISTENER_OPTIONS_METADATA,
TELEGRAF_LISTENER_TYPE, LISTENER_TYPE_METADATA,
} from '../../telegraf.constants'; } from '../../telegraf.constants';
import { ListenerType } from '../../enums/listener-type.enum'; import { ListenerType } from '../../enums/listener-type.enum';
import { TelegrafEmail } from '../../telegraf.types'; import { TelegrafEmail } from '../../telegraf.types';
@ -17,8 +17,8 @@ export interface EmailOptions {
*/ */
export const Email = (email: TelegrafEmail): MethodDecorator => { export const Email = (email: TelegrafEmail): MethodDecorator => {
return applyDecorators( return applyDecorators(
SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Email), SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Email),
SetMetadata(TELEGRAF_LISTENER_OPTIONS, { SetMetadata(LISTENER_OPTIONS_METADATA, {
email, email,
} as EmailOptions), } as EmailOptions),
); );

View File

@ -1,5 +1,5 @@
import { SetMetadata } from '@nestjs/common'; import { SetMetadata } from '@nestjs/common';
import { TELEGRAF_LISTENER_TYPE } from '../../telegraf.constants'; import { LISTENER_TYPE_METADATA } from '../../telegraf.constants';
import { ListenerType } from '../../enums/listener-type.enum'; import { ListenerType } from '../../enums/listener-type.enum';
/** /**
@ -8,5 +8,5 @@ import { ListenerType } from '../../enums/listener-type.enum';
* @see https://telegraf.js.org/#/?id=inlinequery * @see https://telegraf.js.org/#/?id=inlinequery
*/ */
export const GameQuery = (): MethodDecorator => { export const GameQuery = (): MethodDecorator => {
return SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.GameQuery); return SetMetadata(LISTENER_TYPE_METADATA, ListenerType.GameQuery);
}; };

View File

@ -1,7 +1,7 @@
import { applyDecorators, SetMetadata } from '@nestjs/common'; import { applyDecorators, SetMetadata } from '@nestjs/common';
import { import {
TELEGRAF_LISTENER_OPTIONS, LISTENER_OPTIONS_METADATA,
TELEGRAF_LISTENER_TYPE, LISTENER_TYPE_METADATA,
} from '../../telegraf.constants'; } from '../../telegraf.constants';
import { ListenerType } from '../../enums/listener-type.enum'; import { ListenerType } from '../../enums/listener-type.enum';
import { TelegrafHashtag } from '../../telegraf.types'; import { TelegrafHashtag } from '../../telegraf.types';
@ -17,8 +17,8 @@ export interface HashtagOptions {
*/ */
export const Hashtag = (hashtag: TelegrafHashtag): MethodDecorator => { export const Hashtag = (hashtag: TelegrafHashtag): MethodDecorator => {
return applyDecorators( return applyDecorators(
SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Hashtag), SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Hashtag),
SetMetadata(TELEGRAF_LISTENER_OPTIONS, { SetMetadata(LISTENER_OPTIONS_METADATA, {
hashtag, hashtag,
} as HashtagOptions), } as HashtagOptions),
); );

View File

@ -1,7 +1,7 @@
import { applyDecorators, SetMetadata } from '@nestjs/common'; import { applyDecorators, SetMetadata } from '@nestjs/common';
import { import {
TELEGRAF_LISTENER_OPTIONS, LISTENER_OPTIONS_METADATA,
TELEGRAF_LISTENER_TYPE, LISTENER_TYPE_METADATA,
} from '../../telegraf.constants'; } from '../../telegraf.constants';
import { ListenerType } from '../../enums/listener-type.enum'; import { ListenerType } from '../../enums/listener-type.enum';
import { TelegrafHearsTriggers } from '../../telegraf.types'; import { TelegrafHearsTriggers } from '../../telegraf.types';
@ -17,8 +17,8 @@ export interface HearsOptions {
*/ */
export const Hears = (triggers: TelegrafHearsTriggers): MethodDecorator => { export const Hears = (triggers: TelegrafHearsTriggers): MethodDecorator => {
return applyDecorators( return applyDecorators(
SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Hears), SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Hears),
SetMetadata(TELEGRAF_LISTENER_OPTIONS, { SetMetadata(LISTENER_OPTIONS_METADATA, {
triggers, triggers,
} as HearsOptions), } as HearsOptions),
); );

View File

@ -1,5 +1,5 @@
import { SetMetadata } from '@nestjs/common'; import { SetMetadata } from '@nestjs/common';
import { TELEGRAF_LISTENER_TYPE } from '../../telegraf.constants'; import { LISTENER_TYPE_METADATA } from '../../telegraf.constants';
import { ListenerType } from '../../enums/listener-type.enum'; import { ListenerType } from '../../enums/listener-type.enum';
/** /**
@ -8,5 +8,5 @@ import { ListenerType } from '../../enums/listener-type.enum';
* @see https://telegraf.js.org/#/?id=help * @see https://telegraf.js.org/#/?id=help
*/ */
export const Help = (): MethodDecorator => { export const Help = (): MethodDecorator => {
return SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Help); return SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Help);
}; };

View File

@ -1,7 +1,7 @@
import { applyDecorators, SetMetadata } from '@nestjs/common'; import { applyDecorators, SetMetadata } from '@nestjs/common';
import { import {
TELEGRAF_LISTENER_OPTIONS, LISTENER_OPTIONS_METADATA,
TELEGRAF_LISTENER_TYPE, LISTENER_TYPE_METADATA,
} from '../../telegraf.constants'; } from '../../telegraf.constants';
import { ListenerType } from '../../enums/listener-type.enum'; import { ListenerType } from '../../enums/listener-type.enum';
import { TelegrafInlineQueryTriggers } from '../../telegraf.types'; import { TelegrafInlineQueryTriggers } from '../../telegraf.types';
@ -19,8 +19,8 @@ export const InlineQuery = (
triggers: TelegrafInlineQueryTriggers, triggers: TelegrafInlineQueryTriggers,
): MethodDecorator => { ): MethodDecorator => {
return applyDecorators( return applyDecorators(
SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.InlineQuery), SetMetadata(LISTENER_TYPE_METADATA, ListenerType.InlineQuery),
SetMetadata(TELEGRAF_LISTENER_OPTIONS, { SetMetadata(LISTENER_OPTIONS_METADATA, {
triggers, triggers,
} as InlineQueryOptions), } as InlineQueryOptions),
); );

View File

@ -1,7 +1,7 @@
import { applyDecorators, SetMetadata } from '@nestjs/common'; import { applyDecorators, SetMetadata } from '@nestjs/common';
import { import {
TELEGRAF_LISTENER_OPTIONS, LISTENER_OPTIONS_METADATA,
TELEGRAF_LISTENER_TYPE, LISTENER_TYPE_METADATA,
} from '../../telegraf.constants'; } from '../../telegraf.constants';
import { ListenerType } from '../../enums/listener-type.enum'; import { ListenerType } from '../../enums/listener-type.enum';
import { TelegrafMention } from '../../telegraf.types'; import { TelegrafMention } from '../../telegraf.types';
@ -17,8 +17,8 @@ export interface MentionOptions {
*/ */
export const Mention = (mention: TelegrafMention): MethodDecorator => { export const Mention = (mention: TelegrafMention): MethodDecorator => {
return applyDecorators( return applyDecorators(
SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Mention), SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Mention),
SetMetadata(TELEGRAF_LISTENER_OPTIONS, { SetMetadata(LISTENER_OPTIONS_METADATA, {
mention, mention,
} as MentionOptions), } as MentionOptions),
); );

View File

@ -1,7 +1,7 @@
import { applyDecorators, SetMetadata } from '@nestjs/common'; import { applyDecorators, SetMetadata } from '@nestjs/common';
import { import {
TELEGRAF_LISTENER_OPTIONS, LISTENER_OPTIONS_METADATA,
TELEGRAF_LISTENER_TYPE, LISTENER_TYPE_METADATA,
} from '../../telegraf.constants'; } from '../../telegraf.constants';
import { ListenerType } from '../../enums/listener-type.enum'; import { ListenerType } from '../../enums/listener-type.enum';
import { TelegrafPhone } from '../../telegraf.types'; import { TelegrafPhone } from '../../telegraf.types';
@ -17,8 +17,8 @@ export interface PhoneOptions {
*/ */
export const Phone = (phone: TelegrafPhone): MethodDecorator => { export const Phone = (phone: TelegrafPhone): MethodDecorator => {
return applyDecorators( return applyDecorators(
SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Phone), SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Phone),
SetMetadata(TELEGRAF_LISTENER_OPTIONS, { SetMetadata(LISTENER_OPTIONS_METADATA, {
phone, phone,
} as PhoneOptions), } as PhoneOptions),
); );

View File

@ -1,5 +1,5 @@
import { SetMetadata } from '@nestjs/common'; import { SetMetadata } from '@nestjs/common';
import { TELEGRAF_LISTENER_TYPE } from '../../telegraf.constants'; import { LISTENER_TYPE_METADATA } from '../../telegraf.constants';
import { ListenerType } from '../../enums/listener-type.enum'; import { ListenerType } from '../../enums/listener-type.enum';
/** /**
@ -8,5 +8,5 @@ import { ListenerType } from '../../enums/listener-type.enum';
* @see https://telegraf.js.org/#/?id=settings * @see https://telegraf.js.org/#/?id=settings
*/ */
export const Settings = (): MethodDecorator => { export const Settings = (): MethodDecorator => {
return SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Settings); return SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Settings);
}; };

View File

@ -1,5 +1,5 @@
import { SetMetadata } from '@nestjs/common'; import { SetMetadata } from '@nestjs/common';
import { TELEGRAF_LISTENER_TYPE } from '../../telegraf.constants'; import { LISTENER_TYPE_METADATA } from '../../telegraf.constants';
import { ListenerType } from '../../enums/listener-type.enum'; import { ListenerType } from '../../enums/listener-type.enum';
/** /**
@ -8,5 +8,5 @@ import { ListenerType } from '../../enums/listener-type.enum';
* @see https://telegraf.js.org/#/?id=start * @see https://telegraf.js.org/#/?id=start
*/ */
export const Start = (): MethodDecorator => { export const Start = (): MethodDecorator => {
return SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Start); return SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Start);
}; };

View File

@ -1,7 +1,7 @@
import { applyDecorators, SetMetadata } from '@nestjs/common'; import { applyDecorators, SetMetadata } from '@nestjs/common';
import { import {
TELEGRAF_LISTENER_OPTIONS, LISTENER_OPTIONS_METADATA,
TELEGRAF_LISTENER_TYPE, LISTENER_TYPE_METADATA,
} from '../../telegraf.constants'; } from '../../telegraf.constants';
import { ListenerType } from '../../enums/listener-type.enum'; import { ListenerType } from '../../enums/listener-type.enum';
import { TelegrafTextLink } from '../../telegraf.types'; import { TelegrafTextLink } from '../../telegraf.types';
@ -17,8 +17,8 @@ export interface TextLinkOptions {
*/ */
export const TetxLink = (link: TelegrafTextLink): MethodDecorator => { export const TetxLink = (link: TelegrafTextLink): MethodDecorator => {
return applyDecorators( return applyDecorators(
SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.TextLink), SetMetadata(LISTENER_TYPE_METADATA, ListenerType.TextLink),
SetMetadata(TELEGRAF_LISTENER_OPTIONS, { SetMetadata(LISTENER_OPTIONS_METADATA, {
link, link,
} as TextLinkOptions), } as TextLinkOptions),
); );

View File

@ -1,7 +1,7 @@
import { applyDecorators, SetMetadata } from '@nestjs/common'; import { applyDecorators, SetMetadata } from '@nestjs/common';
import { import {
TELEGRAF_LISTENER_OPTIONS, LISTENER_OPTIONS_METADATA,
TELEGRAF_LISTENER_TYPE, LISTENER_TYPE_METADATA,
} from '../../telegraf.constants'; } from '../../telegraf.constants';
import { ListenerType } from '../../enums/listener-type.enum'; import { ListenerType } from '../../enums/listener-type.enum';
import { TelegrafTextMention } from '../../telegraf.types'; import { TelegrafTextMention } from '../../telegraf.types';
@ -17,8 +17,8 @@ export interface TextMentionOptions {
*/ */
export const TetxMention = (mention: TelegrafTextMention): MethodDecorator => { export const TetxMention = (mention: TelegrafTextMention): MethodDecorator => {
return applyDecorators( return applyDecorators(
SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.TextMention), SetMetadata(LISTENER_TYPE_METADATA, ListenerType.TextMention),
SetMetadata(TELEGRAF_LISTENER_OPTIONS, { SetMetadata(LISTENER_OPTIONS_METADATA, {
mention, mention,
} as TextMentionOptions), } as TextMentionOptions),
); );

View File

@ -1,7 +1,7 @@
import { applyDecorators, SetMetadata } from '@nestjs/common'; import { applyDecorators, SetMetadata } from '@nestjs/common';
import { import {
TELEGRAF_LISTENER_OPTIONS, LISTENER_OPTIONS_METADATA,
TELEGRAF_LISTENER_TYPE, LISTENER_TYPE_METADATA,
} from '../../telegraf.constants'; } from '../../telegraf.constants';
import { ListenerType } from '../../enums/listener-type.enum'; import { ListenerType } from '../../enums/listener-type.enum';
import { TelegrafUrl } from '../../telegraf.types'; import { TelegrafUrl } from '../../telegraf.types';
@ -17,8 +17,8 @@ export interface UrlOptions {
*/ */
export const Url = (url: TelegrafUrl): MethodDecorator => { export const Url = (url: TelegrafUrl): MethodDecorator => {
return applyDecorators( return applyDecorators(
SetMetadata(TELEGRAF_LISTENER_TYPE, ListenerType.Url), SetMetadata(LISTENER_TYPE_METADATA, ListenerType.Url),
SetMetadata(TELEGRAF_LISTENER_OPTIONS, { SetMetadata(LISTENER_OPTIONS_METADATA, {
url, url,
} as UrlOptions), } as UrlOptions),
); );

View File

@ -1,11 +1,3 @@
export * as Composer from 'telegraf/composer';
export * as Markup from 'telegraf/markup';
export * as BaseScene from 'telegraf/scenes/base';
export * as session from 'telegraf/session';
export * as Stage from 'telegraf/stage';
export * as WizardScene from 'telegraf/scenes/wizard';
export * as Extra from 'telegraf/extra';
export * from './decorators'; export * from './decorators';
export * from './interfaces'; export * from './interfaces';
export * from './telegraf.module'; export * from './telegraf.module';

View File

@ -1,91 +0,0 @@
import { DiscoveryModule } from '@nestjs/core';
import { Module, DynamicModule, Provider, Type } from '@nestjs/common';
import {
TelegrafModuleOptions,
TelegrafModuleAsyncOptions,
TelegrafOptionsFactory,
} from './interfaces';
import {
TELEGRAF_MODULE_OPTIONS,
TELEGRAF_PROVIDER,
} from './telegraf.constants';
import { TelegrafMetadataAccessor } from './telegraf-metadata.accessor';
import { TelegrafExplorer } from './telegraf.explorer';
import { TelegrafProvider } from './telegraf.provider';
@Module({
imports: [DiscoveryModule],
providers: [TelegrafMetadataAccessor, TelegrafExplorer],
})
export class TelegrafCoreModule {
public static forRoot(options: TelegrafModuleOptions): DynamicModule {
const telegrafProvider = {
provide: TELEGRAF_PROVIDER,
useClass: TelegrafProvider,
inject: [TELEGRAF_MODULE_OPTIONS],
};
return {
module: TelegrafCoreModule,
providers: [
{ provide: TELEGRAF_MODULE_OPTIONS, useValue: options },
telegrafProvider,
],
exports: [telegrafProvider],
};
}
public static forRootAsync(
options: TelegrafModuleAsyncOptions,
): DynamicModule {
const telegrafProvider = {
provide: TELEGRAF_PROVIDER,
useClass: TelegrafProvider,
inject: [TELEGRAF_MODULE_OPTIONS],
};
const asyncProviders = this.createAsyncProviders(options);
return {
module: TelegrafCoreModule,
imports: options.imports,
providers: [...asyncProviders, telegrafProvider],
exports: [telegrafProvider],
};
}
private static createAsyncProviders(
options: TelegrafModuleAsyncOptions,
): Provider[] {
if (options.useExisting || options.useFactory) {
return [this.createAsyncOptionsProvider(options)];
}
const useClass = options.useClass as Type<TelegrafOptionsFactory>;
return [
this.createAsyncOptionsProvider(options),
{
provide: useClass,
useClass,
},
];
}
private static createAsyncOptionsProvider(
options: TelegrafModuleAsyncOptions,
): Provider {
if (options.useFactory) {
return {
provide: TELEGRAF_MODULE_OPTIONS,
useFactory: options.useFactory,
inject: options.inject || [],
};
}
// `as Type<TelegrafOptionsFactory>` is a workaround for microsoft/TypeScript#31603
const inject = [
(options.useClass || options.useExisting) as Type<TelegrafOptionsFactory>,
];
return {
provide: TELEGRAF_MODULE_OPTIONS,
useFactory: async (optionsFactory: TelegrafOptionsFactory) =>
await optionsFactory.createTelegrafOptions(),
inject,
};
}
}

View File

@ -1,17 +1,25 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { Reflector } from '@nestjs/core'; import { Reflector } from '@nestjs/core';
import { import {
TELEGRAF_LISTENER_TYPE, LISTENER_TYPE_METADATA,
TELEGRAF_MODULE_OPTIONS, TELEGRAF_MODULE_OPTIONS,
TELEGRAF_UPDATE_METADATA,
} from './telegraf.constants'; } from './telegraf.constants';
import { ListenerType } from './enums/listener-type.enum'; import { ListenerType } from './enums';
@Injectable() @Injectable()
export class TelegrafMetadataAccessor { export class TelegrafMetadataAccessor {
constructor(private readonly reflector: Reflector) {} constructor(private readonly reflector: Reflector) {}
isUpdate(target: Function): boolean {
return !!this.reflector.get<true | undefined>(
TELEGRAF_UPDATE_METADATA,
target,
);
}
getListenerType(target: Function): ListenerType | undefined { getListenerType(target: Function): ListenerType | undefined {
return this.reflector.get<ListenerType>(TELEGRAF_LISTENER_TYPE, target); return this.reflector.get<ListenerType>(LISTENER_TYPE_METADATA, target);
} }
getListenerOptions<T>(target: Function): unknown | undefined { getListenerOptions<T>(target: Function): unknown | undefined {

View File

@ -1,6 +1,5 @@
export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS'; export const TELEGRAF_MODULE_OPTIONS = 'TELEGRAF_MODULE_OPTIONS';
export const TELEGRAF_PROVIDER = 'TelegrafProvider';
export const TELEGRAF_UPDATE = 'TELEGRAF_UPDATE'; export const TELEGRAF_UPDATE_METADATA = 'TELEGRAF_UPDATE_METADATA';
export const TELEGRAF_LISTENER_TYPE = 'TELEGRAF_LISTENER_TYPE'; export const LISTENER_TYPE_METADATA = 'LISTENER_TYPE_METADATA';
export const TELEGRAF_LISTENER_OPTIONS = 'TELEGRAF_LISTENER_OPTIONS'; export const LISTENER_OPTIONS_METADATA = 'LISTENER_OPTIONS_METADATA';

View File

@ -3,8 +3,7 @@ import { DiscoveryService, ModuleRef } from '@nestjs/core';
import { MetadataScanner } from '@nestjs/core/metadata-scanner'; import { MetadataScanner } from '@nestjs/core/metadata-scanner';
import { TelegrafMetadataAccessor } from './telegraf-metadata.accessor'; import { TelegrafMetadataAccessor } from './telegraf-metadata.accessor';
import { TelegrafProvider } from './telegraf.provider'; import { TelegrafProvider } from './telegraf.provider';
import { TELEGRAF_PROVIDER } from './telegraf.constants'; import { ListenerType } from './enums';
import { ListenerType } from './enums/listener-type.enum';
import { import {
ActionOptions, ActionOptions,
CashtagOptions, CashtagOptions,
@ -33,7 +32,7 @@ export class TelegrafExplorer implements OnModuleInit {
private telegraf: TelegrafProvider; private telegraf: TelegrafProvider;
onModuleInit(): void { onModuleInit(): void {
this.telegraf = this.moduleRef.get<TelegrafProvider>(TELEGRAF_PROVIDER, { this.telegraf = this.moduleRef.get<TelegrafProvider>(TelegrafProvider, {
strict: false, strict: false,
}); });
this.explore(); this.explore();
@ -50,13 +49,18 @@ export class TelegrafExplorer implements OnModuleInit {
this.metadataScanner.scanFromPrototype( this.metadataScanner.scanFromPrototype(
instance, instance,
prototype, prototype,
(methodKey: string) => { (methodKey: string) => this.registerIfUpdate(instance, methodKey),
this.registerIfListener(instance, methodKey);
},
); );
}); });
} }
private registerIfUpdate(
instance: Record<string, Function>,
methodKey: string,
): void {
const isUpdate = this.metadataAccessor.isUpdate(instance);
}
private registerIfListener( private registerIfListener(
instance: Record<string, Function>, instance: Record<string, Function>,
methodKey: string, methodKey: string,

View File

@ -1,27 +1,84 @@
import { Module, DynamicModule } from '@nestjs/common'; import { DiscoveryModule } from '@nestjs/core';
import { TelegrafCoreModule } from './telegraf-core.module'; import { Module, DynamicModule, Provider } from '@nestjs/common';
import { import {
TelegrafModuleOptions, TelegrafModuleOptions,
TelegrafModuleAsyncOptions, TelegrafModuleAsyncOptions,
TelegrafOptionsFactory,
} from './interfaces'; } from './interfaces';
import { TELEGRAF_MODULE_OPTIONS } from './telegraf.constants';
import { TelegrafMetadataAccessor } from './telegraf-metadata.accessor';
import { TelegrafExplorer } from './telegraf.explorer';
import { TelegrafProvider } from './telegraf.provider';
@Module({}) @Module({
imports: [DiscoveryModule],
providers: [TelegrafMetadataAccessor, TelegrafExplorer],
})
export class TelegrafModule { export class TelegrafModule {
public static forRoot(options: TelegrafModuleOptions): DynamicModule { public static forRoot(options: TelegrafModuleOptions): DynamicModule {
const providers = [...this.createProviders(options), TelegrafProvider];
return { return {
module: TelegrafModule, module: TelegrafModule,
imports: [TelegrafCoreModule.forRoot(options)], providers,
exports: [TelegrafCoreModule], exports: providers,
}; };
} }
private static createProviders(options: TelegrafModuleOptions): Provider[] {
return [
{
provide: TELEGRAF_MODULE_OPTIONS,
useValue: options,
},
];
}
public static forRootAsync( public static forRootAsync(
options: TelegrafModuleAsyncOptions, options: TelegrafModuleAsyncOptions,
): DynamicModule { ): DynamicModule {
const providers = [...this.createAsyncProviders(options), TelegrafProvider];
return { return {
module: TelegrafModule, module: TelegrafModule,
imports: [TelegrafCoreModule.forRootAsync(options)], imports: options.imports || [],
exports: [TelegrafCoreModule], providers,
exports: providers,
};
}
private static createAsyncProviders(
options: TelegrafModuleAsyncOptions,
): Provider[] {
if (options.useExisting || options.useFactory) {
return [this.createAsyncOptionsProvider(options)];
}
return [
this.createAsyncOptionsProvider(options),
{
provide: options.useClass,
useClass: options.useClass,
},
];
}
private static createAsyncOptionsProvider(
options: TelegrafModuleAsyncOptions,
): Provider {
if (options.useFactory) {
return {
provide: TELEGRAF_MODULE_OPTIONS,
useFactory: options.useFactory,
inject: options.inject || [],
};
}
return {
provide: TELEGRAF_MODULE_OPTIONS,
useFactory: async (optionsFactory: TelegrafOptionsFactory) =>
await optionsFactory.createTelegrafOptions(),
inject: [options.useExisting || options.useClass],
}; };
} }
} }