@nestia/core 2.5.8 → 2.5.9
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/README.md +9 -6
- package/lib/decorators/SwaggerCustomizer.d.ts +64 -0
- package/lib/decorators/SwaggerCustomizer.js +25 -0
- package/lib/decorators/SwaggerCustomizer.js.map +1 -0
- package/lib/module.d.ts +1 -0
- package/lib/module.js +1 -0
- package/lib/module.js.map +1 -1
- package/lib/structures/ISwagger.d.ts +72 -0
- package/lib/structures/ISwagger.js +3 -0
- package/lib/structures/ISwagger.js.map +1 -0
- package/lib/structures/ISwaggerComponents.d.ts +26 -0
- package/lib/structures/ISwaggerComponents.js +3 -0
- package/lib/structures/ISwaggerComponents.js.map +1 -0
- package/lib/structures/ISwaggerInfo.d.ts +71 -0
- package/lib/structures/ISwaggerInfo.js +3 -0
- package/lib/structures/ISwaggerInfo.js.map +1 -0
- package/lib/structures/ISwaggerRoute.d.ts +46 -0
- package/lib/structures/ISwaggerRoute.js +3 -0
- package/lib/structures/ISwaggerRoute.js.map +1 -0
- package/lib/structures/ISwaggerSecurityScheme.d.ts +56 -0
- package/lib/structures/ISwaggerSecurityScheme.js +3 -0
- package/lib/structures/ISwaggerSecurityScheme.js.map +1 -0
- package/package.json +3 -3
- package/src/decorators/EncryptedBody.ts +105 -105
- package/src/decorators/EncryptedModule.ts +96 -96
- package/src/decorators/PlainBody.ts +75 -75
- package/src/decorators/SwaggerCustomizer.ts +87 -0
- package/src/decorators/TypedBody.ts +62 -62
- package/src/decorators/TypedException.ts +90 -90
- package/src/decorators/TypedRoute.ts +144 -144
- package/src/decorators/internal/get_path_and_querify.ts +106 -106
- package/src/decorators/internal/load_controller.ts +51 -51
- package/src/decorators/internal/validate_request_body.ts +72 -72
- package/src/decorators/internal/validate_request_headers.ts +83 -83
- package/src/decorators/internal/validate_request_query.ts +71 -71
- package/src/module.ts +1 -0
- package/src/structures/ISwagger.ts +91 -0
- package/src/structures/ISwaggerComponents.ts +29 -0
- package/src/structures/ISwaggerInfo.ts +80 -0
- package/src/structures/ISwaggerRoute.ts +50 -0
- package/src/structures/ISwaggerSecurityScheme.ts +65 -0
- package/src/transformers/NodeTransformer.ts +16 -16
- package/src/transformers/TypedExceptionTransformer.ts +48 -48
- package/src/transformers/TypedRouteTransformer.ts +88 -88
- package/src/utils/Singleton.ts +20 -20
|
@@ -1,106 +1,106 @@
|
|
|
1
|
-
import { InternalServerErrorException } from "@nestjs/common";
|
|
2
|
-
import typia, { IValidation, TypeGuardError } from "typia";
|
|
3
|
-
|
|
4
|
-
import { IResponseBodyQuerifier } from "../../options/IResponseBodyQuerifier";
|
|
5
|
-
import { NoTransformConfigureError } from "./NoTransformConfigureError";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @internal
|
|
9
|
-
*/
|
|
10
|
-
export const get_path_and_querify =
|
|
11
|
-
(method: string) =>
|
|
12
|
-
(
|
|
13
|
-
...args: any[]
|
|
14
|
-
): [string | string[] | undefined, (input: any) => URLSearchParams] => {
|
|
15
|
-
const path: string | string[] | null | undefined =
|
|
16
|
-
args[0] === undefined ||
|
|
17
|
-
typeof args[0] === "string" ||
|
|
18
|
-
Array.isArray(args[0])
|
|
19
|
-
? args[0]
|
|
20
|
-
: null;
|
|
21
|
-
const functor: IResponseBodyQuerifier<any> | undefined =
|
|
22
|
-
path === null ? args[0] : args[1];
|
|
23
|
-
return [path ?? undefined, take(method)(functor)];
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* @internal
|
|
28
|
-
*/
|
|
29
|
-
const take =
|
|
30
|
-
(method: string) =>
|
|
31
|
-
<T>(functor?: IResponseBodyQuerifier<T> | null) => {
|
|
32
|
-
if (functor === undefined) throw NoTransformConfigureError(method);
|
|
33
|
-
else if (functor === null) return querify;
|
|
34
|
-
else if (functor.type === "stringify") return functor.stringify;
|
|
35
|
-
else if (functor.type === "assert") return assert(functor.assert);
|
|
36
|
-
else if (functor.type === "is") return is(functor.is);
|
|
37
|
-
else if (functor.type === "validate") return validate(functor.validate);
|
|
38
|
-
throw new Error(
|
|
39
|
-
`Error on nestia.core.${method}(): invalid typed stringify function.`,
|
|
40
|
-
);
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* @internal
|
|
45
|
-
*/
|
|
46
|
-
const querify = (input: Record<string, any>): URLSearchParams => {
|
|
47
|
-
const output: URLSearchParams = new URLSearchParams();
|
|
48
|
-
for (const [key, value] of Object.entries(input))
|
|
49
|
-
if (key === undefined) continue;
|
|
50
|
-
else if (Array.isArray(value))
|
|
51
|
-
for (const elem of value) output.append(key, String(elem));
|
|
52
|
-
else output.append(key, String(value));
|
|
53
|
-
return output;
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* @internal
|
|
58
|
-
*/
|
|
59
|
-
const assert =
|
|
60
|
-
<T>(closure: (data: T) => URLSearchParams) =>
|
|
61
|
-
(data: T) => {
|
|
62
|
-
try {
|
|
63
|
-
return closure(data);
|
|
64
|
-
} catch (exp) {
|
|
65
|
-
if (typia.is<TypeGuardError>(exp))
|
|
66
|
-
throw new InternalServerErrorException({
|
|
67
|
-
path: exp.path,
|
|
68
|
-
reason: exp.message,
|
|
69
|
-
expected: exp.expected,
|
|
70
|
-
value: exp.value,
|
|
71
|
-
message: MESSAGE,
|
|
72
|
-
});
|
|
73
|
-
throw exp;
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* @internal
|
|
79
|
-
*/
|
|
80
|
-
const is =
|
|
81
|
-
<T>(closure: (data: T) => URLSearchParams | null) =>
|
|
82
|
-
(data: T) => {
|
|
83
|
-
const result: URLSearchParams | null = closure(data);
|
|
84
|
-
if (result === null) throw new InternalServerErrorException(MESSAGE);
|
|
85
|
-
return result;
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* @internal
|
|
90
|
-
*/
|
|
91
|
-
const validate =
|
|
92
|
-
<T>(closure: (data: T) => IValidation<URLSearchParams>) =>
|
|
93
|
-
(data: T) => {
|
|
94
|
-
const result: IValidation<URLSearchParams> = closure(data);
|
|
95
|
-
if (result.success === false)
|
|
96
|
-
throw new InternalServerErrorException({
|
|
97
|
-
errors: result.errors,
|
|
98
|
-
message: MESSAGE,
|
|
99
|
-
});
|
|
100
|
-
return result.data;
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* @internal
|
|
105
|
-
*/
|
|
106
|
-
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 { NoTransformConfigureError } from "./NoTransformConfigureError";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export const get_path_and_querify =
|
|
11
|
+
(method: string) =>
|
|
12
|
+
(
|
|
13
|
+
...args: any[]
|
|
14
|
+
): [string | string[] | undefined, (input: any) => URLSearchParams] => {
|
|
15
|
+
const path: string | string[] | null | undefined =
|
|
16
|
+
args[0] === undefined ||
|
|
17
|
+
typeof args[0] === "string" ||
|
|
18
|
+
Array.isArray(args[0])
|
|
19
|
+
? args[0]
|
|
20
|
+
: null;
|
|
21
|
+
const functor: IResponseBodyQuerifier<any> | undefined =
|
|
22
|
+
path === null ? args[0] : args[1];
|
|
23
|
+
return [path ?? undefined, take(method)(functor)];
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
const take =
|
|
30
|
+
(method: string) =>
|
|
31
|
+
<T>(functor?: IResponseBodyQuerifier<T> | null) => {
|
|
32
|
+
if (functor === undefined) throw NoTransformConfigureError(method);
|
|
33
|
+
else if (functor === null) return querify;
|
|
34
|
+
else if (functor.type === "stringify") return functor.stringify;
|
|
35
|
+
else if (functor.type === "assert") return assert(functor.assert);
|
|
36
|
+
else if (functor.type === "is") return is(functor.is);
|
|
37
|
+
else if (functor.type === "validate") return validate(functor.validate);
|
|
38
|
+
throw new Error(
|
|
39
|
+
`Error on nestia.core.${method}(): invalid typed stringify function.`,
|
|
40
|
+
);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @internal
|
|
45
|
+
*/
|
|
46
|
+
const querify = (input: Record<string, any>): URLSearchParams => {
|
|
47
|
+
const output: URLSearchParams = new URLSearchParams();
|
|
48
|
+
for (const [key, value] of Object.entries(input))
|
|
49
|
+
if (key === undefined) continue;
|
|
50
|
+
else if (Array.isArray(value))
|
|
51
|
+
for (const elem of value) output.append(key, String(elem));
|
|
52
|
+
else output.append(key, String(value));
|
|
53
|
+
return output;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @internal
|
|
58
|
+
*/
|
|
59
|
+
const assert =
|
|
60
|
+
<T>(closure: (data: T) => URLSearchParams) =>
|
|
61
|
+
(data: T) => {
|
|
62
|
+
try {
|
|
63
|
+
return closure(data);
|
|
64
|
+
} catch (exp) {
|
|
65
|
+
if (typia.is<TypeGuardError>(exp))
|
|
66
|
+
throw new InternalServerErrorException({
|
|
67
|
+
path: exp.path,
|
|
68
|
+
reason: exp.message,
|
|
69
|
+
expected: exp.expected,
|
|
70
|
+
value: exp.value,
|
|
71
|
+
message: MESSAGE,
|
|
72
|
+
});
|
|
73
|
+
throw exp;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @internal
|
|
79
|
+
*/
|
|
80
|
+
const is =
|
|
81
|
+
<T>(closure: (data: T) => URLSearchParams | null) =>
|
|
82
|
+
(data: T) => {
|
|
83
|
+
const result: URLSearchParams | null = closure(data);
|
|
84
|
+
if (result === null) throw new InternalServerErrorException(MESSAGE);
|
|
85
|
+
return result;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* @internal
|
|
90
|
+
*/
|
|
91
|
+
const validate =
|
|
92
|
+
<T>(closure: (data: T) => IValidation<URLSearchParams>) =>
|
|
93
|
+
(data: T) => {
|
|
94
|
+
const result: IValidation<URLSearchParams> = closure(data);
|
|
95
|
+
if (result.success === false)
|
|
96
|
+
throw new InternalServerErrorException({
|
|
97
|
+
errors: result.errors,
|
|
98
|
+
message: MESSAGE,
|
|
99
|
+
});
|
|
100
|
+
return result.data;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @internal
|
|
105
|
+
*/
|
|
106
|
+
const MESSAGE = "Response body data is not following the promised type.";
|
|
@@ -1,51 +1,51 @@
|
|
|
1
|
-
import is_ts_node from "detect-ts-node";
|
|
2
|
-
|
|
3
|
-
import { Creator } from "../../typings/Creator";
|
|
4
|
-
import { SourceFinder } from "../../utils/SourceFinder";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @internal
|
|
8
|
-
*/
|
|
9
|
-
export const load_controllers = async (
|
|
10
|
-
path: string | string[] | { include: string[]; exclude?: string[] },
|
|
11
|
-
): Promise<Creator<object>[]> => {
|
|
12
|
-
const sources: string[] = await SourceFinder.find({
|
|
13
|
-
include: Array.isArray(path)
|
|
14
|
-
? path
|
|
15
|
-
: typeof path === "object"
|
|
16
|
-
? path.include
|
|
17
|
-
: [path],
|
|
18
|
-
exclude:
|
|
19
|
-
typeof path === "object" && !Array.isArray(path)
|
|
20
|
-
? path.exclude ?? []
|
|
21
|
-
: [],
|
|
22
|
-
filter:
|
|
23
|
-
EXTENSION === "ts"
|
|
24
|
-
? (file) =>
|
|
25
|
-
file.substring(file.length - 3) === ".ts" &&
|
|
26
|
-
file.substring(file.length - 5) !== ".d.ts"
|
|
27
|
-
: (flle) => flle.substring(flle.length - 3) === ".js",
|
|
28
|
-
});
|
|
29
|
-
return mount(sources);
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* @internal
|
|
34
|
-
*/
|
|
35
|
-
async function mount(sources: string[]): Promise<any[]> {
|
|
36
|
-
const controllers: any[] = [];
|
|
37
|
-
for (const file of sources) {
|
|
38
|
-
const external: any = await import(file);
|
|
39
|
-
for (const key in external) {
|
|
40
|
-
const instance: Creator<object> = external[key];
|
|
41
|
-
if (Reflect.getMetadata("path", instance) !== undefined)
|
|
42
|
-
controllers.push(instance);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return controllers;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* @internal
|
|
50
|
-
*/
|
|
51
|
-
const EXTENSION = is_ts_node ? "ts" : "js";
|
|
1
|
+
import is_ts_node from "detect-ts-node";
|
|
2
|
+
|
|
3
|
+
import { Creator } from "../../typings/Creator";
|
|
4
|
+
import { SourceFinder } from "../../utils/SourceFinder";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export const load_controllers = async (
|
|
10
|
+
path: string | string[] | { include: string[]; exclude?: string[] },
|
|
11
|
+
): Promise<Creator<object>[]> => {
|
|
12
|
+
const sources: string[] = await SourceFinder.find({
|
|
13
|
+
include: Array.isArray(path)
|
|
14
|
+
? path
|
|
15
|
+
: typeof path === "object"
|
|
16
|
+
? path.include
|
|
17
|
+
: [path],
|
|
18
|
+
exclude:
|
|
19
|
+
typeof path === "object" && !Array.isArray(path)
|
|
20
|
+
? path.exclude ?? []
|
|
21
|
+
: [],
|
|
22
|
+
filter:
|
|
23
|
+
EXTENSION === "ts"
|
|
24
|
+
? (file) =>
|
|
25
|
+
file.substring(file.length - 3) === ".ts" &&
|
|
26
|
+
file.substring(file.length - 5) !== ".d.ts"
|
|
27
|
+
: (flle) => flle.substring(flle.length - 3) === ".js",
|
|
28
|
+
});
|
|
29
|
+
return mount(sources);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @internal
|
|
34
|
+
*/
|
|
35
|
+
async function mount(sources: string[]): Promise<any[]> {
|
|
36
|
+
const controllers: any[] = [];
|
|
37
|
+
for (const file of sources) {
|
|
38
|
+
const external: any = await import(file);
|
|
39
|
+
for (const key in external) {
|
|
40
|
+
const instance: Creator<object> = external[key];
|
|
41
|
+
if (Reflect.getMetadata("path", instance) !== undefined)
|
|
42
|
+
controllers.push(instance);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return controllers;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @internal
|
|
50
|
+
*/
|
|
51
|
+
const EXTENSION = is_ts_node ? "ts" : "js";
|
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
import { BadRequestException } from "@nestjs/common";
|
|
2
|
-
import typia, { IValidation, TypeGuardError } from "typia";
|
|
3
|
-
|
|
4
|
-
import { IRequestBodyValidator } from "../../options/IRequestBodyValidator";
|
|
5
|
-
import { NoTransformConfigureError } from "./NoTransformConfigureError";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @internal
|
|
9
|
-
*/
|
|
10
|
-
export const validate_request_body =
|
|
11
|
-
(method: string) =>
|
|
12
|
-
<T>(validator?: IRequestBodyValidator<T>) => {
|
|
13
|
-
if (!validator) return () => NoTransformConfigureError(method);
|
|
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
|
-
/**
|
|
22
|
-
* @internal
|
|
23
|
-
*/
|
|
24
|
-
const assert =
|
|
25
|
-
<T>(closure: (data: T) => T) =>
|
|
26
|
-
(input: T) => {
|
|
27
|
-
try {
|
|
28
|
-
closure(input);
|
|
29
|
-
return null;
|
|
30
|
-
} catch (exp) {
|
|
31
|
-
if (typia.is<TypeGuardError>(exp)) {
|
|
32
|
-
return new BadRequestException({
|
|
33
|
-
path: exp.path,
|
|
34
|
-
reason: exp.message,
|
|
35
|
-
expected: exp.expected,
|
|
36
|
-
value: exp.value,
|
|
37
|
-
message: MESSAGE,
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
throw exp;
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* @internal
|
|
46
|
-
*/
|
|
47
|
-
const is =
|
|
48
|
-
<T>(closure: (data: T) => boolean) =>
|
|
49
|
-
(input: T) => {
|
|
50
|
-
const success: boolean = closure(input);
|
|
51
|
-
return success ? null : new BadRequestException(MESSAGE);
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* @internal
|
|
56
|
-
*/
|
|
57
|
-
const validate =
|
|
58
|
-
<T>(closure: (data: T) => IValidation<T>) =>
|
|
59
|
-
(input: T) => {
|
|
60
|
-
const result: IValidation<T> = closure(input);
|
|
61
|
-
return result.success
|
|
62
|
-
? null
|
|
63
|
-
: new BadRequestException({
|
|
64
|
-
errors: result.errors,
|
|
65
|
-
message: MESSAGE,
|
|
66
|
-
});
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* @internal
|
|
71
|
-
*/
|
|
72
|
-
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 { NoTransformConfigureError } from "./NoTransformConfigureError";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export const validate_request_body =
|
|
11
|
+
(method: string) =>
|
|
12
|
+
<T>(validator?: IRequestBodyValidator<T>) => {
|
|
13
|
+
if (!validator) return () => NoTransformConfigureError(method);
|
|
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
|
+
/**
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
const assert =
|
|
25
|
+
<T>(closure: (data: T) => T) =>
|
|
26
|
+
(input: T) => {
|
|
27
|
+
try {
|
|
28
|
+
closure(input);
|
|
29
|
+
return null;
|
|
30
|
+
} catch (exp) {
|
|
31
|
+
if (typia.is<TypeGuardError>(exp)) {
|
|
32
|
+
return new BadRequestException({
|
|
33
|
+
path: exp.path,
|
|
34
|
+
reason: exp.message,
|
|
35
|
+
expected: exp.expected,
|
|
36
|
+
value: exp.value,
|
|
37
|
+
message: MESSAGE,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
throw exp;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @internal
|
|
46
|
+
*/
|
|
47
|
+
const is =
|
|
48
|
+
<T>(closure: (data: T) => boolean) =>
|
|
49
|
+
(input: T) => {
|
|
50
|
+
const success: boolean = closure(input);
|
|
51
|
+
return success ? null : new BadRequestException(MESSAGE);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @internal
|
|
56
|
+
*/
|
|
57
|
+
const validate =
|
|
58
|
+
<T>(closure: (data: T) => IValidation<T>) =>
|
|
59
|
+
(input: T) => {
|
|
60
|
+
const result: IValidation<T> = closure(input);
|
|
61
|
+
return result.success
|
|
62
|
+
? null
|
|
63
|
+
: new BadRequestException({
|
|
64
|
+
errors: result.errors,
|
|
65
|
+
message: MESSAGE,
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @internal
|
|
71
|
+
*/
|
|
72
|
+
const MESSAGE = "Request body data is not following the promised type.";
|
|
@@ -1,83 +1,83 @@
|
|
|
1
|
-
import { BadRequestException } from "@nestjs/common";
|
|
2
|
-
import typia, { IValidation, TypeGuardError } from "typia";
|
|
3
|
-
|
|
4
|
-
import { IRequestHeadersValidator } from "../../options/IRequestHeadersValidator";
|
|
5
|
-
import { NoTransformConfigureError } from "./NoTransformConfigureError";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @internal
|
|
9
|
-
*/
|
|
10
|
-
export const validate_request_headers = <T>(
|
|
11
|
-
validator?: IRequestHeadersValidator<T>,
|
|
12
|
-
) => {
|
|
13
|
-
if (!validator) return () => NoTransformConfigureError("TypedHeaders");
|
|
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.TypedHeaders(): invalid typed validator.`);
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @internal
|
|
23
|
-
*/
|
|
24
|
-
const assert =
|
|
25
|
-
<T>(closure: (input: Record<string, string | string[] | undefined>) => T) =>
|
|
26
|
-
(
|
|
27
|
-
input: Record<string, string | string[] | undefined>,
|
|
28
|
-
): 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
|
-
/**
|
|
46
|
-
* @internal
|
|
47
|
-
*/
|
|
48
|
-
const is =
|
|
49
|
-
<T>(
|
|
50
|
-
closure: (input: Record<string, string | string[] | undefined>) => T | null,
|
|
51
|
-
) =>
|
|
52
|
-
(
|
|
53
|
-
input: Record<string, string | string[] | undefined>,
|
|
54
|
-
): T | BadRequestException => {
|
|
55
|
-
const result: T | null = closure(input);
|
|
56
|
-
return result !== null ? result : new BadRequestException(MESSAGE);
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* @internal
|
|
61
|
-
*/
|
|
62
|
-
const validate =
|
|
63
|
-
<T>(
|
|
64
|
-
closure: (
|
|
65
|
-
input: Record<string, string | string[] | undefined>,
|
|
66
|
-
) => IValidation<T>,
|
|
67
|
-
) =>
|
|
68
|
-
(
|
|
69
|
-
input: Record<string, string | string[] | undefined>,
|
|
70
|
-
): T | BadRequestException => {
|
|
71
|
-
const result: IValidation<T> = closure(input);
|
|
72
|
-
return result.success
|
|
73
|
-
? result.data
|
|
74
|
-
: new BadRequestException({
|
|
75
|
-
errors: result.errors,
|
|
76
|
-
message: MESSAGE,
|
|
77
|
-
});
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* @internal
|
|
82
|
-
*/
|
|
83
|
-
const MESSAGE = "Request headers data is not following the promised type.";
|
|
1
|
+
import { BadRequestException } from "@nestjs/common";
|
|
2
|
+
import typia, { IValidation, TypeGuardError } from "typia";
|
|
3
|
+
|
|
4
|
+
import { IRequestHeadersValidator } from "../../options/IRequestHeadersValidator";
|
|
5
|
+
import { NoTransformConfigureError } from "./NoTransformConfigureError";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export const validate_request_headers = <T>(
|
|
11
|
+
validator?: IRequestHeadersValidator<T>,
|
|
12
|
+
) => {
|
|
13
|
+
if (!validator) return () => NoTransformConfigureError("TypedHeaders");
|
|
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.TypedHeaders(): invalid typed validator.`);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
const assert =
|
|
25
|
+
<T>(closure: (input: Record<string, string | string[] | undefined>) => T) =>
|
|
26
|
+
(
|
|
27
|
+
input: Record<string, string | string[] | undefined>,
|
|
28
|
+
): 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
|
+
/**
|
|
46
|
+
* @internal
|
|
47
|
+
*/
|
|
48
|
+
const is =
|
|
49
|
+
<T>(
|
|
50
|
+
closure: (input: Record<string, string | string[] | undefined>) => T | null,
|
|
51
|
+
) =>
|
|
52
|
+
(
|
|
53
|
+
input: Record<string, string | string[] | undefined>,
|
|
54
|
+
): T | BadRequestException => {
|
|
55
|
+
const result: T | null = closure(input);
|
|
56
|
+
return result !== null ? result : new BadRequestException(MESSAGE);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @internal
|
|
61
|
+
*/
|
|
62
|
+
const validate =
|
|
63
|
+
<T>(
|
|
64
|
+
closure: (
|
|
65
|
+
input: Record<string, string | string[] | undefined>,
|
|
66
|
+
) => IValidation<T>,
|
|
67
|
+
) =>
|
|
68
|
+
(
|
|
69
|
+
input: Record<string, string | string[] | undefined>,
|
|
70
|
+
): T | BadRequestException => {
|
|
71
|
+
const result: IValidation<T> = closure(input);
|
|
72
|
+
return result.success
|
|
73
|
+
? result.data
|
|
74
|
+
: new BadRequestException({
|
|
75
|
+
errors: result.errors,
|
|
76
|
+
message: MESSAGE,
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @internal
|
|
82
|
+
*/
|
|
83
|
+
const MESSAGE = "Request headers data is not following the promised type.";
|