@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,18 +1,18 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
|
|
3
|
-
import { INestiaTransformProject } from "../options/INestiaTransformProject";
|
|
4
|
-
import { MethodTransformer } from "./MethodTransformer";
|
|
5
|
-
import { ParameterTransformer } from "./ParameterTransformer";
|
|
6
|
-
|
|
7
|
-
export namespace NodeTransformer {
|
|
8
|
-
export function transform(
|
|
9
|
-
project: INestiaTransformProject,
|
|
10
|
-
node: ts.Node,
|
|
11
|
-
): ts.Node {
|
|
12
|
-
if (ts.isMethodDeclaration(node))
|
|
13
|
-
return MethodTransformer.transform(project, node);
|
|
14
|
-
else if (ts.isParameter(node))
|
|
15
|
-
return ParameterTransformer.transform(project, node);
|
|
16
|
-
return node;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { INestiaTransformProject } from "../options/INestiaTransformProject";
|
|
4
|
+
import { MethodTransformer } from "./MethodTransformer";
|
|
5
|
+
import { ParameterTransformer } from "./ParameterTransformer";
|
|
6
|
+
|
|
7
|
+
export namespace NodeTransformer {
|
|
8
|
+
export function transform(
|
|
9
|
+
project: INestiaTransformProject,
|
|
10
|
+
node: ts.Node,
|
|
11
|
+
): ts.Node {
|
|
12
|
+
if (ts.isMethodDeclaration(node))
|
|
13
|
+
return MethodTransformer.transform(project, node);
|
|
14
|
+
else if (ts.isParameter(node))
|
|
15
|
+
return ParameterTransformer.transform(project, node);
|
|
16
|
+
return node;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
|
|
3
|
-
import { INestiaTransformProject } from "../options/INestiaTransformProject";
|
|
4
|
-
import { BodyTransformer } from "./BodyTransformer";
|
|
5
|
-
|
|
6
|
-
export namespace ParameterTransformer {
|
|
7
|
-
export function transform(
|
|
8
|
-
project: INestiaTransformProject,
|
|
9
|
-
param: ts.ParameterDeclaration,
|
|
10
|
-
): ts.ParameterDeclaration {
|
|
11
|
-
const decorators: readonly ts.Decorator[] | undefined = ts.getDecorators
|
|
12
|
-
? ts.getDecorators(param)
|
|
13
|
-
: (param as any).decorators;
|
|
14
|
-
if (!decorators?.length) return param;
|
|
15
|
-
|
|
16
|
-
const type: ts.Type = project.checker.getTypeAtLocation(param);
|
|
17
|
-
if (ts.getDecorators !== undefined)
|
|
18
|
-
return ts.factory.updateParameterDeclaration(
|
|
19
|
-
param,
|
|
20
|
-
(param.modifiers || []).map((mod) =>
|
|
21
|
-
ts.isDecorator(mod)
|
|
22
|
-
? BodyTransformer.transform(project, type, mod)
|
|
23
|
-
: mod,
|
|
24
|
-
),
|
|
25
|
-
param.dotDotDotToken,
|
|
26
|
-
param.name,
|
|
27
|
-
param.questionToken,
|
|
28
|
-
param.type,
|
|
29
|
-
param.initializer,
|
|
30
|
-
);
|
|
31
|
-
// eslint-disable-next-line
|
|
32
|
-
return ts.factory.updateParameterDeclaration(
|
|
33
|
-
param,
|
|
34
|
-
decorators.map((deco) =>
|
|
35
|
-
BodyTransformer.transform(project, type, deco),
|
|
36
|
-
),
|
|
37
|
-
(param as any).modifiers,
|
|
38
|
-
param.dotDotDotToken,
|
|
39
|
-
param.name,
|
|
40
|
-
param.questionToken,
|
|
41
|
-
param.type,
|
|
42
|
-
param.initializer,
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { INestiaTransformProject } from "../options/INestiaTransformProject";
|
|
4
|
+
import { BodyTransformer } from "./BodyTransformer";
|
|
5
|
+
|
|
6
|
+
export namespace ParameterTransformer {
|
|
7
|
+
export function transform(
|
|
8
|
+
project: INestiaTransformProject,
|
|
9
|
+
param: ts.ParameterDeclaration,
|
|
10
|
+
): ts.ParameterDeclaration {
|
|
11
|
+
const decorators: readonly ts.Decorator[] | undefined = ts.getDecorators
|
|
12
|
+
? ts.getDecorators(param)
|
|
13
|
+
: (param as any).decorators;
|
|
14
|
+
if (!decorators?.length) return param;
|
|
15
|
+
|
|
16
|
+
const type: ts.Type = project.checker.getTypeAtLocation(param);
|
|
17
|
+
if (ts.getDecorators !== undefined)
|
|
18
|
+
return ts.factory.updateParameterDeclaration(
|
|
19
|
+
param,
|
|
20
|
+
(param.modifiers || []).map((mod) =>
|
|
21
|
+
ts.isDecorator(mod)
|
|
22
|
+
? BodyTransformer.transform(project, type, mod)
|
|
23
|
+
: mod,
|
|
24
|
+
),
|
|
25
|
+
param.dotDotDotToken,
|
|
26
|
+
param.name,
|
|
27
|
+
param.questionToken,
|
|
28
|
+
param.type,
|
|
29
|
+
param.initializer,
|
|
30
|
+
);
|
|
31
|
+
// eslint-disable-next-line
|
|
32
|
+
return ts.factory.updateParameterDeclaration(
|
|
33
|
+
param,
|
|
34
|
+
decorators.map((deco) =>
|
|
35
|
+
BodyTransformer.transform(project, type, deco),
|
|
36
|
+
),
|
|
37
|
+
(param as any).modifiers,
|
|
38
|
+
param.dotDotDotToken,
|
|
39
|
+
param.name,
|
|
40
|
+
param.questionToken,
|
|
41
|
+
param.type,
|
|
42
|
+
param.initializer,
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -1,131 +1,131 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import ts from "typescript";
|
|
3
|
-
import { AssertStringifyProgrammer } from "typia/lib/programmers/AssertStringifyProgrammer";
|
|
4
|
-
import { IsStringifyProgrammer } from "typia/lib/programmers/IsStringifyProgrammer";
|
|
5
|
-
import { StringifyProgrammer } from "typia/lib/programmers/StringifyProgrammer";
|
|
6
|
-
import { ValidateStringifyProgrammer } from "typia/lib/programmers/ValidateStringifyProgrammer";
|
|
7
|
-
import { IProject } from "typia/lib/transformers/IProject";
|
|
8
|
-
|
|
9
|
-
import { INestiaTransformProject } from "../options/INestiaTransformProject";
|
|
10
|
-
|
|
11
|
-
export namespace RouteTransformer {
|
|
12
|
-
export function transform(
|
|
13
|
-
project: INestiaTransformProject,
|
|
14
|
-
type: ts.Type,
|
|
15
|
-
decorator: ts.Decorator,
|
|
16
|
-
): ts.Decorator {
|
|
17
|
-
if (!ts.isCallExpression(decorator.expression)) return decorator;
|
|
18
|
-
return ts.factory.createDecorator(
|
|
19
|
-
stringify(project, type, decorator.expression),
|
|
20
|
-
);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function stringify(
|
|
24
|
-
project: INestiaTransformProject,
|
|
25
|
-
type: ts.Type,
|
|
26
|
-
expression: ts.CallExpression,
|
|
27
|
-
): ts.LeftHandSideExpression {
|
|
28
|
-
//----
|
|
29
|
-
// VALIDATIONS
|
|
30
|
-
//----
|
|
31
|
-
// CHECK SIGNATURE
|
|
32
|
-
const signature: ts.Signature | undefined =
|
|
33
|
-
project.checker.getResolvedSignature(expression);
|
|
34
|
-
if (!signature || !signature.declaration) return expression;
|
|
35
|
-
|
|
36
|
-
// CHECK TO BE TRANSFORMED
|
|
37
|
-
const done: boolean = (() => {
|
|
38
|
-
// CHECK FILENAME
|
|
39
|
-
const location: string = path.resolve(
|
|
40
|
-
signature.declaration.getSourceFile().fileName,
|
|
41
|
-
);
|
|
42
|
-
if (
|
|
43
|
-
LIB_PATHS.every((str) => location.indexOf(str) === -1) &&
|
|
44
|
-
SRC_PATHS.every((str) => location !== str)
|
|
45
|
-
)
|
|
46
|
-
return false;
|
|
47
|
-
|
|
48
|
-
// CHECK DUPLICATE BOOSTER
|
|
49
|
-
if (expression.arguments.length >= 2) return false;
|
|
50
|
-
else if (expression.arguments.length === 1) {
|
|
51
|
-
const last: ts.Expression =
|
|
52
|
-
expression.arguments[expression.arguments.length - 1];
|
|
53
|
-
const type: ts.Type = project.checker.getTypeAtLocation(last);
|
|
54
|
-
if (isObject(project.checker, type)) return false;
|
|
55
|
-
}
|
|
56
|
-
return true;
|
|
57
|
-
})();
|
|
58
|
-
if (done === false) return expression;
|
|
59
|
-
|
|
60
|
-
// CHECK TYPE NODE
|
|
61
|
-
const typeNode: ts.TypeNode | undefined =
|
|
62
|
-
project.checker.typeToTypeNode(type, undefined, undefined);
|
|
63
|
-
if (typeNode === undefined) return expression;
|
|
64
|
-
|
|
65
|
-
//----
|
|
66
|
-
// TRANSFORMATION
|
|
67
|
-
//----
|
|
68
|
-
// GENERATE STRINGIFY PLAN
|
|
69
|
-
const parameter = (
|
|
70
|
-
key: string,
|
|
71
|
-
programmer: (
|
|
72
|
-
project: IProject,
|
|
73
|
-
modulo: ts.LeftHandSideExpression,
|
|
74
|
-
) => (type: ts.Type) => ts.ArrowFunction,
|
|
75
|
-
) =>
|
|
76
|
-
ts.factory.createObjectLiteralExpression([
|
|
77
|
-
ts.factory.createPropertyAssignment(
|
|
78
|
-
ts.factory.createIdentifier("type"),
|
|
79
|
-
ts.factory.createStringLiteral(key),
|
|
80
|
-
),
|
|
81
|
-
ts.factory.createPropertyAssignment(
|
|
82
|
-
ts.factory.createIdentifier(key),
|
|
83
|
-
programmer(project, expression.expression)(type),
|
|
84
|
-
),
|
|
85
|
-
]);
|
|
86
|
-
const stringify: ts.ObjectLiteralExpression = (() => {
|
|
87
|
-
if (project.options.stringify === "stringify")
|
|
88
|
-
return parameter("stringify", StringifyProgrammer.generate);
|
|
89
|
-
else if (project.options.stringify === "assert")
|
|
90
|
-
return parameter("assert", AssertStringifyProgrammer.generate);
|
|
91
|
-
else if (project.options.stringify === "validate")
|
|
92
|
-
return parameter(
|
|
93
|
-
"validate",
|
|
94
|
-
ValidateStringifyProgrammer.generate,
|
|
95
|
-
);
|
|
96
|
-
return parameter("is", IsStringifyProgrammer.generate);
|
|
97
|
-
})();
|
|
98
|
-
|
|
99
|
-
// UPDATE DECORATOR FUNCTION CALL
|
|
100
|
-
return ts.factory.updateCallExpression(
|
|
101
|
-
expression,
|
|
102
|
-
expression.expression,
|
|
103
|
-
expression.typeArguments,
|
|
104
|
-
[...expression.arguments, stringify],
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function isObject(checker: ts.TypeChecker, type: ts.Type): boolean {
|
|
109
|
-
return (
|
|
110
|
-
(type.getFlags() & ts.TypeFlags.Object) !== 0 &&
|
|
111
|
-
!(checker as any).isTupleType(type) &&
|
|
112
|
-
!(checker as any).isArrayType(type) &&
|
|
113
|
-
!(checker as any).isArrayLikeType(type)
|
|
114
|
-
);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const CLASSES = ["EncryptedRoute", "TypedRoute"];
|
|
118
|
-
const LIB_PATHS = CLASSES.map((cla) =>
|
|
119
|
-
path.join(
|
|
120
|
-
"node_modules",
|
|
121
|
-
"@nestia",
|
|
122
|
-
"core",
|
|
123
|
-
"lib",
|
|
124
|
-
"decorators",
|
|
125
|
-
`${cla}.d.ts`,
|
|
126
|
-
),
|
|
127
|
-
);
|
|
128
|
-
const SRC_PATHS = CLASSES.map((cla) =>
|
|
129
|
-
path.resolve(path.join(__dirname, "..", "decorators", `${cla}.ts`)),
|
|
130
|
-
);
|
|
131
|
-
}
|
|
1
|
+
import path from "path";
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
import { AssertStringifyProgrammer } from "typia/lib/programmers/AssertStringifyProgrammer";
|
|
4
|
+
import { IsStringifyProgrammer } from "typia/lib/programmers/IsStringifyProgrammer";
|
|
5
|
+
import { StringifyProgrammer } from "typia/lib/programmers/StringifyProgrammer";
|
|
6
|
+
import { ValidateStringifyProgrammer } from "typia/lib/programmers/ValidateStringifyProgrammer";
|
|
7
|
+
import { IProject } from "typia/lib/transformers/IProject";
|
|
8
|
+
|
|
9
|
+
import { INestiaTransformProject } from "../options/INestiaTransformProject";
|
|
10
|
+
|
|
11
|
+
export namespace RouteTransformer {
|
|
12
|
+
export function transform(
|
|
13
|
+
project: INestiaTransformProject,
|
|
14
|
+
type: ts.Type,
|
|
15
|
+
decorator: ts.Decorator,
|
|
16
|
+
): ts.Decorator {
|
|
17
|
+
if (!ts.isCallExpression(decorator.expression)) return decorator;
|
|
18
|
+
return ts.factory.createDecorator(
|
|
19
|
+
stringify(project, type, decorator.expression),
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function stringify(
|
|
24
|
+
project: INestiaTransformProject,
|
|
25
|
+
type: ts.Type,
|
|
26
|
+
expression: ts.CallExpression,
|
|
27
|
+
): ts.LeftHandSideExpression {
|
|
28
|
+
//----
|
|
29
|
+
// VALIDATIONS
|
|
30
|
+
//----
|
|
31
|
+
// CHECK SIGNATURE
|
|
32
|
+
const signature: ts.Signature | undefined =
|
|
33
|
+
project.checker.getResolvedSignature(expression);
|
|
34
|
+
if (!signature || !signature.declaration) return expression;
|
|
35
|
+
|
|
36
|
+
// CHECK TO BE TRANSFORMED
|
|
37
|
+
const done: boolean = (() => {
|
|
38
|
+
// CHECK FILENAME
|
|
39
|
+
const location: string = path.resolve(
|
|
40
|
+
signature.declaration.getSourceFile().fileName,
|
|
41
|
+
);
|
|
42
|
+
if (
|
|
43
|
+
LIB_PATHS.every((str) => location.indexOf(str) === -1) &&
|
|
44
|
+
SRC_PATHS.every((str) => location !== str)
|
|
45
|
+
)
|
|
46
|
+
return false;
|
|
47
|
+
|
|
48
|
+
// CHECK DUPLICATE BOOSTER
|
|
49
|
+
if (expression.arguments.length >= 2) return false;
|
|
50
|
+
else if (expression.arguments.length === 1) {
|
|
51
|
+
const last: ts.Expression =
|
|
52
|
+
expression.arguments[expression.arguments.length - 1];
|
|
53
|
+
const type: ts.Type = project.checker.getTypeAtLocation(last);
|
|
54
|
+
if (isObject(project.checker, type)) return false;
|
|
55
|
+
}
|
|
56
|
+
return true;
|
|
57
|
+
})();
|
|
58
|
+
if (done === false) return expression;
|
|
59
|
+
|
|
60
|
+
// CHECK TYPE NODE
|
|
61
|
+
const typeNode: ts.TypeNode | undefined =
|
|
62
|
+
project.checker.typeToTypeNode(type, undefined, undefined);
|
|
63
|
+
if (typeNode === undefined) return expression;
|
|
64
|
+
|
|
65
|
+
//----
|
|
66
|
+
// TRANSFORMATION
|
|
67
|
+
//----
|
|
68
|
+
// GENERATE STRINGIFY PLAN
|
|
69
|
+
const parameter = (
|
|
70
|
+
key: string,
|
|
71
|
+
programmer: (
|
|
72
|
+
project: IProject,
|
|
73
|
+
modulo: ts.LeftHandSideExpression,
|
|
74
|
+
) => (type: ts.Type) => ts.ArrowFunction,
|
|
75
|
+
) =>
|
|
76
|
+
ts.factory.createObjectLiteralExpression([
|
|
77
|
+
ts.factory.createPropertyAssignment(
|
|
78
|
+
ts.factory.createIdentifier("type"),
|
|
79
|
+
ts.factory.createStringLiteral(key),
|
|
80
|
+
),
|
|
81
|
+
ts.factory.createPropertyAssignment(
|
|
82
|
+
ts.factory.createIdentifier(key),
|
|
83
|
+
programmer(project, expression.expression)(type),
|
|
84
|
+
),
|
|
85
|
+
]);
|
|
86
|
+
const stringify: ts.ObjectLiteralExpression = (() => {
|
|
87
|
+
if (project.options.stringify === "stringify")
|
|
88
|
+
return parameter("stringify", StringifyProgrammer.generate);
|
|
89
|
+
else if (project.options.stringify === "assert")
|
|
90
|
+
return parameter("assert", AssertStringifyProgrammer.generate);
|
|
91
|
+
else if (project.options.stringify === "validate")
|
|
92
|
+
return parameter(
|
|
93
|
+
"validate",
|
|
94
|
+
ValidateStringifyProgrammer.generate,
|
|
95
|
+
);
|
|
96
|
+
return parameter("is", IsStringifyProgrammer.generate);
|
|
97
|
+
})();
|
|
98
|
+
|
|
99
|
+
// UPDATE DECORATOR FUNCTION CALL
|
|
100
|
+
return ts.factory.updateCallExpression(
|
|
101
|
+
expression,
|
|
102
|
+
expression.expression,
|
|
103
|
+
expression.typeArguments,
|
|
104
|
+
[...expression.arguments, stringify],
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function isObject(checker: ts.TypeChecker, type: ts.Type): boolean {
|
|
109
|
+
return (
|
|
110
|
+
(type.getFlags() & ts.TypeFlags.Object) !== 0 &&
|
|
111
|
+
!(checker as any).isTupleType(type) &&
|
|
112
|
+
!(checker as any).isArrayType(type) &&
|
|
113
|
+
!(checker as any).isArrayLikeType(type)
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const CLASSES = ["EncryptedRoute", "TypedRoute"];
|
|
118
|
+
const LIB_PATHS = CLASSES.map((cla) =>
|
|
119
|
+
path.join(
|
|
120
|
+
"node_modules",
|
|
121
|
+
"@nestia",
|
|
122
|
+
"core",
|
|
123
|
+
"lib",
|
|
124
|
+
"decorators",
|
|
125
|
+
`${cla}.d.ts`,
|
|
126
|
+
),
|
|
127
|
+
);
|
|
128
|
+
const SRC_PATHS = CLASSES.map((cla) =>
|
|
129
|
+
path.resolve(path.join(__dirname, "..", "decorators", `${cla}.ts`)),
|
|
130
|
+
);
|
|
131
|
+
}
|
package/src/typings/Creator.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export type Creator<T extends object> = {
|
|
2
|
-
new (...args: any[]): T;
|
|
3
|
-
};
|
|
1
|
+
export type Creator<T extends object> = {
|
|
2
|
+
new (...args: any[]): T;
|
|
3
|
+
};
|
|
@@ -1,126 +1,126 @@
|
|
|
1
|
-
import { HttpError } from "@nestia/fetcher";
|
|
2
|
-
import { HttpException } from "@nestjs/common";
|
|
3
|
-
import { TypeGuardError } from "typia";
|
|
4
|
-
|
|
5
|
-
import { Creator } from "../typings/Creator";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Exception manager for HTTP server.
|
|
9
|
-
*
|
|
10
|
-
* `ExceptionManager` is an utility class who can insert or erase custom error class with
|
|
11
|
-
* its convertion method to a regular {@link nest.HttpException} instance.
|
|
12
|
-
*
|
|
13
|
-
* If you define an API function through {@link TypedRoute} or {@link EncryptedRoute}
|
|
14
|
-
* instead of the basic router decorator functions like {@link nest.Get} or
|
|
15
|
-
* {@link nest.Post} and the API function throws a custom error whose class has been
|
|
16
|
-
* {@link ExceptionManager.insert inserted} in this `EntityManager`, the error would be
|
|
17
|
-
* automatically converted to the regular {@link nest.HttpException} instance by the
|
|
18
|
-
* {@link ExceptionManager.Closure} function.
|
|
19
|
-
*
|
|
20
|
-
* Therefore, with this `ExceptionManager` and {@link TypedRoute} or {@link EncryptedRoute},
|
|
21
|
-
* you can manage your custom error classes much systemtically. You can avoid 500 internal
|
|
22
|
-
* server error or hard coding implementation about the custom error classes.
|
|
23
|
-
*
|
|
24
|
-
* Below error classes are defaultly configured in this `ExceptionManager`
|
|
25
|
-
*
|
|
26
|
-
* - `typia.TypeGuardError`
|
|
27
|
-
* - `@nestia/fetcher.HttpError`
|
|
28
|
-
*
|
|
29
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
30
|
-
*/
|
|
31
|
-
export namespace ExceptionManager {
|
|
32
|
-
/**
|
|
33
|
-
* Insert an error class with converter.
|
|
34
|
-
*
|
|
35
|
-
* If you've inserted an duplicated error class, the closure would be overwritten.
|
|
36
|
-
*
|
|
37
|
-
* @param creator Target error class
|
|
38
|
-
* @param closure A closure function converting to the `HttpException` class
|
|
39
|
-
*/
|
|
40
|
-
export function insert<T extends Error>(
|
|
41
|
-
creator: Creator<T>,
|
|
42
|
-
closure: Closure<T>,
|
|
43
|
-
): void {
|
|
44
|
-
const index: number = tuples.findIndex((tuple) => tuple[0] === creator);
|
|
45
|
-
if (index !== -1) tuples.splice(index, 1);
|
|
46
|
-
|
|
47
|
-
tuples.push([creator, closure]);
|
|
48
|
-
tuples.sort(([x], [y]) => (x.prototype instanceof y ? -1 : 1));
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Erase an error class.
|
|
53
|
-
*
|
|
54
|
-
* @param creator Target error class
|
|
55
|
-
* @returns Whether be erased or not
|
|
56
|
-
*/
|
|
57
|
-
export function erase<T extends Error>(creator: Creator<T>): boolean {
|
|
58
|
-
const index: number = tuples.findIndex((tuple) => tuple[0] === creator);
|
|
59
|
-
if (index === -1) return false;
|
|
60
|
-
|
|
61
|
-
tuples.splice(index, 1);
|
|
62
|
-
return true;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export function on(closure: (error: any) => any): void {
|
|
66
|
-
listeners.add(closure);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export function off(closure: (error: any) => any): void {
|
|
70
|
-
listeners.delete(closure);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Type of a closure function converting to the regular {@link nest.HttpException}.
|
|
75
|
-
*
|
|
76
|
-
* `ExceptionManager.Closure` is a type of closure function who are converting from
|
|
77
|
-
* custom error to the regular {@link nest.HttpException} instance. It would be used
|
|
78
|
-
* in the {@link ExceptionManager} with {@link TypedRoute} or {@link EncryptedRoute}.
|
|
79
|
-
*/
|
|
80
|
-
export interface Closure<T extends Error> {
|
|
81
|
-
/**
|
|
82
|
-
* Error converter.
|
|
83
|
-
*
|
|
84
|
-
* Convert from custom error to the regular {@link nest.HttpException} instance.
|
|
85
|
-
*
|
|
86
|
-
* @param exception Custom error instance
|
|
87
|
-
* @return Regular {@link nest.HttpException} instance
|
|
88
|
-
*/
|
|
89
|
-
(exception: T): HttpException;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* @internal
|
|
94
|
-
*/
|
|
95
|
-
export const tuples: Array<[Creator<any>, Closure<any>]> = [];
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* @internal
|
|
99
|
-
*/
|
|
100
|
-
export const listeners: Set<(error: any) => any> = new Set();
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
ExceptionManager.insert(
|
|
104
|
-
TypeGuardError,
|
|
105
|
-
(error) =>
|
|
106
|
-
new HttpException(
|
|
107
|
-
{
|
|
108
|
-
path: error.path,
|
|
109
|
-
reason: error.message,
|
|
110
|
-
message: "Request message is not following the promised type.",
|
|
111
|
-
},
|
|
112
|
-
400,
|
|
113
|
-
),
|
|
114
|
-
);
|
|
115
|
-
|
|
116
|
-
ExceptionManager.insert(
|
|
117
|
-
HttpError,
|
|
118
|
-
(error) =>
|
|
119
|
-
new HttpException(
|
|
120
|
-
{
|
|
121
|
-
path: error.path,
|
|
122
|
-
message: error.message,
|
|
123
|
-
},
|
|
124
|
-
error.status,
|
|
125
|
-
),
|
|
126
|
-
);
|
|
1
|
+
import { HttpError } from "@nestia/fetcher";
|
|
2
|
+
import { HttpException } from "@nestjs/common";
|
|
3
|
+
import { TypeGuardError } from "typia";
|
|
4
|
+
|
|
5
|
+
import { Creator } from "../typings/Creator";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Exception manager for HTTP server.
|
|
9
|
+
*
|
|
10
|
+
* `ExceptionManager` is an utility class who can insert or erase custom error class with
|
|
11
|
+
* its convertion method to a regular {@link nest.HttpException} instance.
|
|
12
|
+
*
|
|
13
|
+
* If you define an API function through {@link TypedRoute} or {@link EncryptedRoute}
|
|
14
|
+
* instead of the basic router decorator functions like {@link nest.Get} or
|
|
15
|
+
* {@link nest.Post} and the API function throws a custom error whose class has been
|
|
16
|
+
* {@link ExceptionManager.insert inserted} in this `EntityManager`, the error would be
|
|
17
|
+
* automatically converted to the regular {@link nest.HttpException} instance by the
|
|
18
|
+
* {@link ExceptionManager.Closure} function.
|
|
19
|
+
*
|
|
20
|
+
* Therefore, with this `ExceptionManager` and {@link TypedRoute} or {@link EncryptedRoute},
|
|
21
|
+
* you can manage your custom error classes much systemtically. You can avoid 500 internal
|
|
22
|
+
* server error or hard coding implementation about the custom error classes.
|
|
23
|
+
*
|
|
24
|
+
* Below error classes are defaultly configured in this `ExceptionManager`
|
|
25
|
+
*
|
|
26
|
+
* - `typia.TypeGuardError`
|
|
27
|
+
* - `@nestia/fetcher.HttpError`
|
|
28
|
+
*
|
|
29
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
30
|
+
*/
|
|
31
|
+
export namespace ExceptionManager {
|
|
32
|
+
/**
|
|
33
|
+
* Insert an error class with converter.
|
|
34
|
+
*
|
|
35
|
+
* If you've inserted an duplicated error class, the closure would be overwritten.
|
|
36
|
+
*
|
|
37
|
+
* @param creator Target error class
|
|
38
|
+
* @param closure A closure function converting to the `HttpException` class
|
|
39
|
+
*/
|
|
40
|
+
export function insert<T extends Error>(
|
|
41
|
+
creator: Creator<T>,
|
|
42
|
+
closure: Closure<T>,
|
|
43
|
+
): void {
|
|
44
|
+
const index: number = tuples.findIndex((tuple) => tuple[0] === creator);
|
|
45
|
+
if (index !== -1) tuples.splice(index, 1);
|
|
46
|
+
|
|
47
|
+
tuples.push([creator, closure]);
|
|
48
|
+
tuples.sort(([x], [y]) => (x.prototype instanceof y ? -1 : 1));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Erase an error class.
|
|
53
|
+
*
|
|
54
|
+
* @param creator Target error class
|
|
55
|
+
* @returns Whether be erased or not
|
|
56
|
+
*/
|
|
57
|
+
export function erase<T extends Error>(creator: Creator<T>): boolean {
|
|
58
|
+
const index: number = tuples.findIndex((tuple) => tuple[0] === creator);
|
|
59
|
+
if (index === -1) return false;
|
|
60
|
+
|
|
61
|
+
tuples.splice(index, 1);
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function on(closure: (error: any) => any): void {
|
|
66
|
+
listeners.add(closure);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function off(closure: (error: any) => any): void {
|
|
70
|
+
listeners.delete(closure);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Type of a closure function converting to the regular {@link nest.HttpException}.
|
|
75
|
+
*
|
|
76
|
+
* `ExceptionManager.Closure` is a type of closure function who are converting from
|
|
77
|
+
* custom error to the regular {@link nest.HttpException} instance. It would be used
|
|
78
|
+
* in the {@link ExceptionManager} with {@link TypedRoute} or {@link EncryptedRoute}.
|
|
79
|
+
*/
|
|
80
|
+
export interface Closure<T extends Error> {
|
|
81
|
+
/**
|
|
82
|
+
* Error converter.
|
|
83
|
+
*
|
|
84
|
+
* Convert from custom error to the regular {@link nest.HttpException} instance.
|
|
85
|
+
*
|
|
86
|
+
* @param exception Custom error instance
|
|
87
|
+
* @return Regular {@link nest.HttpException} instance
|
|
88
|
+
*/
|
|
89
|
+
(exception: T): HttpException;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* @internal
|
|
94
|
+
*/
|
|
95
|
+
export const tuples: Array<[Creator<any>, Closure<any>]> = [];
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @internal
|
|
99
|
+
*/
|
|
100
|
+
export const listeners: Set<(error: any) => any> = new Set();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
ExceptionManager.insert(
|
|
104
|
+
TypeGuardError,
|
|
105
|
+
(error) =>
|
|
106
|
+
new HttpException(
|
|
107
|
+
{
|
|
108
|
+
path: error.path,
|
|
109
|
+
reason: error.message,
|
|
110
|
+
message: "Request message is not following the promised type.",
|
|
111
|
+
},
|
|
112
|
+
400,
|
|
113
|
+
),
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
ExceptionManager.insert(
|
|
117
|
+
HttpError,
|
|
118
|
+
(error) =>
|
|
119
|
+
new HttpException(
|
|
120
|
+
{
|
|
121
|
+
path: error.path,
|
|
122
|
+
message: error.message,
|
|
123
|
+
},
|
|
124
|
+
error.status,
|
|
125
|
+
),
|
|
126
|
+
);
|