@nestia/core 11.0.0-dev.20260305 → 11.0.0-dev.20260312
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/programmers/TypedBodyProgrammer.js +2 -2
- package/lib/programmers/TypedBodyProgrammer.js.map +1 -1
- package/lib/programmers/TypedQueryBodyProgrammer.js +2 -1
- package/lib/programmers/TypedQueryBodyProgrammer.js.map +1 -1
- package/lib/programmers/TypedQueryProgrammer.js +2 -1
- package/lib/programmers/TypedQueryProgrammer.js.map +1 -1
- package/lib/programmers/TypedQueryRouteProgrammer.js +2 -1
- package/lib/programmers/TypedQueryRouteProgrammer.js.map +1 -1
- package/lib/programmers/TypedRouteProgrammer.js +2 -2
- package/lib/programmers/TypedRouteProgrammer.js.map +1 -1
- package/package.json +8 -8
- package/src/decorators/DynamicModule.ts +44 -44
- package/src/decorators/PlainBody.ts +76 -76
- package/src/decorators/SwaggerExample.ts +100 -100
- package/src/decorators/TypedBody.ts +57 -57
- package/src/decorators/TypedException.ts +147 -147
- package/src/decorators/TypedHeaders.ts +66 -66
- package/src/decorators/TypedParam.ts +77 -77
- package/src/decorators/TypedQuery.ts +234 -234
- package/src/decorators/WebSocketRoute.ts +242 -242
- package/src/decorators/internal/EncryptedConstant.ts +2 -2
- 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 +94 -94
- package/src/decorators/internal/get_path_and_stringify.ts +110 -110
- package/src/decorators/internal/get_text_body.ts +16 -16
- package/src/decorators/internal/is_request_body_undefined.ts +12 -12
- package/src/decorators/internal/load_controller.ts +45 -45
- package/src/decorators/internal/route_error.ts +43 -43
- package/src/decorators/internal/validate_request_body.ts +64 -64
- package/src/decorators/internal/validate_request_form_data.ts +67 -67
- package/src/decorators/internal/validate_request_headers.ts +76 -76
- package/src/decorators/internal/validate_request_query.ts +64 -64
- package/src/index.ts +5 -5
- 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/transformers/NodeTransformer.ts +23 -23
- package/src/transformers/ParameterTransformer.ts +57 -57
- 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 +115 -115
- package/src/utils/Singleton.ts +16 -16
- package/src/utils/SourceFinder.ts +54 -54
- package/src/utils/VersioningStrategy.ts +27 -27
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import { IValidation } from "typia";
|
|
2
|
-
|
|
3
|
-
export type IRequestHeadersValidator<T> =
|
|
4
|
-
| IRequestHeadersValidator.IAssert<T>
|
|
5
|
-
| IRequestHeadersValidator.IIs<T>
|
|
6
|
-
| IRequestHeadersValidator.IValidate<T>;
|
|
7
|
-
export namespace IRequestHeadersValidator {
|
|
8
|
-
export interface IAssert<T> {
|
|
9
|
-
type: "assert";
|
|
10
|
-
assert: (input: Record<string, string | string[] | undefined>) => T;
|
|
11
|
-
}
|
|
12
|
-
export interface IIs<T> {
|
|
13
|
-
type: "is";
|
|
14
|
-
is: (input: Record<string, string | string[] | undefined>) => T | null;
|
|
15
|
-
}
|
|
16
|
-
export interface IValidate<T> {
|
|
17
|
-
type: "validate";
|
|
18
|
-
validate: (
|
|
19
|
-
input: Record<string, string | string[] | undefined>,
|
|
20
|
-
) => IValidation<T>;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
1
|
+
import { IValidation } from "typia";
|
|
2
|
+
|
|
3
|
+
export type IRequestHeadersValidator<T> =
|
|
4
|
+
| IRequestHeadersValidator.IAssert<T>
|
|
5
|
+
| IRequestHeadersValidator.IIs<T>
|
|
6
|
+
| IRequestHeadersValidator.IValidate<T>;
|
|
7
|
+
export namespace IRequestHeadersValidator {
|
|
8
|
+
export interface IAssert<T> {
|
|
9
|
+
type: "assert";
|
|
10
|
+
assert: (input: Record<string, string | string[] | undefined>) => T;
|
|
11
|
+
}
|
|
12
|
+
export interface IIs<T> {
|
|
13
|
+
type: "is";
|
|
14
|
+
is: (input: Record<string, string | string[] | undefined>) => T | null;
|
|
15
|
+
}
|
|
16
|
+
export interface IValidate<T> {
|
|
17
|
+
type: "validate";
|
|
18
|
+
validate: (
|
|
19
|
+
input: Record<string, string | string[] | undefined>,
|
|
20
|
+
) => IValidation<T>;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import { IValidation } from "typia";
|
|
2
|
-
|
|
3
|
-
export type IRequestQueryValidator<T> =
|
|
4
|
-
| IRequestQueryValidator.IAssert<T>
|
|
5
|
-
| IRequestQueryValidator.IIs<T>
|
|
6
|
-
| IRequestQueryValidator.IValidate<T>;
|
|
7
|
-
export namespace IRequestQueryValidator {
|
|
8
|
-
export interface IAssert<T> {
|
|
9
|
-
type: "assert";
|
|
10
|
-
assert: (input: URLSearchParams) => T;
|
|
11
|
-
}
|
|
12
|
-
export interface IIs<T> {
|
|
13
|
-
type: "is";
|
|
14
|
-
is: (input: URLSearchParams) => T | null;
|
|
15
|
-
}
|
|
16
|
-
export interface IValidate<T> {
|
|
17
|
-
type: "validate";
|
|
18
|
-
validate: (input: URLSearchParams) => IValidation<T>;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
1
|
+
import { IValidation } from "typia";
|
|
2
|
+
|
|
3
|
+
export type IRequestQueryValidator<T> =
|
|
4
|
+
| IRequestQueryValidator.IAssert<T>
|
|
5
|
+
| IRequestQueryValidator.IIs<T>
|
|
6
|
+
| IRequestQueryValidator.IValidate<T>;
|
|
7
|
+
export namespace IRequestQueryValidator {
|
|
8
|
+
export interface IAssert<T> {
|
|
9
|
+
type: "assert";
|
|
10
|
+
assert: (input: URLSearchParams) => T;
|
|
11
|
+
}
|
|
12
|
+
export interface IIs<T> {
|
|
13
|
+
type: "is";
|
|
14
|
+
is: (input: URLSearchParams) => T | null;
|
|
15
|
+
}
|
|
16
|
+
export interface IValidate<T> {
|
|
17
|
+
type: "validate";
|
|
18
|
+
validate: (input: URLSearchParams) => IValidation<T>;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { IValidation } from "typia";
|
|
2
|
-
|
|
3
|
-
export type IResponseBodyQuerifier<T> =
|
|
4
|
-
| IResponseBodyquerifier.IStringify<T>
|
|
5
|
-
| IResponseBodyquerifier.IIs<T>
|
|
6
|
-
| IResponseBodyquerifier.IAssert<T>
|
|
7
|
-
| IResponseBodyquerifier.IValidate<T>;
|
|
8
|
-
export namespace IResponseBodyquerifier {
|
|
9
|
-
export interface IStringify<T> {
|
|
10
|
-
type: "stringify";
|
|
11
|
-
stringify: (input: T) => URLSearchParams;
|
|
12
|
-
}
|
|
13
|
-
export interface IIs<T> {
|
|
14
|
-
type: "is";
|
|
15
|
-
is: (input: T) => URLSearchParams | null;
|
|
16
|
-
}
|
|
17
|
-
export interface IAssert<T> {
|
|
18
|
-
type: "assert";
|
|
19
|
-
assert: (input: T) => URLSearchParams;
|
|
20
|
-
}
|
|
21
|
-
export interface IValidate<T> {
|
|
22
|
-
type: "validate";
|
|
23
|
-
validate: (input: T) => IValidation<URLSearchParams>;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
1
|
+
import { IValidation } from "typia";
|
|
2
|
+
|
|
3
|
+
export type IResponseBodyQuerifier<T> =
|
|
4
|
+
| IResponseBodyquerifier.IStringify<T>
|
|
5
|
+
| IResponseBodyquerifier.IIs<T>
|
|
6
|
+
| IResponseBodyquerifier.IAssert<T>
|
|
7
|
+
| IResponseBodyquerifier.IValidate<T>;
|
|
8
|
+
export namespace IResponseBodyquerifier {
|
|
9
|
+
export interface IStringify<T> {
|
|
10
|
+
type: "stringify";
|
|
11
|
+
stringify: (input: T) => URLSearchParams;
|
|
12
|
+
}
|
|
13
|
+
export interface IIs<T> {
|
|
14
|
+
type: "is";
|
|
15
|
+
is: (input: T) => URLSearchParams | null;
|
|
16
|
+
}
|
|
17
|
+
export interface IAssert<T> {
|
|
18
|
+
type: "assert";
|
|
19
|
+
assert: (input: T) => URLSearchParams;
|
|
20
|
+
}
|
|
21
|
+
export interface IValidate<T> {
|
|
22
|
+
type: "validate";
|
|
23
|
+
validate: (input: T) => IValidation<URLSearchParams>;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
import { IValidation } from "typia";
|
|
2
|
-
|
|
3
|
-
export type IResponseBodyStringifier<T> =
|
|
4
|
-
| IResponseBodyStringifier.IStringify<T>
|
|
5
|
-
| IResponseBodyStringifier.IIs<T>
|
|
6
|
-
| IResponseBodyStringifier.IAssert<T>
|
|
7
|
-
| IResponseBodyStringifier.IValidate<T>
|
|
8
|
-
| IResponseBodyStringifier.IValidateLog<T>;
|
|
9
|
-
export namespace IResponseBodyStringifier {
|
|
10
|
-
export interface IStringify<T> {
|
|
11
|
-
type: "stringify";
|
|
12
|
-
stringify: (input: T) => string;
|
|
13
|
-
}
|
|
14
|
-
export interface IIs<T> {
|
|
15
|
-
type: "is";
|
|
16
|
-
is: (input: T) => string | null;
|
|
17
|
-
}
|
|
18
|
-
export interface IAssert<T> {
|
|
19
|
-
type: "assert";
|
|
20
|
-
assert: (input: T) => string;
|
|
21
|
-
}
|
|
22
|
-
export interface IValidate<T> {
|
|
23
|
-
type: "validate";
|
|
24
|
-
validate: (input: T) => IValidation<string>;
|
|
25
|
-
}
|
|
26
|
-
export interface IValidateLog<T> {
|
|
27
|
-
type: "validate.log";
|
|
28
|
-
validate: (input: T) => IValidation<string>;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
1
|
+
import { IValidation } from "typia";
|
|
2
|
+
|
|
3
|
+
export type IResponseBodyStringifier<T> =
|
|
4
|
+
| IResponseBodyStringifier.IStringify<T>
|
|
5
|
+
| IResponseBodyStringifier.IIs<T>
|
|
6
|
+
| IResponseBodyStringifier.IAssert<T>
|
|
7
|
+
| IResponseBodyStringifier.IValidate<T>
|
|
8
|
+
| IResponseBodyStringifier.IValidateLog<T>;
|
|
9
|
+
export namespace IResponseBodyStringifier {
|
|
10
|
+
export interface IStringify<T> {
|
|
11
|
+
type: "stringify";
|
|
12
|
+
stringify: (input: T) => string;
|
|
13
|
+
}
|
|
14
|
+
export interface IIs<T> {
|
|
15
|
+
type: "is";
|
|
16
|
+
is: (input: T) => string | null;
|
|
17
|
+
}
|
|
18
|
+
export interface IAssert<T> {
|
|
19
|
+
type: "assert";
|
|
20
|
+
assert: (input: T) => string;
|
|
21
|
+
}
|
|
22
|
+
export interface IValidate<T> {
|
|
23
|
+
type: "validate";
|
|
24
|
+
validate: (input: T) => IValidation<string>;
|
|
25
|
+
}
|
|
26
|
+
export interface IValidateLog<T> {
|
|
27
|
+
type: "validate.log";
|
|
28
|
+
validate: (input: T) => IValidation<string>;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
|
|
3
|
-
import { INestiaTransformContext } from "../options/INestiaTransformProject";
|
|
4
|
-
import { MethodTransformer } from "./MethodTransformer";
|
|
5
|
-
import { ParameterTransformer } from "./ParameterTransformer";
|
|
6
|
-
|
|
7
|
-
export namespace NodeTransformer {
|
|
8
|
-
export const transform = (props: {
|
|
9
|
-
context: INestiaTransformContext;
|
|
10
|
-
node: ts.Node;
|
|
11
|
-
}): ts.Node =>
|
|
12
|
-
ts.isMethodDeclaration(props.node)
|
|
13
|
-
? MethodTransformer.transform({
|
|
14
|
-
context: props.context,
|
|
15
|
-
method: props.node,
|
|
16
|
-
})
|
|
17
|
-
: ts.isParameter(props.node)
|
|
18
|
-
? ParameterTransformer.transform({
|
|
19
|
-
context: props.context,
|
|
20
|
-
param: props.node,
|
|
21
|
-
})
|
|
22
|
-
: props.node;
|
|
23
|
-
}
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { INestiaTransformContext } from "../options/INestiaTransformProject";
|
|
4
|
+
import { MethodTransformer } from "./MethodTransformer";
|
|
5
|
+
import { ParameterTransformer } from "./ParameterTransformer";
|
|
6
|
+
|
|
7
|
+
export namespace NodeTransformer {
|
|
8
|
+
export const transform = (props: {
|
|
9
|
+
context: INestiaTransformContext;
|
|
10
|
+
node: ts.Node;
|
|
11
|
+
}): ts.Node =>
|
|
12
|
+
ts.isMethodDeclaration(props.node)
|
|
13
|
+
? MethodTransformer.transform({
|
|
14
|
+
context: props.context,
|
|
15
|
+
method: props.node,
|
|
16
|
+
})
|
|
17
|
+
: ts.isParameter(props.node)
|
|
18
|
+
? ParameterTransformer.transform({
|
|
19
|
+
context: props.context,
|
|
20
|
+
param: props.node,
|
|
21
|
+
})
|
|
22
|
+
: props.node;
|
|
23
|
+
}
|
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
|
|
3
|
-
import { INestiaTransformContext } from "../options/INestiaTransformProject";
|
|
4
|
-
import { ParameterDecoratorTransformer } from "./ParameterDecoratorTransformer";
|
|
5
|
-
|
|
6
|
-
export namespace ParameterTransformer {
|
|
7
|
-
export const transform = (props: {
|
|
8
|
-
context: INestiaTransformContext;
|
|
9
|
-
param: ts.ParameterDeclaration;
|
|
10
|
-
}): ts.ParameterDeclaration => {
|
|
11
|
-
// CHECK DECORATOR
|
|
12
|
-
const decorators: readonly ts.Decorator[] | undefined = ts.getDecorators
|
|
13
|
-
? ts.getDecorators(props.param)
|
|
14
|
-
: (props.param as any).decorators;
|
|
15
|
-
if (!decorators?.length) return props.param;
|
|
16
|
-
|
|
17
|
-
// GET TYPE INFO
|
|
18
|
-
const type: ts.Type = props.context.checker.getTypeAtLocation(props.param);
|
|
19
|
-
|
|
20
|
-
// WHEN LATEST TS VERSION
|
|
21
|
-
if (ts.getDecorators !== undefined)
|
|
22
|
-
return ts.factory.updateParameterDeclaration(
|
|
23
|
-
props.param,
|
|
24
|
-
(props.param.modifiers || []).map((mod) =>
|
|
25
|
-
ts.isDecorator(mod)
|
|
26
|
-
? ParameterDecoratorTransformer.transform({
|
|
27
|
-
context: props.context,
|
|
28
|
-
decorator: mod,
|
|
29
|
-
type,
|
|
30
|
-
})
|
|
31
|
-
: mod,
|
|
32
|
-
),
|
|
33
|
-
props.param.dotDotDotToken,
|
|
34
|
-
props.param.name,
|
|
35
|
-
props.param.questionToken,
|
|
36
|
-
props.param.type,
|
|
37
|
-
props.param.initializer,
|
|
38
|
-
);
|
|
39
|
-
// eslint-disable-next-line
|
|
40
|
-
return (ts.factory.updateParameterDeclaration as any)(
|
|
41
|
-
props.param,
|
|
42
|
-
decorators.map((deco) =>
|
|
43
|
-
ParameterDecoratorTransformer.transform({
|
|
44
|
-
context: props.context,
|
|
45
|
-
decorator: deco,
|
|
46
|
-
type,
|
|
47
|
-
}),
|
|
48
|
-
),
|
|
49
|
-
(props.param as any).modifiers,
|
|
50
|
-
props.param.dotDotDotToken,
|
|
51
|
-
props.param.name,
|
|
52
|
-
props.param.questionToken,
|
|
53
|
-
props.param.type,
|
|
54
|
-
props.param.initializer,
|
|
55
|
-
);
|
|
56
|
-
};
|
|
57
|
-
}
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { INestiaTransformContext } from "../options/INestiaTransformProject";
|
|
4
|
+
import { ParameterDecoratorTransformer } from "./ParameterDecoratorTransformer";
|
|
5
|
+
|
|
6
|
+
export namespace ParameterTransformer {
|
|
7
|
+
export const transform = (props: {
|
|
8
|
+
context: INestiaTransformContext;
|
|
9
|
+
param: ts.ParameterDeclaration;
|
|
10
|
+
}): ts.ParameterDeclaration => {
|
|
11
|
+
// CHECK DECORATOR
|
|
12
|
+
const decorators: readonly ts.Decorator[] | undefined = ts.getDecorators
|
|
13
|
+
? ts.getDecorators(props.param)
|
|
14
|
+
: (props.param as any).decorators;
|
|
15
|
+
if (!decorators?.length) return props.param;
|
|
16
|
+
|
|
17
|
+
// GET TYPE INFO
|
|
18
|
+
const type: ts.Type = props.context.checker.getTypeAtLocation(props.param);
|
|
19
|
+
|
|
20
|
+
// WHEN LATEST TS VERSION
|
|
21
|
+
if (ts.getDecorators !== undefined)
|
|
22
|
+
return ts.factory.updateParameterDeclaration(
|
|
23
|
+
props.param,
|
|
24
|
+
(props.param.modifiers || []).map((mod) =>
|
|
25
|
+
ts.isDecorator(mod)
|
|
26
|
+
? ParameterDecoratorTransformer.transform({
|
|
27
|
+
context: props.context,
|
|
28
|
+
decorator: mod,
|
|
29
|
+
type,
|
|
30
|
+
})
|
|
31
|
+
: mod,
|
|
32
|
+
),
|
|
33
|
+
props.param.dotDotDotToken,
|
|
34
|
+
props.param.name,
|
|
35
|
+
props.param.questionToken,
|
|
36
|
+
props.param.type,
|
|
37
|
+
props.param.initializer,
|
|
38
|
+
);
|
|
39
|
+
// eslint-disable-next-line
|
|
40
|
+
return (ts.factory.updateParameterDeclaration as any)(
|
|
41
|
+
props.param,
|
|
42
|
+
decorators.map((deco) =>
|
|
43
|
+
ParameterDecoratorTransformer.transform({
|
|
44
|
+
context: props.context,
|
|
45
|
+
decorator: deco,
|
|
46
|
+
type,
|
|
47
|
+
}),
|
|
48
|
+
),
|
|
49
|
+
(props.param as any).modifiers,
|
|
50
|
+
props.param.dotDotDotToken,
|
|
51
|
+
props.param.name,
|
|
52
|
+
props.param.questionToken,
|
|
53
|
+
props.param.type,
|
|
54
|
+
props.param.initializer,
|
|
55
|
+
);
|
|
56
|
+
};
|
|
57
|
+
}
|
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,115 +1,115 @@
|
|
|
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
|
|
10
|
-
* class with its conversion method to a regular {@link nest.HttpException}
|
|
11
|
-
* instance.
|
|
12
|
-
*
|
|
13
|
-
* If you define an API function through {@link TypedRoute} or
|
|
14
|
-
* {@link EncryptedRoute} instead of the basic router decorator functions like
|
|
15
|
-
* {@link nest.Get} or {@link nest.Post} and the API function throws a custom
|
|
16
|
-
* error whose class has been {@link ExceptionManager.insert inserted} in this
|
|
17
|
-
* `EntityManager`, the error would be automatically converted to the regular
|
|
18
|
-
* {@link nest.HttpException} instance by the {@link ExceptionManager.Closure}
|
|
19
|
-
* function.
|
|
20
|
-
*
|
|
21
|
-
* Therefore, with this `ExceptionManager` and {@link TypedRoute} or
|
|
22
|
-
* {@link EncryptedRoute}, you can manage your custom error classes much
|
|
23
|
-
* systemtically. You can avoid 500 internal server error or hard coding
|
|
24
|
-
* implementation about the custom error classes.
|
|
25
|
-
*
|
|
26
|
-
* Below error classes are defaultly configured in this `ExceptionManager`
|
|
27
|
-
*
|
|
28
|
-
* - `typia.TypeGuardError`
|
|
29
|
-
* - `@nestia/fetcher.HttpError`
|
|
30
|
-
*
|
|
31
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
32
|
-
*/
|
|
33
|
-
export namespace ExceptionManager {
|
|
34
|
-
/**
|
|
35
|
-
* Insert an error class with converter.
|
|
36
|
-
*
|
|
37
|
-
* If you've inserted an duplicated error class, the closure would be
|
|
38
|
-
* overwritten.
|
|
39
|
-
*
|
|
40
|
-
* @param creator Target error class
|
|
41
|
-
* @param closure A closure function converting to the `HttpException` class
|
|
42
|
-
*/
|
|
43
|
-
export function insert<T extends Error>(
|
|
44
|
-
creator: Creator<T>,
|
|
45
|
-
closure: Closure<T>,
|
|
46
|
-
): void {
|
|
47
|
-
const index: number = tuples.findIndex((tuple) => tuple[0] === creator);
|
|
48
|
-
if (index !== -1) tuples.splice(index, 1);
|
|
49
|
-
|
|
50
|
-
tuples.push([creator, closure]);
|
|
51
|
-
tuples.sort(([x], [y]) => (x.prototype instanceof y ? -1 : 1));
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Erase an error class.
|
|
56
|
-
*
|
|
57
|
-
* @param creator Target error class
|
|
58
|
-
* @returns Whether be erased or not
|
|
59
|
-
*/
|
|
60
|
-
export function erase<T extends Error>(creator: Creator<T>): boolean {
|
|
61
|
-
const index: number = tuples.findIndex((tuple) => tuple[0] === creator);
|
|
62
|
-
if (index === -1) return false;
|
|
63
|
-
|
|
64
|
-
tuples.splice(index, 1);
|
|
65
|
-
return true;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export function on(closure: (error: any) => any): void {
|
|
69
|
-
listeners.add(closure);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export function off(closure: (error: any) => any): void {
|
|
73
|
-
listeners.delete(closure);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Type of a closure function converting to the regular
|
|
78
|
-
* {@link nest.HttpException}.
|
|
79
|
-
*
|
|
80
|
-
* `ExceptionManager.Closure` is a type of closure function who are converting
|
|
81
|
-
* from custom error to the regular {@link nest.HttpException} instance. It
|
|
82
|
-
* would be used in the {@link ExceptionManager} with {@link TypedRoute} or
|
|
83
|
-
* {@link EncryptedRoute}.
|
|
84
|
-
*/
|
|
85
|
-
export interface Closure<T extends Error> {
|
|
86
|
-
/**
|
|
87
|
-
* Error converter.
|
|
88
|
-
*
|
|
89
|
-
* Convert from custom error to the regular {@link nest.HttpException}
|
|
90
|
-
* instance.
|
|
91
|
-
*
|
|
92
|
-
* @param exception Custom error instance
|
|
93
|
-
* @returns Regular {@link nest.HttpException} instance
|
|
94
|
-
*/
|
|
95
|
-
(exception: T): HttpException;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/** @internal */
|
|
99
|
-
export const tuples: Array<[Creator<any>, Closure<any>]> = [];
|
|
100
|
-
|
|
101
|
-
/** @internal */
|
|
102
|
-
export const listeners: Set<(error: any) => any> = new Set();
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
ExceptionManager.insert(
|
|
106
|
-
HttpError,
|
|
107
|
-
(error) =>
|
|
108
|
-
new HttpException(
|
|
109
|
-
{
|
|
110
|
-
path: error.path,
|
|
111
|
-
message: error.message,
|
|
112
|
-
},
|
|
113
|
-
error.status,
|
|
114
|
-
),
|
|
115
|
-
);
|
|
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
|
|
10
|
+
* class with its conversion method to a regular {@link nest.HttpException}
|
|
11
|
+
* instance.
|
|
12
|
+
*
|
|
13
|
+
* If you define an API function through {@link TypedRoute} or
|
|
14
|
+
* {@link EncryptedRoute} instead of the basic router decorator functions like
|
|
15
|
+
* {@link nest.Get} or {@link nest.Post} and the API function throws a custom
|
|
16
|
+
* error whose class has been {@link ExceptionManager.insert inserted} in this
|
|
17
|
+
* `EntityManager`, the error would be automatically converted to the regular
|
|
18
|
+
* {@link nest.HttpException} instance by the {@link ExceptionManager.Closure}
|
|
19
|
+
* function.
|
|
20
|
+
*
|
|
21
|
+
* Therefore, with this `ExceptionManager` and {@link TypedRoute} or
|
|
22
|
+
* {@link EncryptedRoute}, you can manage your custom error classes much
|
|
23
|
+
* systemtically. You can avoid 500 internal server error or hard coding
|
|
24
|
+
* implementation about the custom error classes.
|
|
25
|
+
*
|
|
26
|
+
* Below error classes are defaultly configured in this `ExceptionManager`
|
|
27
|
+
*
|
|
28
|
+
* - `typia.TypeGuardError`
|
|
29
|
+
* - `@nestia/fetcher.HttpError`
|
|
30
|
+
*
|
|
31
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
32
|
+
*/
|
|
33
|
+
export namespace ExceptionManager {
|
|
34
|
+
/**
|
|
35
|
+
* Insert an error class with converter.
|
|
36
|
+
*
|
|
37
|
+
* If you've inserted an duplicated error class, the closure would be
|
|
38
|
+
* overwritten.
|
|
39
|
+
*
|
|
40
|
+
* @param creator Target error class
|
|
41
|
+
* @param closure A closure function converting to the `HttpException` class
|
|
42
|
+
*/
|
|
43
|
+
export function insert<T extends Error>(
|
|
44
|
+
creator: Creator<T>,
|
|
45
|
+
closure: Closure<T>,
|
|
46
|
+
): void {
|
|
47
|
+
const index: number = tuples.findIndex((tuple) => tuple[0] === creator);
|
|
48
|
+
if (index !== -1) tuples.splice(index, 1);
|
|
49
|
+
|
|
50
|
+
tuples.push([creator, closure]);
|
|
51
|
+
tuples.sort(([x], [y]) => (x.prototype instanceof y ? -1 : 1));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Erase an error class.
|
|
56
|
+
*
|
|
57
|
+
* @param creator Target error class
|
|
58
|
+
* @returns Whether be erased or not
|
|
59
|
+
*/
|
|
60
|
+
export function erase<T extends Error>(creator: Creator<T>): boolean {
|
|
61
|
+
const index: number = tuples.findIndex((tuple) => tuple[0] === creator);
|
|
62
|
+
if (index === -1) return false;
|
|
63
|
+
|
|
64
|
+
tuples.splice(index, 1);
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function on(closure: (error: any) => any): void {
|
|
69
|
+
listeners.add(closure);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function off(closure: (error: any) => any): void {
|
|
73
|
+
listeners.delete(closure);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Type of a closure function converting to the regular
|
|
78
|
+
* {@link nest.HttpException}.
|
|
79
|
+
*
|
|
80
|
+
* `ExceptionManager.Closure` is a type of closure function who are converting
|
|
81
|
+
* from custom error to the regular {@link nest.HttpException} instance. It
|
|
82
|
+
* would be used in the {@link ExceptionManager} with {@link TypedRoute} or
|
|
83
|
+
* {@link EncryptedRoute}.
|
|
84
|
+
*/
|
|
85
|
+
export interface Closure<T extends Error> {
|
|
86
|
+
/**
|
|
87
|
+
* Error converter.
|
|
88
|
+
*
|
|
89
|
+
* Convert from custom error to the regular {@link nest.HttpException}
|
|
90
|
+
* instance.
|
|
91
|
+
*
|
|
92
|
+
* @param exception Custom error instance
|
|
93
|
+
* @returns Regular {@link nest.HttpException} instance
|
|
94
|
+
*/
|
|
95
|
+
(exception: T): HttpException;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** @internal */
|
|
99
|
+
export const tuples: Array<[Creator<any>, Closure<any>]> = [];
|
|
100
|
+
|
|
101
|
+
/** @internal */
|
|
102
|
+
export const listeners: Set<(error: any) => any> = new Set();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
ExceptionManager.insert(
|
|
106
|
+
HttpError,
|
|
107
|
+
(error) =>
|
|
108
|
+
new HttpException(
|
|
109
|
+
{
|
|
110
|
+
path: error.path,
|
|
111
|
+
message: error.message,
|
|
112
|
+
},
|
|
113
|
+
error.status,
|
|
114
|
+
),
|
|
115
|
+
);
|
package/src/utils/Singleton.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
/** @internal */
|
|
2
|
-
export class Singleton<T> {
|
|
3
|
-
private value_: T | object;
|
|
4
|
-
|
|
5
|
-
public constructor(private readonly closure_: () => T) {
|
|
6
|
-
this.value_ = NOT_MOUNTED_YET;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
public get(): T {
|
|
10
|
-
if (this.value_ === NOT_MOUNTED_YET) this.value_ = this.closure_();
|
|
11
|
-
return this.value_ as T;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/** @internal */
|
|
16
|
-
const NOT_MOUNTED_YET = {};
|
|
1
|
+
/** @internal */
|
|
2
|
+
export class Singleton<T> {
|
|
3
|
+
private value_: T | object;
|
|
4
|
+
|
|
5
|
+
public constructor(private readonly closure_: () => T) {
|
|
6
|
+
this.value_ = NOT_MOUNTED_YET;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
public get(): T {
|
|
10
|
+
if (this.value_ === NOT_MOUNTED_YET) this.value_ = this.closure_();
|
|
11
|
+
return this.value_ as T;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** @internal */
|
|
16
|
+
const NOT_MOUNTED_YET = {};
|