typia 5.1.0-dev.20230923 → 5.1.0-dev.20230924

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 (146) hide show
  1. package/lib/functional/$HeadersReader.d.ts +6 -0
  2. package/lib/functional/$HeadersReader.js +17 -0
  3. package/lib/functional/$HeadersReader.js.map +1 -0
  4. package/lib/functional/$QueryReader.d.ts +7 -0
  5. package/lib/functional/$QueryReader.js +37 -0
  6. package/lib/functional/$QueryReader.js.map +1 -0
  7. package/lib/functional/Namespace.js +7 -0
  8. package/lib/functional/Namespace.js.map +1 -1
  9. package/lib/http.d.ts +872 -0
  10. package/lib/http.js +110 -0
  11. package/lib/http.js.map +1 -0
  12. package/lib/json.js +2 -2
  13. package/lib/json.js.map +1 -1
  14. package/lib/misc.js +2 -2
  15. package/lib/misc.js.map +1 -1
  16. package/lib/module.d.ts +1 -0
  17. package/lib/module.js +2 -1
  18. package/lib/module.js.map +1 -1
  19. package/lib/programmers/helpers/HttpMetadataUtil.d.ts +5 -0
  20. package/lib/programmers/helpers/HttpMetadataUtil.js +44 -0
  21. package/lib/programmers/helpers/HttpMetadataUtil.js.map +1 -0
  22. package/lib/programmers/http/HttpAssertHeadersProgrammer.d.ts +5 -0
  23. package/lib/programmers/http/HttpAssertHeadersProgrammer.js +41 -0
  24. package/lib/programmers/http/HttpAssertHeadersProgrammer.js.map +1 -0
  25. package/lib/programmers/http/HttpAssertQueryProgrammer.d.ts +5 -0
  26. package/lib/programmers/http/HttpAssertQueryProgrammer.js +41 -0
  27. package/lib/programmers/http/HttpAssertQueryProgrammer.js.map +1 -0
  28. package/lib/programmers/http/HttpHeadersProgrammer.d.ts +6 -0
  29. package/lib/programmers/http/HttpHeadersProgrammer.js +235 -0
  30. package/lib/programmers/http/HttpHeadersProgrammer.js.map +1 -0
  31. package/lib/programmers/http/HttpIsHeadersProgrammer.d.ts +5 -0
  32. package/lib/programmers/http/HttpIsHeadersProgrammer.js +45 -0
  33. package/lib/programmers/http/HttpIsHeadersProgrammer.js.map +1 -0
  34. package/lib/programmers/http/HttpIsQueryProgrammer.d.ts +5 -0
  35. package/lib/programmers/http/HttpIsQueryProgrammer.js +45 -0
  36. package/lib/programmers/http/HttpIsQueryProgrammer.js.map +1 -0
  37. package/lib/programmers/http/HttpParameterProgrammer.d.ts +5 -0
  38. package/lib/programmers/http/HttpParameterProgrammer.js +120 -0
  39. package/lib/programmers/http/HttpParameterProgrammer.js.map +1 -0
  40. package/lib/programmers/http/HttpQueryProgrammer.d.ts +6 -0
  41. package/lib/programmers/http/HttpQueryProgrammer.js +177 -0
  42. package/lib/programmers/http/HttpQueryProgrammer.js.map +1 -0
  43. package/lib/programmers/http/HttpValidateHeadersProgrammer.d.ts +5 -0
  44. package/lib/programmers/http/HttpValidateHeadersProgrammer.js +41 -0
  45. package/lib/programmers/http/HttpValidateHeadersProgrammer.js.map +1 -0
  46. package/lib/programmers/http/HttpValidateQueryProgrammer.d.ts +5 -0
  47. package/lib/programmers/http/HttpValidateQueryProgrammer.js +41 -0
  48. package/lib/programmers/http/HttpValidateQueryProgrammer.js.map +1 -0
  49. package/lib/protobuf.d.ts +24 -24
  50. package/lib/protobuf.js +4 -4
  51. package/lib/protobuf.js.map +1 -1
  52. package/lib/transformers/CallExpressionTransformer.js +40 -0
  53. package/lib/transformers/CallExpressionTransformer.js.map +1 -1
  54. package/lib/transformers/features/http/CreateHttpAssertHeadersTransformer.d.ts +4 -0
  55. package/lib/transformers/features/http/CreateHttpAssertHeadersTransformer.js +12 -0
  56. package/lib/transformers/features/http/CreateHttpAssertHeadersTransformer.js.map +1 -0
  57. package/lib/transformers/features/http/CreateHttpAssertQueryTransformer.d.ts +4 -0
  58. package/lib/transformers/features/http/CreateHttpAssertQueryTransformer.js +12 -0
  59. package/lib/transformers/features/http/CreateHttpAssertQueryTransformer.js.map +1 -0
  60. package/lib/transformers/features/http/CreateHttpHeadersTransformer.d.ts +4 -0
  61. package/lib/transformers/features/http/CreateHttpHeadersTransformer.js +10 -0
  62. package/lib/transformers/features/http/CreateHttpHeadersTransformer.js.map +1 -0
  63. package/lib/transformers/features/http/CreateHttpIsHeadersTransformer.d.ts +4 -0
  64. package/lib/transformers/features/http/CreateHttpIsHeadersTransformer.js +10 -0
  65. package/lib/transformers/features/http/CreateHttpIsHeadersTransformer.js.map +1 -0
  66. package/lib/transformers/features/http/CreateHttpIsQueryTransformer.d.ts +4 -0
  67. package/lib/transformers/features/http/CreateHttpIsQueryTransformer.js +10 -0
  68. package/lib/transformers/features/http/CreateHttpIsQueryTransformer.js.map +1 -0
  69. package/lib/transformers/features/http/CreateHttpParameterTransformer.d.ts +4 -0
  70. package/lib/transformers/features/http/CreateHttpParameterTransformer.js +10 -0
  71. package/lib/transformers/features/http/CreateHttpParameterTransformer.js.map +1 -0
  72. package/lib/transformers/features/http/CreateHttpQueryTransformer.d.ts +4 -0
  73. package/lib/transformers/features/http/CreateHttpQueryTransformer.js +10 -0
  74. package/lib/transformers/features/http/CreateHttpQueryTransformer.js.map +1 -0
  75. package/lib/transformers/features/http/CreateHttpValidateHeadersTransformer copy.d.ts +4 -0
  76. package/lib/transformers/features/http/CreateHttpValidateHeadersTransformer copy.js +12 -0
  77. package/lib/transformers/features/http/CreateHttpValidateHeadersTransformer copy.js.map +1 -0
  78. package/lib/transformers/features/http/CreateHttpValidateQueryTransformer.d.ts +4 -0
  79. package/lib/transformers/features/http/CreateHttpValidateQueryTransformer.js +12 -0
  80. package/lib/transformers/features/http/CreateHttpValidateQueryTransformer.js.map +1 -0
  81. package/lib/transformers/features/http/HttpAssertHeadersTransformer.d.ts +4 -0
  82. package/lib/transformers/features/http/HttpAssertHeadersTransformer.js +12 -0
  83. package/lib/transformers/features/http/HttpAssertHeadersTransformer.js.map +1 -0
  84. package/lib/transformers/features/http/HttpAssertQueryTransformer.d.ts +4 -0
  85. package/lib/transformers/features/http/HttpAssertQueryTransformer.js +12 -0
  86. package/lib/transformers/features/http/HttpAssertQueryTransformer.js.map +1 -0
  87. package/lib/transformers/features/http/HttpHeadersTransformer.d.ts +4 -0
  88. package/lib/transformers/features/http/HttpHeadersTransformer.js +10 -0
  89. package/lib/transformers/features/http/HttpHeadersTransformer.js.map +1 -0
  90. package/lib/transformers/features/http/HttpIsHeadersTransformer.d.ts +4 -0
  91. package/lib/transformers/features/http/HttpIsHeadersTransformer.js +10 -0
  92. package/lib/transformers/features/http/HttpIsHeadersTransformer.js.map +1 -0
  93. package/lib/transformers/features/http/HttpIsQueryTransformer.d.ts +4 -0
  94. package/lib/transformers/features/http/HttpIsQueryTransformer.js +10 -0
  95. package/lib/transformers/features/http/HttpIsQueryTransformer.js.map +1 -0
  96. package/lib/transformers/features/http/HttpParameterTransformer.d.ts +4 -0
  97. package/lib/transformers/features/http/HttpParameterTransformer.js +10 -0
  98. package/lib/transformers/features/http/HttpParameterTransformer.js.map +1 -0
  99. package/lib/transformers/features/http/HttpQueryTransformer.d.ts +4 -0
  100. package/lib/transformers/features/http/HttpQueryTransformer.js +10 -0
  101. package/lib/transformers/features/http/HttpQueryTransformer.js.map +1 -0
  102. package/lib/transformers/features/http/HttpValidateHeadersTransformer.d.ts +4 -0
  103. package/lib/transformers/features/http/HttpValidateHeadersTransformer.js +12 -0
  104. package/lib/transformers/features/http/HttpValidateHeadersTransformer.js.map +1 -0
  105. package/lib/transformers/features/http/HttpValidateQueryTransformer.d.ts +4 -0
  106. package/lib/transformers/features/http/HttpValidateQueryTransformer.js +12 -0
  107. package/lib/transformers/features/http/HttpValidateQueryTransformer.js.map +1 -0
  108. package/package.json +1 -1
  109. package/src/factories/MetadataCollection.ts +274 -274
  110. package/src/functional/$HeadersReader.ts +9 -0
  111. package/src/functional/$QueryReader.ts +32 -0
  112. package/src/functional/Namespace.ts +7 -0
  113. package/src/http.ts +1141 -0
  114. package/src/json.ts +2 -2
  115. package/src/misc.ts +2 -2
  116. package/src/module.ts +1 -0
  117. package/src/programmers/helpers/HttpMetadataUtil.ts +21 -0
  118. package/src/programmers/http/HttpAssertHeadersProgrammer.ts +77 -0
  119. package/src/programmers/http/HttpAssertQueryProgrammer.ts +77 -0
  120. package/src/programmers/http/HttpHeadersProgrammer.ts +321 -0
  121. package/src/programmers/http/HttpIsHeadersProgrammer.ts +87 -0
  122. package/src/programmers/http/HttpIsQueryProgrammer.ts +87 -0
  123. package/src/programmers/http/HttpParameterProgrammer.ts +168 -0
  124. package/src/programmers/http/HttpQueryProgrammer.ts +267 -0
  125. package/src/programmers/http/HttpValidateHeadersProgrammer.ts +77 -0
  126. package/src/programmers/http/HttpValidateQueryProgrammer.ts +77 -0
  127. package/src/protobuf.ts +34 -28
  128. package/src/transformers/CallExpressionTransformer.ts +46 -0
  129. package/src/transformers/features/http/CreateHttpAssertHeadersTransformer.ts +12 -0
  130. package/src/transformers/features/http/CreateHttpAssertQueryTransformer.ts +12 -0
  131. package/src/transformers/features/http/CreateHttpHeadersTransformer.ts +9 -0
  132. package/src/transformers/features/http/CreateHttpIsHeadersTransformer.ts +9 -0
  133. package/src/transformers/features/http/CreateHttpIsQueryTransformer.ts +9 -0
  134. package/src/transformers/features/http/CreateHttpParameterTransformer.ts +9 -0
  135. package/src/transformers/features/http/CreateHttpQueryTransformer.ts +9 -0
  136. package/src/transformers/features/http/CreateHttpValidateHeadersTransformer copy.ts +12 -0
  137. package/src/transformers/features/http/CreateHttpValidateQueryTransformer.ts +12 -0
  138. package/src/transformers/features/http/HttpAssertHeadersTransformer.ts +10 -0
  139. package/src/transformers/features/http/HttpAssertQueryTransformer.ts +10 -0
  140. package/src/transformers/features/http/HttpHeadersTransformer.ts +9 -0
  141. package/src/transformers/features/http/HttpIsHeadersTransformer.ts +9 -0
  142. package/src/transformers/features/http/HttpIsQueryTransformer.ts +9 -0
  143. package/src/transformers/features/http/HttpParameterTransformer.ts +9 -0
  144. package/src/transformers/features/http/HttpQueryTransformer.ts +9 -0
  145. package/src/transformers/features/http/HttpValidateHeadersTransformer.ts +10 -0
  146. package/src/transformers/features/http/HttpValidateQueryTransformer.ts +10 -0
