mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-09-23 09:49:06 +03:00
refactor(): use ExternalContextCreator
This commit is contained in:
@@ -1,39 +1,86 @@
|
||||
import { assignMetadata, PipeTransform, Type } from '@nestjs/common';
|
||||
import { isNil, isString } from '@nestjs/common/utils/shared.utils';
|
||||
import { TelegrafParamtype } from '../enums/telegraf-paramtype.enum';
|
||||
import { LISTENER_ARGS_METADATA } from '../telegraf.constants';
|
||||
import { PARAM_ARGS_METADATA } from '../telegraf.constants';
|
||||
|
||||
export function createTelegrafParamDecorator(
|
||||
paramtype: TelegrafParamtype,
|
||||
): (...pipes: (Type<PipeTransform> | PipeTransform)[]) => ParameterDecorator {
|
||||
return (...pipes: (Type<PipeTransform> | PipeTransform)[]) => (
|
||||
target,
|
||||
key,
|
||||
index,
|
||||
) => {
|
||||
export type ParamData = object | string | number;
|
||||
|
||||
export const createTelegrafParamDecorator = (paramtype: TelegrafParamtype) => {
|
||||
return (data?: ParamData): ParameterDecorator => (target, key, index) => {
|
||||
const args =
|
||||
Reflect.getMetadata(LISTENER_ARGS_METADATA, target.constructor, key) ||
|
||||
{};
|
||||
Reflect.getMetadata(PARAM_ARGS_METADATA, target.constructor, key) || {};
|
||||
Reflect.defineMetadata(
|
||||
LISTENER_ARGS_METADATA,
|
||||
assignMetadata(args, paramtype, index, undefined, ...pipes),
|
||||
PARAM_ARGS_METADATA,
|
||||
assignMetadata(args, paramtype, index, data),
|
||||
target.constructor,
|
||||
key,
|
||||
);
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export const createPipesTelegrafParamDecorator = (
|
||||
export const createTelegrafPipesParamDecorator = (
|
||||
paramtype: TelegrafParamtype,
|
||||
) => (
|
||||
data?: any,
|
||||
...pipes: (Type<PipeTransform> | PipeTransform)[]
|
||||
): ParameterDecorator => (target, key, index) => {
|
||||
addPipesMetadata(paramtype, data, pipes, target, key, index);
|
||||
};
|
||||
|
||||
export const addPipesMetadata = (
|
||||
paramtype: TelegrafParamtype,
|
||||
data: any,
|
||||
pipes: (Type<PipeTransform> | PipeTransform)[],
|
||||
target: Record<string, any>,
|
||||
key: string | symbol,
|
||||
index: number,
|
||||
) => {
|
||||
const args =
|
||||
Reflect.getMetadata(LISTENER_ARGS_METADATA, target.constructor, key) || {};
|
||||
Reflect.getMetadata(PARAM_ARGS_METADATA, target.constructor, key) || {};
|
||||
const hasParamData = isNil(data) || isString(data);
|
||||
const paramData = hasParamData ? data : undefined;
|
||||
const paramPipes = hasParamData ? pipes : [data, ...pipes];
|
||||
|
||||
Reflect.defineMetadata(
|
||||
LISTENER_ARGS_METADATA,
|
||||
assignMetadata(args, paramtype, index, undefined, ...pipes),
|
||||
PARAM_ARGS_METADATA,
|
||||
assignMetadata(args, paramtype, index, paramData, ...paramPipes),
|
||||
target.constructor,
|
||||
key,
|
||||
);
|
||||
};
|
||||
|
||||
// export function createTelegrafParamDecorator(
|
||||
// paramtype: TelegrafParamtype,
|
||||
// ): (...pipes: (Type<PipeTransform> | PipeTransform)[]) => ParameterDecorator {
|
||||
// return (...pipes: (Type<PipeTransform> | PipeTransform)[]) => (
|
||||
// target,
|
||||
// key,
|
||||
// index,
|
||||
// ) => {
|
||||
// const args =
|
||||
// Reflect.getMetadata(LISTENER_ARGS_METADATA, target.constructor, key) ||
|
||||
// {};
|
||||
// Reflect.defineMetadata(
|
||||
// LISTENER_ARGS_METADATA,
|
||||
// assignMetadata(args, paramtype, index, undefined, ...pipes),
|
||||
// target.constructor,
|
||||
// key,
|
||||
// );
|
||||
// };
|
||||
// }
|
||||
//
|
||||
// export const createPipesTelegrafParamDecorator = (
|
||||
// paramtype: TelegrafParamtype,
|
||||
// ) => (
|
||||
// ...pipes: (Type<PipeTransform> | PipeTransform)[]
|
||||
// ): ParameterDecorator => (target, key, index) => {
|
||||
// const args =
|
||||
// Reflect.getMetadata(LISTENER_ARGS_METADATA, target.constructor, key) || {};
|
||||
//
|
||||
// Reflect.defineMetadata(
|
||||
// LISTENER_ARGS_METADATA,
|
||||
// assignMetadata(args, paramtype, index, undefined, ...pipes),
|
||||
// target.constructor,
|
||||
// key,
|
||||
// );
|
||||
// };
|
||||
|
Reference in New Issue
Block a user