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