package/src/json.ts CHANGED
@@ -121,7 +121,7 @@ export function assertParse<T>(input: string): Primitive<T>;
121
121
  export function assertParse<T>(): Primitive<T> {
122
122
  halt("assertParse");
123
123
  }
124
- Object.assign(assertParse, Namespace.assert("assertParse"));
124
+ Object.assign(assertParse, Namespace.assert("json.assertParse"));
125
125
 
126
126
  /**
127
127
  * > You must configure the generic argument `T`.
@@ -308,7 +308,7 @@ export function assertStringify<T>(input: T): unknown;
308
308
  export function assertStringify(): string {
309
309
  halt("assertStringify");
310
310
  }
311
- Object.assign(assertStringify, Namespace.assert("assertStringify"));
311
+ Object.assign(assertStringify, Namespace.assert("json.assertStringify"));
312
312
  Object.assign(assertStringify, Namespace.json.stringify("assertStringify"));
313
313
 
314
314
  /**
package/src/misc.ts CHANGED
@@ -130,7 +130,7 @@ export function assertClone<T>(input: unknown): Resolved<T>;
130
130
  export function assertClone(): never {
131
131
  halt("assertClone");
132
132
  }
133
- Object.assign(assertClone, Namespace.assert("assertClone"));
133
+ Object.assign(assertClone, Namespace.assert("misc.assertClone"));
134
134
  Object.assign(assertClone, Namespace.misc.clone("assertClone"));
135
135
 
136
136
  /**
@@ -301,7 +301,7 @@ export function assertPrune<T>(input: unknown): T;
301
301
  export function assertPrune(): unknown {
302
302
  halt("assertPrune");
303
303
  }
304
- Object.assign(assertPrune, Namespace.assert("assertPrune"));
304
+ Object.assign(assertPrune, Namespace.assert("misc.assertPrune"));
305
305
  Object.assign(assertPrune, Namespace.misc.prune("assertPrune"));
306
306
 
307
307
  /**
package/src/module.ts CHANGED
@@ -6,6 +6,7 @@ import { IRandomGenerator } from "./IRandomGenerator";
6
6
  import { IValidation } from "./IValidation";
7
7
  import { Resolved } from "./Resolved";
8
8
 
9
+ export * as http from "./http";
9
10
  export * as json from "./json";
10
11
  export * as misc from "./misc";
11
12
  export * as protobuf from "./protobuf";
@@ -0,0 +1,21 @@
1
+ import { Metadata } from "../../schemas/metadata/Metadata";
2
+
3
+ export namespace HttpMetadataUtil {
4
+ export const atomics = (
5
+ meta: Metadata,
6
+ ): Set<"boolean" | "bigint" | "number" | "string"> =>
7
+ new Set([
8
+ ...meta.atomics.map((a) => a.type),
9
+ ...meta.constants.map((c) => c.type),
10
+ ...(meta.templates.length ? (["string"] as const) : []),
11
+ ]);
12
+
13
+ export const isUnion = (meta: Metadata): boolean =>
14
+ atomics(meta).size +
15
+ meta.arrays.length +
16
+ meta.tuples.length +
17
+ meta.natives.length +
18
+ meta.maps.length +
19
+ meta.objects.length >
20
+ 1;
21
+ }
@@ -0,0 +1,77 @@
1
+ import ts from "typescript";
2
+
3
+ import { IdentifierFactory } from "../../factories/IdentifierFactory";
4
+ import { StatementFactory } from "../../factories/StatementFactory";
5
+ import { TypeFactory } from "../../factories/TypeFactory";
6
+
7
+ import { IProject } from "../../transformers/IProject";
8
+
9
+ import { AssertProgrammer } from "../AssertProgrammer";
10
+ import { HttpHeadersProgrammer } from "./HttpHeadersProgrammer";
11
+
12
+ export namespace HttpAssertHeadersProgrammer {
13
+ export const write =
14
+ (project: IProject) =>
15
+ (modulo: ts.LeftHandSideExpression) =>
16
+ (type: ts.Type, name?: string): ts.ArrowFunction =>
17
+ ts.factory.createArrowFunction(
18
+ undefined,
19
+ undefined,
20
+ [
21
+ IdentifierFactory.parameter(
22
+ "input",
23
+ ts.factory.createTypeReferenceNode(
24
+ HttpHeadersProgrammer.INPUT_TYPE,
25
+ ),
26
+ ),
27
+ ],
28
+ ts.factory.createTypeReferenceNode(
29
+ `typia.Resolved<${
30
+ name ?? TypeFactory.getFullName(project.checker)(type)
31
+ }>`,
32
+ ),
33
+ undefined,
34
+ ts.factory.createBlock([
35
+ StatementFactory.constant(
36
+ "decode",
37
+ HttpHeadersProgrammer.write({
38
+ ...project,
39
+ options: {
40
+ ...project.options,
41
+ functional: false,
42
+ numeric: false,
43
+ },
44
+ })(modulo)(type, name),
45
+ ),
46
+ StatementFactory.constant(
47
+ "assert",
48
+ AssertProgrammer.write({
49
+ ...project,
50
+ options: {
51
+ ...project.options,
52
+ functional: false,
53
+ numeric: false,
54
+ },
55
+ })(modulo)(false)(type, name),
56
+ ),
57
+ StatementFactory.constant(
58
+ "output",
59
+ ts.factory.createCallExpression(
60
+ ts.factory.createIdentifier("decode"),
61
+ undefined,
62
+ [ts.factory.createIdentifier("input")],
63
+ ),
64
+ ),
65
+ ts.factory.createReturnStatement(
66
+ ts.factory.createAsExpression(
67
+ ts.factory.createCallExpression(
68
+ ts.factory.createIdentifier("assert"),
69
+ undefined,
70
+ [ts.factory.createIdentifier("output")],
71
+ ),
72
+ TypeFactory.keyword("any"),
73
+ ),
74
+ ),
75
+ ]),
76
+ );
77
+ }
@@ -0,0 +1,77 @@
1
+ import ts from "typescript";
2
+
3
+ import { IdentifierFactory } from "../../factories/IdentifierFactory";
4
+ import { StatementFactory } from "../../factories/StatementFactory";
5
+ import { TypeFactory } from "../../factories/TypeFactory";
6
+
7
+ import { IProject } from "../../transformers/IProject";
8
+
9
+ import { AssertProgrammer } from "../AssertProgrammer";
10
+ import { HttpQueryProgrammer } from "./HttpQueryProgrammer";
11
+
12
+ export namespace HttpAssertQueryProgrammer {
13
+ export const write =
14
+ (project: IProject) =>
15
+ (modulo: ts.LeftHandSideExpression) =>
16
+ (type: ts.Type, name?: string): ts.ArrowFunction =>
17
+ ts.factory.createArrowFunction(
18
+ undefined,
19
+ undefined,
20
+ [
21
+ IdentifierFactory.parameter(
22
+ "input",
23
+ ts.factory.createTypeReferenceNode(
24
+ HttpQueryProgrammer.INPUT_TYPE,
25
+ ),
26
+ ),
27
+ ],
28
+ ts.factory.createTypeReferenceNode(
29
+ `typia.Resolved<${
30
+ name ?? TypeFactory.getFullName(project.checker)(type)
31
+ }>`,
32
+ ),
33
+ undefined,
34
+ ts.factory.createBlock([
35
+ StatementFactory.constant(
36
+ "decode",
37
+ HttpQueryProgrammer.write({
38
+ ...project,
39
+ options: {
40
+ ...project.options,
41
+ functional: false,
42
+ numeric: false,
43
+ },
44
+ })(modulo)(type, name),
45
+ ),
46
+ StatementFactory.constant(
47
+ "assert",
48
+ AssertProgrammer.write({
49
+ ...project,
50
+ options: {
51
+ ...project.options,
52
+ functional: false,
53
+ numeric: false,
54
+ },
55
+ })(modulo)(false)(type, name),
56
+ ),
57
+ StatementFactory.constant(
58
+ "output",
59
+ ts.factory.createCallExpression(
60
+ ts.factory.createIdentifier("decode"),
61
+ undefined,
62
+ [ts.factory.createIdentifier("input")],
63
+ ),
64
+ ),
65
+ ts.factory.createReturnStatement(
66
+ ts.factory.createAsExpression(
67
+ ts.factory.createCallExpression(
68
+ ts.factory.createIdentifier("assert"),
69
+ undefined,
70
+ [ts.factory.createIdentifier("output")],
71
+ ),
72
+ TypeFactory.keyword("any"),
73
+ ),
74
+ ),
75
+ ]),
76
+ );
77
+ }
@@ -0,0 +1,321 @@
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 HttpHeadersProgrammer {
25
+ export const INPUT_TYPE = "Record<string, string | string[] | undefined>";
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) insert("headers cannot be null.");
84
+ if (meta.isRequired() === false) insert("headers cannot be null.");
85
+ } else if (
86
+ explore.nested !== null &&
87
+ explore.nested instanceof MetadataArray
88
+ ) {
89
+ const atomics = HttpMetadataUtil.atomics(meta);
90
+ const expected: number =
91
+ meta.atomics.length +
92
+ meta.templates.length +
93
+ meta.constants
94
+ .map((c) => c.values.length)
95
+ .reduce((a, b) => a + b, 0);
96
+ if (atomics.size > 1) insert("union type is not allowed in array.");
97
+ if (meta.nullable) insert("nullable type is not allowed in array.");
98
+ if (meta.isRequired() === false)
99
+ insert("optional type is not allowed.");
100
+ if (meta.size() !== expected)
101
+ insert("only atomic or constant types are allowed in array.");
102
+ } else if (explore.object && explore.property !== null) {
103
+ //----
104
+ // COMMON
105
+ //----
106
+ // PROPERTY MUST BE SOLE
107
+ if (typeof explore.property === "object")
108
+ insert("dynamic property is not allowed.");
109
+ // DO NOT ALLOW TUPLE TYPE
110
+ if (meta.tuples.length) insert("tuple type is not allowed.");
111
+ // DO NOT ALLOW UNION TYPE
112
+ if (HttpMetadataUtil.isUnion(meta))
113
+ insert("union type is not allowed.");
114
+ // DO NOT ALLOW NESTED OBJECT
115
+ if (
116
+ meta.objects.length ||
117
+ meta.sets.length ||
118
+ meta.maps.length ||
119
+ meta.natives.length
120
+ )
121
+ insert("nested object type is not allowed.");
122
+ // DO NOT ALLOW NULLABLE
123
+ if (meta.nullable) insert("nullable type is not allowed.");
124
+
125
+ //----
126
+ // ARRAY CASES
127
+ //----
128
+ const isArray: boolean = meta.arrays.length > 1;
129
+ // ARRAY TYPE MUST BE REQUIRED
130
+ if (isArray && meta.isRequired() === false)
131
+ insert("optional type is not allowed when array.");
132
+ // SET-COOKIE MUST BE ARRAY
133
+ if (explore.property === "set-cookie" && !isArray)
134
+ insert("set-cookie property must be array.");
135
+ // MUST BE SINGULAR CASE
136
+ if (
137
+ typeof explore.property === "string" &&
138
+ SINGULAR.has(explore.property) &&
139
+ isArray
140
+ )
141
+ insert("property cannot be array.");
142
+ }
143
+ return errors;
144
+ };
145
+
146
+ const decode_object =
147
+ (importer: FunctionImporter) =>
148
+ (object: MetadataObject): ts.Statement[] => {
149
+ const output: ts.Identifier = ts.factory.createIdentifier("output");
150
+ const optionals: string[] = [];
151
+ return [
152
+ StatementFactory.constant(
153
+ "output",
154
+ ts.factory.createObjectLiteralExpression(
155
+ object.properties.map((prop) => {
156
+ if (
157
+ !prop.value.isRequired() &&
158
+ prop.value.arrays.length +
159
+ prop.value.tuples.length >
160
+ 0
161
+ )
162
+ optionals.push(
163
+ prop.key.constants[0]!.values[0] as string,
164
+ );
165
+ return decode_regular_property(importer)(object)(
166
+ prop,
167
+ );
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
+ (object: MetadataObject) =>
191
+ (property: MetadataProperty): ts.PropertyAssignment => {
192
+ const key: string = property.key.constants[0]!.values[0] as string;
193
+ const value: Metadata = property.value;
194
+
195
+ const [type, isArray]: [Atomic.Literal, boolean] = value.atomics
196
+ .length
197
+ ? [value.atomics[0]!.type, false]
198
+ : value.constants.length
199
+ ? [value.constants[0]!.type, false]
200
+ : (() => {
201
+ const meta =
202
+ value.arrays[0]?.type.value ??
203
+ value.tuples[0]!.type.elements[0]!;
204
+ return meta.atomics.length
205
+ ? [meta.atomics[0]!.type, true]
206
+ : [meta.constants[0]!.type, true];
207
+ })();
208
+ if (isArray && SINGULAR.has(key))
209
+ throw new Error(
210
+ ErrorMessages.property(importer)(object)(key)(
211
+ `property "${key}" cannot be array.`,
212
+ ),
213
+ );
214
+ else if (!isArray && key === "set-cookie")
215
+ throw new Error(
216
+ ErrorMessages.property(importer)(object)(key)(
217
+ `property "${key}" must be array.`,
218
+ ),
219
+ );
220
+
221
+ const accessor = IdentifierFactory.access(
222
+ ts.factory.createIdentifier("input"),
223
+ )(key.toLowerCase());
224
+
225
+ return ts.factory.createPropertyAssignment(
226
+ Escaper.variable(key)
227
+ ? key
228
+ : ts.factory.createStringLiteral(key),
229
+ isArray
230
+ ? key === "set-cookie"
231
+ ? accessor
232
+ : decode_array(importer)(type)(key)(value)(accessor)
233
+ : decode_value(importer)(type)(accessor),
234
+ );
235
+ };
236
+
237
+ const decode_value =
238
+ (importer: FunctionImporter) =>
239
+ (type: Atomic.Literal) =>
240
+ (value: ts.Expression) =>
241
+ type === "string"
242
+ ? value
243
+ : ts.factory.createCallExpression(
244
+ importer.use(type),
245
+ undefined,
246
+ [value],
247
+ );
248
+
249
+ const decode_array =
250
+ (importer: FunctionImporter) =>
251
+ (type: Atomic.Literal) =>
252
+ (key: string) =>
253
+ (value: Metadata) =>
254
+ (accessor: ts.Expression) => {
255
+ const expression = ts.factory.createCallChain(
256
+ ts.factory.createPropertyAccessChain(
257
+ ts.factory.createCallChain(
258
+ ts.factory.createPropertyAccessChain(
259
+ accessor,
260
+ ts.factory.createToken(
261
+ ts.SyntaxKind.QuestionDotToken,
262
+ ),
263
+ ts.factory.createIdentifier("split"),
264
+ ),
265
+ undefined,
266
+ undefined,
267
+ [
268
+ ts.factory.createStringLiteral(
269
+ key === "cookie" ? "; " : ", ",
270
+ ),
271
+ ],
272
+ ),
273
+ ts.factory.createToken(ts.SyntaxKind.QuestionDotToken),
274
+ ts.factory.createIdentifier("map"),
275
+ ),
276
+ undefined,
277
+ undefined,
278
+ [importer.use(type)],
279
+ );
280
+ if (value.isRequired() === false) return expression;
281
+ return ts.factory.createBinaryExpression(
282
+ expression,
283
+ ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),
284
+ ts.factory.createArrayLiteralExpression([], false),
285
+ );
286
+ };
287
+ }
288
+
289
+ namespace ErrorMessages {
290
+ export const object =
291
+ (importer: FunctionImporter) => (type: Metadata) => (message: string) =>
292
+ `Error on ${importer.method}<${type.getName()}>(): ${message}`;
293
+
294
+ export const property =
295
+ (importer: FunctionImporter) =>
296
+ (obj: MetadataObject) =>
297
+ (key: string) =>
298
+ (message: string) =>
299
+ `Error on ${importer.method}<${obj.name}>(): property "${key}" - ${message}`;
300
+ }
301
+
302
+ const SINGULAR: Set<string> = new Set([
303
+ "age",
304
+ "authorization",
305
+ "content-length",
306
+ "content-type",
307
+ "etag",
308
+ "expires",
309
+ "from",
310
+ "host",
311
+ "if-modified-since",
312
+ "if-unmodified-since",
313
+ "last-modified",
314
+ "location",
315
+ "max-forwards",
316
+ "proxy-authorization",
317
+ "referer",
318
+ "retry-after",
319
+ "server",
320
+ "user-agent",
321
+ ]);
@@ -0,0 +1,87 @@
1
+ import ts from "typescript";
2
+
3
+ import { IdentifierFactory } from "../../factories/IdentifierFactory";
4
+ import { StatementFactory } from "../../factories/StatementFactory";
5
+ import { TypeFactory } from "../../factories/TypeFactory";
6
+
7
+ import { IProject } from "../../transformers/IProject";
8
+
9
+ import { IsProgrammer } from "../IsProgrammer";
10
+ import { HttpHeadersProgrammer } from "./HttpHeadersProgrammer";
11
+
12
+ export namespace HttpIsHeadersProgrammer {
13
+ export const write =
14
+ (project: IProject) =>
15
+ (modulo: ts.LeftHandSideExpression) =>
16
+ (type: ts.Type, name?: string): ts.ArrowFunction =>
17
+ ts.factory.createArrowFunction(
18
+ undefined,
19
+ undefined,
20
+ [
21
+ IdentifierFactory.parameter(
22
+ "input",
23
+ ts.factory.createTypeReferenceNode(
24
+ HttpHeadersProgrammer.INPUT_TYPE,
25
+ ),
26
+ ),
27
+ ],
28
+ ts.factory.createUnionTypeNode([
29
+ ts.factory.createTypeReferenceNode(
30
+ `typia.Resolved<${
31
+ name ??
32
+ TypeFactory.getFullName(project.checker)(type)
33
+ }>`,
34
+ ),
35
+ ts.factory.createLiteralTypeNode(ts.factory.createNull()),
36
+ ]),
37
+ undefined,
38
+ ts.factory.createBlock([
39
+ StatementFactory.constant(
40
+ "is",
41
+ IsProgrammer.write({
42
+ ...project,
43
+ options: {
44
+ ...project.options,
45
+ functional: false,
46
+ numeric: false,
47
+ },
48
+ })(modulo)(false)(type, name),
49
+ ),
50
+ StatementFactory.constant(
51
+ "headers",
52
+ HttpHeadersProgrammer.write({
53
+ ...project,
54
+ options: {
55
+ ...project.options,
56
+ functional: false,
57
+ numeric: false,
58
+ },
59
+ })(modulo)(type, name),
60
+ ),
61
+ StatementFactory.constant(
62
+ "output",
63
+ ts.factory.createCallExpression(
64
+ ts.factory.createIdentifier("headers"),
65
+ undefined,
66
+ [ts.factory.createIdentifier("input")],
67
+ ),
68
+ ),
69
+ ts.factory.createIfStatement(
70
+ ts.factory.createPrefixUnaryExpression(
71
+ ts.SyntaxKind.ExclamationToken,
72
+ ts.factory.createCallExpression(
73
+ ts.factory.createIdentifier("is"),
74
+ undefined,
75
+ [ts.factory.createIdentifier("output")],
76
+ ),
77
+ ),
78
+ ts.factory.createReturnStatement(
79
+ ts.factory.createNull(),
80
+ ),
81
+ ),
82
+ ts.factory.createReturnStatement(
83
+ ts.factory.createIdentifier("output"),
84
+ ),
85
+ ]),
86
+ );
87
+ }