typia 5.1.0-dev.20230924 → 5.1.0-dev.20230925

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 (55) hide show
  1. package/README.md +1 -1
  2. package/lib/functional/$HeadersReader.d.ts +3 -3
  3. package/lib/functional/$HeadersReader.js +21 -3
  4. package/lib/functional/$HeadersReader.js.map +1 -1
  5. package/lib/functional/$ParameterReader.d.ts +6 -0
  6. package/lib/functional/$ParameterReader.js +35 -0
  7. package/lib/functional/$ParameterReader.js.map +1 -0
  8. package/lib/functional/$QueryReader.js +22 -2
  9. package/lib/functional/$QueryReader.js.map +1 -1
  10. package/lib/functional/Namespace.js +2 -0
  11. package/lib/functional/Namespace.js.map +1 -1
  12. package/lib/http.d.ts +2 -2
  13. package/lib/http.js +2 -0
  14. package/lib/http.js.map +1 -1
  15. package/lib/programmers/http/HttpHeadersProgrammer.js +30 -46
  16. package/lib/programmers/http/HttpHeadersProgrammer.js.map +1 -1
  17. package/lib/programmers/http/HttpParameterProgrammer.js +10 -29
  18. package/lib/programmers/http/HttpParameterProgrammer.js.map +1 -1
  19. package/lib/programmers/http/HttpQueryProgrammer.js +15 -11
  20. package/lib/programmers/http/HttpQueryProgrammer.js.map +1 -1
  21. package/package.json +1 -1
  22. package/src/factories/MetadataCollection.ts +274 -274
  23. package/src/functional/$HeadersReader.ts +28 -9
  24. package/src/functional/$ParameterReader.ts +31 -0
  25. package/src/functional/$QueryReader.ts +53 -32
  26. package/src/functional/Namespace.ts +2 -0
  27. package/src/http.ts +1149 -1141
  28. package/src/programmers/helpers/HttpMetadataUtil.ts +21 -21
  29. package/src/programmers/http/HttpAssertHeadersProgrammer.ts +77 -77
  30. package/src/programmers/http/HttpAssertQueryProgrammer.ts +77 -77
  31. package/src/programmers/http/HttpHeadersProgrammer.ts +328 -321
  32. package/src/programmers/http/HttpIsHeadersProgrammer.ts +87 -87
  33. package/src/programmers/http/HttpIsQueryProgrammer.ts +87 -87
  34. package/src/programmers/http/HttpParameterProgrammer.ts +104 -168
  35. package/src/programmers/http/HttpQueryProgrammer.ts +286 -267
  36. package/src/programmers/http/HttpValidateHeadersProgrammer.ts +77 -77
  37. package/src/programmers/http/HttpValidateQueryProgrammer.ts +77 -77
  38. package/src/transformers/features/http/CreateHttpAssertHeadersTransformer.ts +12 -12
  39. package/src/transformers/features/http/CreateHttpAssertQueryTransformer.ts +12 -12
  40. package/src/transformers/features/http/CreateHttpHeadersTransformer.ts +9 -9
  41. package/src/transformers/features/http/CreateHttpIsHeadersTransformer.ts +9 -9
  42. package/src/transformers/features/http/CreateHttpIsQueryTransformer.ts +9 -9
  43. package/src/transformers/features/http/CreateHttpParameterTransformer.ts +9 -9
  44. package/src/transformers/features/http/CreateHttpQueryTransformer.ts +9 -9
  45. package/src/transformers/features/http/CreateHttpValidateHeadersTransformer copy.ts +12 -12
  46. package/src/transformers/features/http/CreateHttpValidateQueryTransformer.ts +12 -12
  47. package/src/transformers/features/http/HttpAssertHeadersTransformer.ts +10 -10
  48. package/src/transformers/features/http/HttpAssertQueryTransformer.ts +10 -10
  49. package/src/transformers/features/http/HttpHeadersTransformer.ts +9 -9
  50. package/src/transformers/features/http/HttpIsHeadersTransformer.ts +9 -9
  51. package/src/transformers/features/http/HttpIsQueryTransformer.ts +9 -9
  52. package/src/transformers/features/http/HttpParameterTransformer.ts +9 -9
  53. package/src/transformers/features/http/HttpQueryTransformer.ts +9 -9
  54. package/src/transformers/features/http/HttpValidateHeadersTransformer.ts +10 -10
  55. package/src/transformers/features/http/HttpValidateQueryTransformer.ts +10 -10
