UNPKG

@nestjs/common

Version:

Nest - modern, fast, powerful node.js web framework (@common)

622 lines (621 loc) 21.9 kB
import type { StandardSchemaV1 } from '@standard-schema/spec'; import { PipeTransform } from '../../index.js'; import { Type } from '../../interfaces/index.js'; /** * The options that can be passed to a handler's parameter decorator, such as `@Query()`, `@Body()`, and others. * These options allow you to specify a schema for validation and transformation, as well as any pipes to apply to the parameter. */ export interface ParameterDecoratorOptions { /** * The schema to use to retrieve within the pipes, * to, for example, validate the parameter against the schema or to apply transformations based on the schema. */ schema?: StandardSchemaV1; /** * The list of pipes to apply to the parameter. */ pipes?: (Type<PipeTransform> | PipeTransform)[]; } /** * The `@Response()`/`@Res` parameter decorator options. */ export interface ResponseDecoratorOptions { /** * Determines whether the response will be sent manually within the route handler, * with the use of native response handling methods exposed by the platform-specific response object, * or if it should passthrough Nest response processing pipeline. * * @default false */ passthrough: boolean; } export type ParamData = object | string | number; export interface RouteParamMetadata { index: number; data?: ParamData; } export declare function assignMetadata<TParamtype = any, TArgs = any>(args: TArgs, paramtype: TParamtype, index: number, options?: ({ data?: ParamData; } & ParameterDecoratorOptions) | ParamData, ...legacyPipes: (Type<PipeTransform> | PipeTransform)[]): TArgs & { [x: string]: { schema?: StandardSchemaV1<unknown, unknown> | undefined; index: number; data: ParamData | undefined; pipes: (PipeTransform<any, any> | Type<PipeTransform<any, any>>)[]; }; }; /** * Route handler parameter decorator. Extracts the `Request` * object from the underlying platform and populates the decorated * parameter with the value of `Request`. * * Example: `logout(@Request() req)` * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * * @publicApi */ export declare const Request: () => ParameterDecorator; /** * Route handler parameter decorator. Extracts the `Response` * object from the underlying platform and populates the decorated * parameter with the value of `Response`. * * Example: `logout(@Response() res)` * * @publicApi */ export declare const Response: (options?: ResponseDecoratorOptions) => ParameterDecorator; /** * Route handler parameter decorator. Extracts reference to the `Next` function * from the underlying platform and populates the decorated * parameter with the value of `Next`. * * @publicApi */ export declare const Next: () => ParameterDecorator; /** * Route handler parameter decorator. Extracts the `Ip` property * from the `req` object and populates the decorated * parameter with the value of `ip`. * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * * @publicApi */ export declare const Ip: () => ParameterDecorator; /** * Route handler parameter decorator. Extracts the `Session` object * from the underlying platform and populates the decorated * parameter with the value of `Session`. * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * * @publicApi */ export declare const Session: () => 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 Express-based applications. * * For example: * ```typescript * uploadFile(@UploadedFile() file) { * console.log(file); * } * ``` * @see [Request object](https://docs.nestjs.com/techniques/file-upload) * * @publicApi */ export declare 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 Express-based applications. * * For example: * ```typescript * uploadFile(@UploadedFile() file) { * console.log(file); * } * ``` * @see [Request object](https://docs.nestjs.com/techniques/file-upload) * * @publicApi */ export declare function UploadedFile(...pipes: (Type<PipeTransform> | 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 Express-based applications. * * For example: * ```typescript * uploadFile(@UploadedFile() file) { * console.log(file); * } * ``` * @see [Request object](https://docs.nestjs.com/techniques/file-upload) * * @publicApi */ export declare function UploadedFile(fileKey?: string, ...pipes: (Type<PipeTransform> | 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 Express-based applications. * * For example: * ```typescript * uploadFile(@UploadedFiles() files) { * console.log(files); * } * ``` * @see [Request object](https://docs.nestjs.com/techniques/file-upload) * * @publicApi */ export declare 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 Express-based applications. * * For example: * ```typescript * uploadFile(@UploadedFiles() files) { * console.log(files); * } * ``` * @see [Request object](https://docs.nestjs.com/techniques/file-upload) * * @publicApi */ export declare function UploadedFiles(...pipes: (Type<PipeTransform> | PipeTransform)[]): ParameterDecorator; /** * Route handler parameter decorator. Extracts the `headers` * property from the `req` object and populates the decorated * parameter with the value of `headers`. * * For example: `async update(@Headers('Cache-Control') cacheControl: string)` * * @param property name of single header property to extract. * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * * @publicApi */ export declare const Headers: (property?: string) => ParameterDecorator; /** * Route handler parameter decorator. Extracts the `query` * property from the `req` object and populates the decorated * parameter with the value of `query`. May also apply pipes to the bound * query parameter. * * For example: * ```typescript * async find(@Query('user') user: string) * ``` * * @param property name of single property to extract from the `query` object * @param pipes one or more pipes to apply to the bound query parameter * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * * @publicApi */ export declare function Query(): ParameterDecorator; /** * Route handler parameter decorator. Extracts the `query` * property from the `req` object and populates the decorated * parameter with the value of `query`. May also apply pipes to the bound * query parameter. * * For example: * ```typescript * async find(@Query('user') user: string) * ``` * * @param property name of single property to extract from the `query` object * @param pipes one or more pipes to apply to the bound query parameter * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * * @publicApi */ export declare function Query(...pipes: (Type<PipeTransform> | PipeTransform)[]): ParameterDecorator; /** * Route handler parameter decorator. Extracts the `query` * property from the `req` object and populates the decorated * parameter with the value of `query`. May also apply pipes to the bound * query parameter. * * For example: * ```typescript * async find(@Query('user') user: string) * ``` * * @param property name of single property to extract from the `query` object * @param pipes one or more pipes to apply to the bound query parameter * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * * @publicApi */ export declare function Query(property: string, ...pipes: (Type<PipeTransform> | PipeTransform)[]): ParameterDecorator; /** * Route handler parameter decorator. Extracts the `query` * property from the `req` object and populates the decorated * parameter with the value of `query`. May also apply pipes to the bound * query parameter. * * For example: * ```typescript * async find(@Query('user') user: string) * ``` * * @param property name of single property to extract from the `query` object * @param options options object containing additional configuration for the decorator, such as pipes and schema * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * * @publicApi */ export declare function Query(property: string, options: ParameterDecoratorOptions): ParameterDecorator; /** * Route handler parameter decorator. Extracts the `query` * property from the `req` object and populates the decorated * parameter with the value of `query`. May also apply pipes to the bound * query parameter. * * For example: * ```typescript * async find(@Query({ schema: z.object({ user: z.string() }) }) query) * ``` * * @param options options object containing additional configuration for the decorator, such as pipes and schema * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * * @publicApi */ export declare function Query(options: ParameterDecoratorOptions): ParameterDecorator; /** * Route handler parameter decorator. Extracts the entire `body` * object from the `req` object and populates the decorated * parameter with the value of `body`. * * For example: * ```typescript * async create(@Body() createDto: CreateCatDto) * ``` * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * * @publicApi */ export declare function Body(): ParameterDecorator; /** * Route handler parameter decorator. Extracts the entire `body` * object from the `req` object and populates the decorated * parameter with the value of `body`. Also applies the specified * pipes to that parameter. * * For example: * ```typescript * async create(@Body(new ValidationPipe()) createDto: CreateCatDto) * ``` * * @param pipes one or more pipes - either instances or classes - to apply to * the bound body parameter. * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * @see [Working with pipes](https://docs.nestjs.com/custom-decorators#working-with-pipes) * * @publicApi */ export declare function Body(...pipes: (Type<PipeTransform> | PipeTransform)[]): ParameterDecorator; /** * Route handler parameter decorator. Extracts the entire `body` object * property, or optionally a named property of the `body` object, from * the `req` object and populates the decorated parameter with that value. * * For example: * ```typescript * async create(@Body('role') role: string) * ``` * * @param options options to apply to the bound body parameter. * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * * @publicApi */ export declare function Body(options: ParameterDecoratorOptions): ParameterDecorator; /** * Route handler parameter decorator. Extracts a single property from * the `body` object property of the `req` object and populates the decorated * parameter with the value of that property. Also applies pipes to the bound * body parameter. * * For example: * ```typescript * async create(@Body('role', new ValidationPipe()) role: string) * ``` * * @param property name of single property to extract from the `body` object * @param pipes one or more pipes - either instances or classes - to apply to * the bound body parameter. * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * @see [Working with pipes](https://docs.nestjs.com/custom-decorators#working-with-pipes) * * @publicApi */ export declare function Body(property: string, ...pipes: (Type<PipeTransform> | PipeTransform)[]): ParameterDecorator; /** * Route handler parameter decorator. Extracts the entire `body` object * property, or optionally a named property of the `body` object, from * the `req` object and populates the decorated parameter with that value. * Also applies pipes to the bound body parameter. * * For example: * ```typescript * async create(@Body('role', new ValidationPipe()) role: string) * ``` * * @param property name of single property to extract from the `body` object * @param options options to apply to the bound body parameter. * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * @see [Working with pipes](https://docs.nestjs.com/custom-decorators#working-with-pipes) * * @publicApi */ export declare function Body(property: string, options: ParameterDecoratorOptions): ParameterDecorator; /** * Route handler parameter decorator. Extracts the `rawBody` Buffer * property from the `req` object and populates the decorated parameter with that value. * * For example: * ```typescript * async create(@RawBody() rawBody: Buffer | undefined) * ``` * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * @see [Raw body](https://docs.nestjs.com/faq/raw-body) * * @publicApi */ export declare function RawBody(): ParameterDecorator; /** * Route handler parameter decorator. Extracts the `rawBody` Buffer * property from the `req` object and populates the decorated parameter with that value. * Also applies pipes to the bound rawBody parameter. * * For example: * ```typescript * async create(@RawBody(new ValidationPipe()) rawBody: Buffer) * ``` * * @param pipes one or more pipes - either instances or classes - to apply to * the bound body parameter. * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * @see [Raw body](https://docs.nestjs.com/faq/raw-body) * @see [Working with pipes](https://docs.nestjs.com/custom-decorators#working-with-pipes) * * @publicApi */ export declare function RawBody(...pipes: (Type<PipeTransform<Buffer | undefined>> | PipeTransform<Buffer | undefined>)[]): ParameterDecorator; /** * Route handler parameter decorator. Extracts the `rawBody` Buffer * property from the `req` object and populates the decorated parameter with that value. * Also applies pipes to the bound rawBody parameter. * * For example: * ```typescript * async create(@RawBody({ schema: z.instanceof(Buffer) }) rawBody: Buffer) * ``` * * @param options options object containing additional configuration for the decorator, such as pipes and schema * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * @see [Raw body](https://docs.nestjs.com/faq/raw-body) * @see [Working with pipes](https://docs.nestjs.com/custom-decorators#working-with-pipes) * * @publicApi */ export declare function RawBody(options: ParameterDecoratorOptions): ParameterDecorator; /** * Route handler parameter decorator. Extracts the `params` * property from the `req` object and populates the decorated * parameter with the value of `params`. May also apply pipes to the bound * parameter. * * For example, extracting all params: * ```typescript * findOne(@Param() params: string[]) * ``` * * For example, extracting a single param: * ```typescript * findOne(@Param('id') id: string) * ``` * @param property name of single property to extract from the `req` object * @param pipes one or more pipes - either instances or classes - to apply to * the bound parameter. * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * @see [Working with pipes](https://docs.nestjs.com/custom-decorators#working-with-pipes) * * @publicApi */ export declare function Param(): ParameterDecorator; /** * Route handler parameter decorator. Extracts the `params` * property from the `req` object and populates the decorated * parameter with the value of `params`. May also apply pipes to the bound * parameter. * * For example, extracting all params: * ```typescript * findOne(@Param() params: string[]) * ``` * * For example, extracting a single param: * ```typescript * findOne(@Param('id') id: string) * ``` * @param property name of single property to extract from the `req` object * @param pipes one or more pipes - either instances or classes - to apply to * the bound parameter. * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * @see [Working with pipes](https://docs.nestjs.com/custom-decorators#working-with-pipes) * * @publicApi */ export declare function Param(...pipes: (Type<PipeTransform> | PipeTransform)[]): ParameterDecorator; /** * Route handler parameter decorator. Extracts the `params` * property from the `req` object and populates the decorated * parameter with the value of `params`. May also apply pipes to the bound * parameter. * * For example, extracting all params: * ```typescript * findOne(@Param() params: string[]) * ``` * * For example, extracting a single param: * ```typescript * findOne(@Param('id') id: string) * ``` * @param property name of single property to extract from the `req` object * @param pipes one or more pipes - either instances or classes - to apply to * the bound parameter. * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * @see [Working with pipes](https://docs.nestjs.com/custom-decorators#working-with-pipes) * * @publicApi */ export declare function Param(property: string, ...pipes: (Type<PipeTransform> | PipeTransform)[]): ParameterDecorator; /** * Route handler parameter decorator. Extracts the `params` * property from the `req` object and populates the decorated * parameter with the value of `params`. May also apply pipes to the bound * parameter. * * For example, extracting a single param: * ```typescript * findOne(@Param('id', { schema: z.string().uuid() }) id: string) * ``` * @param property name of single property to extract from the `req` object * @param options options object containing additional configuration for the decorator, such as pipes and schema * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * @see [Working with pipes](https://docs.nestjs.com/custom-decorators#working-with-pipes) * * @publicApi */ export declare function Param(property: string, options: ParameterDecoratorOptions): ParameterDecorator; /** * Route handler parameter decorator. Extracts the `params` * property from the `req` object and populates the decorated * parameter with the value of `params`. May also apply pipes to the bound * parameter. * * For example: * ```typescript * findOne(@Param({ schema: z.object({ id: z.string().uuid() }) }) params) * ``` * * @param options options object containing additional configuration for the decorator, such as pipes and schema * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * @see [Working with pipes](https://docs.nestjs.com/custom-decorators#working-with-pipes) * * @publicApi */ export declare function Param(options: ParameterDecoratorOptions): ParameterDecorator; /** * Route handler parameter decorator. Extracts the `hosts` * property from the `req` object and populates the decorated * parameter with the value of `hosts`. May also apply pipes to the bound * parameter. * * For example, extracting all params: * ```typescript * findOne(@HostParam() params: string[]) * ``` * * For example, extracting a single param: * ```typescript * findOne(@HostParam('id') id: string) * ``` * @param property name of single property to extract from the `req` object * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * * @publicApi */ export declare function HostParam(): ParameterDecorator; /** * Route handler parameter decorator. Extracts the `hosts` * property from the `req` object and populates the decorated * parameter with the value of `hosts`. May also apply pipes to the bound * parameter. * * For example, extracting all params: * ```typescript * findOne(@HostParam() params: string[]) * ``` * * For example, extracting a single param: * ```typescript * findOne(@HostParam('id') id: string) * ``` * @param property name of single property to extract from the `req` object * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * * @publicApi */ export declare function HostParam(property: string): ParameterDecorator; /** * Route handler parameter decorator. Extracts the `Request` * object from the underlying platform and populates the decorated * parameter with the value of `Request`. * * Alias for @Request(). * * Example: `logout(@Req() req)` * * @see [Request object](https://docs.nestjs.com/controllers#request-object) * * @publicApi */ export declare const Req: () => ParameterDecorator; /** * Route handler parameter decorator. Extracts the `Response` * object from the underlying platform and populates the decorated * parameter with the value of `Response`. * * Alias for @Response(). * * Example: `logout(@Res() res)` * * @see [Response object](https://docs.nestjs.com/controllers#response-object) * * @publicApi */ export declare const Res: (options?: ResponseDecoratorOptions) => ParameterDecorator;