From 2ceddd61c0d217c4cdf050264a8b8fdf7b14ff08 Mon Sep 17 00:00:00 2001 From: Luca Becker Date: Sun, 7 Feb 2021 10:08:53 +0100 Subject: [PATCH] feat(common): Allowing pipes for UploadedFile / UploadedFiles https://github.com/nestjs/nest/issues/4752 --- .../decorators/http/route-params.decorator.ts | 118 +++++++++++++++++- 1 file changed, 113 insertions(+), 5 deletions(-) diff --git a/packages/common/decorators/http/route-params.decorator.ts b/packages/common/decorators/http/route-params.decorator.ts index ef63b0e1467..2917ed7b4ed 100644 --- a/packages/common/decorators/http/route-params.decorator.ts +++ b/packages/common/decorators/http/route-params.decorator.ts @@ -179,9 +179,74 @@ export const Session: () => ParameterDecorator = createRouteParamDecorator( * * @publicApi */ -export const UploadedFile: ( +export function UploadedFile(): ParameterDecorator; + +/** + * Route handler parameter decorator. Extracts the `file` object + * and populates the decorated parameter with the value of `file`. + * Used in conjunction with + * [multer middleware](https://github.com/expressjs/multer). + * + * For example: + * ```typescript + * uploadFile(@UploadedFile() file) { + * console.log(file); + * } + * ``` + * @see [Request object](https://docs.nestjs.com/techniques/file-upload) + * + * @publicApi + */ +export function UploadedFile( + ...pipes: (Type | PipeTransform)[] +): ParameterDecorator; + +/** + * Route handler parameter decorator. Extracts the `file` object + * and populates the decorated parameter with the value of `file`. + * Used in conjunction with + * [multer middleware](https://github.com/expressjs/multer). + * + * For example: + * ```typescript + * uploadFile(@UploadedFile() file) { + * console.log(file); + * } + * ``` + * @see [Request object](https://docs.nestjs.com/techniques/file-upload) + * + * @publicApi + */ +export function UploadedFile( fileKey?: string, -) => ParameterDecorator = createRouteParamDecorator(RouteParamtypes.FILE); + ...pipes: (Type | PipeTransform)[] +): ParameterDecorator; + +/** + * Route handler parameter decorator. Extracts the `file` object + * and populates the decorated parameter with the value of `file`. + * Used in conjunction with + * [multer middleware](https://github.com/expressjs/multer). + * + * For example: + * ```typescript + * uploadFile(@UploadedFile() file) { + * console.log(file); + * } + * ``` + * @see [Request object](https://docs.nestjs.com/techniques/file-upload) + * + * @publicApi + */ +export function UploadedFile( + fileKey?: string | (Type | PipeTransform), + ...pipes: (Type | PipeTransform)[] +): ParameterDecorator { + return createPipesRouteParamDecorator(RouteParamtypes.FILE)( + fileKey, + ...pipes, + ); +} /** * Route handler parameter decorator. Extracts the `files` object @@ -199,9 +264,52 @@ export const UploadedFile: ( * * @publicApi */ -export const UploadedFiles: () => ParameterDecorator = createRouteParamDecorator( - RouteParamtypes.FILES, -); +export function UploadedFiles(): ParameterDecorator; + +/** + * Route handler parameter decorator. Extracts the `files` object + * and populates the decorated parameter with the value of `files`. + * Used in conjunction with + * [multer middleware](https://github.com/expressjs/multer). + * + * For example: + * ```typescript + * uploadFile(@UploadedFiles() files) { + * console.log(files); + * } + * ``` + * @see [Request object](https://docs.nestjs.com/techniques/file-upload) + * + * @publicApi + */ +export function UploadedFiles( + ...pipes: (Type | PipeTransform)[] +): ParameterDecorator; + +/** + * Route handler parameter decorator. Extracts the `files` object + * and populates the decorated parameter with the value of `files`. + * Used in conjunction with + * [multer middleware](https://github.com/expressjs/multer). + * + * For example: + * ```typescript + * uploadFile(@UploadedFiles() files) { + * console.log(files); + * } + * ``` + * @see [Request object](https://docs.nestjs.com/techniques/file-upload) + * + * @publicApi + */ +export function UploadedFiles( + ...pipes: (Type | PipeTransform)[] +): ParameterDecorator { + return createPipesRouteParamDecorator(RouteParamtypes.FILES)( + undefined, + ...pipes, + ); +} /** * Route handler parameter decorator. Extracts the `headers` * property from the `req` object and populates the decorated