@@ -1,267 +1,286 @@
1
- import ts from "typescript";
2
-
3
- import { IdentifierFactory } from "../../factories/IdentifierFactory";
4
- import { MetadataCollection } from "../../factories/MetadataCollection";
5
- import { MetadataFactory } from "../../factories/MetadataFactory";
6
- import { StatementFactory } from "../../factories/StatementFactory";
7
- import { TypeFactory } from "../../factories/TypeFactory";
8
-
9
- import { Metadata } from "../../schemas/metadata/Metadata";
10
- import { MetadataArray } from "../../schemas/metadata/MetadataArray";
11
- import { MetadataObject } from "../../schemas/metadata/MetadataObject";
12
- import { MetadataProperty } from "../../schemas/metadata/MetadataProperty";
13
-
14
- import { IProject } from "../../transformers/IProject";
15
- import { TransformerError } from "../../transformers/TransformerError";
16
-
17
- import { Atomic } from "../../typings/Atomic";
18
-
19
- import { Escaper } from "../../utils/Escaper";
20
-
21
- import { FunctionImporter } from "../helpers/FunctionImporeter";
22
- import { HttpMetadataUtil } from "../helpers/HttpMetadataUtil";
23
-
24
- export namespace HttpQueryProgrammer {
25
- export const INPUT_TYPE = "string | URLSearchParams";
26
-
27
- export const write =
28
- (project: IProject) =>
29
- (modulo: ts.LeftHandSideExpression) =>
30
- (type: ts.Type, name?: string): ts.ArrowFunction => {
31
- // GET OBJECT TYPE
32
- const importer: FunctionImporter = new FunctionImporter(
33
- modulo.getText(),
34
- );
35
- const collection: MetadataCollection = new MetadataCollection();
36
- const result = MetadataFactory.analyze(project.checker)({
37
- escape: false,
38
- constant: true,
39
- absorb: true,
40
- validate,
41
- })(collection)(type);
42
- if (result.success === false)
43
- throw TransformerError.from(`typia.http.${importer.method}`)(
44
- result.errors,
45
- );
46
-
47
- // DO TRANSFORM
48
- const object: MetadataObject = result.data.objects[0]!;
49
- const statements: ts.Statement[] = decode_object(importer)(object);
50
- return ts.factory.createArrowFunction(
51
- undefined,
52
- undefined,
53
- [
54
- IdentifierFactory.parameter(
55
- "input",
56
- ts.factory.createTypeReferenceNode(INPUT_TYPE),
57
- ),
58
- ],
59
- ts.factory.createTypeReferenceNode(
60
- `typia.Resolved<${
61
- name ?? TypeFactory.getFullName(project.checker)(type)
62
- }>`,
63
- ),
64
- undefined,
65
- ts.factory.createBlock(
66
- [...importer.declare(modulo), ...statements],
67
- true,
68
- ),
69
- );
70
- };
71
-
72
- const validate = (
73
- meta: Metadata,
74
- explore: MetadataFactory.IExplore,
75
- ): string[] => {
76
- const errors: string[] = [];
77
- const insert = (msg: string) => errors.push(msg);
78
-
79
- if (explore.top === true) {
80
- // TOP MUST BE ONLY OBJECT
81
- if (meta.objects.length !== 1 || meta.bucket() !== 1)
82
- insert("only one object type is allowed.");
83
- if (meta.nullable === true)
84
- insert("query parameters cannot be null.");
85
- if (meta.isRequired() === false)
86
- insert("query parameters cannot be undefined.");
87
- } else if (
88
- explore.nested !== null &&
89
- explore.nested instanceof MetadataArray
90
- ) {
91
- const atomics = HttpMetadataUtil.atomics(meta);
92
- const expected: number =
93
- meta.atomics.length +
94
- meta.templates.length +
95
- meta.constants
96
- .map((c) => c.values.length)
97
- .reduce((a, b) => a + b, 0);
98
- if (atomics.size > 1) insert("union type is not allowed in array.");
99
- if (meta.nullable) insert("nullable type is not allowed in array.");
100
- if (meta.isRequired() === false)
101
- insert("optional type is not allowed in array.");
102
- if (meta.size() !== expected)
103
- insert("only atomic or constant types are allowed in array.");
104
- } else if (explore.object && explore.property !== null) {
105
- //----
106
- // COMMON
107
- //----
108
- // PROPERTY MUST BE SOLE
109
- if (typeof explore.property === "object")
110
- insert("dynamic property is not allowed.");
111
- // DO NOT ALLOW TUPLE TYPE
112
- if (meta.tuples.length) insert("tuple type is not allowed.");
113
- // DO NOT ALLOW UNION TYPE
114
- if (HttpMetadataUtil.isUnion(meta))
115
- insert("union type is not allowed.");
116
- // DO NOT ALLOW NESTED OBJECT
117
- if (
118
- meta.objects.length ||
119
- meta.sets.length ||
120
- meta.maps.length ||
121
- meta.natives.length
122
- )
123
- insert("nested object type is not allowed.");
124
-
125
- //----
126
- // ARRAY CASES
127
- //----
128
- const isArray: boolean =
129
- meta.arrays.length > 1 || meta.tuples.length > 1;
130
- // ARRAY TYPE MUST BE REQUIRED
131
- if (isArray && meta.isRequired() === false)
132
- insert("optional type is not allowed when array.");
133
- // SET-COOKIE MUST BE ARRAY
134
- if (explore.property === "set-cookie" && !isArray)
135
- insert("set-cookie property must be array.");
136
- }
137
- return errors;
138
- };
139
-
140
- const decode_object =
141
- (importer: FunctionImporter) =>
142
- (object: MetadataObject): ts.Statement[] => {
143
- const input: ts.Identifier = ts.factory.createIdentifier("input");
144
- const output: ts.Identifier = ts.factory.createIdentifier("output");
145
- const optionals: string[] = [];
146
- return [
147
- ts.factory.createExpressionStatement(
148
- ts.factory.createCallExpression(
149
- importer.use("params"),
150
- undefined,
151
- [input],
152
- ),
153
- ),
154
- StatementFactory.constant(
155
- "output",
156
- ts.factory.createObjectLiteralExpression(
157
- object.properties.map((prop) => {
158
- if (
159
- !prop.value.isRequired() &&
160
- prop.value.arrays.length +
161
- prop.value.tuples.length >
162
- 0
163
- )
164
- optionals.push(
165
- prop.key.constants[0]!.values[0] as string,
166
- );
167
- return decode_regular_property(importer)(prop);
168
- }),
169
- true,
170
- ),
171
- ),
172
- ...optionals.map((key) => {
173
- const access = IdentifierFactory.access(output)(key);
174
- return ts.factory.createIfStatement(
175
- ts.factory.createStrictEquality(
176
- ts.factory.createNumericLiteral(0),
177
- IdentifierFactory.access(access)("length"),
178
- ),
179
- ts.factory.createExpressionStatement(
180
- ts.factory.createDeleteExpression(access),
181
- ),
182
- );
183
- }),
184
- ts.factory.createReturnStatement(output),
185
- ];
186
- };
187
-
188
- const decode_regular_property =
189
- (importer: FunctionImporter) =>
190
- (property: MetadataProperty): ts.PropertyAssignment => {
191
- const key: string = property.key.constants[0]!.values[0] as string;
192
- const value: Metadata = property.value;
193
-
194
- const [type, isArray]: [Atomic.Literal, boolean] = value.atomics
195
- .length
196
- ? [value.atomics[0]!.type, false]
197
- : value.constants.length
198
- ? [value.constants[0]!.type, false]
199
- : (() => {
200
- const meta =
201
- value.arrays[0]?.type.value ??
202
- value.tuples[0]!.type.elements[0]!;
203
- return meta.atomics.length
204
- ? [meta.atomics[0]!.type, true]
205
- : [meta.constants[0]!.type, true];
206
- })();
207
- return ts.factory.createPropertyAssignment(
208
- Escaper.variable(key)
209
- ? key
210
- : ts.factory.createStringLiteral(key),
211
- isArray
212
- ? ts.factory.createCallExpression(
213
- IdentifierFactory.access(
214
- ts.factory.createCallExpression(
215
- ts.factory.createIdentifier("input.getAll"),
216
- undefined,
217
- [ts.factory.createStringLiteral(key)],
218
- ),
219
- )("map"),
220
- undefined,
221
- [
222
- ts.factory.createArrowFunction(
223
- undefined,
224
- undefined,
225
- [IdentifierFactory.parameter("elem")],
226
- undefined,
227
- undefined,
228
- decode_value(importer)(type)(false)(
229
- ts.factory.createIdentifier("elem"),
230
- ),
231
- ),
232
- ],
233
- )
234
- : decode_value(importer)(type)(
235
- value.nullable === false &&
236
- value.isRequired() === false,
237
- )(
238
- ts.factory.createCallExpression(
239
- ts.factory.createIdentifier("input.get"),
240
- undefined,
241
- [ts.factory.createStringLiteral(key)],
242
- ),
243
- ),
244
- );
245
- };
246
-
247
- const decode_value =
248
- (importer: FunctionImporter) =>
249
- (type: Atomic.Literal) =>
250
- (onlyUndefindable: boolean) =>
251
- (value: ts.Expression) => {
252
- const call = ts.factory.createCallExpression(
253
- importer.use(type),
254
- undefined,
255
- [value],
256
- );
257
- return onlyUndefindable
258
- ? ts.factory.createBinaryExpression(
259
- call,
260
- ts.factory.createToken(
261
- ts.SyntaxKind.QuestionQuestionToken,
262
- ),
263
- ts.factory.createIdentifier("undefined"),
264
- )
265
- : call;
266
- };
267
- }
1
+ import ts from "typescript";
2
+
3
+ import { IdentifierFactory } from "../../factories/IdentifierFactory";
4
+ import { MetadataCollection } from "../../factories/MetadataCollection";
5
+ import { MetadataFactory } from "../../factories/MetadataFactory";
6
+ import { StatementFactory } from "../../factories/StatementFactory";
7
+ import { TypeFactory } from "../../factories/TypeFactory";
8
+
9
+ import { Metadata } from "../../schemas/metadata/Metadata";
10
+ import { MetadataArrayType } from "../../schemas/metadata/MetadataArrayType";
11
+ import { MetadataObject } from "../../schemas/metadata/MetadataObject";
12
+ import { MetadataProperty } from "../../schemas/metadata/MetadataProperty";
13
+
14
+ import { IProject } from "../../transformers/IProject";
15
+ import { TransformerError } from "../../transformers/TransformerError";
16
+
17
+ import { Atomic } from "../../typings/Atomic";
18
+
19
+ import { Escaper } from "../../utils/Escaper";
20
+
21
+ import { FunctionImporter } from "../helpers/FunctionImporeter";
22
+ import { HttpMetadataUtil } from "../helpers/HttpMetadataUtil";
23
+
24
+ export namespace HttpQueryProgrammer {
25
+ export const INPUT_TYPE = "string | URLSearchParams";
26
+
27
+ export const write =
28
+ (project: IProject) =>
29
+ (modulo: ts.LeftHandSideExpression) =>
30
+ (type: ts.Type, name?: string): ts.ArrowFunction => {
31
+ // GET OBJECT TYPE
32
+ const importer: FunctionImporter = new FunctionImporter(
33
+ modulo.getText(),
34
+ );
35
+ const collection: MetadataCollection = new MetadataCollection();
36
+ const result = MetadataFactory.analyze(project.checker)({
37
+ escape: false,
38
+ constant: true,
39
+ absorb: true,
40
+ validate,
41
+ })(collection)(type);
42
+ if (result.success === false)
43
+ throw TransformerError.from(`typia.http.${importer.method}`)(
44
+ result.errors,
45
+ );
46
+
47
+ // DO TRANSFORM
48
+ const object: MetadataObject = result.data.objects[0]!;
49
+ const statements: ts.Statement[] = decode_object(importer)(object);
50
+ return ts.factory.createArrowFunction(
51
+ undefined,
52
+ undefined,
53
+ [
54
+ IdentifierFactory.parameter(
55
+ "input",
56
+ ts.factory.createTypeReferenceNode(INPUT_TYPE),
57
+ ),
58
+ ],
59
+ ts.factory.createTypeReferenceNode(
60
+ `typia.Resolved<${
61
+ name ?? TypeFactory.getFullName(project.checker)(type)
62
+ }>`,
63
+ ),
64
+ undefined,
65
+ ts.factory.createBlock(
66
+ [...importer.declare(modulo), ...statements],
67
+ true,
68
+ ),
69
+ );
70
+ };
71
+
72
+ const validate = (
73
+ meta: Metadata,
74
+ explore: MetadataFactory.IExplore,
75
+ ): string[] => {
76
+ const errors: string[] = [];
77
+ const insert = (msg: string) => errors.push(msg);
78
+
79
+ if (explore.top === true) {
80
+ // TOP MUST BE ONLY OBJECT
81
+ if (meta.objects.length !== 1 || meta.bucket() !== 1)
82
+ insert("only one object type is allowed.");
83
+ if (meta.nullable === true)
84
+ insert("query parameters cannot be null.");
85
+ if (meta.isRequired() === false)
86
+ insert("query parameters cannot be undefined.");
87
+ } else if (
88
+ explore.nested !== null &&
89
+ explore.nested instanceof MetadataArrayType
90
+ ) {
91
+ const atomics = HttpMetadataUtil.atomics(meta);
92
+ const expected: number =
93
+ meta.atomics.length +
94
+ meta.templates.length +
95
+ meta.constants
96
+ .map((c) => c.values.length)
97
+ .reduce((a, b) => a + b, 0);
98
+ if (atomics.size > 1) insert("union type is not allowed in array.");
99
+ if (meta.nullable) insert("nullable type is not allowed in array.");
100
+ if (meta.isRequired() === false)
101
+ insert("optional type is not allowed in array.");
102
+ if (meta.size() !== expected)
103
+ insert("only atomic or constant types are allowed in array.");
104
+ } else if (explore.object && explore.property !== null) {
105
+ //----
106
+ // COMMON
107
+ //----
108
+ // PROPERTY MUST BE SOLE
109
+ if (typeof explore.property === "object")
110
+ insert("dynamic property is not allowed.");
111
+ // DO NOT ALLOW TUPLE TYPE
112
+ if (meta.tuples.length) insert("tuple type is not allowed.");
113
+ // DO NOT ALLOW UNION TYPE
114
+ if (HttpMetadataUtil.isUnion(meta))
115
+ insert("union type is not allowed.");
116
+ // DO NOT ALLOW NESTED OBJECT
117
+ if (
118
+ meta.objects.length ||
119
+ meta.sets.length ||
120
+ meta.maps.length ||
121
+ meta.natives.length
122
+ )
123
+ insert("nested object type is not allowed.");
124
+
125
+ //----
126
+ // ARRAY CASES
127
+ //----
128
+ const isArray: boolean =
129
+ meta.arrays.length > 1 || meta.tuples.length > 1;
130
+ // ARRAY TYPE MUST BE REQUIRED
131
+ if (isArray && meta.isRequired() === false)
132
+ insert("optional type is not allowed when array.");
133
+ // SET-COOKIE MUST BE ARRAY
134
+ if (explore.property === "set-cookie" && !isArray)
135
+ insert("set-cookie property must be array.");
136
+ }
137
+ return errors;
138
+ };
139
+
140
+ const decode_object =
141
+ (importer: FunctionImporter) =>
142
+ (object: MetadataObject): ts.Statement[] => {
143
+ const input: ts.Identifier = ts.factory.createIdentifier("input");
144
+ const output: ts.Identifier = ts.factory.createIdentifier("output");
145
+ const optionals: string[] = [];
146
+
147
+ return [
148
+ ts.factory.createExpressionStatement(
149
+ ts.factory.createBinaryExpression(
150
+ input,
151
+ ts.factory.createToken(ts.SyntaxKind.EqualsToken),
152
+ ts.factory.createAsExpression(
153
+ ts.factory.createCallExpression(
154
+ importer.use("params"),
155
+ undefined,
156
+ [input],
157
+ ),
158
+ ts.factory.createTypeReferenceNode(
159
+ "URLSearchParams",
160
+ ),
161
+ ),
162
+ ),
163
+ ),
164
+ StatementFactory.constant(
165
+ "output",
166
+ ts.factory.createObjectLiteralExpression(
167
+ object.properties.map((prop) => {
168
+ if (
169
+ !prop.value.isRequired() &&
170
+ prop.value.arrays.length +
171
+ prop.value.tuples.length >
172
+ 0
173
+ )
174
+ optionals.push(
175
+ prop.key.constants[0]!.values[0] as string,
176
+ );
177
+ return decode_regular_property(importer)(prop);
178
+ }),
179
+ true,
180
+ ),
181
+ ),
182
+ ...optionals.map((key) => {
183
+ const access = IdentifierFactory.access(output)(key);
184
+ return ts.factory.createIfStatement(
185
+ ts.factory.createStrictEquality(
186
+ ts.factory.createNumericLiteral(0),
187
+ IdentifierFactory.access(access)("length"),
188
+ ),
189
+ ts.factory.createExpressionStatement(
190
+ ts.factory.createDeleteExpression(access),
191
+ ),
192
+ );
193
+ }),
194
+ ts.factory.createReturnStatement(
195
+ ts.factory.createAsExpression(
196
+ output,
197
+ TypeFactory.keyword("any"),
198
+ ),
199
+ ),
200
+ ];
201
+ };
202
+
203
+ const decode_regular_property =
204
+ (importer: FunctionImporter) =>
205
+ (property: MetadataProperty): ts.PropertyAssignment => {
206
+ const key: string = property.key.constants[0]!.values[0] as string;
207
+ const value: Metadata = property.value;
208
+
209
+ const [type, isArray]: [Atomic.Literal, boolean] = value.atomics
210
+ .length
211
+ ? [value.atomics[0]!.type, false]
212
+ : value.constants.length
213
+ ? [value.constants[0]!.type, false]
214
+ : value.templates.length
215
+ ? ["string", false]
216
+ : (() => {
217
+ const meta =
218
+ value.arrays[0]?.type.value ??
219
+ value.tuples[0]!.type.elements[0]!;
220
+ return meta.atomics.length
221
+ ? [meta.atomics[0]!.type, true]
222
+ : meta.templates.length
223
+ ? ["string", true]
224
+ : [meta.constants[0]!.type, true];
225
+ })();
226
+ return ts.factory.createPropertyAssignment(
227
+ Escaper.variable(key)
228
+ ? key
229
+ : ts.factory.createStringLiteral(key),
230
+ isArray
231
+ ? ts.factory.createCallExpression(
232
+ IdentifierFactory.access(
233
+ ts.factory.createCallExpression(
234
+ ts.factory.createIdentifier("input.getAll"),
235
+ undefined,
236
+ [ts.factory.createStringLiteral(key)],
237
+ ),
238
+ )("map"),
239
+ undefined,
240
+ [
241
+ ts.factory.createArrowFunction(
242
+ undefined,
243
+ undefined,
244
+ [IdentifierFactory.parameter("elem")],
245
+ undefined,
246
+ undefined,
247
+ decode_value(importer)(type)(false)(
248
+ ts.factory.createIdentifier("elem"),
249
+ ),
250
+ ),
251
+ ],
252
+ )
253
+ : decode_value(importer)(type)(
254
+ value.nullable === false &&
255
+ value.isRequired() === false,
256
+ )(
257
+ ts.factory.createCallExpression(
258
+ ts.factory.createIdentifier("input.get"),
259
+ undefined,
260
+ [ts.factory.createStringLiteral(key)],
261
+ ),
262
+ ),
263
+ );
264
+ };
265
+
266
+ const decode_value =
267
+ (importer: FunctionImporter) =>
268
+ (type: Atomic.Literal) =>
269
+ (onlyUndefindable: boolean) =>
270
+ (value: ts.Expression) => {
271
+ const call = ts.factory.createCallExpression(
272
+ importer.use(type),
273
+ undefined,
274
+ [value],
275
+ );
276
+ return onlyUndefindable
277
+ ? ts.factory.createBinaryExpression(
278
+ call,
279
+ ts.factory.createToken(
280
+ ts.SyntaxKind.QuestionQuestionToken,
281
+ ),
282
+ ts.factory.createIdentifier("undefined"),
283
+ )
284
+ : call;
285
+ };
286
+ }