UNPKG

8.37 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const constants_1 = require("../../constants");
4const route_paramtypes_enum_1 = require("../../enums/route-paramtypes.enum");
5const shared_utils_1 = require("../../utils/shared.utils");
6function assignMetadata(args, paramtype, index, data, ...pipes) {
7 return Object.assign(Object.assign({}, args), { [`${paramtype}:${index}`]: {
8 index,
9 data,
10 pipes,
11 } });
12}
13exports.assignMetadata = assignMetadata;
14const createRouteParamDecorator = (paramtype) => {
15 return (data) => (target, key, index) => {
16 const args = Reflect.getMetadata(constants_1.ROUTE_ARGS_METADATA, target.constructor, key) || {};
17 Reflect.defineMetadata(constants_1.ROUTE_ARGS_METADATA, assignMetadata(args, paramtype, index, data), target.constructor, key);
18 };
19};
20const createPipesRouteParamDecorator = (paramtype) => (data, ...pipes) => (target, key, index) => {
21 const args = Reflect.getMetadata(constants_1.ROUTE_ARGS_METADATA, target.constructor, key) || {};
22 const hasParamData = shared_utils_1.isNil(data) || shared_utils_1.isString(data);
23 const paramData = hasParamData ? data : undefined;
24 const paramPipes = hasParamData ? pipes : [data, ...pipes];
25 Reflect.defineMetadata(constants_1.ROUTE_ARGS_METADATA, assignMetadata(args, paramtype, index, paramData, ...paramPipes), target.constructor, key);
26};
27/**
28 * Route handler parameter decorator. Extracts the `Request`
29 * object from the underlying platform and populates the decorated
30 * parameter with the value of `Request`.
31 *
32 * Example: `logout(@Request() req)`
33 *
34 * @see [Request object](https://docs.nestjs.com/controllers#request-object)
35 *
36 * @publicApi
37 */
38exports.Request = createRouteParamDecorator(route_paramtypes_enum_1.RouteParamtypes.REQUEST);
39/**
40 * Route handler parameter decorator. Extracts the `Response`
41 * object from the underlying platform and populates the decorated
42 * parameter with the value of `Response`.
43 *
44 * Example: `logout(@Response() res)`
45 *
46 * @see [Request object](https://docs.nestjs.com/controllers#request-object)
47 *
48 * @publicApi
49 */
50exports.Response = createRouteParamDecorator(route_paramtypes_enum_1.RouteParamtypes.RESPONSE);
51/**
52 * Route handler parameter decorator. Extracts reference to the `Next` function
53 * from the underlying platform and populates the decorated
54 * parameter with the value of `Next`.
55 *
56 * @see [Request object](https://docs.nestjs.com/controllers#request-object)
57 *
58 * @publicApi
59 */
60exports.Next = createRouteParamDecorator(route_paramtypes_enum_1.RouteParamtypes.NEXT);
61/**
62 * Route handler parameter decorator. Extracts the `Ip` property
63 * from the `req` object and populates the decorated
64 * parameter with the value of `ip`.
65 *
66 * @see [Request object](https://docs.nestjs.com/controllers#request-object)
67 *
68 * @publicApi
69 */
70exports.Ip = createRouteParamDecorator(route_paramtypes_enum_1.RouteParamtypes.IP);
71/**
72 * Route handler parameter decorator. Extracts the `Session` object
73 * from the underlying platform and populates the decorated
74 * parameter with the value of `Session`.
75 *
76 * @see [Request object](https://docs.nestjs.com/controllers#request-object)
77 *
78 * @publicApi
79 */
80exports.Session = createRouteParamDecorator(route_paramtypes_enum_1.RouteParamtypes.SESSION);
81/**
82 * Route handler parameter decorator. Extracts the `file` object
83 * and populates the decorated parameter with the value of `file`.
84 * Used in conjunction with
85 * [multer middleware](https://github.com/expressjs/multer).
86 *
87 * For example:
88 * ```typescript
89 * uploadFile(@UploadedFile() file) {
90 * console.log(file);
91 * }
92 * ```
93 * @see [Request object](https://docs.nestjs.com/techniques/file-upload)
94 *
95 * @publicApi
96 */
97exports.UploadedFile = createRouteParamDecorator(route_paramtypes_enum_1.RouteParamtypes.FILE);
98/**
99 * Route handler parameter decorator. Extracts the `files` object
100 * and populates the decorated parameter with the value of `files`.
101 * Used in conjunction with
102 * [multer middleware](https://github.com/expressjs/multer).
103 *
104 * For example:
105 * ```typescript
106 * uploadFile(@UploadedFiles() files) {
107 * console.log(files);
108 * }
109 * ```
110 * @see [Request object](https://docs.nestjs.com/techniques/file-upload)
111 *
112 * @publicApi
113 */
114exports.UploadedFiles = createRouteParamDecorator(route_paramtypes_enum_1.RouteParamtypes.FILES);
115/**
116 * Route handler parameter decorator. Extracts the `headers`
117 * property from the `req` object and populates the decorated
118 * parameter with the value of `headers`.
119 *
120 * For example: `async update(@Headers('Cache-Control') cacheControl: string)`
121 *
122 * @param property name of single header property to extract.
123 *
124 * @see [Request object](https://docs.nestjs.com/controllers#request-object)
125 *
126 * @publicApi
127 */
128exports.Headers = createRouteParamDecorator(route_paramtypes_enum_1.RouteParamtypes.HEADERS);
129/**
130 * Route handler parameter decorator. Extracts the `query`
131 * property from the `req` object and populates the decorated
132 * parameter with the value of `query`. May also apply pipes to the bound
133 * query parameter.
134 *
135 * For example:
136 * ```typescript
137 * async find(@Query('user') user: string)
138 * ```
139 *
140 * @param property name of single property to extract from the `query` object
141 * @param pipes one or more pipes to apply to the bound query parameter
142 *
143 * @see [Request object](https://docs.nestjs.com/controllers#request-object)
144 *
145 * @publicApi
146 */
147function Query(property, ...pipes) {
148 return createPipesRouteParamDecorator(route_paramtypes_enum_1.RouteParamtypes.QUERY)(property, ...pipes);
149}
150exports.Query = Query;
151/**
152 * Route handler parameter decorator. Extracts the entire `body` object
153 * property, or optionally a named property of the `body` object, from
154 * the `req` object and populates the decorated parameter with that value.
155 * Also applies pipes to the bound body parameter.
156 *
157 * For example:
158 * ```typescript
159 * async create(@Body('role', new ValidationPipe()) role: string)
160 * ```
161 *
162 * @param property name of single property to extract from the `body` object
163 * @param pipes one or more pipes - either instances or classes - to apply to
164 * the bound body parameter.
165 *
166 * @see [Request object](https://docs.nestjs.com/controllers#request-object)
167 * @see [Working with pipes](https://docs.nestjs.com/custom-decorators#working-with-pipes)
168 *
169 * @publicApi
170 */
171function Body(property, ...pipes) {
172 return createPipesRouteParamDecorator(route_paramtypes_enum_1.RouteParamtypes.BODY)(property, ...pipes);
173}
174exports.Body = Body;
175/**
176 * Route handler parameter decorator. Extracts the `params`
177 * property from the `req` object and populates the decorated
178 * parameter with the value of `params`. May also apply pipes to the bound
179 * parameter.
180 *
181 * For example, extracting all params:
182 * ```typescript
183 * findOne(@Param() params: string[])
184 * ```
185 *
186 * For example, extracting a single param:
187 * ```typescript
188 * findOne(@Param('id') id: string)
189 * ```
190 * @param property name of single property to extract from the `req` object
191 * @param pipes one or more pipes - either instances or classes - to apply to
192 * the bound parameter.
193 *
194 * @see [Request object](https://docs.nestjs.com/controllers#request-object)
195 * @see [Working with pipes](https://docs.nestjs.com/custom-decorators#working-with-pipes)
196 *
197 * @publicApi
198 */
199function Param(property, ...pipes) {
200 return createPipesRouteParamDecorator(route_paramtypes_enum_1.RouteParamtypes.PARAM)(property, ...pipes);
201}
202exports.Param = Param;
203/**
204 * Route handler parameter decorator. Extracts the `hosts`
205 * property from the `req` object and populates the decorated
206 * parameter with the value of `params`. May also apply pipes to the bound
207 * parameter.
208 *
209 * For example, extracting all params:
210 * ```typescript
211 * findOne(@HostParam() params: string[])
212 * ```
213 *
214 * For example, extracting a single param:
215 * ```typescript
216 * findOne(@HostParam('id') id: string)
217 * ```
218 * @param property name of single property to extract from the `req` object
219 *
220 * @see [Request object](https://docs.nestjs.com/controllers#request-object)
221 *
222 * @publicApi
223 */
224function HostParam(property) {
225 return createRouteParamDecorator(route_paramtypes_enum_1.RouteParamtypes.HOST)(property);
226}
227exports.HostParam = HostParam;
228exports.Req = exports.Request;
229exports.Res = exports.Response;