/
queryParams.ts
96 lines (93 loc) · 2.72 KB
/
queryParams.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import {Type} from "@tsed/core";
import {ParamOptions} from "../domain/ParamOptions";
import {ParamTypes} from "../domain/ParamTypes";
import {mapParamsOptions} from "../utils/mapParamsOptions";
import {UseParam} from "./useParam";
/**
* QueryParams return the value from [request.query](http://expressjs.com/en/4x/api.html#req.query) object.
*
* #### Example
*
* ```typescript
* @Controller('/')
* class MyCtrl {
* @Get('/')
* get(@QueryParams() query: any) {
* console.log('Entire query', query);
* }
*
* @Get('/')
* get(@QueryParams('id') id: string) {
* console.log('ID', id);
* }
*
* @Get('/')
* get(@QueryParams('user') user: User) { // with deserialization
* console.log('user', user);
* }
*
* @Get('/')
* get(@QueryParams('users', User) users: User[]) { // with deserialization
* console.log('users', users);
* }
* }
* ```
* > For more information on deserialization see [converters](/docs/converters.md) page.
*
* @param expression The path of the property to get.
* @param useType The type of the class that to be used to deserialize the data.
* @decorator
* @operation
* @input
*/
export function QueryParams(expression: string, useType: Type<any>): ParameterDecorator;
export function QueryParams(expression: string): ParameterDecorator;
export function QueryParams(useType: Type<any>): ParameterDecorator;
export function QueryParams(options: Partial<ParamOptions>): ParameterDecorator;
export function QueryParams(): ParameterDecorator;
export function QueryParams(...args: any[]): ParameterDecorator {
const {expression, useType, useConverter = true, useValidation = true} = mapParamsOptions(args);
return UseParam({
paramType: ParamTypes.QUERY,
dataPath: "$ctx.request.query",
expression,
useType,
useConverter,
useValidation
});
}
/**
* RawQueryParams return the value from [request.query](http://expressjs.com/en/4x/api.html#req.query) object.
*
* Any validation and transformation are performed on the value. Use [pipes](/docs/pipes.html) to validate and/or transform the value.
*
* #### Example
*
* ```typescript
* @Controller('/')
* class MyCtrl {
* @Get('/')
* get(@RawPathParams() params: any) {
* console.log('Entire params', params);
* }
*
* @Get('/')
* get(@RawPathParams('id') id: string) {
* console.log('ID', id);
* }
* }
* ```
* > For more information on deserialization see [converters](/docs/converters.md) page.
*
* @param expression The path of the property to get.
* @decorator
* @operation
* @input
*/
export function RawQueryParams(expression: string) {
return UseParam({
paramType: ParamTypes.QUERY,
dataPath: "$ctx.request.query",
expression
});
}