@nestia/core 2.0.6 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/decorators/TypedHeaders.d.ts +2 -1
- package/lib/decorators/TypedHeaders.js +13 -60
- package/lib/decorators/TypedHeaders.js.map +1 -1
- package/lib/decorators/TypedParam.d.ts +1 -8
- package/lib/decorators/TypedParam.js +24 -6
- package/lib/decorators/TypedParam.js.map +1 -1
- package/lib/decorators/TypedQuery.d.ts +2 -1
- package/lib/decorators/TypedQuery.js +13 -76
- package/lib/decorators/TypedQuery.js.map +1 -1
- package/lib/decorators/internal/get_path_and_stringify.d.ts +1 -1
- package/lib/decorators/internal/get_path_and_stringify.js +18 -0
- package/lib/decorators/internal/get_path_and_stringify.js.map +1 -1
- package/lib/decorators/internal/get_text_body.d.ts +1 -3
- package/lib/decorators/internal/get_text_body.js +6 -0
- package/lib/decorators/internal/get_text_body.js.map +1 -1
- package/lib/decorators/internal/headers_to_object.d.ts +1 -3
- package/lib/decorators/internal/headers_to_object.js +3 -0
- package/lib/decorators/internal/headers_to_object.js.map +1 -1
- package/lib/decorators/internal/load_controller.d.ts +1 -5
- package/lib/decorators/internal/load_controller.js +9 -0
- package/lib/decorators/internal/load_controller.js.map +1 -1
- package/lib/decorators/internal/route_error.d.ts +1 -3
- package/lib/decorators/internal/route_error.js +3 -0
- package/lib/decorators/internal/route_error.js.map +1 -1
- package/lib/decorators/internal/validate_request_body.d.ts +1 -1
- package/lib/decorators/internal/validate_request_body.js +6 -6
- package/lib/decorators/internal/validate_request_body.js.map +1 -1
- package/lib/decorators/internal/validate_request_headers.d.ts +3 -0
- package/lib/decorators/internal/validate_request_headers.js +64 -0
- package/lib/decorators/internal/validate_request_headers.js.map +1 -0
- package/lib/decorators/internal/validate_request_query.d.ts +3 -0
- package/lib/decorators/internal/validate_request_query.js +64 -0
- package/lib/decorators/internal/validate_request_query.js.map +1 -0
- package/lib/options/IRequestHeadersValidator.d.ts +16 -0
- package/lib/options/IRequestHeadersValidator.js +3 -0
- package/lib/options/IRequestHeadersValidator.js.map +1 -0
- package/lib/options/IRequestQueryValidator.d.ts +16 -0
- package/lib/options/IRequestQueryValidator.js +3 -0
- package/lib/options/IRequestQueryValidator.js.map +1 -0
- package/lib/programmers/TypedHeadersProgrammer.d.ts +0 -3
- package/lib/programmers/TypedHeadersProgrammer.js +21 -228
- package/lib/programmers/TypedHeadersProgrammer.js.map +1 -1
- package/lib/programmers/TypedParamProgrammer.d.ts +0 -2
- package/lib/programmers/TypedParamProgrammer.js +4 -92
- package/lib/programmers/TypedParamProgrammer.js.map +1 -1
- package/lib/programmers/TypedQueryProgrammer.d.ts +0 -3
- package/lib/programmers/TypedQueryProgrammer.js +22 -183
- package/lib/programmers/TypedQueryProgrammer.js.map +1 -1
- package/package.json +5 -5
- package/src/decorators/TypedHeaders.ts +13 -38
- package/src/decorators/TypedParam.ts +19 -18
- package/src/decorators/TypedQuery.ts +12 -50
- package/src/decorators/internal/get_path_and_stringify.ts +18 -0
- package/src/decorators/internal/get_text_body.ts +6 -0
- package/src/decorators/internal/headers_to_object.ts +3 -0
- package/src/decorators/internal/load_controller.ts +9 -0
- package/src/decorators/internal/route_error.ts +3 -0
- package/src/decorators/internal/validate_request_body.ts +6 -6
- package/src/decorators/internal/validate_request_headers.ts +73 -0
- package/src/decorators/internal/validate_request_query.ts +59 -0
- package/src/options/IRequestHeadersValidator.ts +22 -0
- package/src/options/IRequestQueryValidator.ts +20 -0
- package/src/programmers/TypedHeadersProgrammer.ts +40 -285
- package/src/programmers/TypedParamProgrammer.ts +7 -138
- package/src/programmers/TypedQueryProgrammer.ts +38 -251
|
@@ -21,9 +21,9 @@ export const validate_request_body =
|
|
|
21
21
|
|
|
22
22
|
const assert =
|
|
23
23
|
<T>(closure: (data: T) => T) =>
|
|
24
|
-
(
|
|
24
|
+
(input: T) => {
|
|
25
25
|
try {
|
|
26
|
-
closure(
|
|
26
|
+
closure(input);
|
|
27
27
|
return null;
|
|
28
28
|
} catch (exp) {
|
|
29
29
|
if (typia.is<TypeGuardError>(exp)) {
|
|
@@ -41,15 +41,15 @@ const assert =
|
|
|
41
41
|
|
|
42
42
|
const is =
|
|
43
43
|
<T>(closure: (data: T) => boolean) =>
|
|
44
|
-
(
|
|
45
|
-
const success: boolean = closure(
|
|
44
|
+
(input: T) => {
|
|
45
|
+
const success: boolean = closure(input);
|
|
46
46
|
return success ? null : new BadRequestException(MESSAGE);
|
|
47
47
|
};
|
|
48
48
|
|
|
49
49
|
const validate =
|
|
50
50
|
<T>(closure: (data: T) => IValidation<T>) =>
|
|
51
|
-
(
|
|
52
|
-
const result: IValidation<T> = closure(
|
|
51
|
+
(input: T) => {
|
|
52
|
+
const result: IValidation<T> = closure(input);
|
|
53
53
|
return result.success
|
|
54
54
|
? null
|
|
55
55
|
: new BadRequestException({
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { BadRequestException } from "@nestjs/common";
|
|
2
|
+
|
|
3
|
+
import typia, { IValidation, TypeGuardError } from "typia";
|
|
4
|
+
|
|
5
|
+
import { IRequestHeadersValidator } from "../../options/IRequestHeadersValidator";
|
|
6
|
+
import { NoTransformConfigureError } from "./NoTransformConfigureError";
|
|
7
|
+
|
|
8
|
+
export const validate_request_headers = <T>(
|
|
9
|
+
validator?: IRequestHeadersValidator<T>,
|
|
10
|
+
) => {
|
|
11
|
+
if (!validator) return () => NoTransformConfigureError("TypedHeaders");
|
|
12
|
+
else if (validator.type === "assert") return assert(validator.assert);
|
|
13
|
+
else if (validator.type === "is") return is(validator.is);
|
|
14
|
+
else if (validator.type === "validate") return validate(validator.validate);
|
|
15
|
+
return () =>
|
|
16
|
+
new Error(
|
|
17
|
+
`Error on nestia.core.TypedHeaders(): invalid typed validator.`,
|
|
18
|
+
);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const assert =
|
|
22
|
+
<T>(closure: (input: Record<string, string | string[] | undefined>) => T) =>
|
|
23
|
+
(
|
|
24
|
+
input: Record<string, string | string[] | undefined>,
|
|
25
|
+
): T | BadRequestException => {
|
|
26
|
+
try {
|
|
27
|
+
return closure(input);
|
|
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
|
+
const is =
|
|
43
|
+
<T>(
|
|
44
|
+
closure: (
|
|
45
|
+
input: Record<string, string | string[] | undefined>,
|
|
46
|
+
) => T | null,
|
|
47
|
+
) =>
|
|
48
|
+
(
|
|
49
|
+
input: Record<string, string | string[] | undefined>,
|
|
50
|
+
): T | BadRequestException => {
|
|
51
|
+
const result: T | null = closure(input);
|
|
52
|
+
return result !== null ? result : new BadRequestException(MESSAGE);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const validate =
|
|
56
|
+
<T>(
|
|
57
|
+
closure: (
|
|
58
|
+
input: Record<string, string | string[] | undefined>,
|
|
59
|
+
) => IValidation<T>,
|
|
60
|
+
) =>
|
|
61
|
+
(
|
|
62
|
+
input: Record<string, string | string[] | undefined>,
|
|
63
|
+
): T | BadRequestException => {
|
|
64
|
+
const result: IValidation<T> = closure(input);
|
|
65
|
+
return result.success
|
|
66
|
+
? result.data
|
|
67
|
+
: new BadRequestException({
|
|
68
|
+
errors: result.errors,
|
|
69
|
+
message: MESSAGE,
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const MESSAGE = "Request headers data is not following the promised type.";
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { BadRequestException } from "@nestjs/common";
|
|
2
|
+
|
|
3
|
+
import typia, { IValidation, TypeGuardError } from "typia";
|
|
4
|
+
|
|
5
|
+
import { IRequestQueryValidator } from "../../options/IRequestQueryValidator";
|
|
6
|
+
import { NoTransformConfigureError } from "./NoTransformConfigureError";
|
|
7
|
+
|
|
8
|
+
export const validate_request_query = <T>(
|
|
9
|
+
validator?: IRequestQueryValidator<T>,
|
|
10
|
+
) => {
|
|
11
|
+
if (!validator) return () => NoTransformConfigureError("TypedQuery");
|
|
12
|
+
else if (validator.type === "assert") return assert(validator.assert);
|
|
13
|
+
else if (validator.type === "is") return is(validator.is);
|
|
14
|
+
else if (validator.type === "validate") return validate(validator.validate);
|
|
15
|
+
return () =>
|
|
16
|
+
new Error(
|
|
17
|
+
`Error on nestia.core.TypedQuery(): invalid typed validator.`,
|
|
18
|
+
);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const assert =
|
|
22
|
+
<T>(closure: (input: URLSearchParams) => T) =>
|
|
23
|
+
(input: URLSearchParams): T | BadRequestException => {
|
|
24
|
+
try {
|
|
25
|
+
return closure(input);
|
|
26
|
+
} catch (exp) {
|
|
27
|
+
if (typia.is<TypeGuardError>(exp)) {
|
|
28
|
+
return new BadRequestException({
|
|
29
|
+
path: exp.path,
|
|
30
|
+
reason: exp.message,
|
|
31
|
+
expected: exp.expected,
|
|
32
|
+
value: exp.value,
|
|
33
|
+
message: MESSAGE,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
throw exp;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const is =
|
|
41
|
+
<T>(closure: (input: URLSearchParams) => T | null) =>
|
|
42
|
+
(input: URLSearchParams): T | BadRequestException => {
|
|
43
|
+
const result: T | null = closure(input);
|
|
44
|
+
return result !== null ? result : new BadRequestException(MESSAGE);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const validate =
|
|
48
|
+
<T>(closure: (input: URLSearchParams) => IValidation<T>) =>
|
|
49
|
+
(input: URLSearchParams): T | BadRequestException => {
|
|
50
|
+
const result: IValidation<T> = closure(input);
|
|
51
|
+
return result.success
|
|
52
|
+
? result.data
|
|
53
|
+
: new BadRequestException({
|
|
54
|
+
errors: result.errors,
|
|
55
|
+
message: MESSAGE,
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const MESSAGE = "Request query data is not following the promised type.";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { IValidation } from "typia";
|
|
2
|
+
|
|
3
|
+
export type IRequestHeadersValidator<T> =
|
|
4
|
+
| IRequestHeadersValidator.IAssert<T>
|
|
5
|
+
| IRequestHeadersValidator.IIs<T>
|
|
6
|
+
| IRequestHeadersValidator.IValidate<T>;
|
|
7
|
+
export namespace IRequestHeadersValidator {
|
|
8
|
+
export interface IAssert<T> {
|
|
9
|
+
type: "assert";
|
|
10
|
+
assert: (input: Record<string, string | string[] | undefined>) => T;
|
|
11
|
+
}
|
|
12
|
+
export interface IIs<T> {
|
|
13
|
+
type: "is";
|
|
14
|
+
is: (input: Record<string, string | string[] | undefined>) => T | null;
|
|
15
|
+
}
|
|
16
|
+
export interface IValidate<T> {
|
|
17
|
+
type: "validate";
|
|
18
|
+
validate: (
|
|
19
|
+
input: Record<string, string | string[] | undefined>,
|
|
20
|
+
) => IValidation<T>;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { IValidation } from "typia";
|
|
2
|
+
|
|
3
|
+
export type IRequestQueryValidator<T> =
|
|
4
|
+
| IRequestQueryValidator.IAssert<T>
|
|
5
|
+
| IRequestQueryValidator.IIs<T>
|
|
6
|
+
| IRequestQueryValidator.IValidate<T>;
|
|
7
|
+
export namespace IRequestQueryValidator {
|
|
8
|
+
export interface IAssert<T> {
|
|
9
|
+
type: "assert";
|
|
10
|
+
assert: (input: URLSearchParams) => T;
|
|
11
|
+
}
|
|
12
|
+
export interface IIs<T> {
|
|
13
|
+
type: "is";
|
|
14
|
+
is: (input: URLSearchParams) => T | null;
|
|
15
|
+
}
|
|
16
|
+
export interface IValidate<T> {
|
|
17
|
+
type: "validate";
|
|
18
|
+
validate: (input: URLSearchParams) => IValidation<T>;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -1,299 +1,54 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { AssertProgrammer } from "typia/lib/programmers/AssertProgrammer";
|
|
8
|
-
import { FunctionImporter } from "typia/lib/programmers/helpers/FunctionImporeter";
|
|
9
|
-
import { Metadata } from "typia/lib/schemas/metadata/Metadata";
|
|
10
|
-
import { MetadataArray } from "typia/lib/schemas/metadata/MetadataArray";
|
|
11
|
-
import { MetadataObject } from "typia/lib/schemas/metadata/MetadataObject";
|
|
12
|
-
import { MetadataProperty } from "typia/lib/schemas/metadata/MetadataProperty";
|
|
13
|
-
import { TransformerError } from "typia/lib/transformers/TransformerError";
|
|
14
|
-
import { Atomic } from "typia/lib/typings/Atomic";
|
|
15
|
-
import { Escaper } from "typia/lib/utils/Escaper";
|
|
3
|
+
import { HttpAssertHeadersProgrammer } from "typia/lib/programmers/http/HttpAssertHeadersProgrammer";
|
|
4
|
+
import { HttpIsHeadersProgrammer } from "typia/lib/programmers/http/HttpIsHeadersProgrammer";
|
|
5
|
+
import { HttpValidateHeadersProgrammer } from "typia/lib/programmers/http/HttpValidateHeadersProgrammer";
|
|
6
|
+
import { IProject } from "typia/lib/transformers/IProject";
|
|
16
7
|
|
|
17
8
|
import { INestiaTransformProject } from "../options/INestiaTransformProject";
|
|
18
|
-
import {
|
|
9
|
+
import { IRequestHeadersValidator } from "../options/IRequestHeadersValidator";
|
|
19
10
|
|
|
20
11
|
export namespace TypedHeadersProgrammer {
|
|
21
12
|
export const generate =
|
|
22
13
|
(project: INestiaTransformProject) =>
|
|
23
14
|
(modulo: ts.LeftHandSideExpression) =>
|
|
24
15
|
(type: ts.Type): ts.Expression => {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
insert("only one object type is allowed.");
|
|
40
|
-
if (meta.nullable === true) insert("headers cannot be null.");
|
|
41
|
-
if (meta.isRequired() === false) insert("headers cannot be null.");
|
|
42
|
-
} else if (
|
|
43
|
-
explore.nested !== null &&
|
|
44
|
-
explore.nested instanceof MetadataArray
|
|
45
|
-
) {
|
|
46
|
-
const atomics = CoreMetadataUtil.atomics(meta);
|
|
47
|
-
const expected: number =
|
|
48
|
-
meta.atomics.length +
|
|
49
|
-
meta.templates.length +
|
|
50
|
-
meta.constants
|
|
51
|
-
.map((c) => c.values.length)
|
|
52
|
-
.reduce((a, b) => a + b, 0);
|
|
53
|
-
if (atomics.size > 1) insert("union type is not allowed in array.");
|
|
54
|
-
if (meta.nullable) insert("nullable type is not allowed in array.");
|
|
55
|
-
if (meta.isRequired() === false)
|
|
56
|
-
insert("optional type is not allowed.");
|
|
57
|
-
if (meta.size() !== expected)
|
|
58
|
-
insert("only atomic or constant types are allowed in array.");
|
|
59
|
-
} else if (explore.object && explore.property !== null) {
|
|
60
|
-
//----
|
|
61
|
-
// COMMON
|
|
62
|
-
//----
|
|
63
|
-
// PROPERTY MUST BE SOLE
|
|
64
|
-
if (typeof explore.property === "object")
|
|
65
|
-
insert("dynamic property is not allowed.");
|
|
66
|
-
// DO NOT ALLOW TUPLE TYPE
|
|
67
|
-
if (meta.tuples.length) insert("tuple type is not allowed.");
|
|
68
|
-
// DO NOT ALLOW UNION TYPE
|
|
69
|
-
if (CoreMetadataUtil.isUnion(meta))
|
|
70
|
-
insert("union type is not allowed.");
|
|
71
|
-
// DO NOT ALLOW NESTED OBJECT
|
|
72
|
-
if (
|
|
73
|
-
meta.objects.length ||
|
|
74
|
-
meta.sets.length ||
|
|
75
|
-
meta.maps.length ||
|
|
76
|
-
meta.natives.length
|
|
77
|
-
)
|
|
78
|
-
insert("nested object type is not allowed.");
|
|
79
|
-
// DO NOT ALLOW NULLABLE
|
|
80
|
-
if (meta.nullable) insert("nullable type is not allowed.");
|
|
81
|
-
|
|
82
|
-
//----
|
|
83
|
-
// ARRAY CASES
|
|
84
|
-
//----
|
|
85
|
-
const isArray: boolean = meta.arrays.length > 1;
|
|
86
|
-
// ARRAY TYPE MUST BE REQUIRED
|
|
87
|
-
if (isArray && meta.isRequired() === false)
|
|
88
|
-
insert("optional type is not allowed when array.");
|
|
89
|
-
// SET-COOKIE MUST BE ARRAY
|
|
90
|
-
if (explore.property === "set-cookie" && !isArray)
|
|
91
|
-
insert("set-cookie property must be array.");
|
|
92
|
-
// MUST BE SINGULAR CASE
|
|
93
|
-
if (
|
|
94
|
-
typeof explore.property === "string" &&
|
|
95
|
-
SINGULAR.has(explore.property) &&
|
|
96
|
-
isArray
|
|
97
|
-
)
|
|
98
|
-
insert("property cannot be array.");
|
|
99
|
-
}
|
|
100
|
-
return errors;
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
const getObject =
|
|
104
|
-
(checker: ts.TypeChecker) =>
|
|
105
|
-
(type: ts.Type): MetadataObject => {
|
|
106
|
-
const collection: MetadataCollection = new MetadataCollection();
|
|
107
|
-
const result = MetadataFactory.analyze(checker)({
|
|
108
|
-
escape: false,
|
|
109
|
-
constant: true,
|
|
110
|
-
absorb: true,
|
|
111
|
-
validate,
|
|
112
|
-
})(collection)(type);
|
|
113
|
-
if (result.success === false)
|
|
114
|
-
throw TransformerError.from("@core.nestia.TypedHeaders")(
|
|
115
|
-
result.errors,
|
|
116
|
-
);
|
|
117
|
-
return result.data.objects[0]!;
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
const decode =
|
|
121
|
-
(project: INestiaTransformProject, modulo: ts.LeftHandSideExpression) =>
|
|
122
|
-
(type: ts.Type, object: MetadataObject): ts.ArrowFunction =>
|
|
123
|
-
ts.factory.createArrowFunction(
|
|
124
|
-
undefined,
|
|
125
|
-
undefined,
|
|
126
|
-
[IdentifierFactory.parameter("input")],
|
|
127
|
-
undefined,
|
|
128
|
-
undefined,
|
|
129
|
-
decode_object(project, modulo)(type, object),
|
|
130
|
-
);
|
|
131
|
-
|
|
132
|
-
const decode_object =
|
|
133
|
-
(project: INestiaTransformProject, modulo: ts.LeftHandSideExpression) =>
|
|
134
|
-
(type: ts.Type, object: MetadataObject): ts.ConciseBody => {
|
|
135
|
-
const assert: ts.ArrowFunction = AssertProgrammer.write({
|
|
136
|
-
...project,
|
|
137
|
-
options: {
|
|
138
|
-
numeric: true,
|
|
139
|
-
finite: true,
|
|
140
|
-
},
|
|
141
|
-
})(modulo)(false)(type);
|
|
142
|
-
const output: ts.Identifier = ts.factory.createIdentifier("output");
|
|
143
|
-
|
|
144
|
-
const importer: FunctionImporter = new FunctionImporter(
|
|
145
|
-
"TypedHeaders",
|
|
146
|
-
);
|
|
147
|
-
const statements: ts.Statement[] = [
|
|
148
|
-
StatementFactory.constant(
|
|
149
|
-
"output",
|
|
150
|
-
ts.factory.createObjectLiteralExpression(
|
|
151
|
-
object.properties.map((prop) =>
|
|
152
|
-
decode_regular_property(importer)(object)(prop),
|
|
16
|
+
// GENERATE VALIDATION PLAN
|
|
17
|
+
const parameter =
|
|
18
|
+
(key: IRequestHeadersValidator<any>["type"]) =>
|
|
19
|
+
(
|
|
20
|
+
programmer: (
|
|
21
|
+
project: IProject,
|
|
22
|
+
) => (
|
|
23
|
+
modulo: ts.LeftHandSideExpression,
|
|
24
|
+
) => (type: ts.Type) => ts.ArrowFunction,
|
|
25
|
+
) =>
|
|
26
|
+
ts.factory.createObjectLiteralExpression([
|
|
27
|
+
ts.factory.createPropertyAssignment(
|
|
28
|
+
ts.factory.createIdentifier("type"),
|
|
29
|
+
ts.factory.createStringLiteral(key),
|
|
153
30
|
),
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
return ts.factory.createBlock(
|
|
165
|
-
[...importer.declare(modulo), ...statements],
|
|
166
|
-
true,
|
|
167
|
-
);
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
const decode_regular_property =
|
|
171
|
-
(importer: FunctionImporter) =>
|
|
172
|
-
(object: MetadataObject) =>
|
|
173
|
-
(property: MetadataProperty): ts.PropertyAssignment => {
|
|
174
|
-
const key: string = property.key.constants[0]!.values[0] as string;
|
|
175
|
-
const value: Metadata = property.value;
|
|
176
|
-
|
|
177
|
-
const [type, isArray]: [Atomic.Literal, boolean] = value.atomics
|
|
178
|
-
.length
|
|
179
|
-
? [value.atomics[0].type, false]
|
|
180
|
-
: value.constants.length
|
|
181
|
-
? [value.constants[0]!.type, false]
|
|
182
|
-
: (() => {
|
|
183
|
-
const meta =
|
|
184
|
-
value.arrays[0]?.type.value ??
|
|
185
|
-
value.tuples[0].type.elements[0];
|
|
186
|
-
return meta.atomics.length
|
|
187
|
-
? [meta.atomics[0].type, true]
|
|
188
|
-
: [meta.constants[0]!.type, true];
|
|
189
|
-
})();
|
|
190
|
-
if (isArray && SINGULAR.has(key))
|
|
191
|
-
throw new Error(
|
|
192
|
-
ErrorMessages.property(object)(key)(
|
|
193
|
-
`property "${key}" cannot be array.`,
|
|
194
|
-
),
|
|
195
|
-
);
|
|
196
|
-
else if (!isArray && key === "set-cookie")
|
|
197
|
-
throw new Error(
|
|
198
|
-
ErrorMessages.property(object)(key)(
|
|
199
|
-
`property "${key}" must be array.`,
|
|
200
|
-
),
|
|
201
|
-
);
|
|
202
|
-
|
|
203
|
-
const accessor = IdentifierFactory.access(
|
|
204
|
-
ts.factory.createIdentifier("input"),
|
|
205
|
-
)(key.toLowerCase());
|
|
206
|
-
|
|
207
|
-
return ts.factory.createPropertyAssignment(
|
|
208
|
-
Escaper.variable(key)
|
|
209
|
-
? key
|
|
210
|
-
: ts.factory.createStringLiteral(key),
|
|
211
|
-
isArray
|
|
212
|
-
? key === "set-cookie"
|
|
213
|
-
? accessor
|
|
214
|
-
: decode_array(importer)(type)(key)(value)(accessor)
|
|
215
|
-
: decode_value(importer)(type)(accessor),
|
|
216
|
-
);
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
const decode_value =
|
|
220
|
-
(importer: FunctionImporter) =>
|
|
221
|
-
(type: Atomic.Literal) =>
|
|
222
|
-
(value: ts.Expression) =>
|
|
223
|
-
type === "string"
|
|
224
|
-
? value
|
|
225
|
-
: ts.factory.createCallExpression(
|
|
226
|
-
importer.use(type),
|
|
227
|
-
undefined,
|
|
228
|
-
[value],
|
|
229
|
-
);
|
|
230
|
-
|
|
231
|
-
const decode_array =
|
|
232
|
-
(importer: FunctionImporter) =>
|
|
233
|
-
(type: Atomic.Literal) =>
|
|
234
|
-
(key: string) =>
|
|
235
|
-
(value: Metadata) =>
|
|
236
|
-
(accessor: ts.Expression) => {
|
|
237
|
-
const expression = ts.factory.createCallChain(
|
|
238
|
-
ts.factory.createPropertyAccessChain(
|
|
239
|
-
ts.factory.createCallChain(
|
|
240
|
-
ts.factory.createPropertyAccessChain(
|
|
241
|
-
accessor,
|
|
242
|
-
ts.factory.createToken(
|
|
243
|
-
ts.SyntaxKind.QuestionDotToken,
|
|
244
|
-
),
|
|
245
|
-
ts.factory.createIdentifier("split"),
|
|
31
|
+
ts.factory.createPropertyAssignment(
|
|
32
|
+
ts.factory.createIdentifier(key),
|
|
33
|
+
programmer({
|
|
34
|
+
...project,
|
|
35
|
+
options: {
|
|
36
|
+
numeric: false,
|
|
37
|
+
finite: false,
|
|
38
|
+
functional: false,
|
|
39
|
+
},
|
|
40
|
+
})(modulo)(type),
|
|
246
41
|
),
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
undefined,
|
|
259
|
-
undefined,
|
|
260
|
-
[importer.use(type)],
|
|
261
|
-
);
|
|
262
|
-
if (value.isRequired() === false) return expression;
|
|
263
|
-
return ts.factory.createBinaryExpression(
|
|
264
|
-
expression,
|
|
265
|
-
ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),
|
|
266
|
-
ts.factory.createArrayLiteralExpression([], false),
|
|
267
|
-
);
|
|
42
|
+
]);
|
|
43
|
+
|
|
44
|
+
// RETURNS
|
|
45
|
+
const category = project.options.validate;
|
|
46
|
+
if (category === "is" || category === "equals")
|
|
47
|
+
return parameter("is")(HttpIsHeadersProgrammer.write);
|
|
48
|
+
else if (category === "validate" || category === "validateEquals")
|
|
49
|
+
return parameter("validate")(
|
|
50
|
+
HttpValidateHeadersProgrammer.write,
|
|
51
|
+
);
|
|
52
|
+
return parameter("assert")(HttpAssertHeadersProgrammer.write);
|
|
268
53
|
};
|
|
269
54
|
}
|
|
270
|
-
|
|
271
|
-
namespace ErrorMessages {
|
|
272
|
-
export const object = (type: Metadata) => (message: string) =>
|
|
273
|
-
`Error on nestia.core.TypedHeaders<${type.getName()}>(): ${message}`;
|
|
274
|
-
|
|
275
|
-
export const property =
|
|
276
|
-
(obj: MetadataObject) => (key: string) => (message: string) =>
|
|
277
|
-
`Error on nestia.core.TypedHeaders<${obj.name}>(): property "${key}" - ${message}`;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
const SINGULAR: Set<string> = new Set([
|
|
281
|
-
"age",
|
|
282
|
-
"authorization",
|
|
283
|
-
"content-length",
|
|
284
|
-
"content-type",
|
|
285
|
-
"etag",
|
|
286
|
-
"expires",
|
|
287
|
-
"from",
|
|
288
|
-
"host",
|
|
289
|
-
"if-modified-since",
|
|
290
|
-
"if-unmodified-since",
|
|
291
|
-
"last-modified",
|
|
292
|
-
"location",
|
|
293
|
-
"max-forwards",
|
|
294
|
-
"proxy-authorization",
|
|
295
|
-
"referer",
|
|
296
|
-
"retry-after",
|
|
297
|
-
"server",
|
|
298
|
-
"user-agent",
|
|
299
|
-
]);
|