@nestia/core 0.1.5 → 0.1.7
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/LICENSE +1 -1
- package/README.md +336 -0
- package/lib/decorators/PlainBody.d.ts +1 -1
- package/lib/decorators/PlainBody.js +1 -1
- package/lib/executable/core.d.ts +2 -0
- package/lib/executable/core.js +100 -0
- package/lib/executable/core.js.map +1 -0
- package/lib/executable/internal/CommandParser.d.ts +3 -0
- package/lib/executable/internal/CommandParser.js +21 -0
- package/lib/executable/internal/CommandParser.js.map +1 -0
- package/lib/executable/internal/CoreSetupWizard.d.ts +4 -0
- package/lib/executable/internal/CoreSetupWizard.js +259 -0
- package/lib/executable/internal/CoreSetupWizard.js.map +1 -0
- package/package.json +5 -1
- package/src/decorators/EncryptedBody.ts +102 -102
- package/src/decorators/EncryptedController.ts +43 -43
- package/src/decorators/EncryptedModule.ts +77 -77
- package/src/decorators/EncryptedRoute.ts +203 -203
- package/src/decorators/PlainBody.ts +38 -38
- package/src/decorators/TypedBody.ts +49 -49
- package/src/decorators/TypedParam.ts +70 -70
- package/src/decorators/TypedRoute.ts +149 -149
- package/src/decorators/internal/EncryptedConstant.ts +4 -4
- package/src/decorators/internal/get_path_and_stringify.ts +77 -77
- package/src/decorators/internal/headers_to_object.ts +10 -10
- package/src/decorators/internal/route_error.ts +38 -38
- package/src/decorators/internal/validate_request_body.ts +59 -59
- package/src/executable/core.ts +46 -0
- package/src/executable/internal/CommandParser.ts +15 -0
- package/src/executable/internal/CoreSetupWizard.ts +177 -0
- package/src/index.ts +5 -5
- package/src/module.ts +11 -11
- package/src/options/INestiaTransformOptions.ts +6 -6
- package/src/options/INestiaTransformProject.ts +7 -7
- package/src/options/IRequestBodyValidator.ts +20 -20
- package/src/options/IResponseBodyStringifier.ts +25 -25
- package/src/transform.ts +20 -20
- package/src/transformers/BodyTransformer.ts +106 -106
- package/src/transformers/FileTransformer.ts +49 -49
- package/src/transformers/MethodTransformer.ts +91 -91
- package/src/transformers/NodeTransformer.ts +18 -18
- package/src/transformers/ParameterTransformer.ts +45 -45
- package/src/transformers/RouteTransformer.ts +131 -131
- package/src/typings/Creator.ts +3 -3
- package/src/utils/ExceptionManager.ts +126 -126
- package/src/utils/Singleton.ts +20 -20
|
@@ -1,49 +1,49 @@
|
|
|
1
|
-
import {
|
|
2
|
-
BadRequestException,
|
|
3
|
-
ExecutionContext,
|
|
4
|
-
createParamDecorator,
|
|
5
|
-
} from "@nestjs/common";
|
|
6
|
-
import type express from "express";
|
|
7
|
-
import raw from "raw-body";
|
|
8
|
-
import { assert, is, validate } from "typia";
|
|
9
|
-
|
|
10
|
-
import { IRequestBodyValidator } from "../options/IRequestBodyValidator";
|
|
11
|
-
import { validate_request_body } from "./internal/validate_request_body";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Safe body decorator.
|
|
15
|
-
*
|
|
16
|
-
* `TypedBody` is a decorator function getting JSON data from HTTP request. Also,
|
|
17
|
-
* it validates the JSON data type through
|
|
18
|
-
* [`typia.assert()`](https://github.com/samchon/typia#runtime-type-checkers)
|
|
19
|
-
* function and throws `BadRequestException` error (status code: 400), if the JSON
|
|
20
|
-
* data is not following the promised type.
|
|
21
|
-
*
|
|
22
|
-
* @param validator Custom validator if required. Default is `typia.assert()`
|
|
23
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
24
|
-
*/
|
|
25
|
-
export function TypedBody<T>(
|
|
26
|
-
validator?: IRequestBodyValidator<T>,
|
|
27
|
-
): ParameterDecorator {
|
|
28
|
-
const checker = validate_request_body("TypedBody")(validator);
|
|
29
|
-
return createParamDecorator(async function TypedBody(
|
|
30
|
-
_unknown: any,
|
|
31
|
-
context: ExecutionContext,
|
|
32
|
-
) {
|
|
33
|
-
const request: express.Request = context.switchToHttp().getRequest();
|
|
34
|
-
if (request.headers["content-type"] !== "application/json") {
|
|
35
|
-
throw new BadRequestException(
|
|
36
|
-
"Request body is not the application/json.",
|
|
37
|
-
);
|
|
38
|
-
}
|
|
39
|
-
const data: any = request.body
|
|
40
|
-
? request.body
|
|
41
|
-
: JSON.parse((await raw(request, "utf8")).trim());
|
|
42
|
-
checker(data);
|
|
43
|
-
return data;
|
|
44
|
-
})();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
Object.assign(TypedBody, assert);
|
|
48
|
-
Object.assign(TypedBody, is);
|
|
49
|
-
Object.assign(TypedBody, validate);
|
|
1
|
+
import {
|
|
2
|
+
BadRequestException,
|
|
3
|
+
ExecutionContext,
|
|
4
|
+
createParamDecorator,
|
|
5
|
+
} from "@nestjs/common";
|
|
6
|
+
import type express from "express";
|
|
7
|
+
import raw from "raw-body";
|
|
8
|
+
import { assert, is, validate } from "typia";
|
|
9
|
+
|
|
10
|
+
import { IRequestBodyValidator } from "../options/IRequestBodyValidator";
|
|
11
|
+
import { validate_request_body } from "./internal/validate_request_body";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Safe body decorator.
|
|
15
|
+
*
|
|
16
|
+
* `TypedBody` is a decorator function getting JSON data from HTTP request. Also,
|
|
17
|
+
* it validates the JSON data type through
|
|
18
|
+
* [`typia.assert()`](https://github.com/samchon/typia#runtime-type-checkers)
|
|
19
|
+
* function and throws `BadRequestException` error (status code: 400), if the JSON
|
|
20
|
+
* data is not following the promised type.
|
|
21
|
+
*
|
|
22
|
+
* @param validator Custom validator if required. Default is `typia.assert()`
|
|
23
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
24
|
+
*/
|
|
25
|
+
export function TypedBody<T>(
|
|
26
|
+
validator?: IRequestBodyValidator<T>,
|
|
27
|
+
): ParameterDecorator {
|
|
28
|
+
const checker = validate_request_body("TypedBody")(validator);
|
|
29
|
+
return createParamDecorator(async function TypedBody(
|
|
30
|
+
_unknown: any,
|
|
31
|
+
context: ExecutionContext,
|
|
32
|
+
) {
|
|
33
|
+
const request: express.Request = context.switchToHttp().getRequest();
|
|
34
|
+
if (request.headers["content-type"] !== "application/json") {
|
|
35
|
+
throw new BadRequestException(
|
|
36
|
+
"Request body is not the application/json.",
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
const data: any = request.body
|
|
40
|
+
? request.body
|
|
41
|
+
: JSON.parse((await raw(request, "utf8")).trim());
|
|
42
|
+
checker(data);
|
|
43
|
+
return data;
|
|
44
|
+
})();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
Object.assign(TypedBody, assert);
|
|
48
|
+
Object.assign(TypedBody, is);
|
|
49
|
+
Object.assign(TypedBody, validate);
|
|
@@ -1,70 +1,70 @@
|
|
|
1
|
-
import {
|
|
2
|
-
BadRequestException,
|
|
3
|
-
ExecutionContext,
|
|
4
|
-
createParamDecorator,
|
|
5
|
-
} from "@nestjs/common";
|
|
6
|
-
import type express from "express";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* URL parameter decorator with type.
|
|
10
|
-
*
|
|
11
|
-
* `TypedParam` is a decorator function getting specific typed parameter from the HTTP
|
|
12
|
-
* request URL. It's almost same with the {@link nest.Param}, but `TypedParam` can specify
|
|
13
|
-
* the parameter type manually. Beside, the {@link nest.Param} always parses all of the
|
|
14
|
-
* parameters as string type.
|
|
15
|
-
*
|
|
16
|
-
* ```typescript
|
|
17
|
-
* \@TypedRoute.Get("shopping/sales/:section/:id/:paused")
|
|
18
|
-
* public async pause
|
|
19
|
-
* (
|
|
20
|
-
* \@TypedParam("section", "string") section: string,
|
|
21
|
-
* \@TypedParam("id", "number") id: number,
|
|
22
|
-
* \@TypedParam("paused", "boolean", true) paused: boolean | null
|
|
23
|
-
* ): Promise<void>;
|
|
24
|
-
* ```
|
|
25
|
-
*
|
|
26
|
-
* @param name URL Parameter name
|
|
27
|
-
* @param type Type of the URL parameter
|
|
28
|
-
* @returns Parameter decorator
|
|
29
|
-
*
|
|
30
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
31
|
-
*/
|
|
32
|
-
export function TypedParam(
|
|
33
|
-
name: string,
|
|
34
|
-
type: "boolean" | "number" | "string" | "uuid" = "string",
|
|
35
|
-
nullable: boolean = false,
|
|
36
|
-
) {
|
|
37
|
-
return createParamDecorator(function TypedParam(
|
|
38
|
-
{}: any,
|
|
39
|
-
ctx: ExecutionContext,
|
|
40
|
-
) {
|
|
41
|
-
const request: express.Request = ctx.switchToHttp().getRequest();
|
|
42
|
-
const str: string = request.params[name];
|
|
43
|
-
|
|
44
|
-
if (nullable === true && str === "null") return null;
|
|
45
|
-
else if (type === "boolean") {
|
|
46
|
-
if (str === "true" || str === "1") return true;
|
|
47
|
-
else if (str === "false" || str === "0") return false;
|
|
48
|
-
else
|
|
49
|
-
throw new BadRequestException(
|
|
50
|
-
`Value of the URL parameter '${name}' is not a boolean.`,
|
|
51
|
-
);
|
|
52
|
-
} else if (type === "number") {
|
|
53
|
-
const value: number = Number(str);
|
|
54
|
-
if (isNaN(value))
|
|
55
|
-
throw new BadRequestException(
|
|
56
|
-
`Value of the URL parameter "${name}" is not a number.`,
|
|
57
|
-
);
|
|
58
|
-
return value;
|
|
59
|
-
} else if (type === "uuid") {
|
|
60
|
-
if (UUID_PATTERN.test(str) === false)
|
|
61
|
-
throw new BadRequestException(
|
|
62
|
-
`Value of the URL parameter "${name}" is not a valid UUID.`,
|
|
63
|
-
);
|
|
64
|
-
return str;
|
|
65
|
-
} else return str;
|
|
66
|
-
})(name);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const UUID_PATTERN =
|
|
70
|
-
/^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i;
|
|
1
|
+
import {
|
|
2
|
+
BadRequestException,
|
|
3
|
+
ExecutionContext,
|
|
4
|
+
createParamDecorator,
|
|
5
|
+
} from "@nestjs/common";
|
|
6
|
+
import type express from "express";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* URL parameter decorator with type.
|
|
10
|
+
*
|
|
11
|
+
* `TypedParam` is a decorator function getting specific typed parameter from the HTTP
|
|
12
|
+
* request URL. It's almost same with the {@link nest.Param}, but `TypedParam` can specify
|
|
13
|
+
* the parameter type manually. Beside, the {@link nest.Param} always parses all of the
|
|
14
|
+
* parameters as string type.
|
|
15
|
+
*
|
|
16
|
+
* ```typescript
|
|
17
|
+
* \@TypedRoute.Get("shopping/sales/:section/:id/:paused")
|
|
18
|
+
* public async pause
|
|
19
|
+
* (
|
|
20
|
+
* \@TypedParam("section", "string") section: string,
|
|
21
|
+
* \@TypedParam("id", "number") id: number,
|
|
22
|
+
* \@TypedParam("paused", "boolean", true) paused: boolean | null
|
|
23
|
+
* ): Promise<void>;
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @param name URL Parameter name
|
|
27
|
+
* @param type Type of the URL parameter
|
|
28
|
+
* @returns Parameter decorator
|
|
29
|
+
*
|
|
30
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
31
|
+
*/
|
|
32
|
+
export function TypedParam(
|
|
33
|
+
name: string,
|
|
34
|
+
type: "boolean" | "number" | "string" | "uuid" = "string",
|
|
35
|
+
nullable: boolean = false,
|
|
36
|
+
) {
|
|
37
|
+
return createParamDecorator(function TypedParam(
|
|
38
|
+
{}: any,
|
|
39
|
+
ctx: ExecutionContext,
|
|
40
|
+
) {
|
|
41
|
+
const request: express.Request = ctx.switchToHttp().getRequest();
|
|
42
|
+
const str: string = request.params[name];
|
|
43
|
+
|
|
44
|
+
if (nullable === true && str === "null") return null;
|
|
45
|
+
else if (type === "boolean") {
|
|
46
|
+
if (str === "true" || str === "1") return true;
|
|
47
|
+
else if (str === "false" || str === "0") return false;
|
|
48
|
+
else
|
|
49
|
+
throw new BadRequestException(
|
|
50
|
+
`Value of the URL parameter '${name}' is not a boolean.`,
|
|
51
|
+
);
|
|
52
|
+
} else if (type === "number") {
|
|
53
|
+
const value: number = Number(str);
|
|
54
|
+
if (isNaN(value))
|
|
55
|
+
throw new BadRequestException(
|
|
56
|
+
`Value of the URL parameter "${name}" is not a number.`,
|
|
57
|
+
);
|
|
58
|
+
return value;
|
|
59
|
+
} else if (type === "uuid") {
|
|
60
|
+
if (UUID_PATTERN.test(str) === false)
|
|
61
|
+
throw new BadRequestException(
|
|
62
|
+
`Value of the URL parameter "${name}" is not a valid UUID.`,
|
|
63
|
+
);
|
|
64
|
+
return str;
|
|
65
|
+
} else return str;
|
|
66
|
+
})(name);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const UUID_PATTERN =
|
|
70
|
+
/^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i;
|
|
@@ -1,149 +1,149 @@
|
|
|
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 express from "express";
|
|
15
|
-
import { Observable, catchError, map } from "rxjs";
|
|
16
|
-
import {
|
|
17
|
-
assertStringify,
|
|
18
|
-
isStringify,
|
|
19
|
-
stringify,
|
|
20
|
-
validateStringify,
|
|
21
|
-
} from "typia";
|
|
22
|
-
|
|
23
|
-
import { IResponseBodyStringifier } from "../options/IResponseBodyStringifier";
|
|
24
|
-
import { get_path_and_stringify } from "./internal/get_path_and_stringify";
|
|
25
|
-
import { route_error } from "./internal/route_error";
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Safe router decorator functions.
|
|
29
|
-
*
|
|
30
|
-
* `TypedRoute` is a module containing router decorator functions which can boost up
|
|
31
|
-
* JSON string conversion speed about 5x times faster, through
|
|
32
|
-
* [`typia.stringify()`](https://github.com/samchon/typia#fastest-json-string-conversion).
|
|
33
|
-
*
|
|
34
|
-
* Also, router functions in `TypedRoute` can convert custom error classes to the
|
|
35
|
-
* regular {@link nest.HttpException} class automatically, through
|
|
36
|
-
* {@link ExceptionManager}.
|
|
37
|
-
*
|
|
38
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
39
|
-
*/
|
|
40
|
-
export namespace TypedRoute {
|
|
41
|
-
/**
|
|
42
|
-
* Router decorator function for the GET method.
|
|
43
|
-
*
|
|
44
|
-
* @param path Path of the HTTP request
|
|
45
|
-
* @returns Method decorator
|
|
46
|
-
*/
|
|
47
|
-
export const Get = Generator("Get");
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Router decorator function for the POST method.
|
|
51
|
-
*
|
|
52
|
-
* @param path Path of the HTTP request
|
|
53
|
-
* @returns Method decorator
|
|
54
|
-
*/
|
|
55
|
-
export const Post = Generator("Post");
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Router decorator function for the PATH method.
|
|
59
|
-
*
|
|
60
|
-
* @param path Path of the HTTP request
|
|
61
|
-
* @returns Method decorator
|
|
62
|
-
*/
|
|
63
|
-
export const Patch = Generator("Patch");
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Router decorator function for the PUT method.
|
|
67
|
-
*
|
|
68
|
-
* @param path Path of the HTTP request
|
|
69
|
-
* @returns Method decorator
|
|
70
|
-
*/
|
|
71
|
-
export const Put = Generator("Put");
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Router decorator function for the DELETE method.
|
|
75
|
-
*
|
|
76
|
-
* @param path Path of the HTTP request
|
|
77
|
-
* @returns Method decorator
|
|
78
|
-
*/
|
|
79
|
-
export const Delete = Generator("Delete");
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* @internal
|
|
83
|
-
*/
|
|
84
|
-
function Generator(method: "Get" | "Post" | "Put" | "Patch" | "Delete") {
|
|
85
|
-
function route(path?: string | string[]): MethodDecorator;
|
|
86
|
-
function route<T>(
|
|
87
|
-
stringify?: IResponseBodyStringifier<T>,
|
|
88
|
-
): MethodDecorator;
|
|
89
|
-
function route<T>(
|
|
90
|
-
path: string | string[],
|
|
91
|
-
stringify?: IResponseBodyStringifier<T>,
|
|
92
|
-
): MethodDecorator;
|
|
93
|
-
|
|
94
|
-
function route(...args: any[]): MethodDecorator {
|
|
95
|
-
const [path, stringify] = get_path_and_stringify(
|
|
96
|
-
`TypedRoute.${method}`,
|
|
97
|
-
)(...args);
|
|
98
|
-
return applyDecorators(
|
|
99
|
-
ROUTERS[method](path),
|
|
100
|
-
UseInterceptors(new TypedRouteInterceptor(stringify)),
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
return route;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
for (const method of [
|
|
107
|
-
assertStringify,
|
|
108
|
-
isStringify,
|
|
109
|
-
stringify,
|
|
110
|
-
validateStringify,
|
|
111
|
-
]) {
|
|
112
|
-
Object.assign(TypedRoute.Get, method);
|
|
113
|
-
Object.assign(TypedRoute.Delete, method);
|
|
114
|
-
Object.assign(TypedRoute.Post, method);
|
|
115
|
-
Object.assign(TypedRoute.Put, method);
|
|
116
|
-
Object.assign(TypedRoute.Patch, method);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* @internal
|
|
121
|
-
*/
|
|
122
|
-
class TypedRouteInterceptor implements NestInterceptor {
|
|
123
|
-
public constructor(private readonly stringify: (input: any) => string) {}
|
|
124
|
-
|
|
125
|
-
public intercept(
|
|
126
|
-
context: ExecutionContext,
|
|
127
|
-
next: CallHandler,
|
|
128
|
-
): Observable<any> {
|
|
129
|
-
const http: HttpArgumentsHost = context.switchToHttp();
|
|
130
|
-
const response: express.Response = http.getResponse();
|
|
131
|
-
response.header("Content-Type", "application/json");
|
|
132
|
-
|
|
133
|
-
return next.handle().pipe(
|
|
134
|
-
map((value) => this.stringify(value)),
|
|
135
|
-
catchError((err) => route_error(http.getRequest(), err)),
|
|
136
|
-
);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* @internal
|
|
142
|
-
*/
|
|
143
|
-
const ROUTERS = {
|
|
144
|
-
Get,
|
|
145
|
-
Post,
|
|
146
|
-
Patch,
|
|
147
|
-
Put,
|
|
148
|
-
Delete,
|
|
149
|
-
};
|
|
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 express from "express";
|
|
15
|
+
import { Observable, catchError, map } from "rxjs";
|
|
16
|
+
import {
|
|
17
|
+
assertStringify,
|
|
18
|
+
isStringify,
|
|
19
|
+
stringify,
|
|
20
|
+
validateStringify,
|
|
21
|
+
} from "typia";
|
|
22
|
+
|
|
23
|
+
import { IResponseBodyStringifier } from "../options/IResponseBodyStringifier";
|
|
24
|
+
import { get_path_and_stringify } from "./internal/get_path_and_stringify";
|
|
25
|
+
import { route_error } from "./internal/route_error";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Safe router decorator functions.
|
|
29
|
+
*
|
|
30
|
+
* `TypedRoute` is a module containing router decorator functions which can boost up
|
|
31
|
+
* JSON string conversion speed about 5x times faster, through
|
|
32
|
+
* [`typia.stringify()`](https://github.com/samchon/typia#fastest-json-string-conversion).
|
|
33
|
+
*
|
|
34
|
+
* Also, router functions in `TypedRoute` can convert custom error classes to the
|
|
35
|
+
* regular {@link nest.HttpException} class automatically, through
|
|
36
|
+
* {@link ExceptionManager}.
|
|
37
|
+
*
|
|
38
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
39
|
+
*/
|
|
40
|
+
export namespace TypedRoute {
|
|
41
|
+
/**
|
|
42
|
+
* Router decorator function for the GET method.
|
|
43
|
+
*
|
|
44
|
+
* @param path Path of the HTTP request
|
|
45
|
+
* @returns Method decorator
|
|
46
|
+
*/
|
|
47
|
+
export const Get = Generator("Get");
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Router decorator function for the POST method.
|
|
51
|
+
*
|
|
52
|
+
* @param path Path of the HTTP request
|
|
53
|
+
* @returns Method decorator
|
|
54
|
+
*/
|
|
55
|
+
export const Post = Generator("Post");
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Router decorator function for the PATH method.
|
|
59
|
+
*
|
|
60
|
+
* @param path Path of the HTTP request
|
|
61
|
+
* @returns Method decorator
|
|
62
|
+
*/
|
|
63
|
+
export const Patch = Generator("Patch");
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Router decorator function for the PUT method.
|
|
67
|
+
*
|
|
68
|
+
* @param path Path of the HTTP request
|
|
69
|
+
* @returns Method decorator
|
|
70
|
+
*/
|
|
71
|
+
export const Put = Generator("Put");
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Router decorator function for the DELETE method.
|
|
75
|
+
*
|
|
76
|
+
* @param path Path of the HTTP request
|
|
77
|
+
* @returns Method decorator
|
|
78
|
+
*/
|
|
79
|
+
export const Delete = Generator("Delete");
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @internal
|
|
83
|
+
*/
|
|
84
|
+
function Generator(method: "Get" | "Post" | "Put" | "Patch" | "Delete") {
|
|
85
|
+
function route(path?: string | string[]): MethodDecorator;
|
|
86
|
+
function route<T>(
|
|
87
|
+
stringify?: IResponseBodyStringifier<T>,
|
|
88
|
+
): MethodDecorator;
|
|
89
|
+
function route<T>(
|
|
90
|
+
path: string | string[],
|
|
91
|
+
stringify?: IResponseBodyStringifier<T>,
|
|
92
|
+
): MethodDecorator;
|
|
93
|
+
|
|
94
|
+
function route(...args: any[]): MethodDecorator {
|
|
95
|
+
const [path, stringify] = get_path_and_stringify(
|
|
96
|
+
`TypedRoute.${method}`,
|
|
97
|
+
)(...args);
|
|
98
|
+
return applyDecorators(
|
|
99
|
+
ROUTERS[method](path),
|
|
100
|
+
UseInterceptors(new TypedRouteInterceptor(stringify)),
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
return route;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
for (const method of [
|
|
107
|
+
assertStringify,
|
|
108
|
+
isStringify,
|
|
109
|
+
stringify,
|
|
110
|
+
validateStringify,
|
|
111
|
+
]) {
|
|
112
|
+
Object.assign(TypedRoute.Get, method);
|
|
113
|
+
Object.assign(TypedRoute.Delete, method);
|
|
114
|
+
Object.assign(TypedRoute.Post, method);
|
|
115
|
+
Object.assign(TypedRoute.Put, method);
|
|
116
|
+
Object.assign(TypedRoute.Patch, method);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* @internal
|
|
121
|
+
*/
|
|
122
|
+
class TypedRouteInterceptor implements NestInterceptor {
|
|
123
|
+
public constructor(private readonly stringify: (input: any) => string) {}
|
|
124
|
+
|
|
125
|
+
public intercept(
|
|
126
|
+
context: ExecutionContext,
|
|
127
|
+
next: CallHandler,
|
|
128
|
+
): Observable<any> {
|
|
129
|
+
const http: HttpArgumentsHost = context.switchToHttp();
|
|
130
|
+
const response: express.Response = http.getResponse();
|
|
131
|
+
response.header("Content-Type", "application/json");
|
|
132
|
+
|
|
133
|
+
return next.handle().pipe(
|
|
134
|
+
map((value) => this.stringify(value)),
|
|
135
|
+
catchError((err) => route_error(http.getRequest(), err)),
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* @internal
|
|
142
|
+
*/
|
|
143
|
+
const ROUTERS = {
|
|
144
|
+
Get,
|
|
145
|
+
Post,
|
|
146
|
+
Patch,
|
|
147
|
+
Put,
|
|
148
|
+
Delete,
|
|
149
|
+
};
|
|
@@ -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";
|