@nestia/core 7.0.0-dev.20250607 → 7.0.0
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 +21 -21
- package/README.md +92 -92
- package/package.json +3 -3
- package/src/adaptors/WebSocketAdaptor.ts +429 -429
- package/src/decorators/DynamicModule.ts +43 -43
- package/src/decorators/EncryptedBody.ts +101 -101
- package/src/decorators/EncryptedController.ts +38 -38
- package/src/decorators/EncryptedModule.ts +100 -100
- package/src/decorators/EncryptedRoute.ts +219 -219
- package/src/decorators/HumanRoute.ts +22 -22
- package/src/decorators/NoTransformConfigurationError.ts +32 -32
- package/src/decorators/PlainBody.ts +79 -79
- package/src/decorators/SwaggerCustomizer.ts +115 -115
- package/src/decorators/SwaggerExample.ts +100 -100
- package/src/decorators/TypedBody.ts +59 -59
- package/src/decorators/TypedException.ts +166 -166
- package/src/decorators/TypedFormData.ts +195 -195
- package/src/decorators/TypedHeaders.ts +64 -64
- package/src/decorators/TypedParam.ts +77 -77
- package/src/decorators/TypedQuery.ts +245 -245
- package/src/decorators/TypedRoute.ts +214 -214
- package/src/decorators/WebSocketRoute.ts +242 -242
- package/src/decorators/internal/EncryptedConstant.ts +4 -4
- package/src/decorators/internal/IWebSocketRouteReflect.ts +23 -23
- package/src/decorators/internal/NoTransformConfigureError.ts +2 -2
- package/src/decorators/internal/get_path_and_querify.ts +108 -108
- package/src/decorators/internal/get_path_and_stringify.ts +122 -122
- package/src/decorators/internal/get_text_body.ts +20 -20
- package/src/decorators/internal/headers_to_object.ts +13 -13
- package/src/decorators/internal/is_request_body_undefined.ts +14 -14
- package/src/decorators/internal/load_controller.ts +49 -49
- package/src/decorators/internal/route_error.ts +45 -45
- package/src/decorators/internal/validate_request_body.ts +74 -74
- package/src/decorators/internal/validate_request_form_data.ts +77 -77
- package/src/decorators/internal/validate_request_headers.ts +86 -86
- package/src/decorators/internal/validate_request_query.ts +74 -74
- package/src/index.ts +5 -5
- package/src/module.ts +22 -22
- package/src/options/INestiaTransformOptions.ts +38 -38
- package/src/options/INestiaTransformProject.ts +8 -8
- 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 +30 -30
- package/src/programmers/PlainBodyProgrammer.ts +70 -70
- package/src/programmers/TypedBodyProgrammer.ts +142 -142
- package/src/programmers/TypedFormDataBodyProgrammer.ts +118 -118
- package/src/programmers/TypedHeadersProgrammer.ts +63 -63
- package/src/programmers/TypedParamProgrammer.ts +33 -33
- package/src/programmers/TypedQueryBodyProgrammer.ts +112 -112
- package/src/programmers/TypedQueryProgrammer.ts +114 -114
- package/src/programmers/TypedQueryRouteProgrammer.ts +105 -105
- package/src/programmers/TypedRouteProgrammer.ts +94 -94
- package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +72 -72
- package/src/programmers/http/HttpIsQuerifyProgrammer.ts +75 -75
- package/src/programmers/http/HttpQuerifyProgrammer.ts +108 -108
- package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +76 -76
- package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
- package/src/transform.ts +35 -35
- package/src/transformers/FileTransformer.ts +110 -110
- package/src/transformers/MethodTransformer.ts +103 -103
- package/src/transformers/NodeTransformer.ts +23 -23
- package/src/transformers/ParameterDecoratorTransformer.ts +143 -143
- package/src/transformers/ParameterTransformer.ts +57 -57
- package/src/transformers/TypedRouteTransformer.ts +85 -85
- package/src/transformers/WebSocketRouteTransformer.ts +120 -120
- package/src/typings/Creator.ts +3 -3
- package/src/typings/get-function-location.d.ts +7 -7
- package/src/utils/ArrayUtil.ts +7 -7
- package/src/utils/ExceptionManager.ts +112 -112
- package/src/utils/Singleton.ts +20 -20
- package/src/utils/SourceFinder.ts +57 -57
- package/src/utils/VersioningStrategy.ts +27 -27
|
@@ -1,120 +1,120 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import ts from "typescript";
|
|
3
|
-
|
|
4
|
-
import { INestiaTransformContext } from "../options/INestiaTransformProject";
|
|
5
|
-
|
|
6
|
-
export namespace WebSocketRouteTransformer {
|
|
7
|
-
export const validate = (props: {
|
|
8
|
-
context: INestiaTransformContext;
|
|
9
|
-
decorator: ts.Decorator;
|
|
10
|
-
method: ts.MethodDeclaration;
|
|
11
|
-
}): ts.Decorator => {
|
|
12
|
-
if (!ts.isCallExpression(props.decorator.expression))
|
|
13
|
-
return props.decorator;
|
|
14
|
-
|
|
15
|
-
// CHECK SIGNATURE
|
|
16
|
-
const signature: ts.Signature | undefined =
|
|
17
|
-
props.context.checker.getResolvedSignature(props.decorator.expression);
|
|
18
|
-
if (!signature || !signature.declaration) return props.decorator;
|
|
19
|
-
else if (isLocated(signature) === false) return props.decorator;
|
|
20
|
-
|
|
21
|
-
const errors: ts.DiagnosticWithLocation[] = [];
|
|
22
|
-
let accepted: boolean = false;
|
|
23
|
-
|
|
24
|
-
const report = (node: ts.Node, message: string) => {
|
|
25
|
-
errors.push(
|
|
26
|
-
(ts as any).createDiagnosticForNode(node, {
|
|
27
|
-
category: ts.DiagnosticCategory.Error,
|
|
28
|
-
key: "nestia.core.WebSocketRoute",
|
|
29
|
-
code: "(nestia.core.WebSocketRoute)" as any,
|
|
30
|
-
message,
|
|
31
|
-
}),
|
|
32
|
-
);
|
|
33
|
-
};
|
|
34
|
-
props.method.parameters.forEach((param) => {
|
|
35
|
-
const paramDecos: ts.Decorator[] = (param.modifiers ?? []).filter((m) =>
|
|
36
|
-
ts.isDecorator(m),
|
|
37
|
-
) as ts.Decorator[];
|
|
38
|
-
const category: string | null = (() => {
|
|
39
|
-
if (paramDecos.length !== 1) return null;
|
|
40
|
-
const decorator: ts.Decorator = paramDecos[0];
|
|
41
|
-
const signature: ts.Signature | undefined = ts.isCallExpression(
|
|
42
|
-
decorator.expression,
|
|
43
|
-
)
|
|
44
|
-
? props.context.checker.getResolvedSignature(decorator.expression)
|
|
45
|
-
: undefined;
|
|
46
|
-
if (signature === undefined || isLocated(signature) === false)
|
|
47
|
-
return null;
|
|
48
|
-
return (
|
|
49
|
-
decorator.expression.getText().split(".").at(-1)?.split("(")[0] ??
|
|
50
|
-
null
|
|
51
|
-
);
|
|
52
|
-
})();
|
|
53
|
-
if (category === null)
|
|
54
|
-
report(
|
|
55
|
-
param,
|
|
56
|
-
`parameter ${JSON.stringify(param.name.getText())} is not decorated with nested function of WebSocketRoute module.`,
|
|
57
|
-
);
|
|
58
|
-
else if (category === "Acceptor") {
|
|
59
|
-
accepted = true;
|
|
60
|
-
if (
|
|
61
|
-
param.type
|
|
62
|
-
?.getText()
|
|
63
|
-
.split("<")[0]
|
|
64
|
-
?.split(".")
|
|
65
|
-
.at(-1)
|
|
66
|
-
?.startsWith("WebSocketAcceptor") !== true
|
|
67
|
-
)
|
|
68
|
-
report(
|
|
69
|
-
param,
|
|
70
|
-
`parameter ${JSON.stringify(param.name.getText())} must have WebSocketAcceptor<Header, Provider, Listener> type.`,
|
|
71
|
-
);
|
|
72
|
-
} else if (category === "Driver") {
|
|
73
|
-
if (
|
|
74
|
-
param.type
|
|
75
|
-
?.getText()
|
|
76
|
-
.split("<")[0]
|
|
77
|
-
?.split(".")
|
|
78
|
-
.at(-1)
|
|
79
|
-
?.startsWith("Driver") !== true
|
|
80
|
-
)
|
|
81
|
-
report(
|
|
82
|
-
param,
|
|
83
|
-
`parameter ${JSON.stringify(param.name.getText())} must have Driver<Listener> type.`,
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
if (accepted === false)
|
|
88
|
-
report(
|
|
89
|
-
props.method,
|
|
90
|
-
`method ${JSON.stringify(props.method.name.getText())} must have at least one parameter decorated by @WebSocketRoute.Acceptor().`,
|
|
91
|
-
);
|
|
92
|
-
for (const e of errors) props.context.extras.addDiagnostic(e);
|
|
93
|
-
return props.decorator;
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const isLocated = (signature: ts.Signature) => {
|
|
98
|
-
if (!signature.declaration) return false;
|
|
99
|
-
const location: string = path.resolve(
|
|
100
|
-
signature.declaration.getSourceFile().fileName,
|
|
101
|
-
);
|
|
102
|
-
return (
|
|
103
|
-
location.indexOf(LIB_PATH) !== -1 || location.indexOf(MONO_PATH) !== -1
|
|
104
|
-
);
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
const LIB_PATH = path.join(
|
|
108
|
-
"@nestia",
|
|
109
|
-
"core",
|
|
110
|
-
"lib",
|
|
111
|
-
"decorators",
|
|
112
|
-
`WebSocketRoute.d.ts`,
|
|
113
|
-
);
|
|
114
|
-
const MONO_PATH = path.join(
|
|
115
|
-
"packages",
|
|
116
|
-
"core",
|
|
117
|
-
"lib",
|
|
118
|
-
"decorators",
|
|
119
|
-
`WebSocketRoute.d.ts`,
|
|
120
|
-
);
|
|
1
|
+
import path from "path";
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
|
|
4
|
+
import { INestiaTransformContext } from "../options/INestiaTransformProject";
|
|
5
|
+
|
|
6
|
+
export namespace WebSocketRouteTransformer {
|
|
7
|
+
export const validate = (props: {
|
|
8
|
+
context: INestiaTransformContext;
|
|
9
|
+
decorator: ts.Decorator;
|
|
10
|
+
method: ts.MethodDeclaration;
|
|
11
|
+
}): ts.Decorator => {
|
|
12
|
+
if (!ts.isCallExpression(props.decorator.expression))
|
|
13
|
+
return props.decorator;
|
|
14
|
+
|
|
15
|
+
// CHECK SIGNATURE
|
|
16
|
+
const signature: ts.Signature | undefined =
|
|
17
|
+
props.context.checker.getResolvedSignature(props.decorator.expression);
|
|
18
|
+
if (!signature || !signature.declaration) return props.decorator;
|
|
19
|
+
else if (isLocated(signature) === false) return props.decorator;
|
|
20
|
+
|
|
21
|
+
const errors: ts.DiagnosticWithLocation[] = [];
|
|
22
|
+
let accepted: boolean = false;
|
|
23
|
+
|
|
24
|
+
const report = (node: ts.Node, message: string) => {
|
|
25
|
+
errors.push(
|
|
26
|
+
(ts as any).createDiagnosticForNode(node, {
|
|
27
|
+
category: ts.DiagnosticCategory.Error,
|
|
28
|
+
key: "nestia.core.WebSocketRoute",
|
|
29
|
+
code: "(nestia.core.WebSocketRoute)" as any,
|
|
30
|
+
message,
|
|
31
|
+
}),
|
|
32
|
+
);
|
|
33
|
+
};
|
|
34
|
+
props.method.parameters.forEach((param) => {
|
|
35
|
+
const paramDecos: ts.Decorator[] = (param.modifiers ?? []).filter((m) =>
|
|
36
|
+
ts.isDecorator(m),
|
|
37
|
+
) as ts.Decorator[];
|
|
38
|
+
const category: string | null = (() => {
|
|
39
|
+
if (paramDecos.length !== 1) return null;
|
|
40
|
+
const decorator: ts.Decorator = paramDecos[0];
|
|
41
|
+
const signature: ts.Signature | undefined = ts.isCallExpression(
|
|
42
|
+
decorator.expression,
|
|
43
|
+
)
|
|
44
|
+
? props.context.checker.getResolvedSignature(decorator.expression)
|
|
45
|
+
: undefined;
|
|
46
|
+
if (signature === undefined || isLocated(signature) === false)
|
|
47
|
+
return null;
|
|
48
|
+
return (
|
|
49
|
+
decorator.expression.getText().split(".").at(-1)?.split("(")[0] ??
|
|
50
|
+
null
|
|
51
|
+
);
|
|
52
|
+
})();
|
|
53
|
+
if (category === null)
|
|
54
|
+
report(
|
|
55
|
+
param,
|
|
56
|
+
`parameter ${JSON.stringify(param.name.getText())} is not decorated with nested function of WebSocketRoute module.`,
|
|
57
|
+
);
|
|
58
|
+
else if (category === "Acceptor") {
|
|
59
|
+
accepted = true;
|
|
60
|
+
if (
|
|
61
|
+
param.type
|
|
62
|
+
?.getText()
|
|
63
|
+
.split("<")[0]
|
|
64
|
+
?.split(".")
|
|
65
|
+
.at(-1)
|
|
66
|
+
?.startsWith("WebSocketAcceptor") !== true
|
|
67
|
+
)
|
|
68
|
+
report(
|
|
69
|
+
param,
|
|
70
|
+
`parameter ${JSON.stringify(param.name.getText())} must have WebSocketAcceptor<Header, Provider, Listener> type.`,
|
|
71
|
+
);
|
|
72
|
+
} else if (category === "Driver") {
|
|
73
|
+
if (
|
|
74
|
+
param.type
|
|
75
|
+
?.getText()
|
|
76
|
+
.split("<")[0]
|
|
77
|
+
?.split(".")
|
|
78
|
+
.at(-1)
|
|
79
|
+
?.startsWith("Driver") !== true
|
|
80
|
+
)
|
|
81
|
+
report(
|
|
82
|
+
param,
|
|
83
|
+
`parameter ${JSON.stringify(param.name.getText())} must have Driver<Listener> type.`,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
if (accepted === false)
|
|
88
|
+
report(
|
|
89
|
+
props.method,
|
|
90
|
+
`method ${JSON.stringify(props.method.name.getText())} must have at least one parameter decorated by @WebSocketRoute.Acceptor().`,
|
|
91
|
+
);
|
|
92
|
+
for (const e of errors) props.context.extras.addDiagnostic(e);
|
|
93
|
+
return props.decorator;
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const isLocated = (signature: ts.Signature) => {
|
|
98
|
+
if (!signature.declaration) return false;
|
|
99
|
+
const location: string = path.resolve(
|
|
100
|
+
signature.declaration.getSourceFile().fileName,
|
|
101
|
+
);
|
|
102
|
+
return (
|
|
103
|
+
location.indexOf(LIB_PATH) !== -1 || location.indexOf(MONO_PATH) !== -1
|
|
104
|
+
);
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const LIB_PATH = path.join(
|
|
108
|
+
"@nestia",
|
|
109
|
+
"core",
|
|
110
|
+
"lib",
|
|
111
|
+
"decorators",
|
|
112
|
+
`WebSocketRoute.d.ts`,
|
|
113
|
+
);
|
|
114
|
+
const MONO_PATH = path.join(
|
|
115
|
+
"packages",
|
|
116
|
+
"core",
|
|
117
|
+
"lib",
|
|
118
|
+
"decorators",
|
|
119
|
+
`WebSocketRoute.d.ts`,
|
|
120
|
+
);
|
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,7 +1,7 @@
|
|
|
1
|
-
declare module "get-function-location" {
|
|
2
|
-
export default function (func: any): Promise<{
|
|
3
|
-
source: string;
|
|
4
|
-
line: number;
|
|
5
|
-
column: number;
|
|
6
|
-
}>;
|
|
7
|
-
}
|
|
1
|
+
declare module "get-function-location" {
|
|
2
|
+
export default function (func: any): Promise<{
|
|
3
|
+
source: string;
|
|
4
|
+
line: number;
|
|
5
|
+
column: number;
|
|
6
|
+
}>;
|
|
7
|
+
}
|
package/src/utils/ArrayUtil.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export namespace ArrayUtil {
|
|
2
|
-
export function has<T>(array: T[], ...items: T[]): boolean {
|
|
3
|
-
return items.every(
|
|
4
|
-
(elem) => array.find((org) => org === elem) !== undefined,
|
|
5
|
-
);
|
|
6
|
-
}
|
|
7
|
-
}
|
|
1
|
+
export namespace ArrayUtil {
|
|
2
|
+
export function has<T>(array: T[], ...items: T[]): boolean {
|
|
3
|
+
return items.every(
|
|
4
|
+
(elem) => array.find((org) => org === elem) !== undefined,
|
|
5
|
+
);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
@@ -1,112 +1,112 @@
|
|
|
1
|
-
import { HttpError } from "@nestia/fetcher";
|
|
2
|
-
import { HttpException } from "@nestjs/common";
|
|
3
|
-
|
|
4
|
-
import { Creator } from "../typings/Creator";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Exception manager for HTTP server.
|
|
8
|
-
*
|
|
9
|
-
* `ExceptionManager` is an utility class who can insert or erase custom error class with
|
|
10
|
-
* its conversion method to a regular {@link nest.HttpException} instance.
|
|
11
|
-
*
|
|
12
|
-
* If you define an API function through {@link TypedRoute} or {@link EncryptedRoute}
|
|
13
|
-
* instead of the basic router decorator functions like {@link nest.Get} or
|
|
14
|
-
* {@link nest.Post} and the API function throws a custom error whose class has been
|
|
15
|
-
* {@link ExceptionManager.insert inserted} in this `EntityManager`, the error would be
|
|
16
|
-
* automatically converted to the regular {@link nest.HttpException} instance by the
|
|
17
|
-
* {@link ExceptionManager.Closure} function.
|
|
18
|
-
*
|
|
19
|
-
* Therefore, with this `ExceptionManager` and {@link TypedRoute} or {@link EncryptedRoute},
|
|
20
|
-
* you can manage your custom error classes much systemtically. You can avoid 500 internal
|
|
21
|
-
* server error or hard coding implementation about the custom error classes.
|
|
22
|
-
*
|
|
23
|
-
* Below error classes are defaultly configured in this `ExceptionManager`
|
|
24
|
-
*
|
|
25
|
-
* - `typia.TypeGuardError`
|
|
26
|
-
* - `@nestia/fetcher.HttpError`
|
|
27
|
-
*
|
|
28
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
29
|
-
*/
|
|
30
|
-
export namespace ExceptionManager {
|
|
31
|
-
/**
|
|
32
|
-
* Insert an error class with converter.
|
|
33
|
-
*
|
|
34
|
-
* If you've inserted an duplicated error class, the closure would be overwritten.
|
|
35
|
-
*
|
|
36
|
-
* @param creator Target error class
|
|
37
|
-
* @param closure A closure function converting to the `HttpException` class
|
|
38
|
-
*/
|
|
39
|
-
export function insert<T extends Error>(
|
|
40
|
-
creator: Creator<T>,
|
|
41
|
-
closure: Closure<T>,
|
|
42
|
-
): void {
|
|
43
|
-
const index: number = tuples.findIndex((tuple) => tuple[0] === creator);
|
|
44
|
-
if (index !== -1) tuples.splice(index, 1);
|
|
45
|
-
|
|
46
|
-
tuples.push([creator, closure]);
|
|
47
|
-
tuples.sort(([x], [y]) => (x.prototype instanceof y ? -1 : 1));
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Erase an error class.
|
|
52
|
-
*
|
|
53
|
-
* @param creator Target error class
|
|
54
|
-
* @returns Whether be erased or not
|
|
55
|
-
*/
|
|
56
|
-
export function erase<T extends Error>(creator: Creator<T>): boolean {
|
|
57
|
-
const index: number = tuples.findIndex((tuple) => tuple[0] === creator);
|
|
58
|
-
if (index === -1) return false;
|
|
59
|
-
|
|
60
|
-
tuples.splice(index, 1);
|
|
61
|
-
return true;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function on(closure: (error: any) => any): void {
|
|
65
|
-
listeners.add(closure);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export function off(closure: (error: any) => any): void {
|
|
69
|
-
listeners.delete(closure);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Type of a closure function converting to the regular {@link nest.HttpException}.
|
|
74
|
-
*
|
|
75
|
-
* `ExceptionManager.Closure` is a type of closure function who are converting from
|
|
76
|
-
* custom error to the regular {@link nest.HttpException} instance. It would be used
|
|
77
|
-
* in the {@link ExceptionManager} with {@link TypedRoute} or {@link EncryptedRoute}.
|
|
78
|
-
*/
|
|
79
|
-
export interface Closure<T extends Error> {
|
|
80
|
-
/**
|
|
81
|
-
* Error converter.
|
|
82
|
-
*
|
|
83
|
-
* Convert from custom error to the regular {@link nest.HttpException} instance.
|
|
84
|
-
*
|
|
85
|
-
* @param exception Custom error instance
|
|
86
|
-
* @return Regular {@link nest.HttpException} instance
|
|
87
|
-
*/
|
|
88
|
-
(exception: T): HttpException;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* @internal
|
|
93
|
-
*/
|
|
94
|
-
export const tuples: Array<[Creator<any>, Closure<any>]> = [];
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* @internal
|
|
98
|
-
*/
|
|
99
|
-
export const listeners: Set<(error: any) => any> = new Set();
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
ExceptionManager.insert(
|
|
103
|
-
HttpError,
|
|
104
|
-
(error) =>
|
|
105
|
-
new HttpException(
|
|
106
|
-
{
|
|
107
|
-
path: error.path,
|
|
108
|
-
message: error.message,
|
|
109
|
-
},
|
|
110
|
-
error.status,
|
|
111
|
-
),
|
|
112
|
-
);
|
|
1
|
+
import { HttpError } from "@nestia/fetcher";
|
|
2
|
+
import { HttpException } from "@nestjs/common";
|
|
3
|
+
|
|
4
|
+
import { Creator } from "../typings/Creator";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Exception manager for HTTP server.
|
|
8
|
+
*
|
|
9
|
+
* `ExceptionManager` is an utility class who can insert or erase custom error class with
|
|
10
|
+
* its conversion method to a regular {@link nest.HttpException} instance.
|
|
11
|
+
*
|
|
12
|
+
* If you define an API function through {@link TypedRoute} or {@link EncryptedRoute}
|
|
13
|
+
* instead of the basic router decorator functions like {@link nest.Get} or
|
|
14
|
+
* {@link nest.Post} and the API function throws a custom error whose class has been
|
|
15
|
+
* {@link ExceptionManager.insert inserted} in this `EntityManager`, the error would be
|
|
16
|
+
* automatically converted to the regular {@link nest.HttpException} instance by the
|
|
17
|
+
* {@link ExceptionManager.Closure} function.
|
|
18
|
+
*
|
|
19
|
+
* Therefore, with this `ExceptionManager` and {@link TypedRoute} or {@link EncryptedRoute},
|
|
20
|
+
* you can manage your custom error classes much systemtically. You can avoid 500 internal
|
|
21
|
+
* server error or hard coding implementation about the custom error classes.
|
|
22
|
+
*
|
|
23
|
+
* Below error classes are defaultly configured in this `ExceptionManager`
|
|
24
|
+
*
|
|
25
|
+
* - `typia.TypeGuardError`
|
|
26
|
+
* - `@nestia/fetcher.HttpError`
|
|
27
|
+
*
|
|
28
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
29
|
+
*/
|
|
30
|
+
export namespace ExceptionManager {
|
|
31
|
+
/**
|
|
32
|
+
* Insert an error class with converter.
|
|
33
|
+
*
|
|
34
|
+
* If you've inserted an duplicated error class, the closure would be overwritten.
|
|
35
|
+
*
|
|
36
|
+
* @param creator Target error class
|
|
37
|
+
* @param closure A closure function converting to the `HttpException` class
|
|
38
|
+
*/
|
|
39
|
+
export function insert<T extends Error>(
|
|
40
|
+
creator: Creator<T>,
|
|
41
|
+
closure: Closure<T>,
|
|
42
|
+
): void {
|
|
43
|
+
const index: number = tuples.findIndex((tuple) => tuple[0] === creator);
|
|
44
|
+
if (index !== -1) tuples.splice(index, 1);
|
|
45
|
+
|
|
46
|
+
tuples.push([creator, closure]);
|
|
47
|
+
tuples.sort(([x], [y]) => (x.prototype instanceof y ? -1 : 1));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Erase an error class.
|
|
52
|
+
*
|
|
53
|
+
* @param creator Target error class
|
|
54
|
+
* @returns Whether be erased or not
|
|
55
|
+
*/
|
|
56
|
+
export function erase<T extends Error>(creator: Creator<T>): boolean {
|
|
57
|
+
const index: number = tuples.findIndex((tuple) => tuple[0] === creator);
|
|
58
|
+
if (index === -1) return false;
|
|
59
|
+
|
|
60
|
+
tuples.splice(index, 1);
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function on(closure: (error: any) => any): void {
|
|
65
|
+
listeners.add(closure);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function off(closure: (error: any) => any): void {
|
|
69
|
+
listeners.delete(closure);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Type of a closure function converting to the regular {@link nest.HttpException}.
|
|
74
|
+
*
|
|
75
|
+
* `ExceptionManager.Closure` is a type of closure function who are converting from
|
|
76
|
+
* custom error to the regular {@link nest.HttpException} instance. It would be used
|
|
77
|
+
* in the {@link ExceptionManager} with {@link TypedRoute} or {@link EncryptedRoute}.
|
|
78
|
+
*/
|
|
79
|
+
export interface Closure<T extends Error> {
|
|
80
|
+
/**
|
|
81
|
+
* Error converter.
|
|
82
|
+
*
|
|
83
|
+
* Convert from custom error to the regular {@link nest.HttpException} instance.
|
|
84
|
+
*
|
|
85
|
+
* @param exception Custom error instance
|
|
86
|
+
* @return Regular {@link nest.HttpException} instance
|
|
87
|
+
*/
|
|
88
|
+
(exception: T): HttpException;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @internal
|
|
93
|
+
*/
|
|
94
|
+
export const tuples: Array<[Creator<any>, Closure<any>]> = [];
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @internal
|
|
98
|
+
*/
|
|
99
|
+
export const listeners: Set<(error: any) => any> = new Set();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
ExceptionManager.insert(
|
|
103
|
+
HttpError,
|
|
104
|
+
(error) =>
|
|
105
|
+
new HttpException(
|
|
106
|
+
{
|
|
107
|
+
path: error.path,
|
|
108
|
+
message: error.message,
|
|
109
|
+
},
|
|
110
|
+
error.status,
|
|
111
|
+
),
|
|
112
|
+
);
|
package/src/utils/Singleton.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @internal
|
|
3
|
-
*/
|
|
4
|
-
export class Singleton<T> {
|
|
5
|
-
private value_: T | object;
|
|
6
|
-
|
|
7
|
-
public constructor(private readonly closure_: () => T) {
|
|
8
|
-
this.value_ = NOT_MOUNTED_YET;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
public get(): T {
|
|
12
|
-
if (this.value_ === NOT_MOUNTED_YET) this.value_ = this.closure_();
|
|
13
|
-
return this.value_ as T;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @internal
|
|
19
|
-
*/
|
|
20
|
-
const NOT_MOUNTED_YET = {};
|
|
1
|
+
/**
|
|
2
|
+
* @internal
|
|
3
|
+
*/
|
|
4
|
+
export class Singleton<T> {
|
|
5
|
+
private value_: T | object;
|
|
6
|
+
|
|
7
|
+
public constructor(private readonly closure_: () => T) {
|
|
8
|
+
this.value_ = NOT_MOUNTED_YET;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
public get(): T {
|
|
12
|
+
if (this.value_ === NOT_MOUNTED_YET) this.value_ = this.closure_();
|
|
13
|
+
return this.value_ as T;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
const NOT_MOUNTED_YET = {};
|
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import glob from "glob";
|
|
3
|
-
import path from "path";
|
|
4
|
-
|
|
5
|
-
export namespace SourceFinder {
|
|
6
|
-
export const find = async (props: IProps): Promise<string[]> => {
|
|
7
|
-
const dict: Set<string> = new Set();
|
|
8
|
-
|
|
9
|
-
await emplace(props.filter)(props.include)((str) => dict.add(str));
|
|
10
|
-
if (props.exclude?.length)
|
|
11
|
-
await emplace(props.filter)(props.exclude)((str) => dict.delete(str));
|
|
12
|
-
|
|
13
|
-
return [...dict];
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
const emplace =
|
|
17
|
-
(filter: (file: string) => boolean) =>
|
|
18
|
-
(input: string[]) =>
|
|
19
|
-
async (closure: (location: string) => void): Promise<void> => {
|
|
20
|
-
for (const pattern of input) {
|
|
21
|
-
for (const file of await _Glob(path.resolve(pattern))) {
|
|
22
|
-
const stats: fs.Stats = await fs.promises.stat(file);
|
|
23
|
-
if (stats.isDirectory() === true)
|
|
24
|
-
await iterate(filter)(closure)(file);
|
|
25
|
-
else if (stats.isFile() && filter(file)) closure(file);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const iterate =
|
|
31
|
-
(filter: (location: string) => boolean) =>
|
|
32
|
-
(closure: (location: string) => void) =>
|
|
33
|
-
async (location: string): Promise<void> => {
|
|
34
|
-
const directory: string[] = await fs.promises.readdir(location);
|
|
35
|
-
for (const file of directory) {
|
|
36
|
-
const next: string = path.resolve(`${location}/${file}`);
|
|
37
|
-
const stats: fs.Stats = await fs.promises.stat(next);
|
|
38
|
-
|
|
39
|
-
if (stats.isDirectory() === true) await iterate(filter)(closure)(next);
|
|
40
|
-
else if (stats.isFile() && filter(next)) closure(next);
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const _Glob = (pattern: string): Promise<string[]> =>
|
|
45
|
-
new Promise((resolve, reject) => {
|
|
46
|
-
glob(pattern, (err, matches) => {
|
|
47
|
-
if (err) reject(err);
|
|
48
|
-
else resolve(matches.map((str) => path.resolve(str)));
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
interface IProps {
|
|
54
|
-
exclude?: string[];
|
|
55
|
-
include: string[];
|
|
56
|
-
filter: (location: string) => boolean;
|
|
57
|
-
}
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import glob from "glob";
|
|
3
|
+
import path from "path";
|
|
4
|
+
|
|
5
|
+
export namespace SourceFinder {
|
|
6
|
+
export const find = async (props: IProps): Promise<string[]> => {
|
|
7
|
+
const dict: Set<string> = new Set();
|
|
8
|
+
|
|
9
|
+
await emplace(props.filter)(props.include)((str) => dict.add(str));
|
|
10
|
+
if (props.exclude?.length)
|
|
11
|
+
await emplace(props.filter)(props.exclude)((str) => dict.delete(str));
|
|
12
|
+
|
|
13
|
+
return [...dict];
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const emplace =
|
|
17
|
+
(filter: (file: string) => boolean) =>
|
|
18
|
+
(input: string[]) =>
|
|
19
|
+
async (closure: (location: string) => void): Promise<void> => {
|
|
20
|
+
for (const pattern of input) {
|
|
21
|
+
for (const file of await _Glob(path.resolve(pattern))) {
|
|
22
|
+
const stats: fs.Stats = await fs.promises.stat(file);
|
|
23
|
+
if (stats.isDirectory() === true)
|
|
24
|
+
await iterate(filter)(closure)(file);
|
|
25
|
+
else if (stats.isFile() && filter(file)) closure(file);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const iterate =
|
|
31
|
+
(filter: (location: string) => boolean) =>
|
|
32
|
+
(closure: (location: string) => void) =>
|
|
33
|
+
async (location: string): Promise<void> => {
|
|
34
|
+
const directory: string[] = await fs.promises.readdir(location);
|
|
35
|
+
for (const file of directory) {
|
|
36
|
+
const next: string = path.resolve(`${location}/${file}`);
|
|
37
|
+
const stats: fs.Stats = await fs.promises.stat(next);
|
|
38
|
+
|
|
39
|
+
if (stats.isDirectory() === true) await iterate(filter)(closure)(next);
|
|
40
|
+
else if (stats.isFile() && filter(next)) closure(next);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const _Glob = (pattern: string): Promise<string[]> =>
|
|
45
|
+
new Promise((resolve, reject) => {
|
|
46
|
+
glob(pattern, (err, matches) => {
|
|
47
|
+
if (err) reject(err);
|
|
48
|
+
else resolve(matches.map((str) => path.resolve(str)));
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface IProps {
|
|
54
|
+
exclude?: string[];
|
|
55
|
+
include: string[];
|
|
56
|
+
filter: (location: string) => boolean;
|
|
57
|
+
}
|