@nestia/core 2.4.3 → 3.0.0-dev.20231209
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/decorators/EncryptedBody.js +13 -11
- package/lib/decorators/EncryptedBody.js.map +1 -1
- package/lib/decorators/EncryptedRoute.js +29 -12
- package/lib/decorators/EncryptedRoute.js.map +1 -1
- package/lib/decorators/internal/get_binary_body.d.ts +1 -0
- package/lib/decorators/internal/get_binary_body.js +66 -0
- package/lib/decorators/internal/get_binary_body.js.map +1 -0
- package/package.json +3 -3
- package/src/decorators/DynamicModule.ts +39 -39
- package/src/decorators/EncryptedBody.ts +17 -12
- package/src/decorators/EncryptedController.ts +38 -38
- package/src/decorators/EncryptedModule.ts +79 -79
- package/src/decorators/EncryptedRoute.ts +38 -14
- package/src/decorators/PlainBody.ts +72 -72
- package/src/decorators/TypedBody.ts +59 -59
- package/src/decorators/TypedException.ts +89 -89
- package/src/decorators/TypedHeaders.ts +69 -69
- package/src/decorators/TypedParam.ts +65 -65
- package/src/decorators/TypedQuery.ts +246 -246
- package/src/decorators/TypedRoute.ts +144 -144
- package/src/decorators/internal/EncryptedConstant.ts +4 -4
- package/src/decorators/internal/NoTransformConfigureError.ts +8 -8
- package/src/decorators/internal/get_binary_body.ts +18 -0
- package/src/decorators/internal/get_path_and_querify.ts +103 -103
- package/src/decorators/internal/get_path_and_stringify.ts +91 -91
- package/src/decorators/internal/get_text_body.ts +20 -20
- package/src/decorators/internal/headers_to_object.ts +13 -13
- package/src/decorators/internal/load_controller.ts +51 -51
- package/src/decorators/internal/route_error.ts +45 -45
- package/src/decorators/internal/validate_request_body.ts +57 -57
- package/src/decorators/internal/validate_request_headers.ts +68 -68
- package/src/decorators/internal/validate_request_query.ts +56 -56
- package/src/index.ts +5 -5
- package/src/module.ts +14 -14
- package/src/options/INestiaTransformOptions.ts +17 -17
- package/src/options/INestiaTransformProject.ts +7 -7
- package/src/options/IRequestBodyValidator.ts +20 -20
- package/src/options/IRequestHeadersValidator.ts +22 -22
- package/src/options/IRequestQueryValidator.ts +20 -20
- package/src/options/IResponseBodyQuerifier.ts +25 -25
- package/src/options/IResponseBodyStringifier.ts +25 -25
- package/src/programmers/PlainBodyProgrammer.ts +52 -52
- package/src/programmers/TypedBodyProgrammer.ts +108 -108
- package/src/programmers/TypedExceptionProgrammer.ts +72 -72
- package/src/programmers/TypedHeadersProgrammer.ts +56 -56
- package/src/programmers/TypedParamProgrammer.ts +24 -24
- package/src/programmers/TypedQueryBodyProgrammer.ts +56 -56
- package/src/programmers/TypedQueryProgrammer.ts +56 -56
- package/src/programmers/TypedQueryRouteProgrammer.ts +51 -51
- package/src/programmers/TypedRouteProgrammer.ts +51 -51
- package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +58 -58
- package/src/programmers/http/HttpIsQuerifyProgrammer.ts +62 -62
- package/src/programmers/http/HttpQuerifyProgrammer.ts +96 -96
- package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +63 -63
- package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
- package/src/transform.ts +35 -35
- package/src/transformers/FileTransformer.ts +66 -66
- package/src/transformers/MethodTransformer.ts +94 -94
- package/src/transformers/NodeTransformer.ts +16 -16
- package/src/transformers/ParameterDecoratorTransformer.ts +121 -121
- package/src/transformers/ParameterTransformer.ts +48 -48
- package/src/transformers/TypedExceptionTransformer.ts +49 -49
- package/src/transformers/TypedRouteTransformer.ts +95 -95
- package/src/typings/Creator.ts +3 -3
- package/src/utils/ExceptionManager.ts +112 -112
- package/src/utils/Singleton.ts +20 -20
- package/src/utils/SourceFinder.ts +57 -57
|
@@ -1,89 +1,89 @@
|
|
|
1
|
-
import "reflect-metadata";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* > You must configure the generic argument `T`
|
|
5
|
-
*
|
|
6
|
-
* Exception decorator.
|
|
7
|
-
*
|
|
8
|
-
* `TypedException` is a decorator function describing HTTP exception and its type
|
|
9
|
-
* which could be occured in the method.
|
|
10
|
-
*
|
|
11
|
-
* For reference, this decorator function does not affect to the method's behavior,
|
|
12
|
-
* but only affects to the swagger documents generation. Also, it does not affect to
|
|
13
|
-
* the SDK library generation yet, but will be used in the future.
|
|
14
|
-
*
|
|
15
|
-
* @param status Status number or pattern like "2XX", "3XX", "4XX", "5XX"
|
|
16
|
-
* @param description Description about the exception
|
|
17
|
-
* @returns Method decorator
|
|
18
|
-
*
|
|
19
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
20
|
-
*/
|
|
21
|
-
export function TypedException(
|
|
22
|
-
status: number | "2XX" | "3XX" | "4XX" | "5XX",
|
|
23
|
-
description?: string | undefined,
|
|
24
|
-
): never;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Exception decorator.
|
|
28
|
-
*
|
|
29
|
-
* `TypedException` is a decorator function describing HTTP exception and its type
|
|
30
|
-
* which could be occured in the method.
|
|
31
|
-
*
|
|
32
|
-
* For reference, this decorator function does not affect to the method's behavior,
|
|
33
|
-
* but only affects to the swagger documents generation. Also, it does not affect to
|
|
34
|
-
* the SDK library generation yet, but will be used in the future.
|
|
35
|
-
*
|
|
36
|
-
* @template T Type of the exception
|
|
37
|
-
* @param status Status number or pattern like "2XX", "3XX", "4XX", "5XX"
|
|
38
|
-
* @param description Description about the exception
|
|
39
|
-
* @returns Method decorator
|
|
40
|
-
*
|
|
41
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
42
|
-
*/
|
|
43
|
-
export function TypedException<T>(
|
|
44
|
-
status: number | "2XX" | "3XX" | "4XX" | "5XX",
|
|
45
|
-
description?: string | undefined,
|
|
46
|
-
): MethodDecorator;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* @internal
|
|
50
|
-
*/
|
|
51
|
-
export function TypedException<T>(
|
|
52
|
-
status: number | "2XX" | "3XX" | "4XX" | "5XX",
|
|
53
|
-
description?: string | undefined,
|
|
54
|
-
type?: string | undefined,
|
|
55
|
-
): MethodDecorator {
|
|
56
|
-
return function TypedException(
|
|
57
|
-
target: Object | T,
|
|
58
|
-
propertyKey: string | symbol,
|
|
59
|
-
descriptor: TypedPropertyDescriptor<any>,
|
|
60
|
-
) {
|
|
61
|
-
const array: IProps[] = (() => {
|
|
62
|
-
const oldbie: IProps[] | undefined = Reflect.getMetadata(
|
|
63
|
-
`nestia/TypedException`,
|
|
64
|
-
(target as any)[propertyKey],
|
|
65
|
-
);
|
|
66
|
-
if (oldbie !== undefined) return oldbie;
|
|
67
|
-
|
|
68
|
-
const newbie: IProps[] = [];
|
|
69
|
-
Reflect.defineMetadata(
|
|
70
|
-
`nestia/TypedException`,
|
|
71
|
-
newbie,
|
|
72
|
-
(target as any)[propertyKey],
|
|
73
|
-
);
|
|
74
|
-
return newbie;
|
|
75
|
-
})();
|
|
76
|
-
array.push({
|
|
77
|
-
status,
|
|
78
|
-
description,
|
|
79
|
-
type: type!,
|
|
80
|
-
});
|
|
81
|
-
return descriptor;
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
interface IProps {
|
|
86
|
-
status: number | "2XX" | "3XX" | "4XX" | "5XX";
|
|
87
|
-
description?: string | undefined;
|
|
88
|
-
type: string;
|
|
89
|
-
}
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* > You must configure the generic argument `T`
|
|
5
|
+
*
|
|
6
|
+
* Exception decorator.
|
|
7
|
+
*
|
|
8
|
+
* `TypedException` is a decorator function describing HTTP exception and its type
|
|
9
|
+
* which could be occured in the method.
|
|
10
|
+
*
|
|
11
|
+
* For reference, this decorator function does not affect to the method's behavior,
|
|
12
|
+
* but only affects to the swagger documents generation. Also, it does not affect to
|
|
13
|
+
* the SDK library generation yet, but will be used in the future.
|
|
14
|
+
*
|
|
15
|
+
* @param status Status number or pattern like "2XX", "3XX", "4XX", "5XX"
|
|
16
|
+
* @param description Description about the exception
|
|
17
|
+
* @returns Method decorator
|
|
18
|
+
*
|
|
19
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
20
|
+
*/
|
|
21
|
+
export function TypedException(
|
|
22
|
+
status: number | "2XX" | "3XX" | "4XX" | "5XX",
|
|
23
|
+
description?: string | undefined,
|
|
24
|
+
): never;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Exception decorator.
|
|
28
|
+
*
|
|
29
|
+
* `TypedException` is a decorator function describing HTTP exception and its type
|
|
30
|
+
* which could be occured in the method.
|
|
31
|
+
*
|
|
32
|
+
* For reference, this decorator function does not affect to the method's behavior,
|
|
33
|
+
* but only affects to the swagger documents generation. Also, it does not affect to
|
|
34
|
+
* the SDK library generation yet, but will be used in the future.
|
|
35
|
+
*
|
|
36
|
+
* @template T Type of the exception
|
|
37
|
+
* @param status Status number or pattern like "2XX", "3XX", "4XX", "5XX"
|
|
38
|
+
* @param description Description about the exception
|
|
39
|
+
* @returns Method decorator
|
|
40
|
+
*
|
|
41
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
42
|
+
*/
|
|
43
|
+
export function TypedException<T>(
|
|
44
|
+
status: number | "2XX" | "3XX" | "4XX" | "5XX",
|
|
45
|
+
description?: string | undefined,
|
|
46
|
+
): MethodDecorator;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @internal
|
|
50
|
+
*/
|
|
51
|
+
export function TypedException<T>(
|
|
52
|
+
status: number | "2XX" | "3XX" | "4XX" | "5XX",
|
|
53
|
+
description?: string | undefined,
|
|
54
|
+
type?: string | undefined,
|
|
55
|
+
): MethodDecorator {
|
|
56
|
+
return function TypedException(
|
|
57
|
+
target: Object | T,
|
|
58
|
+
propertyKey: string | symbol,
|
|
59
|
+
descriptor: TypedPropertyDescriptor<any>,
|
|
60
|
+
) {
|
|
61
|
+
const array: IProps[] = (() => {
|
|
62
|
+
const oldbie: IProps[] | undefined = Reflect.getMetadata(
|
|
63
|
+
`nestia/TypedException`,
|
|
64
|
+
(target as any)[propertyKey],
|
|
65
|
+
);
|
|
66
|
+
if (oldbie !== undefined) return oldbie;
|
|
67
|
+
|
|
68
|
+
const newbie: IProps[] = [];
|
|
69
|
+
Reflect.defineMetadata(
|
|
70
|
+
`nestia/TypedException`,
|
|
71
|
+
newbie,
|
|
72
|
+
(target as any)[propertyKey],
|
|
73
|
+
);
|
|
74
|
+
return newbie;
|
|
75
|
+
})();
|
|
76
|
+
array.push({
|
|
77
|
+
status,
|
|
78
|
+
description,
|
|
79
|
+
type: type!,
|
|
80
|
+
});
|
|
81
|
+
return descriptor;
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
interface IProps {
|
|
86
|
+
status: number | "2XX" | "3XX" | "4XX" | "5XX";
|
|
87
|
+
description?: string | undefined;
|
|
88
|
+
type: string;
|
|
89
|
+
}
|
|
@@ -1,69 +1,69 @@
|
|
|
1
|
-
import { ExecutionContext, createParamDecorator } from "@nestjs/common";
|
|
2
|
-
import type express from "express";
|
|
3
|
-
import type { FastifyRequest } from "fastify";
|
|
4
|
-
import typia from "typia";
|
|
5
|
-
|
|
6
|
-
import { IRequestHeadersValidator } from "../options/IRequestHeadersValidator";
|
|
7
|
-
import { validate_request_headers } from "./internal/validate_request_headers";
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Type safe HTTP headers decorator.
|
|
11
|
-
*
|
|
12
|
-
* `TypedHeaders` is a decorator function that can parse HTTP headers. It is almost
|
|
13
|
-
* same with {@link nest.Headers}, but it can automatically cast property type following
|
|
14
|
-
* its DTO definition. Also, `TypedHeaders` performs type validation.
|
|
15
|
-
*
|
|
16
|
-
* For reference, target type `T` must follow such restrictions. Also, if actual HTTP
|
|
17
|
-
* header values are different with their promised type `T`, `BadRequestException`
|
|
18
|
-
* error (status code: 400) would be thrown.
|
|
19
|
-
*
|
|
20
|
-
* 1. Type `T` must be an object type
|
|
21
|
-
* 2. Do not allow dynamic property
|
|
22
|
-
* 3. Property key must be lower case
|
|
23
|
-
* 4. Property value cannot be `null`, but `undefined` is possible
|
|
24
|
-
* 5. Only `boolean`, `bigint`, `number`, `string` or their array types are allowed
|
|
25
|
-
* 6. By the way, union type never be not allowed
|
|
26
|
-
* 7. Property `set-cookie` must be array type
|
|
27
|
-
* 8. Those properties cannot be array type
|
|
28
|
-
* - age
|
|
29
|
-
* - authorization
|
|
30
|
-
* - content-length
|
|
31
|
-
* - content-type
|
|
32
|
-
* - etag
|
|
33
|
-
* - expires
|
|
34
|
-
* - from
|
|
35
|
-
* - host
|
|
36
|
-
* - if-modified-since
|
|
37
|
-
* - if-unmodified-since
|
|
38
|
-
* - last-modified
|
|
39
|
-
* - location
|
|
40
|
-
* - max-forwards
|
|
41
|
-
* - proxy-authorization
|
|
42
|
-
* - referer
|
|
43
|
-
* - retry-after
|
|
44
|
-
* - server
|
|
45
|
-
* - user-agent
|
|
46
|
-
*
|
|
47
|
-
* @returns Parameter decorator
|
|
48
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
49
|
-
*/
|
|
50
|
-
export function TypedHeaders<T extends object>(
|
|
51
|
-
validator?: IRequestHeadersValidator<T>,
|
|
52
|
-
): ParameterDecorator {
|
|
53
|
-
const checker = validate_request_headers(validator);
|
|
54
|
-
return createParamDecorator(function TypedHeaders(
|
|
55
|
-
_unknown: any,
|
|
56
|
-
context: ExecutionContext,
|
|
57
|
-
) {
|
|
58
|
-
const request: express.Request | FastifyRequest = context
|
|
59
|
-
.switchToHttp()
|
|
60
|
-
.getRequest();
|
|
61
|
-
|
|
62
|
-
const output: T | Error = checker(request.headers);
|
|
63
|
-
if (output instanceof Error) throw output;
|
|
64
|
-
return output;
|
|
65
|
-
})();
|
|
66
|
-
}
|
|
67
|
-
Object.assign(TypedHeaders, typia.http.assertHeaders);
|
|
68
|
-
Object.assign(TypedHeaders, typia.http.isHeaders);
|
|
69
|
-
Object.assign(TypedHeaders, typia.http.validateHeaders);
|
|
1
|
+
import { ExecutionContext, createParamDecorator } from "@nestjs/common";
|
|
2
|
+
import type express from "express";
|
|
3
|
+
import type { FastifyRequest } from "fastify";
|
|
4
|
+
import typia from "typia";
|
|
5
|
+
|
|
6
|
+
import { IRequestHeadersValidator } from "../options/IRequestHeadersValidator";
|
|
7
|
+
import { validate_request_headers } from "./internal/validate_request_headers";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Type safe HTTP headers decorator.
|
|
11
|
+
*
|
|
12
|
+
* `TypedHeaders` is a decorator function that can parse HTTP headers. It is almost
|
|
13
|
+
* same with {@link nest.Headers}, but it can automatically cast property type following
|
|
14
|
+
* its DTO definition. Also, `TypedHeaders` performs type validation.
|
|
15
|
+
*
|
|
16
|
+
* For reference, target type `T` must follow such restrictions. Also, if actual HTTP
|
|
17
|
+
* header values are different with their promised type `T`, `BadRequestException`
|
|
18
|
+
* error (status code: 400) would be thrown.
|
|
19
|
+
*
|
|
20
|
+
* 1. Type `T` must be an object type
|
|
21
|
+
* 2. Do not allow dynamic property
|
|
22
|
+
* 3. Property key must be lower case
|
|
23
|
+
* 4. Property value cannot be `null`, but `undefined` is possible
|
|
24
|
+
* 5. Only `boolean`, `bigint`, `number`, `string` or their array types are allowed
|
|
25
|
+
* 6. By the way, union type never be not allowed
|
|
26
|
+
* 7. Property `set-cookie` must be array type
|
|
27
|
+
* 8. Those properties cannot be array type
|
|
28
|
+
* - age
|
|
29
|
+
* - authorization
|
|
30
|
+
* - content-length
|
|
31
|
+
* - content-type
|
|
32
|
+
* - etag
|
|
33
|
+
* - expires
|
|
34
|
+
* - from
|
|
35
|
+
* - host
|
|
36
|
+
* - if-modified-since
|
|
37
|
+
* - if-unmodified-since
|
|
38
|
+
* - last-modified
|
|
39
|
+
* - location
|
|
40
|
+
* - max-forwards
|
|
41
|
+
* - proxy-authorization
|
|
42
|
+
* - referer
|
|
43
|
+
* - retry-after
|
|
44
|
+
* - server
|
|
45
|
+
* - user-agent
|
|
46
|
+
*
|
|
47
|
+
* @returns Parameter decorator
|
|
48
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
49
|
+
*/
|
|
50
|
+
export function TypedHeaders<T extends object>(
|
|
51
|
+
validator?: IRequestHeadersValidator<T>,
|
|
52
|
+
): ParameterDecorator {
|
|
53
|
+
const checker = validate_request_headers(validator);
|
|
54
|
+
return createParamDecorator(function TypedHeaders(
|
|
55
|
+
_unknown: any,
|
|
56
|
+
context: ExecutionContext,
|
|
57
|
+
) {
|
|
58
|
+
const request: express.Request | FastifyRequest = context
|
|
59
|
+
.switchToHttp()
|
|
60
|
+
.getRequest();
|
|
61
|
+
|
|
62
|
+
const output: T | Error = checker(request.headers);
|
|
63
|
+
if (output instanceof Error) throw output;
|
|
64
|
+
return output;
|
|
65
|
+
})();
|
|
66
|
+
}
|
|
67
|
+
Object.assign(TypedHeaders, typia.http.assertHeaders);
|
|
68
|
+
Object.assign(TypedHeaders, typia.http.isHeaders);
|
|
69
|
+
Object.assign(TypedHeaders, typia.http.validateHeaders);
|
|
@@ -1,65 +1,65 @@
|
|
|
1
|
-
import {
|
|
2
|
-
BadRequestException,
|
|
3
|
-
ExecutionContext,
|
|
4
|
-
createParamDecorator,
|
|
5
|
-
} from "@nestjs/common";
|
|
6
|
-
import type express from "express";
|
|
7
|
-
import type { FastifyRequest } from "fastify";
|
|
8
|
-
import typia, { TypeGuardError } from "typia";
|
|
9
|
-
|
|
10
|
-
import { NoTransformConfigureError } from "./internal/NoTransformConfigureError";
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Type safe URL parameter decorator.
|
|
14
|
-
*
|
|
15
|
-
* `TypedParam` is a decorator function getting specific typed parameter from the
|
|
16
|
-
* HTTP request URL. It's almost same with the {@link nest.Param}, but `TypedParam`
|
|
17
|
-
* automatically casts parameter value to be following its type, and validates it.
|
|
18
|
-
*
|
|
19
|
-
* ```typescript
|
|
20
|
-
* import { tags } from "typia";
|
|
21
|
-
*
|
|
22
|
-
* \@TypedRoute.Get("shopping/sales/:id/:no/:paused")
|
|
23
|
-
* public async pause
|
|
24
|
-
* (
|
|
25
|
-
* \@TypedParam("id", "uuid"), id: string & tags.Format<"uuid">,
|
|
26
|
-
* \@TypedParam("no") id: number & tags.Type<"uint32">
|
|
27
|
-
* \@TypedParam("paused") paused: boolean | null
|
|
28
|
-
* ): Promise<void>;
|
|
29
|
-
* ```
|
|
30
|
-
*
|
|
31
|
-
* @param name URL Parameter name
|
|
32
|
-
* @returns Parameter decorator
|
|
33
|
-
*
|
|
34
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
35
|
-
*/
|
|
36
|
-
export function TypedParam<T extends boolean | bigint | number | string | null>(
|
|
37
|
-
name: string,
|
|
38
|
-
assert?: (value: string) => T,
|
|
39
|
-
): ParameterDecorator {
|
|
40
|
-
if (assert === undefined) throw NoTransformConfigureError("TypedParam");
|
|
41
|
-
|
|
42
|
-
return createParamDecorator(function TypedParam(
|
|
43
|
-
{}: any,
|
|
44
|
-
context: ExecutionContext,
|
|
45
|
-
) {
|
|
46
|
-
const request: express.Request | FastifyRequest = context
|
|
47
|
-
.switchToHttp()
|
|
48
|
-
.getRequest();
|
|
49
|
-
const str: string = (request.params as any)[name];
|
|
50
|
-
try {
|
|
51
|
-
return assert(str);
|
|
52
|
-
} catch (exp) {
|
|
53
|
-
if (typia.is<TypeGuardError>(exp))
|
|
54
|
-
throw new BadRequestException({
|
|
55
|
-
path: exp.path,
|
|
56
|
-
reason: exp.message,
|
|
57
|
-
expected: exp.expected,
|
|
58
|
-
value: exp.value,
|
|
59
|
-
message: `Invalid URL parameter value on "${name}".`,
|
|
60
|
-
});
|
|
61
|
-
throw exp;
|
|
62
|
-
}
|
|
63
|
-
})(name);
|
|
64
|
-
}
|
|
65
|
-
Object.assign(TypedParam, typia.http.parameter);
|
|
1
|
+
import {
|
|
2
|
+
BadRequestException,
|
|
3
|
+
ExecutionContext,
|
|
4
|
+
createParamDecorator,
|
|
5
|
+
} from "@nestjs/common";
|
|
6
|
+
import type express from "express";
|
|
7
|
+
import type { FastifyRequest } from "fastify";
|
|
8
|
+
import typia, { TypeGuardError } from "typia";
|
|
9
|
+
|
|
10
|
+
import { NoTransformConfigureError } from "./internal/NoTransformConfigureError";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Type safe URL parameter decorator.
|
|
14
|
+
*
|
|
15
|
+
* `TypedParam` is a decorator function getting specific typed parameter from the
|
|
16
|
+
* HTTP request URL. It's almost same with the {@link nest.Param}, but `TypedParam`
|
|
17
|
+
* automatically casts parameter value to be following its type, and validates it.
|
|
18
|
+
*
|
|
19
|
+
* ```typescript
|
|
20
|
+
* import { tags } from "typia";
|
|
21
|
+
*
|
|
22
|
+
* \@TypedRoute.Get("shopping/sales/:id/:no/:paused")
|
|
23
|
+
* public async pause
|
|
24
|
+
* (
|
|
25
|
+
* \@TypedParam("id", "uuid"), id: string & tags.Format<"uuid">,
|
|
26
|
+
* \@TypedParam("no") id: number & tags.Type<"uint32">
|
|
27
|
+
* \@TypedParam("paused") paused: boolean | null
|
|
28
|
+
* ): Promise<void>;
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @param name URL Parameter name
|
|
32
|
+
* @returns Parameter decorator
|
|
33
|
+
*
|
|
34
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
35
|
+
*/
|
|
36
|
+
export function TypedParam<T extends boolean | bigint | number | string | null>(
|
|
37
|
+
name: string,
|
|
38
|
+
assert?: (value: string) => T,
|
|
39
|
+
): ParameterDecorator {
|
|
40
|
+
if (assert === undefined) throw NoTransformConfigureError("TypedParam");
|
|
41
|
+
|
|
42
|
+
return createParamDecorator(function TypedParam(
|
|
43
|
+
{}: any,
|
|
44
|
+
context: ExecutionContext,
|
|
45
|
+
) {
|
|
46
|
+
const request: express.Request | FastifyRequest = context
|
|
47
|
+
.switchToHttp()
|
|
48
|
+
.getRequest();
|
|
49
|
+
const str: string = (request.params as any)[name];
|
|
50
|
+
try {
|
|
51
|
+
return assert(str);
|
|
52
|
+
} catch (exp) {
|
|
53
|
+
if (typia.is<TypeGuardError>(exp))
|
|
54
|
+
throw new BadRequestException({
|
|
55
|
+
path: exp.path,
|
|
56
|
+
reason: exp.message,
|
|
57
|
+
expected: exp.expected,
|
|
58
|
+
value: exp.value,
|
|
59
|
+
message: `Invalid URL parameter value on "${name}".`,
|
|
60
|
+
});
|
|
61
|
+
throw exp;
|
|
62
|
+
}
|
|
63
|
+
})(name);
|
|
64
|
+
}
|
|
65
|
+
Object.assign(TypedParam, typia.http.parameter);
|