@tsed/platform-params 8.0.1 → 8.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/types/decorators/locals.d.ts +1 -1
- package/package.json +14 -13
- package/src/builder/PlatformParams.spec.ts +431 -0
- package/src/builder/PlatformParams.ts +86 -0
- package/src/decorators/bodyParams.spec.ts +121 -0
- package/src/decorators/bodyParams.ts +93 -0
- package/src/decorators/context.spec.ts +16 -0
- package/src/decorators/context.ts +70 -0
- package/src/decorators/cookies.spec.ts +19 -0
- package/src/decorators/cookies.ts +102 -0
- package/src/decorators/headerParams.spec.ts +16 -0
- package/src/decorators/headerParams.ts +49 -0
- package/src/decorators/locals.spec.ts +16 -0
- package/src/decorators/locals.ts +57 -0
- package/src/decorators/paramFn.ts +14 -0
- package/src/decorators/pathParams.spec.ts +58 -0
- package/src/decorators/pathParams.ts +97 -0
- package/src/decorators/queryParams.spec.ts +58 -0
- package/src/decorators/queryParams.ts +102 -0
- package/src/decorators/session.spec.ts +47 -0
- package/src/decorators/session.ts +51 -0
- package/src/decorators/useDeserialization.ts +14 -0
- package/src/decorators/useParam.spec.ts +82 -0
- package/src/decorators/useParam.ts +38 -0
- package/src/decorators/useParamExpression.ts +16 -0
- package/src/decorators/useParamType.ts +36 -0
- package/src/decorators/usePipe.ts +21 -0
- package/src/decorators/useType.ts +35 -0
- package/src/decorators/useValidation.ts +18 -0
- package/src/domain/ParamMetadata.ts +1 -0
- package/src/domain/ParamOptions.ts +12 -0
- package/src/domain/ParamTypes.ts +37 -0
- package/src/errors/ParamValidationError.spec.ts +152 -0
- package/src/errors/ParamValidationError.ts +38 -0
- package/src/errors/RequiredValidationError.spec.ts +49 -0
- package/src/errors/RequiredValidationError.ts +30 -0
- package/src/errors/ValidationError.spec.ts +18 -0
- package/src/errors/ValidationError.ts +11 -0
- package/src/exports.ts +1 -0
- package/src/index.ts +31 -0
- package/src/pipes/DeserializerPipe.spec.ts +39 -0
- package/src/pipes/DeserializerPipe.ts +15 -0
- package/src/pipes/ParseExpressionPipe.spec.ts +242 -0
- package/src/pipes/ParseExpressionPipe.ts +33 -0
- package/src/pipes/ValidationPipe.spec.ts +645 -0
- package/src/pipes/ValidationPipe.ts +112 -0
- package/src/utils/mapParamsOptions.spec.ts +28 -0
- package/src/utils/mapParamsOptions.ts +34 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {Type} from "@tsed/core";
|
|
2
|
+
|
|
3
|
+
import {ParamOptions} from "../domain/ParamOptions.js";
|
|
4
|
+
import {ParamTypes} from "../domain/ParamTypes.js";
|
|
5
|
+
import {mapParamsOptions} from "../utils/mapParamsOptions.js";
|
|
6
|
+
import {UseParam} from "./useParam.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* BodyParams return the value from [request.body](http://expressjs.com/en/4x/api.html#req.body) object.
|
|
10
|
+
*
|
|
11
|
+
* #### Example
|
|
12
|
+
*
|
|
13
|
+
* ```typescript
|
|
14
|
+
* @Controller('/')
|
|
15
|
+
* class MyCtrl {
|
|
16
|
+
* @Post('/')
|
|
17
|
+
* create(@BodyParams() body: any) {
|
|
18
|
+
* console.log('Entire body', body);
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* @Post('/')
|
|
22
|
+
* create(@BodyParams('id') id: string) {
|
|
23
|
+
* console.log('ID', id);
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* @Post('/')
|
|
27
|
+
* create(@BodyParams('user') user: User) { // with deserialization
|
|
28
|
+
* console.log('user', user);
|
|
29
|
+
* }
|
|
30
|
+
*
|
|
31
|
+
* @Post('/')
|
|
32
|
+
* create(@BodyParams('users', User) users: User[]) { // with deserialization
|
|
33
|
+
* console.log('users', users);
|
|
34
|
+
* }
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
* > For more information on deserialization see [converters](/docs/json-mapper.md) page.
|
|
38
|
+
*
|
|
39
|
+
* @param expression The path of the property to get.
|
|
40
|
+
* @param useType The type of the class that to be used to deserialize the data.
|
|
41
|
+
* @decorator
|
|
42
|
+
* @operation
|
|
43
|
+
* @input
|
|
44
|
+
*/
|
|
45
|
+
export function BodyParams(expression: string, useType: Type<any>): ParameterDecorator;
|
|
46
|
+
export function BodyParams(expression: string): ParameterDecorator;
|
|
47
|
+
export function BodyParams(useType: Type<any>): ParameterDecorator;
|
|
48
|
+
export function BodyParams(options: Partial<ParamOptions>): ParameterDecorator;
|
|
49
|
+
export function BodyParams(): ParameterDecorator;
|
|
50
|
+
export function BodyParams(...args: any[]): ParameterDecorator {
|
|
51
|
+
const {expression, useType, useMapper = true, useValidation = true} = mapParamsOptions(args);
|
|
52
|
+
|
|
53
|
+
return UseParam({
|
|
54
|
+
paramType: ParamTypes.BODY,
|
|
55
|
+
expression,
|
|
56
|
+
useType,
|
|
57
|
+
useMapper,
|
|
58
|
+
useValidation
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* RawBodyParams return the value from [request.body](http://expressjs.com/en/4x/api.html#req.body) as a Buffer.
|
|
64
|
+
*
|
|
65
|
+
* #### Example
|
|
66
|
+
*
|
|
67
|
+
* ```typescript
|
|
68
|
+
* @Controller('/')
|
|
69
|
+
* class MyCtrl {
|
|
70
|
+
* @Post('/')
|
|
71
|
+
* create(@RawBodyParams() body: Buffer) {
|
|
72
|
+
* console.log('Entire body', body.toString("utf8"));
|
|
73
|
+
* }
|
|
74
|
+
* }
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* @decorator
|
|
78
|
+
* @operation
|
|
79
|
+
* @input
|
|
80
|
+
* @alias BodyParams Example: @BodyParams() payload: Buffer
|
|
81
|
+
*/
|
|
82
|
+
export function RawBodyParams(options: Partial<Omit<ParamOptions, "expression">>): ParameterDecorator;
|
|
83
|
+
export function RawBodyParams(): ParameterDecorator;
|
|
84
|
+
export function RawBodyParams(...args: any[]): ParameterDecorator {
|
|
85
|
+
const {useType, useMapper = false, useValidation = false} = mapParamsOptions(args);
|
|
86
|
+
|
|
87
|
+
return UseParam({
|
|
88
|
+
paramType: ParamTypes.RAW_BODY,
|
|
89
|
+
useType,
|
|
90
|
+
useMapper,
|
|
91
|
+
useValidation
|
|
92
|
+
});
|
|
93
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {JsonParameterStore} from "@tsed/schema";
|
|
2
|
+
|
|
3
|
+
import {ParamTypes} from "../domain/ParamTypes.js";
|
|
4
|
+
import {Context} from "./context.js";
|
|
5
|
+
|
|
6
|
+
describe("@Context", () => {
|
|
7
|
+
it("should call ParamFilter.useParam method with the correct parameters", () => {
|
|
8
|
+
class Ctrl {
|
|
9
|
+
test(@Context("expression") test: any) {}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const param = JsonParameterStore.get(Ctrl, "test", 0);
|
|
13
|
+
expect(param.expression).toEqual("expression");
|
|
14
|
+
expect(param.paramType).toEqual(ParamTypes.$CTX);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import {BaseContext} from "@tsed/di";
|
|
2
|
+
|
|
3
|
+
import {ParamTypes} from "../domain/ParamTypes.js";
|
|
4
|
+
import {mapParamsOptions} from "../utils/mapParamsOptions.js";
|
|
5
|
+
import {UseParam} from "./useParam.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Context decorator return the @@PlatformContext@@ created by Ts.ED when request is handled by the server.
|
|
9
|
+
*
|
|
10
|
+
* It contains some information as following:
|
|
11
|
+
*
|
|
12
|
+
* - The request id,
|
|
13
|
+
* - The request container used by the Ts.ED DI. It contain all services annotated with `@Scope(ProviderScope.REQUEST)`,
|
|
14
|
+
* - The current @@EndpointMetadata@@ resolved by Ts.ED during the request,
|
|
15
|
+
* - The data return by the previous endpoint if you use multiple handler on the same route. By default data is empty.
|
|
16
|
+
*
|
|
17
|
+
* ::: tip
|
|
18
|
+
* The @@PlatformContext@@ inherit from Map class. So you can store any information with.
|
|
19
|
+
* :::
|
|
20
|
+
*
|
|
21
|
+
* #### Example
|
|
22
|
+
*
|
|
23
|
+
* ```typescript
|
|
24
|
+
* @Middleware()
|
|
25
|
+
* class AuthTokenMiddleware {
|
|
26
|
+
* use(@Req() request: Req, @Context() context: PlatformContext) {
|
|
27
|
+
* if (!context.has("auth")){
|
|
28
|
+
* context.set('auth', new AuthToken(request))
|
|
29
|
+
* }
|
|
30
|
+
*
|
|
31
|
+
* try {
|
|
32
|
+
* context.get("auth").claims() // check token
|
|
33
|
+
* } catch(er){
|
|
34
|
+
* throw new Forbidden("Access forbidden - Bad token")
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
* }
|
|
38
|
+
*
|
|
39
|
+
* @Controller('/')
|
|
40
|
+
* @UseBefore(AuthTokenMiddleware) // protect all routes for this controller
|
|
41
|
+
* class MyCtrl {
|
|
42
|
+
* @Get('/')
|
|
43
|
+
* get(@Context('auth') auth: AuthToken) {
|
|
44
|
+
* console.log('auth', auth);
|
|
45
|
+
* console.log('auth.accessToken', auth.accessToken);
|
|
46
|
+
* console.log('auth.idToken', auth.idToken);
|
|
47
|
+
* }
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @param expression The path of the property to get.
|
|
52
|
+
* @decorator
|
|
53
|
+
* @operation
|
|
54
|
+
* @input
|
|
55
|
+
*/
|
|
56
|
+
export function Context(expression: string): ParameterDecorator;
|
|
57
|
+
export function Context(): ParameterDecorator;
|
|
58
|
+
export function Context(...args: any[]): ParameterDecorator {
|
|
59
|
+
const {expression, useType, useMapper = false, useValidation = false} = mapParamsOptions(args);
|
|
60
|
+
|
|
61
|
+
return UseParam({
|
|
62
|
+
paramType: ParamTypes.$CTX,
|
|
63
|
+
expression,
|
|
64
|
+
useType,
|
|
65
|
+
useMapper,
|
|
66
|
+
useValidation
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export type Context = BaseContext;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {JsonParameterStore} from "@tsed/schema";
|
|
2
|
+
|
|
3
|
+
import {ParamTypes} from "../domain/ParamTypes.js";
|
|
4
|
+
import {Cookies} from "./cookies.js";
|
|
5
|
+
|
|
6
|
+
describe("@Cookies", () => {
|
|
7
|
+
it("should call ParamFilter.useParam method with the correct parameters", () => {
|
|
8
|
+
class Test {}
|
|
9
|
+
|
|
10
|
+
class Ctrl {
|
|
11
|
+
test(@Cookies("expression", Test) body: Test) {}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const param = JsonParameterStore.get(Ctrl, "test", 0);
|
|
15
|
+
expect(param.expression).toEqual("expression");
|
|
16
|
+
expect(param.paramType).toEqual(ParamTypes.COOKIES);
|
|
17
|
+
expect(param.type).toEqual(Test);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import {Type} from "@tsed/core";
|
|
2
|
+
|
|
3
|
+
import {ParamOptions} from "../domain/ParamOptions.js";
|
|
4
|
+
import {ParamTypes} from "../domain/ParamTypes.js";
|
|
5
|
+
import {mapParamsOptions} from "../utils/mapParamsOptions.js";
|
|
6
|
+
import {UseParam} from "./useParam.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Cookies or CookiesParams return the value from [request.cookies](http://expressjs.com/en/4x/api.html#req.cookies) object.
|
|
10
|
+
*
|
|
11
|
+
* #### Example
|
|
12
|
+
*
|
|
13
|
+
* ```typescript
|
|
14
|
+
* @Controller('/')
|
|
15
|
+
* class MyCtrl {
|
|
16
|
+
* @Post('/')
|
|
17
|
+
* create(@Cookies() cookies: any) {
|
|
18
|
+
* console.log('Entire cookies', cookies);
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* @Post('/')
|
|
22
|
+
* create(@Cookies('id') id: string) {
|
|
23
|
+
* console.log('ID', id);
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* @Post('/')
|
|
27
|
+
* create(@Cookies('user') user: IUser) {
|
|
28
|
+
* console.log('user', user);
|
|
29
|
+
* }
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
* > For more information on deserialization see [converters](/docs/json-mapper.md) page.
|
|
33
|
+
*
|
|
34
|
+
* @param expression The path of the property to get.
|
|
35
|
+
* @param useType The type of the class that to be used to deserialize the data.
|
|
36
|
+
* @decorator
|
|
37
|
+
* @operation
|
|
38
|
+
* @input
|
|
39
|
+
*/
|
|
40
|
+
export function CookiesParams(expression: string | any, useType: any): ParameterDecorator;
|
|
41
|
+
export function CookiesParams(): ParameterDecorator;
|
|
42
|
+
export function CookiesParams(expression: string | any): ParameterDecorator;
|
|
43
|
+
export function CookiesParams(options: ParamOptions<any>): ParameterDecorator;
|
|
44
|
+
export function CookiesParams(...args: any[]): ParameterDecorator {
|
|
45
|
+
const {expression, useType, useMapper = false, useValidation = false} = mapParamsOptions(args);
|
|
46
|
+
|
|
47
|
+
return UseParam({
|
|
48
|
+
paramType: ParamTypes.COOKIES,
|
|
49
|
+
expression,
|
|
50
|
+
useType,
|
|
51
|
+
useMapper,
|
|
52
|
+
useValidation
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Cookies or CookiesParams return the value from [request.cookies](http://expressjs.com/en/4x/api.html#req.cookies) object.
|
|
58
|
+
*
|
|
59
|
+
* #### Example
|
|
60
|
+
*
|
|
61
|
+
* ```typescript
|
|
62
|
+
* @Controller('/')
|
|
63
|
+
* class MyCtrl {
|
|
64
|
+
* @Post('/')
|
|
65
|
+
* create(@Cookies() body: any) {
|
|
66
|
+
* console.log('Entire body', body);
|
|
67
|
+
* }
|
|
68
|
+
*
|
|
69
|
+
* @Post('/')
|
|
70
|
+
* create(@Cookies('id') id: string) {
|
|
71
|
+
* console.log('ID', id);
|
|
72
|
+
* }
|
|
73
|
+
*
|
|
74
|
+
* @Post('/')
|
|
75
|
+
* create(@Cookies('user') user: User) { // with deserialization
|
|
76
|
+
* console.log('user', user);
|
|
77
|
+
* }
|
|
78
|
+
*
|
|
79
|
+
* @Post('/')
|
|
80
|
+
* create(@Cookies('users', User) users: User[]) { // with deserialization
|
|
81
|
+
* console.log('users', users);
|
|
82
|
+
* }
|
|
83
|
+
* }
|
|
84
|
+
* ```
|
|
85
|
+
* > For more information on deserialization see [converters](/docs/json-mapper.md) page.
|
|
86
|
+
*
|
|
87
|
+
* @param expression The path of the property to get.
|
|
88
|
+
* @param useType The type of the class that to be used to deserialize the data.
|
|
89
|
+
* @decorator
|
|
90
|
+
* @aliasof CookiesParams
|
|
91
|
+
* @operation
|
|
92
|
+
* @input
|
|
93
|
+
*/
|
|
94
|
+
export function Cookies(expression: string, useType: Type<any>): ParameterDecorator;
|
|
95
|
+
export function Cookies(expression: string): ParameterDecorator;
|
|
96
|
+
export function Cookies(useType: Type<any>): ParameterDecorator;
|
|
97
|
+
export function Cookies(options: Partial<ParamOptions>): ParameterDecorator;
|
|
98
|
+
export function Cookies(): ParameterDecorator;
|
|
99
|
+
export function Cookies(...args: any[]): ParameterDecorator {
|
|
100
|
+
// @ts-ignore
|
|
101
|
+
return CookiesParams(...args);
|
|
102
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {JsonParameterStore} from "@tsed/schema";
|
|
2
|
+
|
|
3
|
+
import {ParamTypes} from "../domain/ParamTypes.js";
|
|
4
|
+
import {HeaderParams} from "./headerParams.js";
|
|
5
|
+
|
|
6
|
+
describe("@HeaderParams", () => {
|
|
7
|
+
it("should call ParamFilter.useParam method with the correct parameters", () => {
|
|
8
|
+
class Ctrl {
|
|
9
|
+
test(@HeaderParams("expression") header: string) {}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const param = JsonParameterStore.get(Ctrl, "test", 0);
|
|
13
|
+
expect(param.expression).toEqual("expression");
|
|
14
|
+
expect(param.paramType).toEqual(ParamTypes.HEADER);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import {Type} from "@tsed/core";
|
|
2
|
+
|
|
3
|
+
import {ParamOptions} from "../domain/ParamOptions.js";
|
|
4
|
+
import {ParamTypes} from "../domain/ParamTypes.js";
|
|
5
|
+
import {mapParamsOptions} from "../utils/mapParamsOptions.js";
|
|
6
|
+
import {UseParam} from "./useParam.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* HeaderParams return the value from [`request.get()`](http://expressjs.com/en/4x/api.html#req.get) method.
|
|
10
|
+
*
|
|
11
|
+
* #### Example
|
|
12
|
+
*
|
|
13
|
+
* ```typescript
|
|
14
|
+
* @Controller('/')
|
|
15
|
+
* class MyCtrl {
|
|
16
|
+
* @Get('/')
|
|
17
|
+
* get(@HeaderParams() body: any) {
|
|
18
|
+
* console.log('Entire body', body);
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* @Get('/')
|
|
22
|
+
* get(@HeaderParams('x-token') token: string) {
|
|
23
|
+
* console.log('token', id);
|
|
24
|
+
* }
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @param expression The path of the property to get.
|
|
29
|
+
* @param useType
|
|
30
|
+
* @decorator
|
|
31
|
+
* @operation
|
|
32
|
+
* @input
|
|
33
|
+
*/
|
|
34
|
+
export function HeaderParams(expression: string, useType: Type<any>): ParameterDecorator;
|
|
35
|
+
export function HeaderParams(expression: string): ParameterDecorator;
|
|
36
|
+
export function HeaderParams(useType: Type<any>): ParameterDecorator;
|
|
37
|
+
export function HeaderParams(options: Partial<ParamOptions>): ParameterDecorator;
|
|
38
|
+
export function HeaderParams(): ParameterDecorator;
|
|
39
|
+
export function HeaderParams(...args: any[]): ParameterDecorator {
|
|
40
|
+
const {expression, useType, useMapper = false, useValidation = false} = mapParamsOptions(args);
|
|
41
|
+
|
|
42
|
+
return UseParam({
|
|
43
|
+
paramType: ParamTypes.HEADER,
|
|
44
|
+
expression,
|
|
45
|
+
useType,
|
|
46
|
+
useMapper,
|
|
47
|
+
useValidation
|
|
48
|
+
});
|
|
49
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {JsonParameterStore} from "@tsed/schema";
|
|
2
|
+
|
|
3
|
+
import {ParamTypes} from "../domain/ParamTypes.js";
|
|
4
|
+
import {Locals} from "./locals.js";
|
|
5
|
+
|
|
6
|
+
describe("@Locals", () => {
|
|
7
|
+
it("should call ParamFilter.useParam method with the correct parameters", () => {
|
|
8
|
+
class Ctrl {
|
|
9
|
+
test(@Locals("expression") test: any) {}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const param = JsonParameterStore.get(Ctrl, "test", 0);
|
|
13
|
+
expect(param.expression).toEqual("expression");
|
|
14
|
+
expect(param.paramType).toEqual(ParamTypes.LOCALS);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {ParamTypes} from "../domain/ParamTypes.js";
|
|
2
|
+
import {mapParamsOptions} from "../utils/mapParamsOptions.js";
|
|
3
|
+
import {UseParam} from "./useParam.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Locals return the value from [response.locals](http://expressjs.com/en/4x/api.html#res.locals) object.
|
|
7
|
+
*
|
|
8
|
+
* ::: tip
|
|
9
|
+
* Locals are generally used by express and third-party like templating engine to render a page/template.
|
|
10
|
+
* See [Templating](https://tsed.dev/tutorials/templating.html) section for more details.
|
|
11
|
+
* :::
|
|
12
|
+
*
|
|
13
|
+
* #### Example
|
|
14
|
+
*
|
|
15
|
+
* ```typescript
|
|
16
|
+
* * @Middleware()
|
|
17
|
+
* class LocalsMiddleware {
|
|
18
|
+
* use(@Locals() locals: any) {
|
|
19
|
+
* // set some on locals
|
|
20
|
+
* locals.user = "user"
|
|
21
|
+
* }
|
|
22
|
+
* }
|
|
23
|
+
*
|
|
24
|
+
* @Controller('/')
|
|
25
|
+
* @UseBefore(LocalsMiddleware)
|
|
26
|
+
* class MyCtrl {
|
|
27
|
+
* @Get('/')
|
|
28
|
+
* @View('home.ejs') // will use locals and returned data to render the page
|
|
29
|
+
* get(@Locals('user') user: any) {
|
|
30
|
+
* console.log('user', user);
|
|
31
|
+
*
|
|
32
|
+
* return {
|
|
33
|
+
* description: 'Hello world'
|
|
34
|
+
* }
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @param expression The path of the property to get.
|
|
40
|
+
* @decorator
|
|
41
|
+
* @operation
|
|
42
|
+
* @input
|
|
43
|
+
* @response
|
|
44
|
+
*/
|
|
45
|
+
export function Locals(expression: string): ParameterDecorator;
|
|
46
|
+
export function Locals(): ParameterDecorator;
|
|
47
|
+
export function Locals(...args: any[]): ParameterDecorator {
|
|
48
|
+
const {expression, useType, useMapper = false, useValidation = false} = mapParamsOptions(args);
|
|
49
|
+
|
|
50
|
+
return UseParam({
|
|
51
|
+
paramType: ParamTypes.LOCALS,
|
|
52
|
+
expression,
|
|
53
|
+
useType,
|
|
54
|
+
useMapper,
|
|
55
|
+
useValidation
|
|
56
|
+
});
|
|
57
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import {DecoratorParameters} from "@tsed/core";
|
|
2
|
+
import {JsonEntityFn, JsonParameterStore} from "@tsed/schema";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Get the Param metadata. Use this decorator to compose your own decorator.
|
|
6
|
+
*
|
|
7
|
+
* @param fn
|
|
8
|
+
* @decorator
|
|
9
|
+
* @operation
|
|
10
|
+
* @input
|
|
11
|
+
*/
|
|
12
|
+
export function ParamFn(fn: (param: JsonParameterStore, parameters: DecoratorParameters) => void): ParameterDecorator {
|
|
13
|
+
return JsonEntityFn<JsonParameterStore>(fn);
|
|
14
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {JsonParameterStore} from "@tsed/schema";
|
|
2
|
+
|
|
3
|
+
import {ParamTypes} from "../domain/ParamTypes.js";
|
|
4
|
+
import {PathParams, RawPathParams} from "./pathParams.js";
|
|
5
|
+
|
|
6
|
+
describe("@PathParams", () => {
|
|
7
|
+
it("should declare a path params", () => {
|
|
8
|
+
class Test {}
|
|
9
|
+
|
|
10
|
+
class Ctrl {
|
|
11
|
+
test(@PathParams("expression", Test) header: Test) {}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const param = JsonParameterStore.get(Ctrl, "test", 0);
|
|
15
|
+
expect(param.expression).toEqual("expression");
|
|
16
|
+
expect(param.paramType).toEqual(ParamTypes.PATH);
|
|
17
|
+
expect(param.type).toEqual(Test);
|
|
18
|
+
expect(param.pipes).toHaveLength(2);
|
|
19
|
+
});
|
|
20
|
+
it("should declara a path params with options", () => {
|
|
21
|
+
class Test {}
|
|
22
|
+
|
|
23
|
+
class Ctrl {
|
|
24
|
+
test(@PathParams({expression: "expression", useType: Test, useValidation: false}) param: Test) {}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const param = JsonParameterStore.get(Ctrl, "test", 0);
|
|
28
|
+
expect(param.expression).toEqual("expression");
|
|
29
|
+
expect(param.paramType).toEqual(ParamTypes.PATH);
|
|
30
|
+
expect(param.type).toEqual(Test);
|
|
31
|
+
expect(param.pipes).toHaveLength(1);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe("@RawPathParams", () => {
|
|
36
|
+
it("should declare a raw path params", () => {
|
|
37
|
+
class Ctrl {
|
|
38
|
+
test(@RawPathParams("expression") header: string) {}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const param = JsonParameterStore.get(Ctrl, "test", 0);
|
|
42
|
+
expect(param.expression).toEqual("expression");
|
|
43
|
+
expect(param.paramType).toEqual(ParamTypes.PATH);
|
|
44
|
+
expect(param.type).toEqual(String);
|
|
45
|
+
expect(param.pipes).toHaveLength(0);
|
|
46
|
+
});
|
|
47
|
+
it("should declare a raw path params with options", () => {
|
|
48
|
+
class Ctrl {
|
|
49
|
+
test(@RawPathParams({expression: "expression", useValidation: true, useType: String}) param: Date) {}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const param = JsonParameterStore.get(Ctrl, "test", 0);
|
|
53
|
+
expect(param.expression).toEqual("expression");
|
|
54
|
+
expect(param.paramType).toEqual(ParamTypes.PATH);
|
|
55
|
+
expect(param.type).toEqual(String);
|
|
56
|
+
expect(param.pipes).toHaveLength(1);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import {Type} from "@tsed/core";
|
|
2
|
+
|
|
3
|
+
import {ParamOptions} from "../domain/ParamOptions.js";
|
|
4
|
+
import {ParamTypes} from "../domain/ParamTypes.js";
|
|
5
|
+
import {mapParamsOptions} from "../utils/mapParamsOptions.js";
|
|
6
|
+
import {UseParam} from "./useParam.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* PathParams return the value from [request.params](http://expressjs.com/en/4x/api.html#req.params) object.
|
|
10
|
+
*
|
|
11
|
+
* #### Example
|
|
12
|
+
*
|
|
13
|
+
* ```typescript
|
|
14
|
+
* @Controller('/')
|
|
15
|
+
* class MyCtrl {
|
|
16
|
+
* @Get('/')
|
|
17
|
+
* get(@PathParams() params: any) {
|
|
18
|
+
* console.log('Entire params', params);
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* @Get('/')
|
|
22
|
+
* get(@PathParams('id') id: string) {
|
|
23
|
+
* console.log('ID', id);
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* @Get('/')
|
|
27
|
+
* get(@PathParams({expression: 'id', useType: () => new MyCustomModel() }) id: string) {
|
|
28
|
+
* console.log('ID', id);
|
|
29
|
+
* }
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
* > For more information on deserialization see [converters](/docs/json-mapper.md) page.
|
|
33
|
+
*
|
|
34
|
+
* @param expression The path of the property to get.
|
|
35
|
+
* @param useType The type of the class that to be used to deserialize the data.
|
|
36
|
+
* @decorator
|
|
37
|
+
* @operation
|
|
38
|
+
* @input
|
|
39
|
+
*/
|
|
40
|
+
export function PathParams(expression: string, useType: Type<any>): ParameterDecorator;
|
|
41
|
+
export function PathParams(expression: string): ParameterDecorator;
|
|
42
|
+
export function PathParams(useType: Type<any>): ParameterDecorator;
|
|
43
|
+
export function PathParams(options: Partial<ParamOptions>): ParameterDecorator;
|
|
44
|
+
export function PathParams(): ParameterDecorator;
|
|
45
|
+
export function PathParams(...args: any[]): ParameterDecorator {
|
|
46
|
+
const {expression, useType, useMapper = true, useValidation = true} = mapParamsOptions(args);
|
|
47
|
+
|
|
48
|
+
return UseParam({
|
|
49
|
+
paramType: ParamTypes.PATH,
|
|
50
|
+
expression,
|
|
51
|
+
useType,
|
|
52
|
+
useMapper,
|
|
53
|
+
useValidation
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* RawPathParams return the raw value from [request.params](http://expressjs.com/en/4x/api.html#req.params) object.
|
|
59
|
+
*
|
|
60
|
+
* Any validation and transformation are performed on the value. Use [pipes](/docs/pipes.html) to validate and/or transform the value.
|
|
61
|
+
*
|
|
62
|
+
* #### Example
|
|
63
|
+
*
|
|
64
|
+
* ```typescript
|
|
65
|
+
* @Controller('/')
|
|
66
|
+
* class MyCtrl {
|
|
67
|
+
* @Get('/')
|
|
68
|
+
* get(@RawPathParams() params: string) {
|
|
69
|
+
* console.log('Entire params', params);
|
|
70
|
+
* }
|
|
71
|
+
*
|
|
72
|
+
* @Get('/')
|
|
73
|
+
* get(@RawPathParams('id') id: string) {
|
|
74
|
+
* console.log('ID', id);
|
|
75
|
+
* }
|
|
76
|
+
* }
|
|
77
|
+
* ```
|
|
78
|
+
* > For more information on deserialization see [converters](/docs/json-mapper.md) page.
|
|
79
|
+
*
|
|
80
|
+
* @param expression The path of the property to get.
|
|
81
|
+
* @decorator
|
|
82
|
+
* @operation
|
|
83
|
+
* @input
|
|
84
|
+
*/
|
|
85
|
+
export function RawPathParams(expression: string): ParameterDecorator;
|
|
86
|
+
export function RawPathParams(options: Partial<ParamOptions>): ParameterDecorator;
|
|
87
|
+
export function RawPathParams(): ParameterDecorator;
|
|
88
|
+
export function RawPathParams(...args: any[]): ParameterDecorator {
|
|
89
|
+
const {expression, useType, useMapper = false, useValidation = false} = mapParamsOptions(args);
|
|
90
|
+
|
|
91
|
+
return PathParams({
|
|
92
|
+
expression,
|
|
93
|
+
useValidation,
|
|
94
|
+
useMapper,
|
|
95
|
+
useType
|
|
96
|
+
});
|
|
97
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {JsonParameterStore} from "@tsed/schema";
|
|
2
|
+
|
|
3
|
+
import {ParamTypes} from "../domain/ParamTypes.js";
|
|
4
|
+
import {QueryParams, RawQueryParams} from "./queryParams.js";
|
|
5
|
+
|
|
6
|
+
describe("@QueryParams", () => {
|
|
7
|
+
it("should declare query params", () => {
|
|
8
|
+
class Test {}
|
|
9
|
+
|
|
10
|
+
class Ctrl {
|
|
11
|
+
test(@QueryParams("expression", Test) header: Test) {}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const param = JsonParameterStore.get(Ctrl, "test", 0);
|
|
15
|
+
expect(param.expression).toEqual("expression");
|
|
16
|
+
expect(param.paramType).toEqual(ParamTypes.QUERY);
|
|
17
|
+
expect(param.type).toEqual(Test);
|
|
18
|
+
expect(param.pipes).toHaveLength(2);
|
|
19
|
+
});
|
|
20
|
+
it("should declare query params with options", () => {
|
|
21
|
+
class Test {}
|
|
22
|
+
|
|
23
|
+
class Ctrl {
|
|
24
|
+
test(@QueryParams({expression: "expression", useType: Test, useValidation: false}) param: Test) {}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const param = JsonParameterStore.get(Ctrl, "test", 0);
|
|
28
|
+
expect(param.expression).toEqual("expression");
|
|
29
|
+
expect(param.paramType).toEqual(ParamTypes.QUERY);
|
|
30
|
+
expect(param.type).toEqual(Test);
|
|
31
|
+
expect(param.pipes).toHaveLength(1);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe("@RawQueryParams", () => {
|
|
36
|
+
it("should declare raw query params", () => {
|
|
37
|
+
class Ctrl {
|
|
38
|
+
test(@RawQueryParams("expression") header: string) {}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const param = JsonParameterStore.get(Ctrl, "test", 0);
|
|
42
|
+
expect(param.expression).toEqual("expression");
|
|
43
|
+
expect(param.paramType).toEqual(ParamTypes.QUERY);
|
|
44
|
+
expect(param.type).toEqual(String);
|
|
45
|
+
expect(param.pipes).toHaveLength(0);
|
|
46
|
+
});
|
|
47
|
+
it("should declare raw query params with options", () => {
|
|
48
|
+
class Ctrl {
|
|
49
|
+
test(@RawQueryParams({expression: "expression", useValidation: true, useType: String}) param: Date) {}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const param = JsonParameterStore.get(Ctrl, "test", 0);
|
|
53
|
+
expect(param.expression).toEqual("expression");
|
|
54
|
+
expect(param.paramType).toEqual(ParamTypes.QUERY);
|
|
55
|
+
expect(param.type).toEqual(String);
|
|
56
|
+
expect(param.pipes).toHaveLength(1);
|
|
57
|
+
});
|
|
58
|
+
});
|