@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.
Files changed (65) hide show
  1. package/lib/decorators/TypedHeaders.d.ts +2 -1
  2. package/lib/decorators/TypedHeaders.js +13 -60
  3. package/lib/decorators/TypedHeaders.js.map +1 -1
  4. package/lib/decorators/TypedParam.d.ts +1 -8
  5. package/lib/decorators/TypedParam.js +24 -6
  6. package/lib/decorators/TypedParam.js.map +1 -1
  7. package/lib/decorators/TypedQuery.d.ts +2 -1
  8. package/lib/decorators/TypedQuery.js +13 -76
  9. package/lib/decorators/TypedQuery.js.map +1 -1
  10. package/lib/decorators/internal/get_path_and_stringify.d.ts +1 -1
  11. package/lib/decorators/internal/get_path_and_stringify.js +18 -0
  12. package/lib/decorators/internal/get_path_and_stringify.js.map +1 -1
  13. package/lib/decorators/internal/get_text_body.d.ts +1 -3
  14. package/lib/decorators/internal/get_text_body.js +6 -0
  15. package/lib/decorators/internal/get_text_body.js.map +1 -1
  16. package/lib/decorators/internal/headers_to_object.d.ts +1 -3
  17. package/lib/decorators/internal/headers_to_object.js +3 -0
  18. package/lib/decorators/internal/headers_to_object.js.map +1 -1
  19. package/lib/decorators/internal/load_controller.d.ts +1 -5
  20. package/lib/decorators/internal/load_controller.js +9 -0
  21. package/lib/decorators/internal/load_controller.js.map +1 -1
  22. package/lib/decorators/internal/route_error.d.ts +1 -3
  23. package/lib/decorators/internal/route_error.js +3 -0
  24. package/lib/decorators/internal/route_error.js.map +1 -1
  25. package/lib/decorators/internal/validate_request_body.d.ts +1 -1
  26. package/lib/decorators/internal/validate_request_body.js +6 -6
  27. package/lib/decorators/internal/validate_request_body.js.map +1 -1
  28. package/lib/decorators/internal/validate_request_headers.d.ts +3 -0
  29. package/lib/decorators/internal/validate_request_headers.js +64 -0
  30. package/lib/decorators/internal/validate_request_headers.js.map +1 -0
  31. package/lib/decorators/internal/validate_request_query.d.ts +3 -0
  32. package/lib/decorators/internal/validate_request_query.js +64 -0
  33. package/lib/decorators/internal/validate_request_query.js.map +1 -0
  34. package/lib/options/IRequestHeadersValidator.d.ts +16 -0
  35. package/lib/options/IRequestHeadersValidator.js +3 -0
  36. package/lib/options/IRequestHeadersValidator.js.map +1 -0
  37. package/lib/options/IRequestQueryValidator.d.ts +16 -0
  38. package/lib/options/IRequestQueryValidator.js +3 -0
  39. package/lib/options/IRequestQueryValidator.js.map +1 -0
  40. package/lib/programmers/TypedHeadersProgrammer.d.ts +0 -3
  41. package/lib/programmers/TypedHeadersProgrammer.js +21 -228
  42. package/lib/programmers/TypedHeadersProgrammer.js.map +1 -1
  43. package/lib/programmers/TypedParamProgrammer.d.ts +0 -2
  44. package/lib/programmers/TypedParamProgrammer.js +4 -92
  45. package/lib/programmers/TypedParamProgrammer.js.map +1 -1
  46. package/lib/programmers/TypedQueryProgrammer.d.ts +0 -3
  47. package/lib/programmers/TypedQueryProgrammer.js +22 -183
  48. package/lib/programmers/TypedQueryProgrammer.js.map +1 -1
  49. package/package.json +5 -5
  50. package/src/decorators/TypedHeaders.ts +13 -38
  51. package/src/decorators/TypedParam.ts +19 -18
  52. package/src/decorators/TypedQuery.ts +12 -50
  53. package/src/decorators/internal/get_path_and_stringify.ts +18 -0
  54. package/src/decorators/internal/get_text_body.ts +6 -0
  55. package/src/decorators/internal/headers_to_object.ts +3 -0
  56. package/src/decorators/internal/load_controller.ts +9 -0
  57. package/src/decorators/internal/route_error.ts +3 -0
  58. package/src/decorators/internal/validate_request_body.ts +6 -6
  59. package/src/decorators/internal/validate_request_headers.ts +73 -0
  60. package/src/decorators/internal/validate_request_query.ts +59 -0
  61. package/src/options/IRequestHeadersValidator.ts +22 -0
  62. package/src/options/IRequestQueryValidator.ts +20 -0
  63. package/src/programmers/TypedHeadersProgrammer.ts +40 -285
  64. package/src/programmers/TypedParamProgrammer.ts +7 -138
  65. package/src/programmers/TypedQueryProgrammer.ts +38 -251
