mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-09-23 09:49:06 +03:00
feat!(): add custom execution context
This commit is contained in:
3
lib/execution-context/index.ts
Normal file
3
lib/execution-context/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from './tg-arguments-host.interace';
|
||||
export * from './telegraf-arguments-host';
|
||||
export * from './telegraf-arguments-host';
|
22
lib/execution-context/telegraf-arguments-host.ts
Normal file
22
lib/execution-context/telegraf-arguments-host.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { ArgumentsHost } from '@nestjs/common';
|
||||
import { ExecutionContextHost } from '@nestjs/core/helpers/execution-context-host';
|
||||
import { TgArgumentsHost } from './tg-arguments-host.interace';
|
||||
|
||||
export class TelegrafArgumentsHost
|
||||
extends ExecutionContextHost
|
||||
implements TgArgumentsHost {
|
||||
static create(context: ArgumentsHost): TelegrafArgumentsHost {
|
||||
const type = context.getType();
|
||||
const tgContext = new TelegrafArgumentsHost(context.getArgs());
|
||||
tgContext.setType(type);
|
||||
return tgContext;
|
||||
}
|
||||
|
||||
getContext<T = any>(): T {
|
||||
return this.getArgByIndex(0);
|
||||
}
|
||||
|
||||
getNext<T = any>(): T {
|
||||
return this.getArgByIndex(1);
|
||||
}
|
||||
}
|
32
lib/execution-context/telegraf-execution-context.ts
Normal file
32
lib/execution-context/telegraf-execution-context.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { ContextType, ExecutionContext } from '@nestjs/common';
|
||||
import { ExecutionContextHost } from '@nestjs/core/helpers/execution-context-host';
|
||||
import { TgArgumentsHost } from './tg-arguments-host.interace';
|
||||
|
||||
export type TelegrafContextType = 'telegraf' | ContextType;
|
||||
|
||||
export class TelegrafExecutionContext
|
||||
extends ExecutionContextHost
|
||||
implements TgArgumentsHost {
|
||||
static create(context: ExecutionContext): TelegrafExecutionContext {
|
||||
const type = context.getType();
|
||||
const tgContext = new TelegrafExecutionContext(
|
||||
context.getArgs(),
|
||||
context.getClass(),
|
||||
context.getHandler(),
|
||||
);
|
||||
tgContext.setType(type);
|
||||
return tgContext;
|
||||
}
|
||||
|
||||
getType<TContext extends string = TelegrafContextType>(): TContext {
|
||||
return super.getType();
|
||||
}
|
||||
|
||||
getContext<T = any>(): T {
|
||||
return this.getArgByIndex(0);
|
||||
}
|
||||
|
||||
getNext<T = any>(): T {
|
||||
return this.getArgByIndex(0);
|
||||
}
|
||||
}
|
6
lib/execution-context/tg-arguments-host.interace.ts
Normal file
6
lib/execution-context/tg-arguments-host.interace.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { ArgumentsHost } from '@nestjs/common';
|
||||
|
||||
export interface TgArgumentsHost extends ArgumentsHost {
|
||||
getContext<T = any>(): T;
|
||||
getNext<T = any>(): T;
|
||||
}
|
Reference in New Issue
Block a user