@nestia/core 12.0.0-dev.20260521.6 → 12.0.0-dev.20260612.1
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/MIGRATION.md +169 -169
- package/README.md +93 -93
- package/lib/adaptors/McpAdaptor.d.ts +75 -0
- package/lib/adaptors/McpAdaptor.js +257 -0
- package/lib/adaptors/McpAdaptor.js.map +1 -0
- package/lib/adaptors/WebSocketAdaptor.js +4 -4
- package/lib/adaptors/WebSocketAdaptor.js.map +1 -1
- package/lib/decorators/McpRoute.d.ts +69 -0
- package/lib/decorators/McpRoute.js +58 -0
- package/lib/decorators/McpRoute.js.map +1 -0
- package/lib/decorators/TypedParam.js +4 -4
- package/lib/decorators/TypedParam.js.map +1 -1
- package/lib/decorators/TypedRoute.js +1 -1
- package/lib/decorators/TypedRoute.js.map +1 -1
- package/lib/decorators/internal/IMcpRouteReflect.d.ts +2 -0
- package/lib/decorators/internal/IMcpRouteReflect.js +3 -0
- package/lib/decorators/internal/IMcpRouteReflect.js.map +1 -0
- package/lib/decorators/internal/get_path_and_querify.js +4 -4
- package/lib/decorators/internal/get_path_and_querify.js.map +1 -1
- package/lib/decorators/internal/get_path_and_stringify.js +4 -4
- package/lib/decorators/internal/get_path_and_stringify.js.map +1 -1
- package/lib/decorators/internal/load_controller.js +34 -65
- package/lib/decorators/internal/load_controller.js.map +1 -1
- package/lib/decorators/internal/validate_request_body.js +4 -4
- package/lib/decorators/internal/validate_request_body.js.map +1 -1
- package/lib/decorators/internal/validate_request_form_data.js +4 -4
- package/lib/decorators/internal/validate_request_form_data.js.map +1 -1
- package/lib/decorators/internal/validate_request_headers.js +4 -4
- package/lib/decorators/internal/validate_request_headers.js.map +1 -1
- package/lib/decorators/internal/validate_request_query.js +4 -4
- package/lib/decorators/internal/validate_request_query.js.map +1 -1
- package/lib/module.d.ts +2 -0
- package/lib/module.js +2 -0
- package/lib/module.js.map +1 -1
- package/native/cmd/ttsc-nestia/main.go +11 -11
- package/native/go.mod +32 -32
- package/native/go.sum +54 -54
- package/native/plugin/plan.go +102 -102
- package/native/transform/ast.go +32 -32
- package/native/transform/build.go +380 -437
- package/native/transform/cleanup.go +408 -408
- package/native/transform/contributor.go +97 -68
- package/native/transform/core_querify.go +231 -227
- package/native/transform/core_transform.go +1996 -1713
- package/native/transform/core_websocket.go +115 -115
- package/native/transform/exports.go +13 -13
- package/native/transform/mcp_transform.go +414 -0
- package/native/transform/node_transform.go +357 -0
- package/native/transform/path_rewrite.go +285 -285
- package/native/transform/printer.go +244 -244
- package/native/transform/rewrite.go +668 -662
- package/native/transform/run.go +73 -73
- package/native/transform/transform.go +336 -387
- package/native/transform/typia_fast.go +352 -326
- package/native/transform/typia_replacement.go +24 -24
- package/native/transform.cjs +43 -43
- package/package.json +15 -8
- package/src/adaptors/McpAdaptor.ts +276 -0
- package/src/adaptors/WebSocketAdaptor.ts +429 -429
- package/src/decorators/DynamicModule.ts +44 -44
- package/src/decorators/EncryptedBody.ts +97 -97
- package/src/decorators/EncryptedController.ts +40 -40
- package/src/decorators/EncryptedModule.ts +98 -98
- package/src/decorators/EncryptedRoute.ts +213 -213
- package/src/decorators/HumanRoute.ts +21 -21
- package/src/decorators/McpRoute.ts +154 -0
- package/src/decorators/NoTransformConfigurationError.ts +40 -40
- package/src/decorators/PlainBody.ts +76 -76
- package/src/decorators/SwaggerCustomizer.ts +97 -97
- package/src/decorators/SwaggerExample.ts +180 -180
- package/src/decorators/TypedBody.ts +57 -57
- package/src/decorators/TypedException.ts +147 -147
- package/src/decorators/TypedFormData.ts +187 -187
- package/src/decorators/TypedHeaders.ts +66 -66
- package/src/decorators/TypedParam.ts +77 -77
- package/src/decorators/TypedQuery.ts +234 -234
- package/src/decorators/TypedRoute.ts +198 -196
- package/src/decorators/WebSocketRoute.ts +242 -242
- package/src/decorators/doNotThrowTransformError.ts +5 -5
- package/src/decorators/internal/EncryptedConstant.ts +2 -2
- package/src/decorators/internal/IMcpRouteReflect.ts +40 -0
- package/src/decorators/internal/IWebSocketRouteReflect.ts +23 -23
- 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/headers_to_object.ts +11 -11
- package/src/decorators/internal/is_request_body_undefined.ts +12 -12
- package/src/decorators/internal/load_controller.ts +91 -76
- 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 +83 -83
- package/src/index.ts +5 -5
- package/src/module.ts +25 -23
- 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/transform.ts +101 -101
- 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
- package/native/transform/cleanup_test.go +0 -76
- package/native/transform/commonjs_import_alias_test.go +0 -49
- package/native/transform/core_dispatch_test.go +0 -127
- package/native/transform/path_rewrite_test.go +0 -243
- package/native/transform/rewrite_test.go +0 -118
- package/native/transform/rewrite_unique_base_test.go +0 -48
|
@@ -1,94 +1,94 @@
|
|
|
1
|
-
import { InternalServerErrorException } from "@nestjs/common";
|
|
2
|
-
import typia, { IValidation, TypeGuardError } from "typia";
|
|
3
|
-
|
|
4
|
-
import { IResponseBodyQuerifier } from "../../options/IResponseBodyQuerifier";
|
|
5
|
-
import { NoTransformConfigurationError } from "../NoTransformConfigurationError";
|
|
6
|
-
|
|
7
|
-
/** @internal */
|
|
8
|
-
export const get_path_and_querify =
|
|
9
|
-
(method: string) =>
|
|
10
|
-
(
|
|
11
|
-
...args: any[]
|
|
12
|
-
): [string | string[] | undefined, (input: any) => URLSearchParams] => {
|
|
13
|
-
const path: string | string[] | null | undefined =
|
|
14
|
-
args[0] === undefined ||
|
|
15
|
-
typeof args[0] === "string" ||
|
|
16
|
-
Array.isArray(args[0])
|
|
17
|
-
? args[0]
|
|
18
|
-
: null;
|
|
19
|
-
const functor: IResponseBodyQuerifier<any> | undefined =
|
|
20
|
-
path === null ? args[0] : args[1];
|
|
21
|
-
return [path ?? undefined, take(method)(functor)];
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
/** @internal */
|
|
25
|
-
const take =
|
|
26
|
-
(method: string) =>
|
|
27
|
-
<T>(functor?: IResponseBodyQuerifier<T> | null) => {
|
|
28
|
-
if (functor === undefined) {
|
|
29
|
-
NoTransformConfigurationError(method);
|
|
30
|
-
return querify;
|
|
31
|
-
} else if (functor === null) return querify;
|
|
32
|
-
else if (functor.type === "stringify") return functor.stringify;
|
|
33
|
-
else if (functor.type === "assert") return assert(functor.assert);
|
|
34
|
-
else if (functor.type === "is") return is(functor.is);
|
|
35
|
-
else if (functor.type === "validate") return validate(functor.validate);
|
|
36
|
-
throw new Error(
|
|
37
|
-
`Error on nestia.core.${method}(): invalid typed stringify function.`,
|
|
38
|
-
);
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
/** @internal */
|
|
42
|
-
const querify = (input: Record<string, any>): URLSearchParams => {
|
|
43
|
-
const output: URLSearchParams = new URLSearchParams();
|
|
44
|
-
for (const [key, value] of Object.entries(input))
|
|
45
|
-
if (key === undefined) continue;
|
|
46
|
-
else if (Array.isArray(value))
|
|
47
|
-
for (const elem of value) output.append(key, String(elem));
|
|
48
|
-
else output.append(key, String(value));
|
|
49
|
-
return output;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
/** @internal */
|
|
53
|
-
const assert =
|
|
54
|
-
<T>(closure: (data: T) => URLSearchParams) =>
|
|
55
|
-
(data: T) => {
|
|
56
|
-
try {
|
|
57
|
-
return closure(data);
|
|
58
|
-
} catch (exp) {
|
|
59
|
-
if (typia.is<TypeGuardError>(exp))
|
|
60
|
-
throw new InternalServerErrorException({
|
|
61
|
-
path: exp.path,
|
|
62
|
-
reason: exp.message,
|
|
63
|
-
expected: exp.expected,
|
|
64
|
-
value: exp.value,
|
|
65
|
-
message: MESSAGE,
|
|
66
|
-
});
|
|
67
|
-
throw exp;
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
/** @internal */
|
|
72
|
-
const is =
|
|
73
|
-
<T>(closure: (data: T) => URLSearchParams | null) =>
|
|
74
|
-
(data: T) => {
|
|
75
|
-
const result: URLSearchParams | null = closure(data);
|
|
76
|
-
if (result === null) throw new InternalServerErrorException(MESSAGE);
|
|
77
|
-
return result;
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
/** @internal */
|
|
81
|
-
const validate =
|
|
82
|
-
<T>(closure: (data: T) => IValidation<URLSearchParams>) =>
|
|
83
|
-
(data: T) => {
|
|
84
|
-
const result: IValidation<URLSearchParams> = closure(data);
|
|
85
|
-
if (result.success === false)
|
|
86
|
-
throw new InternalServerErrorException({
|
|
87
|
-
errors: result.errors,
|
|
88
|
-
message: MESSAGE,
|
|
89
|
-
});
|
|
90
|
-
return result.data;
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
/** @internal */
|
|
94
|
-
const MESSAGE = "Response body data is not following the promised type.";
|
|
1
|
+
import { InternalServerErrorException } from "@nestjs/common";
|
|
2
|
+
import typia, { IValidation, TypeGuardError } from "typia";
|
|
3
|
+
|
|
4
|
+
import { IResponseBodyQuerifier } from "../../options/IResponseBodyQuerifier";
|
|
5
|
+
import { NoTransformConfigurationError } from "../NoTransformConfigurationError";
|
|
6
|
+
|
|
7
|
+
/** @internal */
|
|
8
|
+
export const get_path_and_querify =
|
|
9
|
+
(method: string) =>
|
|
10
|
+
(
|
|
11
|
+
...args: any[]
|
|
12
|
+
): [string | string[] | undefined, (input: any) => URLSearchParams] => {
|
|
13
|
+
const path: string | string[] | null | undefined =
|
|
14
|
+
args[0] === undefined ||
|
|
15
|
+
typeof args[0] === "string" ||
|
|
16
|
+
Array.isArray(args[0])
|
|
17
|
+
? args[0]
|
|
18
|
+
: null;
|
|
19
|
+
const functor: IResponseBodyQuerifier<any> | undefined =
|
|
20
|
+
path === null ? args[0] : args[1];
|
|
21
|
+
return [path ?? undefined, take(method)(functor)];
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/** @internal */
|
|
25
|
+
const take =
|
|
26
|
+
(method: string) =>
|
|
27
|
+
<T>(functor?: IResponseBodyQuerifier<T> | null) => {
|
|
28
|
+
if (functor === undefined) {
|
|
29
|
+
NoTransformConfigurationError(method);
|
|
30
|
+
return querify;
|
|
31
|
+
} else if (functor === null) return querify;
|
|
32
|
+
else if (functor.type === "stringify") return functor.stringify;
|
|
33
|
+
else if (functor.type === "assert") return assert(functor.assert);
|
|
34
|
+
else if (functor.type === "is") return is(functor.is);
|
|
35
|
+
else if (functor.type === "validate") return validate(functor.validate);
|
|
36
|
+
throw new Error(
|
|
37
|
+
`Error on nestia.core.${method}(): invalid typed stringify function.`,
|
|
38
|
+
);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** @internal */
|
|
42
|
+
const querify = (input: Record<string, any>): URLSearchParams => {
|
|
43
|
+
const output: URLSearchParams = new URLSearchParams();
|
|
44
|
+
for (const [key, value] of Object.entries(input))
|
|
45
|
+
if (key === undefined) continue;
|
|
46
|
+
else if (Array.isArray(value))
|
|
47
|
+
for (const elem of value) output.append(key, String(elem));
|
|
48
|
+
else output.append(key, String(value));
|
|
49
|
+
return output;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** @internal */
|
|
53
|
+
const assert =
|
|
54
|
+
<T>(closure: (data: T) => URLSearchParams) =>
|
|
55
|
+
(data: T) => {
|
|
56
|
+
try {
|
|
57
|
+
return closure(data);
|
|
58
|
+
} catch (exp) {
|
|
59
|
+
if (typia.is<TypeGuardError>(exp))
|
|
60
|
+
throw new InternalServerErrorException({
|
|
61
|
+
path: exp.path,
|
|
62
|
+
reason: exp.message,
|
|
63
|
+
expected: exp.expected,
|
|
64
|
+
value: exp.value,
|
|
65
|
+
message: MESSAGE,
|
|
66
|
+
});
|
|
67
|
+
throw exp;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** @internal */
|
|
72
|
+
const is =
|
|
73
|
+
<T>(closure: (data: T) => URLSearchParams | null) =>
|
|
74
|
+
(data: T) => {
|
|
75
|
+
const result: URLSearchParams | null = closure(data);
|
|
76
|
+
if (result === null) throw new InternalServerErrorException(MESSAGE);
|
|
77
|
+
return result;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/** @internal */
|
|
81
|
+
const validate =
|
|
82
|
+
<T>(closure: (data: T) => IValidation<URLSearchParams>) =>
|
|
83
|
+
(data: T) => {
|
|
84
|
+
const result: IValidation<URLSearchParams> = closure(data);
|
|
85
|
+
if (result.success === false)
|
|
86
|
+
throw new InternalServerErrorException({
|
|
87
|
+
errors: result.errors,
|
|
88
|
+
message: MESSAGE,
|
|
89
|
+
});
|
|
90
|
+
return result.data;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/** @internal */
|
|
94
|
+
const MESSAGE = "Response body data is not following the promised type.";
|
|
@@ -1,110 +1,110 @@
|
|
|
1
|
-
import { InternalServerErrorException } from "@nestjs/common";
|
|
2
|
-
import typia, { IValidation, TypeGuardError } from "typia";
|
|
3
|
-
|
|
4
|
-
import { IResponseBodyStringifier } from "../../options/IResponseBodyStringifier";
|
|
5
|
-
import { NoTransformConfigurationError } from "../NoTransformConfigurationError";
|
|
6
|
-
import { TypedRoute } from "../TypedRoute";
|
|
7
|
-
|
|
8
|
-
/** @internal */
|
|
9
|
-
export const get_path_and_stringify =
|
|
10
|
-
(logger: () => (log: TypedRoute.IValidateErrorLog) => void) =>
|
|
11
|
-
(method: string) =>
|
|
12
|
-
(
|
|
13
|
-
...args: any[]
|
|
14
|
-
): [
|
|
15
|
-
string | string[] | undefined,
|
|
16
|
-
(input: any, _method: string, _path: string) => string,
|
|
17
|
-
] => {
|
|
18
|
-
const path: string | string[] | null | undefined =
|
|
19
|
-
args[0] === undefined ||
|
|
20
|
-
typeof args[0] === "string" ||
|
|
21
|
-
Array.isArray(args[0])
|
|
22
|
-
? args[0]
|
|
23
|
-
: null;
|
|
24
|
-
const functor: IResponseBodyStringifier<any> | undefined =
|
|
25
|
-
path === null ? args[0] : args[1];
|
|
26
|
-
return [path ?? undefined, take(logger)(method)(functor)];
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
/** @internal */
|
|
30
|
-
const take =
|
|
31
|
-
(logger: () => (log: TypedRoute.IValidateErrorLog) => void) =>
|
|
32
|
-
(method: string) =>
|
|
33
|
-
<T>(functor?: IResponseBodyStringifier<T> | null) => {
|
|
34
|
-
if (functor === undefined) {
|
|
35
|
-
NoTransformConfigurationError(method);
|
|
36
|
-
return (input: T, _method: string, _path: string) =>
|
|
37
|
-
JSON.stringify(input);
|
|
38
|
-
} else if (functor === null)
|
|
39
|
-
return (input: T, _method: string, _path: string) =>
|
|
40
|
-
JSON.stringify(input);
|
|
41
|
-
else if (functor.type === "stringify") return functor.stringify;
|
|
42
|
-
else if (functor.type === "assert") return assert(functor.assert);
|
|
43
|
-
else if (functor.type === "is") return is(functor.is);
|
|
44
|
-
else if (functor.type === "validate") return validate(functor.validate);
|
|
45
|
-
else if (functor.type === "validate.log")
|
|
46
|
-
return validateLog(logger)(functor.validate);
|
|
47
|
-
throw new Error(
|
|
48
|
-
`Error on nestia.core.${method}(): invalid typed stringify function.`,
|
|
49
|
-
);
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
/** @internal */
|
|
53
|
-
const assert =
|
|
54
|
-
<T>(closure: (data: T) => string) =>
|
|
55
|
-
(data: T): string => {
|
|
56
|
-
try {
|
|
57
|
-
return closure(data);
|
|
58
|
-
} catch (exp) {
|
|
59
|
-
if (typia.is<TypeGuardError>(exp))
|
|
60
|
-
throw new InternalServerErrorException({
|
|
61
|
-
path: exp.path,
|
|
62
|
-
reason: exp.message,
|
|
63
|
-
expected: exp.expected,
|
|
64
|
-
value: exp.value,
|
|
65
|
-
message: MESSAGE,
|
|
66
|
-
});
|
|
67
|
-
throw exp;
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
/** @internal */
|
|
72
|
-
const is =
|
|
73
|
-
<T>(closure: (data: T) => string | null) =>
|
|
74
|
-
(data: T, _method: string, _path: string) => {
|
|
75
|
-
const result: string | null = closure(data);
|
|
76
|
-
if (result === null) throw new InternalServerErrorException(MESSAGE);
|
|
77
|
-
return result;
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
/** @internal */
|
|
81
|
-
const validate =
|
|
82
|
-
<T>(closure: (data: T) => IValidation<string>) =>
|
|
83
|
-
(data: T, _method: string, _path: string): string => {
|
|
84
|
-
const result: IValidation<string> = closure(data);
|
|
85
|
-
if (result.success === false)
|
|
86
|
-
throw new InternalServerErrorException({
|
|
87
|
-
errors: result.errors,
|
|
88
|
-
message: MESSAGE,
|
|
89
|
-
});
|
|
90
|
-
return result.data;
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
const validateLog =
|
|
94
|
-
(logger: () => (log: TypedRoute.IValidateErrorLog) => void) =>
|
|
95
|
-
<T>(closure: (data: T) => IValidation<any>) =>
|
|
96
|
-
(data: T, method: string, path: string): string => {
|
|
97
|
-
const result: IValidation<string> = closure(data);
|
|
98
|
-
if (result.success === true) return result.data;
|
|
99
|
-
if (result.success === false)
|
|
100
|
-
logger()({
|
|
101
|
-
errors: result.errors,
|
|
102
|
-
method,
|
|
103
|
-
path,
|
|
104
|
-
data,
|
|
105
|
-
});
|
|
106
|
-
return JSON.stringify(data);
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
/** @internal */
|
|
110
|
-
const MESSAGE = "Response body data is not following the promised type.";
|
|
1
|
+
import { InternalServerErrorException } from "@nestjs/common";
|
|
2
|
+
import typia, { IValidation, TypeGuardError } from "typia";
|
|
3
|
+
|
|
4
|
+
import { IResponseBodyStringifier } from "../../options/IResponseBodyStringifier";
|
|
5
|
+
import { NoTransformConfigurationError } from "../NoTransformConfigurationError";
|
|
6
|
+
import { TypedRoute } from "../TypedRoute";
|
|
7
|
+
|
|
8
|
+
/** @internal */
|
|
9
|
+
export const get_path_and_stringify =
|
|
10
|
+
(logger: () => (log: TypedRoute.IValidateErrorLog) => void) =>
|
|
11
|
+
(method: string) =>
|
|
12
|
+
(
|
|
13
|
+
...args: any[]
|
|
14
|
+
): [
|
|
15
|
+
string | string[] | undefined,
|
|
16
|
+
(input: any, _method: string, _path: string) => string,
|
|
17
|
+
] => {
|
|
18
|
+
const path: string | string[] | null | undefined =
|
|
19
|
+
args[0] === undefined ||
|
|
20
|
+
typeof args[0] === "string" ||
|
|
21
|
+
Array.isArray(args[0])
|
|
22
|
+
? args[0]
|
|
23
|
+
: null;
|
|
24
|
+
const functor: IResponseBodyStringifier<any> | undefined =
|
|
25
|
+
path === null ? args[0] : args[1];
|
|
26
|
+
return [path ?? undefined, take(logger)(method)(functor)];
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** @internal */
|
|
30
|
+
const take =
|
|
31
|
+
(logger: () => (log: TypedRoute.IValidateErrorLog) => void) =>
|
|
32
|
+
(method: string) =>
|
|
33
|
+
<T>(functor?: IResponseBodyStringifier<T> | null) => {
|
|
34
|
+
if (functor === undefined) {
|
|
35
|
+
NoTransformConfigurationError(method);
|
|
36
|
+
return (input: T, _method: string, _path: string) =>
|
|
37
|
+
JSON.stringify(input);
|
|
38
|
+
} else if (functor === null)
|
|
39
|
+
return (input: T, _method: string, _path: string) =>
|
|
40
|
+
JSON.stringify(input);
|
|
41
|
+
else if (functor.type === "stringify") return functor.stringify;
|
|
42
|
+
else if (functor.type === "assert") return assert(functor.assert);
|
|
43
|
+
else if (functor.type === "is") return is(functor.is);
|
|
44
|
+
else if (functor.type === "validate") return validate(functor.validate);
|
|
45
|
+
else if (functor.type === "validate.log")
|
|
46
|
+
return validateLog(logger)(functor.validate);
|
|
47
|
+
throw new Error(
|
|
48
|
+
`Error on nestia.core.${method}(): invalid typed stringify function.`,
|
|
49
|
+
);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** @internal */
|
|
53
|
+
const assert =
|
|
54
|
+
<T>(closure: (data: T) => string) =>
|
|
55
|
+
(data: T): string => {
|
|
56
|
+
try {
|
|
57
|
+
return closure(data);
|
|
58
|
+
} catch (exp) {
|
|
59
|
+
if (typia.is<TypeGuardError>(exp))
|
|
60
|
+
throw new InternalServerErrorException({
|
|
61
|
+
path: exp.path,
|
|
62
|
+
reason: exp.message,
|
|
63
|
+
expected: exp.expected,
|
|
64
|
+
value: exp.value,
|
|
65
|
+
message: MESSAGE,
|
|
66
|
+
});
|
|
67
|
+
throw exp;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** @internal */
|
|
72
|
+
const is =
|
|
73
|
+
<T>(closure: (data: T) => string | null) =>
|
|
74
|
+
(data: T, _method: string, _path: string) => {
|
|
75
|
+
const result: string | null = closure(data);
|
|
76
|
+
if (result === null) throw new InternalServerErrorException(MESSAGE);
|
|
77
|
+
return result;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/** @internal */
|
|
81
|
+
const validate =
|
|
82
|
+
<T>(closure: (data: T) => IValidation<string>) =>
|
|
83
|
+
(data: T, _method: string, _path: string): string => {
|
|
84
|
+
const result: IValidation<string> = closure(data);
|
|
85
|
+
if (result.success === false)
|
|
86
|
+
throw new InternalServerErrorException({
|
|
87
|
+
errors: result.errors,
|
|
88
|
+
message: MESSAGE,
|
|
89
|
+
});
|
|
90
|
+
return result.data;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const validateLog =
|
|
94
|
+
(logger: () => (log: TypedRoute.IValidateErrorLog) => void) =>
|
|
95
|
+
<T>(closure: (data: T) => IValidation<any>) =>
|
|
96
|
+
(data: T, method: string, path: string): string => {
|
|
97
|
+
const result: IValidation<string> = closure(data);
|
|
98
|
+
if (result.success === true) return result.data;
|
|
99
|
+
if (result.success === false)
|
|
100
|
+
logger()({
|
|
101
|
+
errors: result.errors,
|
|
102
|
+
method,
|
|
103
|
+
path,
|
|
104
|
+
data,
|
|
105
|
+
});
|
|
106
|
+
return JSON.stringify(data);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/** @internal */
|
|
110
|
+
const MESSAGE = "Response body data is not following the promised type.";
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import type express from "express";
|
|
2
|
-
import type { FastifyRequest } from "fastify";
|
|
3
|
-
import raw from "raw-body";
|
|
4
|
-
|
|
5
|
-
/** @internal */
|
|
6
|
-
export const get_text_body = async (
|
|
7
|
-
request: express.Request | FastifyRequest,
|
|
8
|
-
): Promise<string> =>
|
|
9
|
-
isExpressRequest(request)
|
|
10
|
-
? (await raw(request)).toString("utf8")
|
|
11
|
-
: (request.body as string);
|
|
12
|
-
|
|
13
|
-
/** @internal */
|
|
14
|
-
const isExpressRequest = (
|
|
15
|
-
request: express.Request | FastifyRequest,
|
|
16
|
-
): request is express.Request => (request as express.Request).app !== undefined;
|
|
1
|
+
import type express from "express";
|
|
2
|
+
import type { FastifyRequest } from "fastify";
|
|
3
|
+
import raw from "raw-body";
|
|
4
|
+
|
|
5
|
+
/** @internal */
|
|
6
|
+
export const get_text_body = async (
|
|
7
|
+
request: express.Request | FastifyRequest,
|
|
8
|
+
): Promise<string> =>
|
|
9
|
+
isExpressRequest(request)
|
|
10
|
+
? (await raw(request)).toString("utf8")
|
|
11
|
+
: (request.body as string);
|
|
12
|
+
|
|
13
|
+
/** @internal */
|
|
14
|
+
const isExpressRequest = (
|
|
15
|
+
request: express.Request | FastifyRequest,
|
|
16
|
+
): request is express.Request => (request as express.Request).app !== undefined;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import http from "http";
|
|
2
|
-
|
|
3
|
-
/** @internal */
|
|
4
|
-
export function headers_to_object(
|
|
5
|
-
headers: http.IncomingHttpHeaders,
|
|
6
|
-
): Record<string, string> {
|
|
7
|
-
const output: Record<string, string> = {};
|
|
8
|
-
for (const [key, value] of Object.entries(headers))
|
|
9
|
-
output[key] = value instanceof Array ? value[0]! : value || "";
|
|
10
|
-
return output;
|
|
11
|
-
}
|
|
1
|
+
import http from "http";
|
|
2
|
+
|
|
3
|
+
/** @internal */
|
|
4
|
+
export function headers_to_object(
|
|
5
|
+
headers: http.IncomingHttpHeaders,
|
|
6
|
+
): Record<string, string> {
|
|
7
|
+
const output: Record<string, string> = {};
|
|
8
|
+
for (const [key, value] of Object.entries(headers))
|
|
9
|
+
output[key] = value instanceof Array ? value[0]! : value || "";
|
|
10
|
+
return output;
|
|
11
|
+
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import type express from "express";
|
|
2
|
-
import type { FastifyRequest } from "fastify";
|
|
3
|
-
|
|
4
|
-
/** @internal */
|
|
5
|
-
export const is_request_body_undefined = (
|
|
6
|
-
request: express.Request | FastifyRequest,
|
|
7
|
-
): boolean =>
|
|
8
|
-
request.headers["content-type"] === undefined &&
|
|
9
|
-
(request.body === undefined ||
|
|
10
|
-
(typeof request.body === "object" &&
|
|
11
|
-
request.body !== null &&
|
|
12
|
-
Object.keys(request.body).length === 0));
|
|
1
|
+
import type express from "express";
|
|
2
|
+
import type { FastifyRequest } from "fastify";
|
|
3
|
+
|
|
4
|
+
/** @internal */
|
|
5
|
+
export const is_request_body_undefined = (
|
|
6
|
+
request: express.Request | FastifyRequest,
|
|
7
|
+
): boolean =>
|
|
8
|
+
request.headers["content-type"] === undefined &&
|
|
9
|
+
(request.body === undefined ||
|
|
10
|
+
(typeof request.body === "object" &&
|
|
11
|
+
request.body !== null &&
|
|
12
|
+
Object.keys(request.body).length === 0));
|
|
@@ -1,76 +1,91 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
file.substring(file.length -
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
1
|
+
import { pathToFileURL } from "url";
|
|
2
|
+
|
|
3
|
+
import { Creator } from "../../typings/Creator";
|
|
4
|
+
import { SourceFinder } from "../../utils/SourceFinder";
|
|
5
|
+
|
|
6
|
+
export const load_controllers = async (
|
|
7
|
+
path: string | string[] | { include: string[]; exclude?: string[] },
|
|
8
|
+
isTsNode?: boolean,
|
|
9
|
+
): Promise<Creator<object>[]> => {
|
|
10
|
+
const include: string[] = Array.isArray(path)
|
|
11
|
+
? path
|
|
12
|
+
: typeof path === "object"
|
|
13
|
+
? path.include
|
|
14
|
+
: [path];
|
|
15
|
+
const exclude: string[] =
|
|
16
|
+
typeof path === "object" && !Array.isArray(path)
|
|
17
|
+
? (path.exclude ?? [])
|
|
18
|
+
: [];
|
|
19
|
+
const filter =
|
|
20
|
+
isTsNode === true
|
|
21
|
+
? (file: string) =>
|
|
22
|
+
file.substring(file.length - 3) === ".ts" &&
|
|
23
|
+
file.substring(file.length - 5) !== ".d.ts"
|
|
24
|
+
: (file: string) => file.substring(file.length - 3) === ".js";
|
|
25
|
+
const sources: string[] = await SourceFinder.find({
|
|
26
|
+
include,
|
|
27
|
+
exclude,
|
|
28
|
+
filter,
|
|
29
|
+
});
|
|
30
|
+
const controllers: Creator<object>[] = await mount(sources);
|
|
31
|
+
if (controllers.length !== 0 || isTsNode === true) return controllers;
|
|
32
|
+
|
|
33
|
+
// No compiled `.js` controllers were found. Under `ttsx`, the project runs
|
|
34
|
+
// straight from its TypeScript sources: `__dirname` still points at the
|
|
35
|
+
// source tree (the runtime hooks serve the emitted JS under the source
|
|
36
|
+
// URLs), so the controllers on disk are `.ts`, not `.js`. Detect that
|
|
37
|
+
// source-run context and retry with the TypeScript filter; `import()` of each
|
|
38
|
+
// `.ts` file is then served as the transformed emit by the hooks.
|
|
39
|
+
if (!isTsxRuntime()) return controllers;
|
|
40
|
+
|
|
41
|
+
const tsFilter = (file: string) =>
|
|
42
|
+
file.substring(file.length - 3) === ".ts" &&
|
|
43
|
+
file.substring(file.length - 5) !== ".d.ts";
|
|
44
|
+
const fallback: string[] = await SourceFinder.find({
|
|
45
|
+
include,
|
|
46
|
+
exclude,
|
|
47
|
+
filter: tsFilter,
|
|
48
|
+
});
|
|
49
|
+
return fallback.length === 0 ? controllers : mount(fallback);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** @internal */
|
|
53
|
+
async function mount(sources: string[]): Promise<any[]> {
|
|
54
|
+
const controllers: any[] = [];
|
|
55
|
+
for (const file of sources) {
|
|
56
|
+
const external: any = await dynamicImport(pathToFileURL(file).href);
|
|
57
|
+
for (const key in external) {
|
|
58
|
+
const instance: Creator<object> = external[key];
|
|
59
|
+
if (
|
|
60
|
+
instance === null ||
|
|
61
|
+
(typeof instance !== "function" && typeof instance !== "object")
|
|
62
|
+
)
|
|
63
|
+
continue;
|
|
64
|
+
if (Reflect.getMetadata("path", instance) !== undefined)
|
|
65
|
+
controllers.push(instance);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return controllers;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const dynamicImport: (specifier: string) => Promise<any> = Function(
|
|
72
|
+
"specifier",
|
|
73
|
+
"return import(specifier);",
|
|
74
|
+
) as (specifier: string) => Promise<any>;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Whether the process is running from TypeScript source under `ttsx`.
|
|
78
|
+
*
|
|
79
|
+
* `ttsx` runs a TypeScript entry from source: it builds the owning project to a
|
|
80
|
+
* temporary directory and installs runtime module hooks that serve that emit
|
|
81
|
+
* under the original source URLs, exporting the manifest path through
|
|
82
|
+
* `TTSX_RUNTIME_MANIFEST`. Its presence is the reliable signal that the
|
|
83
|
+
* controllers on disk are `.ts` (the `.js` glob will be empty) yet `import()`
|
|
84
|
+
* of those `.ts` files resolves to transformed JavaScript.
|
|
85
|
+
*/
|
|
86
|
+
function isTsxRuntime(): boolean {
|
|
87
|
+
return (
|
|
88
|
+
typeof process.env.TTSX_RUNTIME_MANIFEST === "string" &&
|
|
89
|
+
process.env.TTSX_RUNTIME_MANIFEST.length !== 0
|
|
90
|
+
);
|
|
91
|
+
}
|