@nestia/core 12.0.0-rc.2 → 12.0.0-rc.3
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 +138 -169
- package/lib/decorators/NoTransformConfigurationError.js +5 -5
- package/lib/decorators/NoTransformConfigurationError.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 +388 -380
- package/native/transform/cleanup.go +408 -408
- package/native/transform/contributor.go +97 -97
- package/native/transform/core_querify.go +231 -231
- package/native/transform/core_transform.go +1996 -1996
- package/native/transform/core_websocket.go +115 -115
- package/native/transform/exports.go +13 -13
- package/native/transform/mcp_transform.go +414 -414
- package/native/transform/node_transform.go +357 -357
- package/native/transform/path_rewrite.go +285 -285
- package/native/transform/printer.go +244 -244
- package/native/transform/rewrite.go +668 -668
- package/native/transform/run.go +73 -73
- package/native/transform/transform.go +336 -336
- package/native/transform/typia_fast.go +375 -352
- package/native/transform/typia_replacement.go +24 -24
- package/native/transform.cjs +43 -43
- package/package.json +9 -9
- package/src/adaptors/McpAdaptor.ts +276 -276
- 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 -154
- 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 -198
- 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 -40
- 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 +94 -94
- 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 -25
- 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 +26 -26
- 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/README.md +0 -93
|
@@ -1,94 +1,94 @@
|
|
|
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 = isTsNode === true ? isTypeScriptSource : isJavaScriptModule;
|
|
20
|
-
const sources: string[] = await SourceFinder.find({
|
|
21
|
-
include,
|
|
22
|
-
exclude,
|
|
23
|
-
filter,
|
|
24
|
-
});
|
|
25
|
-
const controllers: Creator<object>[] = await mount(sources);
|
|
26
|
-
if (controllers.length !== 0 || isTsNode === true) return controllers;
|
|
27
|
-
|
|
28
|
-
// No compiled `.js` controllers were found. Under `ttsx`, the project runs
|
|
29
|
-
// straight from its TypeScript sources: `__dirname` still points at the
|
|
30
|
-
// source tree (the runtime hooks serve the emitted JS under the source
|
|
31
|
-
// URLs), so the controllers on disk are `.ts`, not `.js`. Detect that
|
|
32
|
-
// source-run context and retry with the TypeScript filter; `import()` of each
|
|
33
|
-
// `.ts` file is then served as the transformed emit by the hooks.
|
|
34
|
-
if (!isTsxRuntime()) return controllers;
|
|
35
|
-
|
|
36
|
-
const fallback: string[] = await SourceFinder.find({
|
|
37
|
-
include,
|
|
38
|
-
exclude,
|
|
39
|
-
filter: isTypeScriptSource,
|
|
40
|
-
});
|
|
41
|
-
return fallback.length === 0 ? controllers : mount(fallback);
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
/** @internal */
|
|
45
|
-
async function mount(sources: string[]): Promise<any[]> {
|
|
46
|
-
const controllers: any[] = [];
|
|
47
|
-
for (const file of sources) {
|
|
48
|
-
const external: any = await dynamicImport(pathToFileURL(file).href);
|
|
49
|
-
for (const key in external) {
|
|
50
|
-
const instance: Creator<object> = external[key];
|
|
51
|
-
if (
|
|
52
|
-
instance === null ||
|
|
53
|
-
(typeof instance !== "function" && typeof instance !== "object")
|
|
54
|
-
)
|
|
55
|
-
continue;
|
|
56
|
-
if (Reflect.getMetadata("path", instance) !== undefined)
|
|
57
|
-
controllers.push(instance);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return controllers;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const dynamicImport: (specifier: string) => Promise<any> = Function(
|
|
64
|
-
"specifier",
|
|
65
|
-
"return import(specifier);",
|
|
66
|
-
) as (specifier: string) => Promise<any>;
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Whether the process is running from TypeScript source under `ttsx`.
|
|
70
|
-
*
|
|
71
|
-
* `ttsx` runs a TypeScript entry from source: it builds the owning project to a
|
|
72
|
-
* temporary directory and installs runtime module hooks that serve that emit
|
|
73
|
-
* under the original source URLs, exporting the manifest path through
|
|
74
|
-
* `TTSX_RUNTIME_MANIFEST`. Its presence is the reliable signal that the
|
|
75
|
-
* controllers on disk are `.ts` (the `.js` glob will be empty) yet `import()`
|
|
76
|
-
* of those `.ts` files resolves to transformed JavaScript.
|
|
77
|
-
*/
|
|
78
|
-
function isTsxRuntime(): boolean {
|
|
79
|
-
return (
|
|
80
|
-
typeof process.env.TTSX_RUNTIME_MANIFEST === "string" &&
|
|
81
|
-
process.env.TTSX_RUNTIME_MANIFEST.length !== 0
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const isJavaScriptModule = (file: string): boolean =>
|
|
86
|
-
/\.(?:[cm]?js)$/.test(file.toLowerCase());
|
|
87
|
-
|
|
88
|
-
const isTypeScriptSource = (file: string): boolean => {
|
|
89
|
-
const lower: string = file.toLowerCase();
|
|
90
|
-
return (
|
|
91
|
-
/\.(?:[cm]?ts)$/.test(lower) &&
|
|
92
|
-
/\.(?:d\.[cm]?ts|d\.ts)$/.test(lower) === false
|
|
93
|
-
);
|
|
94
|
-
};
|
|
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 = isTsNode === true ? isTypeScriptSource : isJavaScriptModule;
|
|
20
|
+
const sources: string[] = await SourceFinder.find({
|
|
21
|
+
include,
|
|
22
|
+
exclude,
|
|
23
|
+
filter,
|
|
24
|
+
});
|
|
25
|
+
const controllers: Creator<object>[] = await mount(sources);
|
|
26
|
+
if (controllers.length !== 0 || isTsNode === true) return controllers;
|
|
27
|
+
|
|
28
|
+
// No compiled `.js` controllers were found. Under `ttsx`, the project runs
|
|
29
|
+
// straight from its TypeScript sources: `__dirname` still points at the
|
|
30
|
+
// source tree (the runtime hooks serve the emitted JS under the source
|
|
31
|
+
// URLs), so the controllers on disk are `.ts`, not `.js`. Detect that
|
|
32
|
+
// source-run context and retry with the TypeScript filter; `import()` of each
|
|
33
|
+
// `.ts` file is then served as the transformed emit by the hooks.
|
|
34
|
+
if (!isTsxRuntime()) return controllers;
|
|
35
|
+
|
|
36
|
+
const fallback: string[] = await SourceFinder.find({
|
|
37
|
+
include,
|
|
38
|
+
exclude,
|
|
39
|
+
filter: isTypeScriptSource,
|
|
40
|
+
});
|
|
41
|
+
return fallback.length === 0 ? controllers : mount(fallback);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** @internal */
|
|
45
|
+
async function mount(sources: string[]): Promise<any[]> {
|
|
46
|
+
const controllers: any[] = [];
|
|
47
|
+
for (const file of sources) {
|
|
48
|
+
const external: any = await dynamicImport(pathToFileURL(file).href);
|
|
49
|
+
for (const key in external) {
|
|
50
|
+
const instance: Creator<object> = external[key];
|
|
51
|
+
if (
|
|
52
|
+
instance === null ||
|
|
53
|
+
(typeof instance !== "function" && typeof instance !== "object")
|
|
54
|
+
)
|
|
55
|
+
continue;
|
|
56
|
+
if (Reflect.getMetadata("path", instance) !== undefined)
|
|
57
|
+
controllers.push(instance);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return controllers;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const dynamicImport: (specifier: string) => Promise<any> = Function(
|
|
64
|
+
"specifier",
|
|
65
|
+
"return import(specifier);",
|
|
66
|
+
) as (specifier: string) => Promise<any>;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Whether the process is running from TypeScript source under `ttsx`.
|
|
70
|
+
*
|
|
71
|
+
* `ttsx` runs a TypeScript entry from source: it builds the owning project to a
|
|
72
|
+
* temporary directory and installs runtime module hooks that serve that emit
|
|
73
|
+
* under the original source URLs, exporting the manifest path through
|
|
74
|
+
* `TTSX_RUNTIME_MANIFEST`. Its presence is the reliable signal that the
|
|
75
|
+
* controllers on disk are `.ts` (the `.js` glob will be empty) yet `import()`
|
|
76
|
+
* of those `.ts` files resolves to transformed JavaScript.
|
|
77
|
+
*/
|
|
78
|
+
function isTsxRuntime(): boolean {
|
|
79
|
+
return (
|
|
80
|
+
typeof process.env.TTSX_RUNTIME_MANIFEST === "string" &&
|
|
81
|
+
process.env.TTSX_RUNTIME_MANIFEST.length !== 0
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const isJavaScriptModule = (file: string): boolean =>
|
|
86
|
+
/\.(?:[cm]?js)$/.test(file.toLowerCase());
|
|
87
|
+
|
|
88
|
+
const isTypeScriptSource = (file: string): boolean => {
|
|
89
|
+
const lower: string = file.toLowerCase();
|
|
90
|
+
return (
|
|
91
|
+
/\.(?:[cm]?ts)$/.test(lower) &&
|
|
92
|
+
/\.(?:d\.[cm]?ts|d\.ts)$/.test(lower) === false
|
|
93
|
+
);
|
|
94
|
+
};
|
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
import { HttpException } from "@nestjs/common";
|
|
2
|
-
import express from "express";
|
|
3
|
-
import type { FastifyRequest } from "fastify";
|
|
4
|
-
import { throwError } from "rxjs";
|
|
5
|
-
|
|
6
|
-
import { ExceptionManager } from "../../utils/ExceptionManager";
|
|
7
|
-
|
|
8
|
-
/** @internal */
|
|
9
|
-
export function route_error(
|
|
10
|
-
request: express.Request | FastifyRequest,
|
|
11
|
-
error: any,
|
|
12
|
-
) {
|
|
13
|
-
error = (() => {
|
|
14
|
-
// HTTP-ERROR
|
|
15
|
-
if (error instanceof HttpException) return error;
|
|
16
|
-
|
|
17
|
-
// CUSTOM-REGISTERED ERROR
|
|
18
|
-
for (const [creator, closure] of ExceptionManager.tuples)
|
|
19
|
-
if (error instanceof creator) return closure(error);
|
|
20
|
-
|
|
21
|
-
// MAYBE INTERNAL ERROR
|
|
22
|
-
return error;
|
|
23
|
-
})();
|
|
24
|
-
|
|
25
|
-
try {
|
|
26
|
-
error.method = request.method;
|
|
27
|
-
error.path =
|
|
28
|
-
(request as express.Request).path ??
|
|
29
|
-
(request as FastifyRequest).routeOptions?.url ??
|
|
30
|
-
(request as any).routerPath;
|
|
31
|
-
} catch {}
|
|
32
|
-
|
|
33
|
-
setTimeout(() => {
|
|
34
|
-
for (const listener of ExceptionManager.listeners) {
|
|
35
|
-
try {
|
|
36
|
-
const res: any | Promise<any> = listener(error);
|
|
37
|
-
if (typeof res === "object" && typeof res.catch === "function")
|
|
38
|
-
res.catch(() => {});
|
|
39
|
-
} catch {}
|
|
40
|
-
}
|
|
41
|
-
}, 0);
|
|
42
|
-
return throwError(() => error);
|
|
43
|
-
}
|
|
1
|
+
import { HttpException } from "@nestjs/common";
|
|
2
|
+
import express from "express";
|
|
3
|
+
import type { FastifyRequest } from "fastify";
|
|
4
|
+
import { throwError } from "rxjs";
|
|
5
|
+
|
|
6
|
+
import { ExceptionManager } from "../../utils/ExceptionManager";
|
|
7
|
+
|
|
8
|
+
/** @internal */
|
|
9
|
+
export function route_error(
|
|
10
|
+
request: express.Request | FastifyRequest,
|
|
11
|
+
error: any,
|
|
12
|
+
) {
|
|
13
|
+
error = (() => {
|
|
14
|
+
// HTTP-ERROR
|
|
15
|
+
if (error instanceof HttpException) return error;
|
|
16
|
+
|
|
17
|
+
// CUSTOM-REGISTERED ERROR
|
|
18
|
+
for (const [creator, closure] of ExceptionManager.tuples)
|
|
19
|
+
if (error instanceof creator) return closure(error);
|
|
20
|
+
|
|
21
|
+
// MAYBE INTERNAL ERROR
|
|
22
|
+
return error;
|
|
23
|
+
})();
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
error.method = request.method;
|
|
27
|
+
error.path =
|
|
28
|
+
(request as express.Request).path ??
|
|
29
|
+
(request as FastifyRequest).routeOptions?.url ??
|
|
30
|
+
(request as any).routerPath;
|
|
31
|
+
} catch {}
|
|
32
|
+
|
|
33
|
+
setTimeout(() => {
|
|
34
|
+
for (const listener of ExceptionManager.listeners) {
|
|
35
|
+
try {
|
|
36
|
+
const res: any | Promise<any> = listener(error);
|
|
37
|
+
if (typeof res === "object" && typeof res.catch === "function")
|
|
38
|
+
res.catch(() => {});
|
|
39
|
+
} catch {}
|
|
40
|
+
}
|
|
41
|
+
}, 0);
|
|
42
|
+
return throwError(() => error);
|
|
43
|
+
}
|
|
@@ -1,64 +1,64 @@
|
|
|
1
|
-
import { BadRequestException } from "@nestjs/common";
|
|
2
|
-
import typia, { IValidation, TypeGuardError } from "typia";
|
|
3
|
-
|
|
4
|
-
import { IRequestBodyValidator } from "../../options/IRequestBodyValidator";
|
|
5
|
-
import { NoTransformConfigurationError } from "../NoTransformConfigurationError";
|
|
6
|
-
|
|
7
|
-
/** @internal */
|
|
8
|
-
export const validate_request_body =
|
|
9
|
-
(method: string) =>
|
|
10
|
-
<T>(validator?: IRequestBodyValidator<T>): ((input: T) => Error | null) => {
|
|
11
|
-
if (!validator) {
|
|
12
|
-
NoTransformConfigurationError(method);
|
|
13
|
-
return () => null;
|
|
14
|
-
} else if (validator.type === "assert") return assert(validator.assert);
|
|
15
|
-
else if (validator.type === "is") return is(validator.is);
|
|
16
|
-
else if (validator.type === "validate") return validate(validator.validate);
|
|
17
|
-
return () =>
|
|
18
|
-
new Error(`Error on nestia.core.${method}(): invalid typed validator.`);
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
/** @internal */
|
|
22
|
-
const assert =
|
|
23
|
-
<T>(closure: (data: T) => T) =>
|
|
24
|
-
(input: T) => {
|
|
25
|
-
try {
|
|
26
|
-
closure(input);
|
|
27
|
-
return null;
|
|
28
|
-
} catch (exp) {
|
|
29
|
-
if (typia.is<TypeGuardError>(exp)) {
|
|
30
|
-
return new BadRequestException({
|
|
31
|
-
path: exp.path,
|
|
32
|
-
reason: exp.message,
|
|
33
|
-
expected: exp.expected,
|
|
34
|
-
value: exp.value,
|
|
35
|
-
message: MESSAGE,
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
throw exp;
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
/** @internal */
|
|
43
|
-
const is =
|
|
44
|
-
<T>(closure: (data: T) => boolean) =>
|
|
45
|
-
(input: T) => {
|
|
46
|
-
const success: boolean = closure(input);
|
|
47
|
-
return success ? null : new BadRequestException(MESSAGE);
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
/** @internal */
|
|
51
|
-
const validate =
|
|
52
|
-
<T>(closure: (data: T) => IValidation<T>) =>
|
|
53
|
-
(input: T) => {
|
|
54
|
-
const result: IValidation<T> = closure(input);
|
|
55
|
-
return result.success
|
|
56
|
-
? null
|
|
57
|
-
: new BadRequestException({
|
|
58
|
-
errors: result.errors,
|
|
59
|
-
message: MESSAGE,
|
|
60
|
-
});
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
/** @internal */
|
|
64
|
-
const MESSAGE = "Request body data is not following the promised type.";
|
|
1
|
+
import { BadRequestException } from "@nestjs/common";
|
|
2
|
+
import typia, { IValidation, TypeGuardError } from "typia";
|
|
3
|
+
|
|
4
|
+
import { IRequestBodyValidator } from "../../options/IRequestBodyValidator";
|
|
5
|
+
import { NoTransformConfigurationError } from "../NoTransformConfigurationError";
|
|
6
|
+
|
|
7
|
+
/** @internal */
|
|
8
|
+
export const validate_request_body =
|
|
9
|
+
(method: string) =>
|
|
10
|
+
<T>(validator?: IRequestBodyValidator<T>): ((input: T) => Error | null) => {
|
|
11
|
+
if (!validator) {
|
|
12
|
+
NoTransformConfigurationError(method);
|
|
13
|
+
return () => null;
|
|
14
|
+
} else if (validator.type === "assert") return assert(validator.assert);
|
|
15
|
+
else if (validator.type === "is") return is(validator.is);
|
|
16
|
+
else if (validator.type === "validate") return validate(validator.validate);
|
|
17
|
+
return () =>
|
|
18
|
+
new Error(`Error on nestia.core.${method}(): invalid typed validator.`);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** @internal */
|
|
22
|
+
const assert =
|
|
23
|
+
<T>(closure: (data: T) => T) =>
|
|
24
|
+
(input: T) => {
|
|
25
|
+
try {
|
|
26
|
+
closure(input);
|
|
27
|
+
return null;
|
|
28
|
+
} catch (exp) {
|
|
29
|
+
if (typia.is<TypeGuardError>(exp)) {
|
|
30
|
+
return new BadRequestException({
|
|
31
|
+
path: exp.path,
|
|
32
|
+
reason: exp.message,
|
|
33
|
+
expected: exp.expected,
|
|
34
|
+
value: exp.value,
|
|
35
|
+
message: MESSAGE,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
throw exp;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/** @internal */
|
|
43
|
+
const is =
|
|
44
|
+
<T>(closure: (data: T) => boolean) =>
|
|
45
|
+
(input: T) => {
|
|
46
|
+
const success: boolean = closure(input);
|
|
47
|
+
return success ? null : new BadRequestException(MESSAGE);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/** @internal */
|
|
51
|
+
const validate =
|
|
52
|
+
<T>(closure: (data: T) => IValidation<T>) =>
|
|
53
|
+
(input: T) => {
|
|
54
|
+
const result: IValidation<T> = closure(input);
|
|
55
|
+
return result.success
|
|
56
|
+
? null
|
|
57
|
+
: new BadRequestException({
|
|
58
|
+
errors: result.errors,
|
|
59
|
+
message: MESSAGE,
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/** @internal */
|
|
64
|
+
const MESSAGE = "Request body data is not following the promised type.";
|
|
@@ -1,67 +1,67 @@
|
|
|
1
|
-
import { BadRequestException } from "@nestjs/common";
|
|
2
|
-
import typia, { IValidation, TypeGuardError } from "typia";
|
|
3
|
-
|
|
4
|
-
import { IRequestFormDataProps } from "../../options/IRequestFormDataProps";
|
|
5
|
-
import { NoTransformConfigurationError } from "../NoTransformConfigurationError";
|
|
6
|
-
|
|
7
|
-
/** @internal */
|
|
8
|
-
export const validate_request_form_data = <T>(
|
|
9
|
-
props?: IRequestFormDataProps<T>,
|
|
10
|
-
): ((value: FormData) => T | Error) => {
|
|
11
|
-
if (!props) {
|
|
12
|
-
NoTransformConfigurationError("TypedFormData.Body");
|
|
13
|
-
return (input: FormData) => Object.entries(input) as T;
|
|
14
|
-
} else if (props.validator.type === "assert")
|
|
15
|
-
return assert(props.validator.assert);
|
|
16
|
-
else if (props.validator.type === "is") return is(props.validator.is);
|
|
17
|
-
else if (props.validator.type === "validate")
|
|
18
|
-
return validate(props.validator.validate);
|
|
19
|
-
return () =>
|
|
20
|
-
new Error(
|
|
21
|
-
`Error on nestia.core.TypedFormData.Body(): invalid typed validator.`,
|
|
22
|
-
);
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
/** @internal */
|
|
26
|
-
const assert =
|
|
27
|
-
<T>(closure: (input: FormData) => T) =>
|
|
28
|
-
(input: FormData): T | BadRequestException => {
|
|
29
|
-
try {
|
|
30
|
-
return closure(input);
|
|
31
|
-
} catch (exp) {
|
|
32
|
-
if (typia.is<TypeGuardError>(exp)) {
|
|
33
|
-
return new BadRequestException({
|
|
34
|
-
path: exp.path,
|
|
35
|
-
reason: exp.message,
|
|
36
|
-
expected: exp.expected,
|
|
37
|
-
value: exp.value,
|
|
38
|
-
message: MESSAGE,
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
throw exp;
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
/** @internal */
|
|
46
|
-
const is =
|
|
47
|
-
<T>(closure: (input: FormData) => T | null) =>
|
|
48
|
-
(input: FormData): T | BadRequestException => {
|
|
49
|
-
const result: T | null = closure(input);
|
|
50
|
-
return result !== null ? result : new BadRequestException(MESSAGE);
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
/** @internal */
|
|
54
|
-
const validate =
|
|
55
|
-
<T>(closure: (input: FormData) => IValidation<T>) =>
|
|
56
|
-
(input: FormData): T | BadRequestException => {
|
|
57
|
-
const result: IValidation<T> = closure(input);
|
|
58
|
-
return result.success
|
|
59
|
-
? result.data
|
|
60
|
-
: new BadRequestException({
|
|
61
|
-
errors: result.errors,
|
|
62
|
-
message: MESSAGE,
|
|
63
|
-
});
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
/** @internal */
|
|
67
|
-
const MESSAGE = "Request multipart data is not following the promised type.";
|
|
1
|
+
import { BadRequestException } from "@nestjs/common";
|
|
2
|
+
import typia, { IValidation, TypeGuardError } from "typia";
|
|
3
|
+
|
|
4
|
+
import { IRequestFormDataProps } from "../../options/IRequestFormDataProps";
|
|
5
|
+
import { NoTransformConfigurationError } from "../NoTransformConfigurationError";
|
|
6
|
+
|
|
7
|
+
/** @internal */
|
|
8
|
+
export const validate_request_form_data = <T>(
|
|
9
|
+
props?: IRequestFormDataProps<T>,
|
|
10
|
+
): ((value: FormData) => T | Error) => {
|
|
11
|
+
if (!props) {
|
|
12
|
+
NoTransformConfigurationError("TypedFormData.Body");
|
|
13
|
+
return (input: FormData) => Object.entries(input) as T;
|
|
14
|
+
} else if (props.validator.type === "assert")
|
|
15
|
+
return assert(props.validator.assert);
|
|
16
|
+
else if (props.validator.type === "is") return is(props.validator.is);
|
|
17
|
+
else if (props.validator.type === "validate")
|
|
18
|
+
return validate(props.validator.validate);
|
|
19
|
+
return () =>
|
|
20
|
+
new Error(
|
|
21
|
+
`Error on nestia.core.TypedFormData.Body(): invalid typed validator.`,
|
|
22
|
+
);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/** @internal */
|
|
26
|
+
const assert =
|
|
27
|
+
<T>(closure: (input: FormData) => T) =>
|
|
28
|
+
(input: FormData): T | BadRequestException => {
|
|
29
|
+
try {
|
|
30
|
+
return closure(input);
|
|
31
|
+
} catch (exp) {
|
|
32
|
+
if (typia.is<TypeGuardError>(exp)) {
|
|
33
|
+
return new BadRequestException({
|
|
34
|
+
path: exp.path,
|
|
35
|
+
reason: exp.message,
|
|
36
|
+
expected: exp.expected,
|
|
37
|
+
value: exp.value,
|
|
38
|
+
message: MESSAGE,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
throw exp;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** @internal */
|
|
46
|
+
const is =
|
|
47
|
+
<T>(closure: (input: FormData) => T | null) =>
|
|
48
|
+
(input: FormData): T | BadRequestException => {
|
|
49
|
+
const result: T | null = closure(input);
|
|
50
|
+
return result !== null ? result : new BadRequestException(MESSAGE);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/** @internal */
|
|
54
|
+
const validate =
|
|
55
|
+
<T>(closure: (input: FormData) => IValidation<T>) =>
|
|
56
|
+
(input: FormData): T | BadRequestException => {
|
|
57
|
+
const result: IValidation<T> = closure(input);
|
|
58
|
+
return result.success
|
|
59
|
+
? result.data
|
|
60
|
+
: new BadRequestException({
|
|
61
|
+
errors: result.errors,
|
|
62
|
+
message: MESSAGE,
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/** @internal */
|
|
67
|
+
const MESSAGE = "Request multipart data is not following the promised type.";
|