@nestia/core 3.1.0-dev.20240429 → 3.1.0-dev.20240430-2
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/transformers/WebSocketRouteTransformer.js +3 -4
- package/lib/transformers/WebSocketRouteTransformer.js.map +1 -1
- package/package.json +3 -3
- package/src/decorators/DynamicModule.ts +39 -39
- package/src/decorators/EncryptedBody.ts +105 -105
- package/src/decorators/EncryptedController.ts +38 -38
- package/src/decorators/EncryptedModule.ts +96 -96
- package/src/decorators/EncryptedRoute.ts +182 -182
- package/src/decorators/PlainBody.ts +75 -75
- package/src/decorators/TypedBody.ts +62 -62
- package/src/decorators/TypedException.ts +90 -90
- package/src/decorators/TypedFormData.ts +219 -219
- package/src/decorators/TypedHeaders.ts +69 -69
- package/src/decorators/TypedParam.ts +64 -64
- 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_path_and_querify.ts +106 -106
- 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/index.ts +5 -5
- package/src/options/INestiaTransformOptions.ts +17 -17
- package/src/options/INestiaTransformProject.ts +7 -7
- package/src/options/IRequestBodyValidator.ts +20 -20
- package/src/options/IRequestFormDataProps.ts +27 -27
- 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 +71 -71
- 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/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 +97 -97
- package/src/transformers/NodeTransformer.ts +16 -16
- package/src/transformers/ParameterTransformer.ts +48 -48
- package/src/transformers/TypedExceptionTransformer.ts +44 -44
- package/src/transformers/TypedRouteTransformer.ts +81 -81
- package/src/transformers/WebSocketRouteTransformer.ts +8 -3
- 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,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,64 +1,64 @@
|
|
|
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
|
-
* \@TypedParam("id", "uuid"), id: string & tags.Format<"uuid">,
|
|
25
|
-
* \@TypedParam("no") id: number & tags.Type<"uint32">
|
|
26
|
-
* \@TypedParam("paused") paused: boolean | null
|
|
27
|
-
* ): Promise<void>;
|
|
28
|
-
* ```
|
|
29
|
-
*
|
|
30
|
-
* @param name URL Parameter name
|
|
31
|
-
* @returns Parameter decorator
|
|
32
|
-
*
|
|
33
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
34
|
-
*/
|
|
35
|
-
export function TypedParam<T extends boolean | bigint | number | string | null>(
|
|
36
|
-
name: string,
|
|
37
|
-
assert?: (value: string) => T,
|
|
38
|
-
): ParameterDecorator {
|
|
39
|
-
if (assert === undefined) throw NoTransformConfigureError("TypedParam");
|
|
40
|
-
|
|
41
|
-
return createParamDecorator(function TypedParam(
|
|
42
|
-
{}: any,
|
|
43
|
-
context: ExecutionContext,
|
|
44
|
-
) {
|
|
45
|
-
const request: express.Request | FastifyRequest = context
|
|
46
|
-
.switchToHttp()
|
|
47
|
-
.getRequest();
|
|
48
|
-
const str: string = (request.params as any)[name];
|
|
49
|
-
try {
|
|
50
|
-
return assert(str);
|
|
51
|
-
} catch (exp) {
|
|
52
|
-
if (typia.is<TypeGuardError>(exp))
|
|
53
|
-
throw new BadRequestException({
|
|
54
|
-
path: exp.path,
|
|
55
|
-
reason: exp.message,
|
|
56
|
-
expected: exp.expected,
|
|
57
|
-
value: exp.value,
|
|
58
|
-
message: `Invalid URL parameter value on "${name}".`,
|
|
59
|
-
});
|
|
60
|
-
throw exp;
|
|
61
|
-
}
|
|
62
|
-
})(name);
|
|
63
|
-
}
|
|
64
|
-
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
|
+
* \@TypedParam("id", "uuid"), id: string & tags.Format<"uuid">,
|
|
25
|
+
* \@TypedParam("no") id: number & tags.Type<"uint32">
|
|
26
|
+
* \@TypedParam("paused") paused: boolean | null
|
|
27
|
+
* ): Promise<void>;
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @param name URL Parameter name
|
|
31
|
+
* @returns Parameter decorator
|
|
32
|
+
*
|
|
33
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
34
|
+
*/
|
|
35
|
+
export function TypedParam<T extends boolean | bigint | number | string | null>(
|
|
36
|
+
name: string,
|
|
37
|
+
assert?: (value: string) => T,
|
|
38
|
+
): ParameterDecorator {
|
|
39
|
+
if (assert === undefined) throw NoTransformConfigureError("TypedParam");
|
|
40
|
+
|
|
41
|
+
return createParamDecorator(function TypedParam(
|
|
42
|
+
{}: any,
|
|
43
|
+
context: ExecutionContext,
|
|
44
|
+
) {
|
|
45
|
+
const request: express.Request | FastifyRequest = context
|
|
46
|
+
.switchToHttp()
|
|
47
|
+
.getRequest();
|
|
48
|
+
const str: string = (request.params as any)[name];
|
|
49
|
+
try {
|
|
50
|
+
return assert(str);
|
|
51
|
+
} catch (exp) {
|
|
52
|
+
if (typia.is<TypeGuardError>(exp))
|
|
53
|
+
throw new BadRequestException({
|
|
54
|
+
path: exp.path,
|
|
55
|
+
reason: exp.message,
|
|
56
|
+
expected: exp.expected,
|
|
57
|
+
value: exp.value,
|
|
58
|
+
message: `Invalid URL parameter value on "${name}".`,
|
|
59
|
+
});
|
|
60
|
+
throw exp;
|
|
61
|
+
}
|
|
62
|
+
})(name);
|
|
63
|
+
}
|
|
64
|
+
Object.assign(TypedParam, typia.http.parameter);
|
|
@@ -1,144 +1,144 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CallHandler,
|
|
3
|
-
Delete,
|
|
4
|
-
ExecutionContext,
|
|
5
|
-
Get,
|
|
6
|
-
NestInterceptor,
|
|
7
|
-
Patch,
|
|
8
|
-
Post,
|
|
9
|
-
Put,
|
|
10
|
-
UseInterceptors,
|
|
11
|
-
applyDecorators,
|
|
12
|
-
} from "@nestjs/common";
|
|
13
|
-
import { HttpArgumentsHost } from "@nestjs/common/interfaces";
|
|
14
|
-
import type express from "express";
|
|
15
|
-
import { catchError, map } from "rxjs/operators";
|
|
16
|
-
import typia from "typia";
|
|
17
|
-
|
|
18
|
-
import { IResponseBodyStringifier } from "../options/IResponseBodyStringifier";
|
|
19
|
-
import { get_path_and_stringify } from "./internal/get_path_and_stringify";
|
|
20
|
-
import { route_error } from "./internal/route_error";
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Type safe router decorator functions.
|
|
24
|
-
*
|
|
25
|
-
* `TypedRoute` is a module containing router decorator functions which can boost up
|
|
26
|
-
* JSON string conversion speed about 200x times faster than `class-transformer`.
|
|
27
|
-
* Furthermore, such JSON string conversion is even type safe through
|
|
28
|
-
* [typia](https://github.com/samchon/typia).
|
|
29
|
-
*
|
|
30
|
-
* For reference, if you try to invalid data that is not following the promised
|
|
31
|
-
* type `T`, 500 internal server error would be thrown. Also, as `TypedRoute` composes
|
|
32
|
-
* JSON string through `typia.assertStringify<T>()` function, it is not possible to
|
|
33
|
-
* modify response data through interceptors.
|
|
34
|
-
*
|
|
35
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
36
|
-
*/
|
|
37
|
-
export namespace TypedRoute {
|
|
38
|
-
/**
|
|
39
|
-
* Router decorator function for the GET method.
|
|
40
|
-
*
|
|
41
|
-
* @param path Path of the HTTP request
|
|
42
|
-
* @returns Method decorator
|
|
43
|
-
*/
|
|
44
|
-
export const Get = Generator("Get");
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Router decorator function for the POST method.
|
|
48
|
-
*
|
|
49
|
-
* @param path Path of the HTTP request
|
|
50
|
-
* @returns Method decorator
|
|
51
|
-
*/
|
|
52
|
-
export const Post = Generator("Post");
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Router decorator function for the PATH method.
|
|
56
|
-
*
|
|
57
|
-
* @param path Path of the HTTP request
|
|
58
|
-
* @returns Method decorator
|
|
59
|
-
*/
|
|
60
|
-
export const Patch = Generator("Patch");
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Router decorator function for the PUT method.
|
|
64
|
-
*
|
|
65
|
-
* @param path Path of the HTTP request
|
|
66
|
-
* @returns Method decorator
|
|
67
|
-
*/
|
|
68
|
-
export const Put = Generator("Put");
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Router decorator function for the DELETE method.
|
|
72
|
-
*
|
|
73
|
-
* @param path Path of the HTTP request
|
|
74
|
-
* @returns Method decorator
|
|
75
|
-
*/
|
|
76
|
-
export const Delete = Generator("Delete");
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* @internal
|
|
80
|
-
*/
|
|
81
|
-
function Generator(method: "Get" | "Post" | "Put" | "Patch" | "Delete") {
|
|
82
|
-
function route(path?: string | string[]): MethodDecorator;
|
|
83
|
-
function route<T>(stringify?: IResponseBodyStringifier<T>): MethodDecorator;
|
|
84
|
-
function route<T>(
|
|
85
|
-
path: string | string[],
|
|
86
|
-
stringify?: IResponseBodyStringifier<T>,
|
|
87
|
-
): MethodDecorator;
|
|
88
|
-
|
|
89
|
-
function route(...args: any[]): MethodDecorator {
|
|
90
|
-
const [path, stringify] = get_path_and_stringify(`TypedRoute.${method}`)(
|
|
91
|
-
...args,
|
|
92
|
-
);
|
|
93
|
-
return applyDecorators(
|
|
94
|
-
ROUTERS[method](path),
|
|
95
|
-
UseInterceptors(new TypedRouteInterceptor(stringify)),
|
|
96
|
-
);
|
|
97
|
-
}
|
|
98
|
-
return route;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
for (const method of [
|
|
102
|
-
typia.json.stringify,
|
|
103
|
-
typia.json.isStringify,
|
|
104
|
-
typia.json.assertStringify,
|
|
105
|
-
typia.json.validateStringify,
|
|
106
|
-
])
|
|
107
|
-
for (const [key, value] of Object.entries(method))
|
|
108
|
-
for (const deco of [
|
|
109
|
-
TypedRoute.Get,
|
|
110
|
-
TypedRoute.Delete,
|
|
111
|
-
TypedRoute.Post,
|
|
112
|
-
TypedRoute.Put,
|
|
113
|
-
TypedRoute.Patch,
|
|
114
|
-
])
|
|
115
|
-
(deco as any)[key] = value;
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* @internal
|
|
119
|
-
*/
|
|
120
|
-
class TypedRouteInterceptor implements NestInterceptor {
|
|
121
|
-
public constructor(private readonly stringify: (input: any) => string) {}
|
|
122
|
-
|
|
123
|
-
public intercept(context: ExecutionContext, next: CallHandler) {
|
|
124
|
-
const http: HttpArgumentsHost = context.switchToHttp();
|
|
125
|
-
const response: express.Response = http.getResponse();
|
|
126
|
-
response.header("Content-Type", "application/json");
|
|
127
|
-
|
|
128
|
-
return next.handle().pipe(
|
|
129
|
-
map((value) => this.stringify(value)),
|
|
130
|
-
catchError((err) => route_error(http.getRequest(), err)),
|
|
131
|
-
);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* @internal
|
|
137
|
-
*/
|
|
138
|
-
const ROUTERS = {
|
|
139
|
-
Get,
|
|
140
|
-
Post,
|
|
141
|
-
Patch,
|
|
142
|
-
Put,
|
|
143
|
-
Delete,
|
|
144
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
CallHandler,
|
|
3
|
+
Delete,
|
|
4
|
+
ExecutionContext,
|
|
5
|
+
Get,
|
|
6
|
+
NestInterceptor,
|
|
7
|
+
Patch,
|
|
8
|
+
Post,
|
|
9
|
+
Put,
|
|
10
|
+
UseInterceptors,
|
|
11
|
+
applyDecorators,
|
|
12
|
+
} from "@nestjs/common";
|
|
13
|
+
import { HttpArgumentsHost } from "@nestjs/common/interfaces";
|
|
14
|
+
import type express from "express";
|
|
15
|
+
import { catchError, map } from "rxjs/operators";
|
|
16
|
+
import typia from "typia";
|
|
17
|
+
|
|
18
|
+
import { IResponseBodyStringifier } from "../options/IResponseBodyStringifier";
|
|
19
|
+
import { get_path_and_stringify } from "./internal/get_path_and_stringify";
|
|
20
|
+
import { route_error } from "./internal/route_error";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Type safe router decorator functions.
|
|
24
|
+
*
|
|
25
|
+
* `TypedRoute` is a module containing router decorator functions which can boost up
|
|
26
|
+
* JSON string conversion speed about 200x times faster than `class-transformer`.
|
|
27
|
+
* Furthermore, such JSON string conversion is even type safe through
|
|
28
|
+
* [typia](https://github.com/samchon/typia).
|
|
29
|
+
*
|
|
30
|
+
* For reference, if you try to invalid data that is not following the promised
|
|
31
|
+
* type `T`, 500 internal server error would be thrown. Also, as `TypedRoute` composes
|
|
32
|
+
* JSON string through `typia.assertStringify<T>()` function, it is not possible to
|
|
33
|
+
* modify response data through interceptors.
|
|
34
|
+
*
|
|
35
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
36
|
+
*/
|
|
37
|
+
export namespace TypedRoute {
|
|
38
|
+
/**
|
|
39
|
+
* Router decorator function for the GET method.
|
|
40
|
+
*
|
|
41
|
+
* @param path Path of the HTTP request
|
|
42
|
+
* @returns Method decorator
|
|
43
|
+
*/
|
|
44
|
+
export const Get = Generator("Get");
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Router decorator function for the POST method.
|
|
48
|
+
*
|
|
49
|
+
* @param path Path of the HTTP request
|
|
50
|
+
* @returns Method decorator
|
|
51
|
+
*/
|
|
52
|
+
export const Post = Generator("Post");
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Router decorator function for the PATH method.
|
|
56
|
+
*
|
|
57
|
+
* @param path Path of the HTTP request
|
|
58
|
+
* @returns Method decorator
|
|
59
|
+
*/
|
|
60
|
+
export const Patch = Generator("Patch");
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Router decorator function for the PUT method.
|
|
64
|
+
*
|
|
65
|
+
* @param path Path of the HTTP request
|
|
66
|
+
* @returns Method decorator
|
|
67
|
+
*/
|
|
68
|
+
export const Put = Generator("Put");
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Router decorator function for the DELETE method.
|
|
72
|
+
*
|
|
73
|
+
* @param path Path of the HTTP request
|
|
74
|
+
* @returns Method decorator
|
|
75
|
+
*/
|
|
76
|
+
export const Delete = Generator("Delete");
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @internal
|
|
80
|
+
*/
|
|
81
|
+
function Generator(method: "Get" | "Post" | "Put" | "Patch" | "Delete") {
|
|
82
|
+
function route(path?: string | string[]): MethodDecorator;
|
|
83
|
+
function route<T>(stringify?: IResponseBodyStringifier<T>): MethodDecorator;
|
|
84
|
+
function route<T>(
|
|
85
|
+
path: string | string[],
|
|
86
|
+
stringify?: IResponseBodyStringifier<T>,
|
|
87
|
+
): MethodDecorator;
|
|
88
|
+
|
|
89
|
+
function route(...args: any[]): MethodDecorator {
|
|
90
|
+
const [path, stringify] = get_path_and_stringify(`TypedRoute.${method}`)(
|
|
91
|
+
...args,
|
|
92
|
+
);
|
|
93
|
+
return applyDecorators(
|
|
94
|
+
ROUTERS[method](path),
|
|
95
|
+
UseInterceptors(new TypedRouteInterceptor(stringify)),
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
return route;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
for (const method of [
|
|
102
|
+
typia.json.stringify,
|
|
103
|
+
typia.json.isStringify,
|
|
104
|
+
typia.json.assertStringify,
|
|
105
|
+
typia.json.validateStringify,
|
|
106
|
+
])
|
|
107
|
+
for (const [key, value] of Object.entries(method))
|
|
108
|
+
for (const deco of [
|
|
109
|
+
TypedRoute.Get,
|
|
110
|
+
TypedRoute.Delete,
|
|
111
|
+
TypedRoute.Post,
|
|
112
|
+
TypedRoute.Put,
|
|
113
|
+
TypedRoute.Patch,
|
|
114
|
+
])
|
|
115
|
+
(deco as any)[key] = value;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @internal
|
|
119
|
+
*/
|
|
120
|
+
class TypedRouteInterceptor implements NestInterceptor {
|
|
121
|
+
public constructor(private readonly stringify: (input: any) => string) {}
|
|
122
|
+
|
|
123
|
+
public intercept(context: ExecutionContext, next: CallHandler) {
|
|
124
|
+
const http: HttpArgumentsHost = context.switchToHttp();
|
|
125
|
+
const response: express.Response = http.getResponse();
|
|
126
|
+
response.header("Content-Type", "application/json");
|
|
127
|
+
|
|
128
|
+
return next.handle().pipe(
|
|
129
|
+
map((value) => this.stringify(value)),
|
|
130
|
+
catchError((err) => route_error(http.getRequest(), err)),
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* @internal
|
|
137
|
+
*/
|
|
138
|
+
const ROUTERS = {
|
|
139
|
+
Get,
|
|
140
|
+
Post,
|
|
141
|
+
Patch,
|
|
142
|
+
Put,
|
|
143
|
+
Delete,
|
|
144
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @internal
|
|
3
|
-
*/
|
|
4
|
-
export const ENCRYPTION_METADATA_KEY = "nestia:core:encryption:password";
|
|
1
|
+
/**
|
|
2
|
+
* @internal
|
|
3
|
+
*/
|
|
4
|
+
export const ENCRYPTION_METADATA_KEY = "nestia:core:encryption:password";
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @internal
|
|
3
|
-
*/
|
|
4
|
-
export function NoTransformConfigureError(method: string): Error {
|
|
5
|
-
return new Error(
|
|
6
|
-
`Error on nestia.core.${method}(): no transform has been configured. Run "npx typia setup" command, or check if you're using non-standard TypeScript compiler like Babel or SWC.`,
|
|
7
|
-
);
|
|
8
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @internal
|
|
3
|
+
*/
|
|
4
|
+
export function NoTransformConfigureError(method: string): Error {
|
|
5
|
+
return new Error(
|
|
6
|
+
`Error on nestia.core.${method}(): no transform has been configured. Run "npx typia setup" command, or check if you're using non-standard TypeScript compiler like Babel or SWC.`,
|
|
7
|
+
);
|
|
8
|
+
}
|