mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
feat(websockets): messagebody param extract
Add ability to extract a paramater from the MessageBody decorator.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { PipeTransform, Type } from '@nestjs/common';
|
||||
import { assignMetadata } from '@nestjs/common/decorators/http/route-params.decorator';
|
||||
import { isNil, isString } from '@nestjs/common/utils/shared.utils';
|
||||
import 'reflect-metadata';
|
||||
import { PARAM_ARGS_METADATA } from '../constants';
|
||||
import { WsParamtype } from '../enums/ws-paramtype.enum';
|
||||
@@ -24,14 +25,18 @@ export function createWsParamDecorator(
|
||||
}
|
||||
|
||||
export const createPipesWsParamDecorator = (paramtype: WsParamtype) => (
|
||||
data?: any,
|
||||
...pipes: (Type<PipeTransform> | PipeTransform)[]
|
||||
): ParameterDecorator => (target, key, index) => {
|
||||
const args =
|
||||
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(
|
||||
PARAM_ARGS_METADATA,
|
||||
assignMetadata(args, paramtype, index, undefined, ...pipes),
|
||||
assignMetadata(args, paramtype, index, paramData, ...paramPipes),
|
||||
target.constructor,
|
||||
key,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user