@nestia/core 2.5.8 → 2.5.9-dev.20240223
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 +16 -0
- package/lib/decorators/SwaggerCustomizer.js +11 -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 +33 -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,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.";
|
|
@@ -1,71 +1,71 @@
|
|
|
1
|
-
import { BadRequestException } from "@nestjs/common";
|
|
2
|
-
import typia, { IValidation, TypeGuardError } from "typia";
|
|
3
|
-
|
|
4
|
-
import { IRequestQueryValidator } from "../../options/IRequestQueryValidator";
|
|
5
|
-
import { NoTransformConfigureError } from "./NoTransformConfigureError";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @internal
|
|
9
|
-
*/
|
|
10
|
-
export const validate_request_query = <T>(
|
|
11
|
-
validator?: IRequestQueryValidator<T>,
|
|
12
|
-
) => {
|
|
13
|
-
if (!validator) return () => NoTransformConfigureError("TypedQuery");
|
|
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.TypedQuery(): invalid typed validator.`);
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @internal
|
|
23
|
-
*/
|
|
24
|
-
const assert =
|
|
25
|
-
<T>(closure: (input: URLSearchParams) => T) =>
|
|
26
|
-
(input: URLSearchParams): T | BadRequestException => {
|
|
27
|
-
try {
|
|
28
|
-
return closure(input);
|
|
29
|
-
} catch (exp) {
|
|
30
|
-
if (typia.is<TypeGuardError>(exp)) {
|
|
31
|
-
return new BadRequestException({
|
|
32
|
-
path: exp.path,
|
|
33
|
-
reason: exp.message,
|
|
34
|
-
expected: exp.expected,
|
|
35
|
-
value: exp.value,
|
|
36
|
-
message: MESSAGE,
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
throw exp;
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* @internal
|
|
45
|
-
*/
|
|
46
|
-
const is =
|
|
47
|
-
<T>(closure: (input: URLSearchParams) => T | null) =>
|
|
48
|
-
(input: URLSearchParams): T | BadRequestException => {
|
|
49
|
-
const result: T | null = closure(input);
|
|
50
|
-
return result !== null ? result : new BadRequestException(MESSAGE);
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* @internal
|
|
55
|
-
*/
|
|
56
|
-
const validate =
|
|
57
|
-
<T>(closure: (input: URLSearchParams) => IValidation<T>) =>
|
|
58
|
-
(input: URLSearchParams): T | BadRequestException => {
|
|
59
|
-
const result: IValidation<T> = closure(input);
|
|
60
|
-
return result.success
|
|
61
|
-
? result.data
|
|
62
|
-
: new BadRequestException({
|
|
63
|
-
errors: result.errors,
|
|
64
|
-
message: MESSAGE,
|
|
65
|
-
});
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* @internal
|
|
70
|
-
*/
|
|
71
|
-
const MESSAGE = "Request query data is not following the promised type.";
|
|
1
|
+
import { BadRequestException } from "@nestjs/common";
|
|
2
|
+
import typia, { IValidation, TypeGuardError } from "typia";
|
|
3
|
+
|
|
4
|
+
import { IRequestQueryValidator } from "../../options/IRequestQueryValidator";
|
|
5
|
+
import { NoTransformConfigureError } from "./NoTransformConfigureError";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export const validate_request_query = <T>(
|
|
11
|
+
validator?: IRequestQueryValidator<T>,
|
|
12
|
+
) => {
|
|
13
|
+
if (!validator) return () => NoTransformConfigureError("TypedQuery");
|
|
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.TypedQuery(): invalid typed validator.`);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
const assert =
|
|
25
|
+
<T>(closure: (input: URLSearchParams) => T) =>
|
|
26
|
+
(input: URLSearchParams): T | BadRequestException => {
|
|
27
|
+
try {
|
|
28
|
+
return closure(input);
|
|
29
|
+
} catch (exp) {
|
|
30
|
+
if (typia.is<TypeGuardError>(exp)) {
|
|
31
|
+
return new BadRequestException({
|
|
32
|
+
path: exp.path,
|
|
33
|
+
reason: exp.message,
|
|
34
|
+
expected: exp.expected,
|
|
35
|
+
value: exp.value,
|
|
36
|
+
message: MESSAGE,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
throw exp;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @internal
|
|
45
|
+
*/
|
|
46
|
+
const is =
|
|
47
|
+
<T>(closure: (input: URLSearchParams) => T | null) =>
|
|
48
|
+
(input: URLSearchParams): T | BadRequestException => {
|
|
49
|
+
const result: T | null = closure(input);
|
|
50
|
+
return result !== null ? result : new BadRequestException(MESSAGE);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @internal
|
|
55
|
+
*/
|
|
56
|
+
const validate =
|
|
57
|
+
<T>(closure: (input: URLSearchParams) => IValidation<T>) =>
|
|
58
|
+
(input: URLSearchParams): T | BadRequestException => {
|
|
59
|
+
const result: IValidation<T> = closure(input);
|
|
60
|
+
return result.success
|
|
61
|
+
? result.data
|
|
62
|
+
: new BadRequestException({
|
|
63
|
+
errors: result.errors,
|
|
64
|
+
message: MESSAGE,
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @internal
|
|
70
|
+
*/
|
|
71
|
+
const MESSAGE = "Request query data is not following the promised type.";
|
package/src/module.ts
CHANGED
|
@@ -5,6 +5,7 @@ export * from "./decorators/EncryptedModule";
|
|
|
5
5
|
export * from "./decorators/EncryptedRoute";
|
|
6
6
|
export * from "./utils/ExceptionManager";
|
|
7
7
|
export * from "./decorators/PlainBody";
|
|
8
|
+
export * from "./decorators/SwaggerCustomizer";
|
|
8
9
|
export * from "./decorators/TypedBody";
|
|
9
10
|
export * from "./decorators/TypedException";
|
|
10
11
|
export * from "./decorators/TypedHeaders";
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { ISwaggerComponents } from "./ISwaggerComponents";
|
|
2
|
+
import { ISwaggerInfo } from "./ISwaggerInfo";
|
|
3
|
+
import { ISwaggerRoute } from "./ISwaggerRoute";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Swagger Document.
|
|
7
|
+
*
|
|
8
|
+
* `ISwagger` is a data structure representing content of `swagger.json` file
|
|
9
|
+
* generated by Nestia. Note that, this is not an universal structure, but a dedicated
|
|
10
|
+
* structure only for Nestia.
|
|
11
|
+
*
|
|
12
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
13
|
+
*/
|
|
14
|
+
export interface ISwagger {
|
|
15
|
+
/**
|
|
16
|
+
* The version of the OpenAPI document.
|
|
17
|
+
*
|
|
18
|
+
* Nestia always generate OpenAPI 3.0.x document.
|
|
19
|
+
*/
|
|
20
|
+
openapi: `3.0.${number}`;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* List of servers that provide the API.
|
|
24
|
+
*/
|
|
25
|
+
servers: ISwagger.IServer[];
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Information about the API.
|
|
29
|
+
*/
|
|
30
|
+
info: ISwaggerInfo;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The available paths and operations for the API.
|
|
34
|
+
*
|
|
35
|
+
* The 1st key is the path, and the 2nd key is the HTTP method.
|
|
36
|
+
*/
|
|
37
|
+
paths: Record<string, Record<string, ISwaggerRoute>>;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* An object to hold reusable data structures.
|
|
41
|
+
*
|
|
42
|
+
* It stores both DTO schemas and security schemes.
|
|
43
|
+
*
|
|
44
|
+
* For reference, `nestia` defines every object and alias types as reusable DTO
|
|
45
|
+
* schemas. The alias type means that defined by `type` keyword in TypeScript.
|
|
46
|
+
*/
|
|
47
|
+
components: ISwaggerComponents;
|
|
48
|
+
|
|
49
|
+
// /**
|
|
50
|
+
// * A declaration of which security mechanisms can be used across the API.
|
|
51
|
+
// *
|
|
52
|
+
// * When this property be configured, it would be overwritten in every API routes.
|
|
53
|
+
// *
|
|
54
|
+
// * For reference, key means the name of security scheme and value means the `scopes`.
|
|
55
|
+
// * The `scopes` can be used only when target security scheme is `oauth2` type,
|
|
56
|
+
// * especially for {@link ISwaggerSecurityScheme.IOAuth2.IFlow.scopes} property.
|
|
57
|
+
// */
|
|
58
|
+
// security?: Record<string, string[]>[];
|
|
59
|
+
}
|
|
60
|
+
export namespace ISwagger {
|
|
61
|
+
/**
|
|
62
|
+
* Remote server definition.
|
|
63
|
+
*/
|
|
64
|
+
export interface IServer {
|
|
65
|
+
/**
|
|
66
|
+
* A URL to the target host.
|
|
67
|
+
*
|
|
68
|
+
* @format uri
|
|
69
|
+
*/
|
|
70
|
+
url: string;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* An optional string describing the target server.
|
|
74
|
+
*/
|
|
75
|
+
description?: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface IExternalDocs {
|
|
79
|
+
/**
|
|
80
|
+
* The URL for target documentation.
|
|
81
|
+
*
|
|
82
|
+
* @format uri
|
|
83
|
+
*/
|
|
84
|
+
url: string;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* A short description of the target documentation.
|
|
88
|
+
*/
|
|
89
|
+
description?: string;
|
|
90
|
+
}
|
|
91
|
+
}
|