@@ -1,14 +1,8 @@
1
1
  import ts from "typescript";
2
2
 
3
- import { IdentifierFactory } from "typia/lib/factories/IdentifierFactory";
4
- import { MetadataCollection } from "typia/lib/factories/MetadataCollection";
5
- import { MetadataFactory } from "typia/lib/factories/MetadataFactory";
6
- import { IsProgrammer } from "typia/lib/programmers/IsProgrammer";
7
- import { Metadata } from "typia/lib/schemas/metadata/Metadata";
8
- import { TransformerError } from "typia/lib/transformers/TransformerError";
3
+ import { HttpParameterProgrammer } from "typia/lib/programmers/http/HttpParameterProgrammer";
9
4
 
10
5
  import { INestiaTransformProject } from "../options/INestiaTransformProject";
11
- import { CoreMetadataUtil } from "./internal/CoreMetadataUtil";
12
6
 
13
7
  export namespace TypedParamProgrammer {
14
8
  export const generate =
@@ -18,139 +12,14 @@ export namespace TypedParamProgrammer {
18
12
  (type: ts.Type): readonly ts.Expression[] => {
19
13
  // ALREADY BEING TRANSFORMED
20
14
  if (parameters.length !== 1) return parameters;
21
-
22
- const result = MetadataFactory.analyze(project.checker)({
23
- escape: false,
24
- constant: true,
25
- absorb: true,
26
- validate,
27
- })(new MetadataCollection())(type);
28
- if (result.success === false)
29
- throw TransformerError.from("@core.nestia.TypedParam")(
30
- result.errors,
31
- );
32
- const [atomic] = [...CoreMetadataUtil.atomics(result.data)];
33
- const name: string = result.data.getName();
34
- const is: ts.ArrowFunction = IsProgrammer.write({
35
- ...project,
36
- options: {
37
- numeric: true,
38
- },
39
- })(modulo)(false)(type);
40
- const cast: ts.ArrowFunction = CASTERS[atomic](
41
- result.data.nullable,
42
- );
43
15
  return [
44
16
  parameters[0],
45
- ts.factory.createObjectLiteralExpression(
46
- [
47
- ts.factory.createPropertyAssignment(
48
- "name",
49
- ts.factory.createStringLiteral(name),
50
- ),
51
- ts.factory.createPropertyAssignment("is", is),
52
- ts.factory.createPropertyAssignment("cast", cast),
53
- ],
54
- true,
55
- ),
17
+ HttpParameterProgrammer.write({
18
+ ...project,
19
+ options: {
20
+ numeric: true,
21
+ },
22
+ })(modulo)(type),
56
23
  ];
57
24
  };
58
-
59
- export const validate = (meta: Metadata): string[] => {
60
- const errors: string[] = [];
61
- const insert = (msg: string) => errors.push(msg);
62
-
63
- if (meta.any) insert("do not allow any type");
64
- if (meta.isRequired() === false)
65
- insert("do not allow undefindable type");
66
-
67
- const atomics = CoreMetadataUtil.atomics(meta);
68
- const expected: number =
69
- meta.atomics.length +
70
- meta.templates.length +
71
- meta.constants
72
- .map((c) => c.values.length)
73
- .reduce((a, b) => a + b, 0);
74
- if (meta.size() !== expected || atomics.size === 0)
75
- insert("only atomic or constant types are allowed");
76
- if (atomics.size > 1) insert("do not allow union type");
77
-
78
- return errors;
79
- };
80
25
  }
81
-
82
- const CASTERS = {
83
- boolean: (nullable: boolean) =>
84
- createArrow(nullable)(
85
- ts.factory.createConditionalExpression(
86
- ts.factory.createLogicalOr(
87
- ts.factory.createStrictEquality(
88
- ts.factory.createStringLiteral("false"),
89
- ts.factory.createIdentifier("str"),
90
- ),
91
- ts.factory.createStrictEquality(
92
- ts.factory.createStringLiteral("0"),
93
- ts.factory.createIdentifier("str"),
94
- ),
95
- ),
96
- undefined,
97
- ts.factory.createFalse(),
98
- undefined,
99
- ts.factory.createConditionalExpression(
100
- ts.factory.createLogicalOr(
101
- ts.factory.createStrictEquality(
102
- ts.factory.createStringLiteral("true"),
103
- ts.factory.createIdentifier("str"),
104
- ),
105
- ts.factory.createStrictEquality(
106
- ts.factory.createStringLiteral("1"),
107
- ts.factory.createIdentifier("str"),
108
- ),
109
- ),
110
- undefined,
111
- ts.factory.createTrue(),
112
- undefined,
113
- ts.factory.createIdentifier("str"),
114
- ),
115
- ),
116
- ),
117
- number: (nullable: boolean) =>
118
- createArrow(nullable)(
119
- ts.factory.createCallExpression(
120
- ts.factory.createIdentifier("Number"),
121
- undefined,
122
- [ts.factory.createIdentifier("str")],
123
- ),
124
- ),
125
- bigint: (nullable: boolean) =>
126
- createArrow(nullable)(
127
- ts.factory.createCallExpression(
128
- ts.factory.createIdentifier("BigInt"),
129
- undefined,
130
- [ts.factory.createIdentifier("str")],
131
- ),
132
- ),
133
- string: (nullable: boolean) =>
134
- createArrow(nullable)(ts.factory.createIdentifier("str")),
135
- };
136
-
137
- const createArrow = (nullable: boolean) => (expr: ts.Expression) =>
138
- ts.factory.createArrowFunction(
139
- undefined,
140
- undefined,
141
- [IdentifierFactory.parameter("str")],
142
- undefined,
143
- undefined,
144
- nullable
145
- ? ts.factory.createConditionalExpression(
146
- ts.factory.createStrictEquality(
147
- ts.factory.createStringLiteral("null"),
148
- ts.factory.createIdentifier("str"),
149
- ),
150
- undefined,
151
- ts.factory.createNull(),
152
- undefined,
153
- expr,
154
- )
155
- : expr,
156
- );
@@ -1,265 +1,52 @@
1
1
  import ts from "typescript";
2
2
 
3
- import { IdentifierFactory } from "typia/lib/factories/IdentifierFactory";
4
- import { MetadataCollection } from "typia/lib/factories/MetadataCollection";
5
- import { MetadataFactory } from "typia/lib/factories/MetadataFactory";
6
- import { StatementFactory } from "typia/lib/factories/StatementFactory";
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 { HttpAssertQueryProgrammer } from "typia/lib/programmers/http/HttpAssertQueryProgrammer";
4
+ import { HttpIsQueryProgrammer } from "typia/lib/programmers/http/HttpIsQueryProgrammer";
5
+ import { HttpValidateQueryProgrammer } from "typia/lib/programmers/http/HttpValidateQueryProgrammer";
6
+ import { IProject } from "typia/lib/transformers/IProject";
16
7
 
17
8
  import { INestiaTransformProject } from "../options/INestiaTransformProject";
18
- import { CoreMetadataUtil } from "./internal/CoreMetadataUtil";
9
+ import { IRequestQueryValidator } from "../options/IRequestQueryValidator";
19
10
 
20
11
  export namespace TypedQueryProgrammer {
21
12
  export const generate =
22
13
  (project: INestiaTransformProject) =>
23
14
  (modulo: ts.LeftHandSideExpression) =>
24
15
  (type: ts.Type): ts.Expression => {
25
- const object: MetadataObject = getObject(project.checker)(type);
26
- return decode(project, modulo)(type, object);
27
- };
28
-
29
- export const validate = (
30
- meta: Metadata,
31
- explore: MetadataFactory.IExplore,
32
- ): string[] => {
33
- const errors: string[] = [];
34
- const insert = (msg: string) => errors.push(msg);
35
-
36
- if (explore.top === true) {
37
- // TOP MUST BE ONLY OBJECT
38
- if (meta.objects.length !== 1 || meta.bucket() !== 1)
39
- insert("only one object type is allowed.");
40
- if (meta.nullable === true)
41
- insert("query parameters cannot be null.");
42
- if (meta.isRequired() === false)
43
- insert("query parameters cannot be undefined.");
44
- } else if (
45
- explore.nested !== null &&
46
- explore.nested instanceof MetadataArray
47
- ) {
48
- const atomics = CoreMetadataUtil.atomics(meta);
49
- const expected: number =
50
- meta.atomics.length +
51
- meta.templates.length +
52
- meta.constants
53
- .map((c) => c.values.length)
54
- .reduce((a, b) => a + b, 0);
55
- if (atomics.size > 1) insert("union type is not allowed in array.");
56
- if (meta.nullable) insert("nullable type is not allowed in array.");
57
- if (meta.isRequired() === false)
58
- insert("optional type is not allowed in array.");
59
- if (meta.size() !== expected)
60
- insert("only atomic or constant types are allowed in array.");
61
- } else if (explore.object && explore.property !== null) {
62
- //----
63
- // COMMON
64
- //----
65
- // PROPERTY MUST BE SOLE
66
- if (typeof explore.property === "object")
67
- insert("dynamic property is not allowed.");
68
- // DO NOT ALLOW TUPLE TYPE
69
- if (meta.tuples.length) insert("tuple type is not allowed.");
70
- // DO NOT ALLOW UNION TYPE
71
- if (CoreMetadataUtil.isUnion(meta))
72
- insert("union type is not allowed.");
73
- // DO NOT ALLOW NESTED OBJECT
74
- if (
75
- meta.objects.length ||
76
- meta.sets.length ||
77
- meta.maps.length ||
78
- meta.natives.length
79
- )
80
- insert("nested object type is not allowed.");
81
-
82
- //----
83
- // ARRAY CASES
84
- //----
85
- const isArray: boolean =
86
- meta.arrays.length > 1 || meta.tuples.length > 1;
87
- // ARRAY TYPE MUST BE REQUIRED
88
- if (isArray && meta.isRequired() === false)
89
- insert("optional type is not allowed when array.");
90
- // SET-COOKIE MUST BE ARRAY
91
- if (explore.property === "set-cookie" && !isArray)
92
- insert("set-cookie property must be array.");
93
- }
94
- return errors;
95
- };
96
-
97
- const getObject =
98
- (checker: ts.TypeChecker) =>
99
- (type: ts.Type): MetadataObject => {
100
- const collection: MetadataCollection = new MetadataCollection();
101
- const result = MetadataFactory.analyze(checker)({
102
- escape: false,
103
- constant: true,
104
- absorb: true,
105
- validate,
106
- })(collection)(type);
107
- if (result.success === false)
108
- throw TransformerError.from("@core.nestia.TypedHeaders")(
109
- result.errors,
110
- );
111
- return result.data.objects[0]!;
112
- };
113
-
114
- const decode =
115
- (project: INestiaTransformProject, modulo: ts.LeftHandSideExpression) =>
116
- (type: ts.Type, object: MetadataObject): ts.ArrowFunction =>
117
- ts.factory.createArrowFunction(
118
- undefined,
119
- undefined,
120
- [IdentifierFactory.parameter("input")],
121
- undefined,
122
- undefined,
123
- decode_object(project, modulo)(type, object),
124
- );
125
-
126
- const decode_object =
127
- (project: INestiaTransformProject, modulo: ts.LeftHandSideExpression) =>
128
- (type: ts.Type, object: MetadataObject): ts.ConciseBody => {
129
- const assert: ts.ArrowFunction = AssertProgrammer.write({
130
- ...project,
131
- options: {
132
- numeric: true,
133
- finite: true,
134
- },
135
- })(modulo)(false)(type);
136
- const output: ts.Identifier = ts.factory.createIdentifier("output");
137
-
138
- const importer: FunctionImporter = new FunctionImporter(
139
- "TypedQuery",
140
- );
141
- const optionalArrays: string[] = [];
142
- const statements: ts.Statement[] = [
143
- StatementFactory.constant(
144
- "output",
145
- ts.factory.createObjectLiteralExpression(
146
- object.properties.map((prop) => {
147
- if (
148
- !prop.value.isRequired() &&
149
- prop.value.arrays.length +
150
- prop.value.tuples.length >
151
- 0
152
- )
153
- optionalArrays.push(
154
- prop.key.constants[0]!.values[0] as string,
155
- );
156
- return decode_regular_property(importer)(prop);
157
- }),
158
- true,
159
- ),
160
- ),
161
- ...optionalArrays.map((key) => {
162
- const access = IdentifierFactory.access(output)(key);
163
- return ts.factory.createIfStatement(
164
- ts.factory.createStrictEquality(
165
- ts.factory.createNumericLiteral(0),
166
- IdentifierFactory.access(access)("length"),
16
+ // GENERATE VALIDATION PLAN
17
+ const parameter =
18
+ (key: IRequestQueryValidator<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),
167
30
  ),
168
- ts.factory.createExpressionStatement(
169
- ts.factory.createDeleteExpression(access),
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),
170
41
  ),
171
- );
172
- }),
173
- ts.factory.createReturnStatement(
174
- ts.factory.createCallExpression(assert, undefined, [
175
- output,
176
- ]),
177
- ),
178
- ];
179
-
180
- return ts.factory.createBlock(
181
- [...importer.declare(modulo), ...statements],
182
- true,
183
- );
184
- };
185
-
186
- const decode_regular_property =
187
- (importer: FunctionImporter) =>
188
- (property: MetadataProperty): ts.PropertyAssignment => {
189
- const key: string = property.key.constants[0]!.values[0] as string;
190
- const value: Metadata = property.value;
191
-
192
- const [type, isArray]: [Atomic.Literal, boolean] = value.atomics
193
- .length
194
- ? [value.atomics[0].type, false]
195
- : value.constants.length
196
- ? [value.constants[0]!.type, false]
197
- : (() => {
198
- const meta =
199
- value.arrays[0]?.type.value ??
200
- value.tuples[0].type.elements[0];
201
- return meta.atomics.length
202
- ? [meta.atomics[0].type, true]
203
- : [meta.constants[0]!.type, true];
204
- })();
205
- return ts.factory.createPropertyAssignment(
206
- Escaper.variable(key)
207
- ? key
208
- : ts.factory.createStringLiteral(key),
209
- isArray
210
- ? ts.factory.createCallExpression(
211
- IdentifierFactory.access(
212
- ts.factory.createCallExpression(
213
- ts.factory.createIdentifier("input.getAll"),
214
- undefined,
215
- [ts.factory.createStringLiteral(key)],
216
- ),
217
- )("map"),
218
- undefined,
219
- [
220
- ts.factory.createArrowFunction(
221
- undefined,
222
- undefined,
223
- [IdentifierFactory.parameter("elem")],
224
- undefined,
225
- undefined,
226
- decode_value(importer)(type)(false)(
227
- ts.factory.createIdentifier("elem"),
228
- ),
229
- ),
230
- ],
231
- )
232
- : decode_value(importer)(type)(
233
- value.nullable === false &&
234
- value.isRequired() === false,
235
- )(
236
- ts.factory.createCallExpression(
237
- ts.factory.createIdentifier("input.get"),
238
- undefined,
239
- [ts.factory.createStringLiteral(key)],
240
- ),
241
- ),
242
- );
243
- };
244
-
245
- const decode_value =
246
- (importer: FunctionImporter) =>
247
- (type: Atomic.Literal) =>
248
- (onlyUndefindable: boolean) =>
249
- (value: ts.Expression) => {
250
- const call = ts.factory.createCallExpression(
251
- importer.use(type),
252
- undefined,
253
- [value],
254
- );
255
- return onlyUndefindable
256
- ? ts.factory.createBinaryExpression(
257
- call,
258
- ts.factory.createToken(
259
- ts.SyntaxKind.QuestionQuestionToken,
260
- ),
261
- ts.factory.createIdentifier("undefined"),
262
- )
263
- : call;
42
+ ]);
43
+
44
+ // RETURNS
45
+ const category = project.options.validate;
46
+ if (category === "is" || category === "equals")
47
+ return parameter("is")(HttpIsQueryProgrammer.write);
48
+ else if (category === "validate" || category === "validateEquals")
49
+ return parameter("validate")(HttpValidateQueryProgrammer.write);
50
+ return parameter("assert")(HttpAssertQueryProgrammer.write);
264
51
  };
265
52
  }