typia 5.1.4 → 5.1.5

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 (88) hide show
  1. package/lib/TypeGuardError.d.ts +2 -2
  2. package/lib/executable/TypiaGenerateWizard.js.map +1 -1
  3. package/lib/executable/TypiaSetupWizard.js +16 -7
  4. package/lib/executable/TypiaSetupWizard.js.map +1 -1
  5. package/lib/factories/internal/metadata/emplace_metadata_object.js +25 -6
  6. package/lib/factories/internal/metadata/emplace_metadata_object.js.map +1 -1
  7. package/lib/programmers/CheckerProgrammer.d.ts +3 -3
  8. package/lib/programmers/CheckerProgrammer.js.map +1 -1
  9. package/lib/programmers/FeatureProgrammer.d.ts +11 -11
  10. package/lib/programmers/FeatureProgrammer.js.map +1 -1
  11. package/lib/schemas/json/IJsonComponents.d.ts +12 -12
  12. package/lib/schemas/json/IJsonSchema.d.ts +32 -32
  13. package/lib/schemas/metadata/IMetadataObject.d.ts +1 -1
  14. package/lib/transformers/ITransformOptions.d.ts +4 -4
  15. package/package.json +1 -1
  16. package/src/TypeGuardError.ts +2 -2
  17. package/src/executable/TypiaGenerateWizard.ts +2 -2
  18. package/src/executable/TypiaSetupWizard.ts +7 -3
  19. package/src/factories/MetadataCollection.ts +277 -277
  20. package/src/factories/MetadataFactory.ts +238 -238
  21. package/src/factories/MetadataTypeTagFactory.ts +325 -325
  22. package/src/factories/internal/metadata/emend_metadata_atomics.ts +41 -41
  23. package/src/factories/internal/metadata/emplace_metadata_object.ts +30 -8
  24. package/src/factories/internal/metadata/iterate_metadata_intersection.ts +259 -259
  25. package/src/functional/$HeadersReader.ts +28 -28
  26. package/src/functional/$ParameterReader.ts +31 -31
  27. package/src/functional/$QueryReader.ts +56 -56
  28. package/src/functional/Namespace.ts +142 -142
  29. package/src/http.ts +1149 -1149
  30. package/src/json.ts +648 -648
  31. package/src/misc.ts +651 -651
  32. package/src/module.ts +657 -657
  33. package/src/programmers/CheckerProgrammer.ts +11 -9
  34. package/src/programmers/FeatureProgrammer.ts +25 -17
  35. package/src/programmers/helpers/HttpMetadataUtil.ts +21 -21
  36. package/src/programmers/helpers/RandomRanger.ts +1 -1
  37. package/src/programmers/http/HttpAssertHeadersProgrammer.ts +77 -77
  38. package/src/programmers/http/HttpAssertQueryProgrammer.ts +77 -77
  39. package/src/programmers/http/HttpHeadersProgrammer.ts +339 -339
  40. package/src/programmers/http/HttpIsHeadersProgrammer.ts +87 -87
  41. package/src/programmers/http/HttpIsQueryProgrammer.ts +87 -87
  42. package/src/programmers/http/HttpParameterProgrammer.ts +104 -104
  43. package/src/programmers/http/HttpQueryProgrammer.ts +273 -273
  44. package/src/programmers/http/HttpValidateHeadersProgrammer.ts +77 -77
  45. package/src/programmers/http/HttpValidateQueryProgrammer.ts +77 -77
  46. package/src/programmers/internal/application_boolean.ts +30 -30
  47. package/src/programmers/internal/application_number.ts +90 -90
  48. package/src/programmers/internal/application_object.ts +1 -1
  49. package/src/programmers/internal/application_schema.ts +180 -180
  50. package/src/programmers/internal/application_string.ts +54 -54
  51. package/src/programmers/internal/check_array_length.ts +44 -44
  52. package/src/programmers/internal/check_bigint.ts +48 -48
  53. package/src/programmers/internal/check_number.ts +108 -108
  54. package/src/programmers/internal/check_object.ts +2 -2
  55. package/src/programmers/internal/check_string.ts +48 -48
  56. package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +882 -882
  57. package/src/protobuf.ts +887 -887
  58. package/src/schemas/json/IJsonComponents.ts +36 -34
  59. package/src/schemas/json/IJsonSchema.ts +112 -112
  60. package/src/schemas/metadata/IMetadataConstant.ts +25 -25
  61. package/src/schemas/metadata/IMetadataObject.ts +1 -1
  62. package/src/schemas/metadata/IMetadataTypeTag.ts +8 -8
  63. package/src/schemas/metadata/Metadata.ts +686 -686
  64. package/src/tags/Default.ts +15 -15
  65. package/src/tags/Format.ts +30 -30
  66. package/src/tags/Pattern.ts +9 -9
  67. package/src/tags/TagBase.ts +68 -68
  68. package/src/tags/index.ts +14 -14
  69. package/src/transformers/CallExpressionTransformer.ts +289 -289
  70. package/src/transformers/ITransformOptions.ts +4 -4
  71. package/src/transformers/features/http/CreateHttpAssertHeadersTransformer.ts +12 -12
  72. package/src/transformers/features/http/CreateHttpAssertQueryTransformer.ts +12 -12
  73. package/src/transformers/features/http/CreateHttpHeadersTransformer.ts +9 -9
  74. package/src/transformers/features/http/CreateHttpIsHeadersTransformer.ts +9 -9
  75. package/src/transformers/features/http/CreateHttpIsQueryTransformer.ts +9 -9
  76. package/src/transformers/features/http/CreateHttpParameterTransformer.ts +9 -9
  77. package/src/transformers/features/http/CreateHttpQueryTransformer.ts +9 -9
  78. package/src/transformers/features/http/CreateHttpValidateHeadersTransformer.ts +12 -12
  79. package/src/transformers/features/http/CreateHttpValidateQueryTransformer.ts +12 -12
  80. package/src/transformers/features/http/HttpAssertHeadersTransformer.ts +10 -10
  81. package/src/transformers/features/http/HttpAssertQueryTransformer.ts +10 -10
  82. package/src/transformers/features/http/HttpHeadersTransformer.ts +9 -9
  83. package/src/transformers/features/http/HttpIsHeadersTransformer.ts +9 -9
  84. package/src/transformers/features/http/HttpIsQueryTransformer.ts +9 -9
  85. package/src/transformers/features/http/HttpParameterTransformer.ts +9 -9
  86. package/src/transformers/features/http/HttpQueryTransformer.ts +9 -9
  87. package/src/transformers/features/http/HttpValidateHeadersTransformer.ts +10 -10
  88. package/src/transformers/features/http/HttpValidateQueryTransformer.ts +10 -10
