UNPKG

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