2021-01-03 01:30:57 +03:00
|
|
|
import { assignMetadata, PipeTransform, Type } from '@nestjs/common';
|
2021-01-03 02:39:50 +03:00
|
|
|
import { isNil, isString } from '@nestjs/common/utils/shared.utils';
|
2021-01-03 01:30:57 +03:00
|
|
|
import { TelegrafParamtype } from '../enums/telegraf-paramtype.enum';
|
2021-01-03 02:39:50 +03:00
|
|
|
import { PARAM_ARGS_METADATA } from '../telegraf.constants';
|
2021-01-03 01:30:57 +03:00
|
|
|
|
2021-01-03 02:39:50 +03:00
|
|
|
export type ParamData = object | string | number;
|
|
|
|
|
2022-07-09 12:40:49 +03:00
|
|
|
export const createTelegrafParamDecorator =
|
|
|
|
(paramtype: TelegrafParamtype) =>
|
|
|
|
(data?: ParamData): ParameterDecorator =>
|
|
|
|
(target, key, index) => {
|
2021-01-03 01:30:57 +03:00
|
|
|
const args =
|
2021-01-03 02:39:50 +03:00
|
|
|
Reflect.getMetadata(PARAM_ARGS_METADATA, target.constructor, key) || {};
|
2021-01-03 01:30:57 +03:00
|
|
|
Reflect.defineMetadata(
|
2021-01-03 02:39:50 +03:00
|
|
|
PARAM_ARGS_METADATA,
|
|
|
|
assignMetadata(args, paramtype, index, data),
|
2021-01-03 01:30:57 +03:00
|
|
|
target.constructor,
|
|
|
|
key,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-07-09 12:40:49 +03:00
|
|
|
export const createTelegrafPipesParamDecorator =
|
|
|
|
(paramtype: TelegrafParamtype) =>
|
|
|
|
(
|
|
|
|
data?: any,
|
|
|
|
...pipes: (Type<PipeTransform> | PipeTransform)[]
|
|
|
|
): ParameterDecorator =>
|
|
|
|
(target, key, index) => {
|
|
|
|
addPipesMetadata(paramtype, data, pipes, target, key, index);
|
|
|
|
};
|
2021-01-03 02:39:50 +03:00
|
|
|
|
|
|
|
export const addPipesMetadata = (
|
|
|
|
paramtype: TelegrafParamtype,
|
|
|
|
data: any,
|
|
|
|
pipes: (Type<PipeTransform> | PipeTransform)[],
|
|
|
|
target: Record<string, any>,
|
|
|
|
key: string | symbol,
|
|
|
|
index: number,
|
|
|
|
) => {
|
2021-01-03 01:30:57 +03:00
|
|
|
const args =
|
2021-01-03 02:39:50 +03:00
|
|
|
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];
|
2021-01-03 01:30:57 +03:00
|
|
|
|
|
|
|
Reflect.defineMetadata(
|
2021-01-03 02:39:50 +03:00
|
|
|
PARAM_ARGS_METADATA,
|
|
|
|
assignMetadata(args, paramtype, index, paramData, ...paramPipes),
|
2021-01-03 01:30:57 +03:00
|
|
|
target.constructor,
|
|
|
|
key,
|
|
|
|
);
|
|
|
|
};
|