@@ -1,339 +1,339 @@
1
- import ts from "typescript";
2
-
3
- import { ExpressionFactory } from "../../factories/ExpressionFactory";
4
- import { IdentifierFactory } from "../../factories/IdentifierFactory";
5
- import { MetadataCollection } from "../../factories/MetadataCollection";
6
- import { MetadataFactory } from "../../factories/MetadataFactory";
7
- import { StatementFactory } from "../../factories/StatementFactory";
8
- import { TypeFactory } from "../../factories/TypeFactory";
9
-
10
- import { Metadata } from "../../schemas/metadata/Metadata";
11
- import { MetadataArrayType } from "../../schemas/metadata/MetadataArrayType";
12
- import { MetadataObject } from "../../schemas/metadata/MetadataObject";
13
- import { MetadataProperty } from "../../schemas/metadata/MetadataProperty";
14
-
15
- import { IProject } from "../../transformers/IProject";
16
- import { TransformerError } from "../../transformers/TransformerError";
17
-
18
- import { Atomic } from "../../typings/Atomic";
19
-
20
- import { Escaper } from "../../utils/Escaper";
21
- import { MapUtil } from "../../utils/MapUtil";
22
-
23
- import { FunctionImporter } from "../helpers/FunctionImporeter";
24
- import { HttpMetadataUtil } from "../helpers/HttpMetadataUtil";
25
-
26
- export namespace HttpHeadersProgrammer {
27
- export const INPUT_TYPE = "Record<string, string | string[] | undefined>";
28
-
29
- export const write =
30
- (project: IProject) =>
31
- (modulo: ts.LeftHandSideExpression) =>
32
- (type: ts.Type, name?: string): ts.ArrowFunction => {
33
- // GET OBJECT TYPE
34
- const importer: FunctionImporter = new FunctionImporter(
35
- modulo.getText(),
36
- );
37
- const collection: MetadataCollection = new MetadataCollection();
38
- const result = MetadataFactory.analyze(project.checker)({
39
- escape: false,
40
- constant: true,
41
- absorb: true,
42
- validate,
43
- })(collection)(type);
44
- if (result.success === false)
45
- throw TransformerError.from(`typia.http.${importer.method}`)(
46
- result.errors,
47
- );
48
-
49
- // DO TRANSFORM
50
- const object: MetadataObject = result.data.objects[0]!;
51
- const statements: ts.Statement[] = decode_object(importer)(object);
52
- return ts.factory.createArrowFunction(
53
- undefined,
54
- undefined,
55
- [
56
- IdentifierFactory.parameter(
57
- "input",
58
- ts.factory.createTypeReferenceNode(INPUT_TYPE),
59
- ),
60
- ],
61
- ts.factory.createTypeReferenceNode(
62
- `typia.Resolved<${
63
- name ?? TypeFactory.getFullName(project.checker)(type)
64
- }>`,
65
- ),
66
- undefined,
67
- ts.factory.createBlock(
68
- [...importer.declare(modulo), ...statements],
69
- true,
70
- ),
71
- );
72
- };
73
-
74
- export const validate = (
75
- meta: Metadata,
76
- explore: MetadataFactory.IExplore,
77
- ): string[] => {
78
- const errors: string[] = [];
79
- const insert = (msg: string) => errors.push(msg);
80
-
81
- if (explore.top === true) {
82
- // TOP MUST BE ONLY OBJECT
83
- if (meta.objects.length !== 1 || meta.bucket() !== 1)
84
- insert("only one object type is allowed.");
85
- if (meta.nullable === true) insert("headers cannot be null.");
86
- if (meta.isRequired() === false) insert("headers cannot be null.");
87
- } else if (
88
- explore.nested !== null &&
89
- explore.nested instanceof MetadataArrayType
90
- ) {
91
- //----
92
- // ARRAY
93
- //----
94
- const atomics = HttpMetadataUtil.atomics(meta);
95
- const expected: number =
96
- meta.atomics.length +
97
- meta.templates.length +
98
- meta.constants
99
- .map((c) => c.values.length)
100
- .reduce((a, b) => a + b, 0);
101
- if (atomics.size > 1) insert("union type is not allowed in array.");
102
- if (meta.size() !== expected)
103
- insert("only atomic or constant types are allowed in array.");
104
- if (meta.nullable === true)
105
- insert("nullable type is not allowed in array.");
106
- if (meta.isRequired() === false)
107
- insert("optional type is not allowed in array.");
108
- } else if (explore.object && explore.property !== null) {
109
- //----
110
- // COMMON
111
- //----
112
- // PROPERTY MUST BE SOLE
113
- if (typeof explore.property === "object")
114
- insert("dynamic property is not allowed.");
115
- // DO NOT ALLOW TUPLE TYPE
116
- if (meta.tuples.length) insert("tuple type is not allowed.");
117
- // DO NOT ALLOW UNION TYPE
118
- if (HttpMetadataUtil.isUnion(meta))
119
- insert("union type is not allowed.");
120
- // DO NOT ALLOW NESTED OBJECT
121
- if (
122
- meta.objects.length ||
123
- meta.sets.length ||
124
- meta.maps.length ||
125
- meta.natives.length
126
- )
127
- insert("nested object type is not allowed.");
128
- // DO NOT ALLOW NULLABLE
129
- if (meta.nullable === true) insert("nullable type is not allowed.");
130
-
131
- //----
132
- // SPECIAL KEY NAMES
133
- //----
134
- const isArray: boolean =
135
- meta.arrays.length >= 1 || meta.tuples.length >= 1;
136
- // SET-COOKIE MUST BE ARRAY
137
- if (
138
- typeof explore.property === "string" &&
139
- explore.property.toLowerCase() === "set-cookie" &&
140
- isArray === false
141
- )
142
- insert(`${explore.property} property must be array.`);
143
- // MUST BE SINGULAR CASE
144
- if (
145
- typeof explore.property === "string" &&
146
- SINGULAR.has(explore.property.toLowerCase()) &&
147
- isArray === true
148
- )
149
- insert("property cannot be array.");
150
- } else if (explore.object && explore.property === null) {
151
- const counter: Map<string, Set<string>> = new Map();
152
- for (const prop of explore.object.properties) {
153
- const key: string | null = prop.key.getSoleLiteral();
154
- if (key === null) continue;
155
-
156
- MapUtil.take(counter)(key.toLowerCase(), () => new Set()).add(
157
- key,
158
- );
159
- }
160
- for (const [key, set] of counter)
161
- if (set.size > 1)
162
- insert(
163
- `duplicated keys when converting to lowercase letters: [${[
164
- ...set,
165
- ].join(", ")}] -> ${key}`,
166
- );
167
- }
168
- return errors;
169
- };
170
-
171
- const decode_object =
172
- (importer: FunctionImporter) =>
173
- (object: MetadataObject): ts.Statement[] => {
174
- const output: ts.Identifier = ts.factory.createIdentifier("output");
175
- const optionals: string[] = [];
176
- return [
177
- StatementFactory.constant(
178
- "output",
179
- ts.factory.createObjectLiteralExpression(
180
- object.properties.map((prop) => {
181
- if (
182
- !prop.value.isRequired() &&
183
- prop.value.arrays.length +
184
- prop.value.tuples.length >
185
- 0
186
- )
187
- optionals.push(
188
- prop.key.constants[0]!.values[0] as string,
189
- );
190
- return decode_regular_property(importer)(prop);
191
- }),
192
- true,
193
- ),
194
- ),
195
- ...optionals.map((key) => {
196
- const access = IdentifierFactory.access(output)(key);
197
- return ts.factory.createIfStatement(
198
- ts.factory.createStrictEquality(
199
- ts.factory.createNumericLiteral(0),
200
- IdentifierFactory.access(access)("length"),
201
- ),
202
- ts.factory.createExpressionStatement(
203
- ts.factory.createDeleteExpression(access),
204
- ),
205
- );
206
- }),
207
- ts.factory.createReturnStatement(
208
- ts.factory.createAsExpression(
209
- output,
210
- TypeFactory.keyword("any"),
211
- ),
212
- ),
213
- ];
214
- };
215
-
216
- const decode_regular_property =
217
- (importer: FunctionImporter) =>
218
- (property: MetadataProperty): ts.PropertyAssignment => {
219
- const key: string = property.key.constants[0]!.values[0] as string;
220
- const value: Metadata = property.value;
221
-
222
- const [type, isArray]: [Atomic.Literal, boolean] = value.atomics
223
- .length
224
- ? [value.atomics[0]!.type, false]
225
- : value.constants.length
226
- ? [value.constants[0]!.type, false]
227
- : value.templates.length
228
- ? ["string", false]
229
- : (() => {
230
- const meta: Metadata =
231
- value.arrays[0]?.type.value ??
232
- value.tuples[0]!.type.elements[0]!;
233
- return meta.atomics.length
234
- ? [meta.atomics[0]!.type, true]
235
- : meta.templates.length
236
- ? ["string", true]
237
- : [meta.constants[0]!.type, true];
238
- })();
239
- const accessor = IdentifierFactory.access(
240
- ts.factory.createIdentifier("input"),
241
- )(key.toLowerCase());
242
-
243
- return ts.factory.createPropertyAssignment(
244
- Escaper.variable(key)
245
- ? key
246
- : ts.factory.createStringLiteral(key),
247
- isArray
248
- ? key === "set-cookie"
249
- ? accessor
250
- : decode_array(importer)(type)(key)(value)(accessor)
251
- : decode_value(importer)(type)(accessor),
252
- );
253
- };
254
-
255
- const decode_value =
256
- (importer: FunctionImporter) =>
257
- (type: Atomic.Literal) =>
258
- (value: ts.Expression) =>
259
- type === "string"
260
- ? value
261
- : ts.factory.createCallExpression(
262
- importer.use(type),
263
- undefined,
264
- [value],
265
- );
266
-
267
- const decode_array =
268
- (importer: FunctionImporter) =>
269
- (type: Atomic.Literal) =>
270
- (key: string) =>
271
- (value: Metadata) =>
272
- (accessor: ts.Expression) => {
273
- const split: ts.CallChain = ts.factory.createCallChain(
274
- ts.factory.createPropertyAccessChain(
275
- ts.factory.createCallChain(
276
- ts.factory.createPropertyAccessChain(
277
- accessor,
278
- ts.factory.createToken(
279
- ts.SyntaxKind.QuestionDotToken,
280
- ),
281
- ts.factory.createIdentifier("split"),
282
- ),
283
- undefined,
284
- undefined,
285
- [
286
- ts.factory.createStringLiteral(
287
- key === "cookie" ? "; " : ", ",
288
- ),
289
- ],
290
- ),
291
- ts.factory.createToken(ts.SyntaxKind.QuestionDotToken),
292
- ts.factory.createIdentifier("map"),
293
- ),
294
- undefined,
295
- undefined,
296
- [importer.use(type)],
297
- );
298
- return ts.factory.createConditionalExpression(
299
- ExpressionFactory.isArray(accessor),
300
- undefined,
301
- ts.factory.createCallExpression(
302
- IdentifierFactory.access(accessor)("map"),
303
- undefined,
304
- [importer.use(type)],
305
- ),
306
- undefined,
307
- value.isRequired() === false
308
- ? split
309
- : ts.factory.createBinaryExpression(
310
- split,
311
- ts.factory.createToken(
312
- ts.SyntaxKind.QuestionQuestionToken,
313
- ),
314
- ts.factory.createArrayLiteralExpression([], false),
315
- ),
316
- );
317
- };
318
- }
319
-
320
- const SINGULAR: Set<string> = new Set([
321
- "age",
322
- "authorization",
323
- "content-length",
324
- "content-type",
325
- "etag",
326
- "expires",
327
- "from",
328
- "host",
329
- "if-modified-since",
330
- "if-unmodified-since",
331
- "last-modified",
332
- "location",
333
- "max-forwards",
334
- "proxy-authorization",
335
- "referer",
336
- "retry-after",
337
- "server",
338
- "user-agent",
339
- ]);
1
+ import ts from "typescript";
2
+
3
+ import { ExpressionFactory } from "../../factories/ExpressionFactory";
4
+ import { IdentifierFactory } from "../../factories/IdentifierFactory";
5
+ import { MetadataCollection } from "../../factories/MetadataCollection";
6
+ import { MetadataFactory } from "../../factories/MetadataFactory";
7
+ import { StatementFactory } from "../../factories/StatementFactory";
8
+ import { TypeFactory } from "../../factories/TypeFactory";
9
+
10
+ import { Metadata } from "../../schemas/metadata/Metadata";
11
+ import { MetadataArrayType } from "../../schemas/metadata/MetadataArrayType";
12
+ import { MetadataObject } from "../../schemas/metadata/MetadataObject";
13
+ import { MetadataProperty } from "../../schemas/metadata/MetadataProperty";
14
+
15
+ import { IProject } from "../../transformers/IProject";
16
+ import { TransformerError } from "../../transformers/TransformerError";
17
+
18
+ import { Atomic } from "../../typings/Atomic";
19
+
20
+ import { Escaper } from "../../utils/Escaper";
21
+ import { MapUtil } from "../../utils/MapUtil";
22
+
23
+ import { FunctionImporter } from "../helpers/FunctionImporeter";
24
+ import { HttpMetadataUtil } from "../helpers/HttpMetadataUtil";
25
+
26
+ export namespace HttpHeadersProgrammer {
27
+ export const INPUT_TYPE = "Record<string, string | string[] | undefined>";
28
+
29
+ export const write =
30
+ (project: IProject) =>
31
+ (modulo: ts.LeftHandSideExpression) =>
32
+ (type: ts.Type, name?: string): ts.ArrowFunction => {
33
+ // GET OBJECT TYPE
34
+ const importer: FunctionImporter = new FunctionImporter(
35
+ modulo.getText(),
36
+ );
37
+ const collection: MetadataCollection = new MetadataCollection();
38
+ const result = MetadataFactory.analyze(project.checker)({
39
+ escape: false,
40
+ constant: true,
41
+ absorb: true,
42
+ validate,
43
+ })(collection)(type);
44
+ if (result.success === false)
45
+ throw TransformerError.from(`typia.http.${importer.method}`)(
46
+ result.errors,
47
+ );
48
+
49
+ // DO TRANSFORM
50
+ const object: MetadataObject = result.data.objects[0]!;
51
+ const statements: ts.Statement[] = decode_object(importer)(object);
52
+ return ts.factory.createArrowFunction(
53
+ undefined,
54
+ undefined,
55
+ [
56
+ IdentifierFactory.parameter(
57
+ "input",
58
+ ts.factory.createTypeReferenceNode(INPUT_TYPE),
59
+ ),
60
+ ],
61
+ ts.factory.createTypeReferenceNode(
62
+ `typia.Resolved<${
63
+ name ?? TypeFactory.getFullName(project.checker)(type)
64
+ }>`,
65
+ ),
66
+ undefined,
67
+ ts.factory.createBlock(
68
+ [...importer.declare(modulo), ...statements],
69
+ true,
70
+ ),
71
+ );
72
+ };
73
+
74
+ export const validate = (
75
+ meta: Metadata,
76
+ explore: MetadataFactory.IExplore,
77
+ ): string[] => {
78
+ const errors: string[] = [];
79
+ const insert = (msg: string) => errors.push(msg);
80
+
81
+ if (explore.top === true) {
82
+ // TOP MUST BE ONLY OBJECT
83
+ if (meta.objects.length !== 1 || meta.bucket() !== 1)
84
+ insert("only one object type is allowed.");
85
+ if (meta.nullable === true) insert("headers cannot be null.");
86
+ if (meta.isRequired() === false) insert("headers cannot be null.");
87
+ } else if (
88
+ explore.nested !== null &&
89
+ explore.nested instanceof MetadataArrayType
90
+ ) {
91
+ //----
92
+ // ARRAY
93
+ //----
94
+ const atomics = HttpMetadataUtil.atomics(meta);
95
+ const expected: number =
96
+ meta.atomics.length +
97
+ meta.templates.length +
98
+ meta.constants
99
+ .map((c) => c.values.length)
100
+ .reduce((a, b) => a + b, 0);
101
+ if (atomics.size > 1) insert("union type is not allowed in array.");
102
+ if (meta.size() !== expected)
103
+ insert("only atomic or constant types are allowed in array.");
104
+ if (meta.nullable === true)
105
+ insert("nullable type is not allowed in array.");
106
+ if (meta.isRequired() === false)
107
+ insert("optional type is not allowed in array.");
108
+ } else if (explore.object && explore.property !== null) {
109
+ //----
110
+ // COMMON
111
+ //----
112
+ // PROPERTY MUST BE SOLE
113
+ if (typeof explore.property === "object")
114
+ insert("dynamic property is not allowed.");
115
+ // DO NOT ALLOW TUPLE TYPE
116
+ if (meta.tuples.length) insert("tuple type is not allowed.");
117
+ // DO NOT ALLOW UNION TYPE
118
+ if (HttpMetadataUtil.isUnion(meta))
119
+ insert("union type is not allowed.");
120
+ // DO NOT ALLOW NESTED OBJECT
121
+ if (
122
+ meta.objects.length ||
123
+ meta.sets.length ||
124
+ meta.maps.length ||
125
+ meta.natives.length
126
+ )
127
+ insert("nested object type is not allowed.");
128
+ // DO NOT ALLOW NULLABLE
129
+ if (meta.nullable === true) insert("nullable type is not allowed.");
130
+
131
+ //----
132
+ // SPECIAL KEY NAMES
133
+ //----
134
+ const isArray: boolean =
135
+ meta.arrays.length >= 1 || meta.tuples.length >= 1;
136
+ // SET-COOKIE MUST BE ARRAY
137
+ if (
138
+ typeof explore.property === "string" &&
139
+ explore.property.toLowerCase() === "set-cookie" &&
140
+ isArray === false
141
+ )
142
+ insert(`${explore.property} property must be array.`);
143
+ // MUST BE SINGULAR CASE
144
+ if (
145
+ typeof explore.property === "string" &&
146
+ SINGULAR.has(explore.property.toLowerCase()) &&
147
+ isArray === true
148
+ )
149
+ insert("property cannot be array.");
150
+ } else if (explore.object && explore.property === null) {
151
+ const counter: Map<string, Set<string>> = new Map();
152
+ for (const prop of explore.object.properties) {
153
+ const key: string | null = prop.key.getSoleLiteral();
154
+ if (key === null) continue;
155
+
156
+ MapUtil.take(counter)(key.toLowerCase(), () => new Set()).add(
157
+ key,
158
+ );
159
+ }
160
+ for (const [key, set] of counter)
161
+ if (set.size > 1)
162
+ insert(
163
+ `duplicated keys when converting to lowercase letters: [${[
164
+ ...set,
165
+ ].join(", ")}] -> ${key}`,
166
+ );
167
+ }
168
+ return errors;
169
+ };
170
+
171
+ const decode_object =
172
+ (importer: FunctionImporter) =>
173
+ (object: MetadataObject): ts.Statement[] => {
174
+ const output: ts.Identifier = ts.factory.createIdentifier("output");
175
+ const optionals: string[] = [];
176
+ return [
177
+ StatementFactory.constant(
178
+ "output",
179
+ ts.factory.createObjectLiteralExpression(
180
+ object.properties.map((prop) => {
181
+ if (
182
+ !prop.value.isRequired() &&
183
+ prop.value.arrays.length +
184
+ prop.value.tuples.length >
185
+ 0
186
+ )
187
+ optionals.push(
188
+ prop.key.constants[0]!.values[0] as string,
189
+ );
190
+ return decode_regular_property(importer)(prop);
191
+ }),
192
+ true,
193
+ ),
194
+ ),
195
+ ...optionals.map((key) => {
196
+ const access = IdentifierFactory.access(output)(key);
197
+ return ts.factory.createIfStatement(
198
+ ts.factory.createStrictEquality(
199
+ ts.factory.createNumericLiteral(0),
200
+ IdentifierFactory.access(access)("length"),
201
+ ),
202
+ ts.factory.createExpressionStatement(
203
+ ts.factory.createDeleteExpression(access),
204
+ ),
205
+ );
206
+ }),
207
+ ts.factory.createReturnStatement(
208
+ ts.factory.createAsExpression(
209
+ output,
210
+ TypeFactory.keyword("any"),
211
+ ),
212
+ ),
213
+ ];
214
+ };
215
+
216
+ const decode_regular_property =
217
+ (importer: FunctionImporter) =>
218
+ (property: MetadataProperty): ts.PropertyAssignment => {
219
+ const key: string = property.key.constants[0]!.values[0] as string;
220
+ const value: Metadata = property.value;
221
+
222
+ const [type, isArray]: [Atomic.Literal, boolean] = value.atomics
223
+ .length
224
+ ? [value.atomics[0]!.type, false]
225
+ : value.constants.length
226
+ ? [value.constants[0]!.type, false]
227
+ : value.templates.length
228
+ ? ["string", false]
229
+ : (() => {
230
+ const meta: Metadata =
231
+ value.arrays[0]?.type.value ??
232
+ value.tuples[0]!.type.elements[0]!;
233
+ return meta.atomics.length
234
+ ? [meta.atomics[0]!.type, true]
235
+ : meta.templates.length
236
+ ? ["string", true]
237
+ : [meta.constants[0]!.type, true];
238
+ })();
239
+ const accessor = IdentifierFactory.access(
240
+ ts.factory.createIdentifier("input"),
241
+ )(key.toLowerCase());
242
+
243
+ return ts.factory.createPropertyAssignment(
244
+ Escaper.variable(key)
245
+ ? key
246
+ : ts.factory.createStringLiteral(key),
247
+ isArray
248
+ ? key === "set-cookie"
249
+ ? accessor
250
+ : decode_array(importer)(type)(key)(value)(accessor)
251
+ : decode_value(importer)(type)(accessor),
252
+ );
253
+ };
254
+
255
+ const decode_value =
256
+ (importer: FunctionImporter) =>
257
+ (type: Atomic.Literal) =>
258
+ (value: ts.Expression) =>
259
+ type === "string"
260
+ ? value
261
+ : ts.factory.createCallExpression(
262
+ importer.use(type),
263
+ undefined,
264
+ [value],
265
+ );
266
+
267
+ const decode_array =
268
+ (importer: FunctionImporter) =>
269
+ (type: Atomic.Literal) =>
270
+ (key: string) =>
271
+ (value: Metadata) =>
272
+ (accessor: ts.Expression) => {
273
+ const split: ts.CallChain = ts.factory.createCallChain(
274
+ ts.factory.createPropertyAccessChain(
275
+ ts.factory.createCallChain(
276
+ ts.factory.createPropertyAccessChain(
277
+ accessor,
278
+ ts.factory.createToken(
279
+ ts.SyntaxKind.QuestionDotToken,
280
+ ),
281
+ ts.factory.createIdentifier("split"),
282
+ ),
283
+ undefined,
284
+ undefined,
285
+ [
286
+ ts.factory.createStringLiteral(
287
+ key === "cookie" ? "; " : ", ",
288
+ ),
289
+ ],
290
+ ),
291
+ ts.factory.createToken(ts.SyntaxKind.QuestionDotToken),
292
+ ts.factory.createIdentifier("map"),
293
+ ),
294
+ undefined,
295
+ undefined,
296
+ [importer.use(type)],
297
+ );
298
+ return ts.factory.createConditionalExpression(
299
+ ExpressionFactory.isArray(accessor),
300
+ undefined,
301
+ ts.factory.createCallExpression(
302
+ IdentifierFactory.access(accessor)("map"),
303
+ undefined,
304
+ [importer.use(type)],
305
+ ),
306
+ undefined,
307
+ value.isRequired() === false
308
+ ? split
309
+ : ts.factory.createBinaryExpression(
310
+ split,
311
+ ts.factory.createToken(
312
+ ts.SyntaxKind.QuestionQuestionToken,
313
+ ),
314
+ ts.factory.createArrayLiteralExpression([], false),
315
+ ),
316
+ );
317
+ };
318
+ }
319
+
320
+ const SINGULAR: Set<string> = new Set([
321
+ "age",
322
+ "authorization",
323
+ "content-length",
324
+ "content-type",
325
+ "etag",
326
+ "expires",
327
+ "from",
328
+ "host",
329
+ "if-modified-since",
330
+ "if-unmodified-since",
331
+ "last-modified",
332
+ "location",
333
+ "max-forwards",
334
+ "proxy-authorization",
335
+ "referer",
336
+ "retry-after",
337
+ "server",
338
+ "user-agent",
339
+ ]);