@nestia/sdk 2.4.3 → 2.4.4

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 (70) hide show
  1. package/lib/INestiaConfig.d.ts +13 -0
  2. package/lib/analyses/ControllerAnalyzer.js +12 -1
  3. package/lib/analyses/ControllerAnalyzer.js.map +1 -1
  4. package/lib/analyses/PathAnalyzer.d.ts +2 -2
  5. package/lib/analyses/PathAnalyzer.js +27 -11
  6. package/lib/analyses/PathAnalyzer.js.map +1 -1
  7. package/lib/analyses/ReflectAnalyzer.js +11 -2
  8. package/lib/analyses/ReflectAnalyzer.js.map +1 -1
  9. package/lib/executable/internal/NestiaConfigLoader.js +5 -1
  10. package/lib/executable/internal/NestiaConfigLoader.js.map +1 -1
  11. package/lib/executable/sdk.js +11 -11
  12. package/lib/generates/SwaggerGenerator.js +16 -22
  13. package/lib/generates/SwaggerGenerator.js.map +1 -1
  14. package/lib/generates/internal/SwaggerSchemaGenerator.js +22 -15
  15. package/lib/generates/internal/SwaggerSchemaGenerator.js.map +1 -1
  16. package/lib/structures/ISwaggerComponents.d.ts +1 -1
  17. package/lib/structures/ISwaggerRoute.d.ts +3 -3
  18. package/package.json +5 -5
  19. package/src/INestiaConfig.ts +248 -234
  20. package/src/NestiaSdkApplication.ts +253 -253
  21. package/src/analyses/AccessorAnalyzer.ts +60 -60
  22. package/src/analyses/ConfigAnalyzer.ts +147 -147
  23. package/src/analyses/ControllerAnalyzer.ts +390 -379
  24. package/src/analyses/ExceptionAnalyzer.ts +115 -115
  25. package/src/analyses/GenericAnalyzer.ts +51 -51
  26. package/src/analyses/ImportAnalyzer.ts +138 -138
  27. package/src/analyses/PathAnalyzer.ts +110 -98
  28. package/src/analyses/ReflectAnalyzer.ts +11 -6
  29. package/src/analyses/SecurityAnalyzer.ts +20 -20
  30. package/src/executable/internal/CommandParser.ts +15 -15
  31. package/src/executable/internal/NestiaConfigLoader.ts +67 -67
  32. package/src/executable/internal/NestiaSdkCommand.ts +60 -60
  33. package/src/executable/sdk.ts +73 -73
  34. package/src/generates/E2eGenerator.ts +64 -64
  35. package/src/generates/SdkGenerator.ts +96 -96
  36. package/src/generates/SwaggerGenerator.ts +376 -372
  37. package/src/generates/internal/E2eFileProgrammer.ts +123 -123
  38. package/src/generates/internal/SdkDistributionComposer.ts +91 -91
  39. package/src/generates/internal/SdkDtoGenerator.ts +424 -424
  40. package/src/generates/internal/SdkFileProgrammer.ts +106 -106
  41. package/src/generates/internal/SdkImportWizard.ts +55 -55
  42. package/src/generates/internal/SdkRouteDirectory.ts +17 -17
  43. package/src/generates/internal/SdkSimulationProgrammer.ts +133 -133
  44. package/src/generates/internal/SdkTypeDefiner.ts +119 -119
  45. package/src/generates/internal/SwaggerSchemaGenerator.ts +18 -2
  46. package/src/generates/internal/SwaggerSchemaValidator.ts +198 -198
  47. package/src/index.ts +4 -4
  48. package/src/module.ts +2 -2
  49. package/src/structures/IErrorReport.ts +6 -6
  50. package/src/structures/INestiaProject.ts +13 -13
  51. package/src/structures/INormalizedInput.ts +20 -20
  52. package/src/structures/ISwagger.ts +91 -91
  53. package/src/structures/ISwaggerComponents.ts +29 -29
  54. package/src/structures/ISwaggerError.ts +8 -8
  55. package/src/structures/ISwaggerInfo.ts +80 -80
  56. package/src/structures/ISwaggerLazyProperty.ts +7 -7
  57. package/src/structures/ISwaggerLazySchema.ts +7 -7
  58. package/src/structures/ISwaggerRoute.ts +51 -51
  59. package/src/structures/ISwaggerSecurityScheme.ts +65 -65
  60. package/src/structures/ITypeTuple.ts +6 -6
  61. package/src/structures/MethodType.ts +5 -5
  62. package/src/structures/ParamCategory.ts +1 -1
  63. package/src/structures/TypeEntry.ts +22 -22
  64. package/src/utils/ArrayUtil.ts +26 -26
  65. package/src/utils/FileRetriever.ts +22 -22
  66. package/src/utils/ImportDictionary.ts +125 -125
  67. package/src/utils/MapUtil.ts +14 -14
  68. package/src/utils/PathUtil.ts +10 -10
  69. package/src/utils/SourceFinder.ts +66 -66
  70. package/src/utils/StripEnums.ts +5 -5
@@ -1,119 +1,119 @@
1
- import { INestiaConfig } from "../../INestiaConfig";
2
- import { IRoute } from "../../structures/IRoute";
3
- import { ImportDictionary } from "../../utils/ImportDictionary";
4
- import { SdkDtoGenerator } from "./SdkDtoGenerator";
5
-
6
- export namespace SdkTypeDefiner {
7
- export const name =
8
- (config: INestiaConfig) =>
9
- (importer: ImportDictionary) =>
10
- (p: IRoute.IParameter | IRoute.IOutput): string =>
11
- p.metadata
12
- ? SdkDtoGenerator.decode(config)(importer)(p.metadata)
13
- : p.typeName;
14
-
15
- export const headers =
16
- (config: INestiaConfig) =>
17
- (importer: ImportDictionary) =>
18
- (param: IRoute.IParameter): string => {
19
- const type: string = name(config)(importer)(param);
20
- if (config.primitive === false) return type;
21
-
22
- const resolved: string = importer.external({
23
- type: true,
24
- library: "@nestia/fetcher",
25
- instance: "Resolved",
26
- });
27
- return `${resolved}<${type}>`;
28
- };
29
-
30
- export const query =
31
- (config: INestiaConfig) =>
32
- (importer: ImportDictionary) =>
33
- (param: IRoute.IParameter): string => {
34
- const type: string = name(config)(importer)(param);
35
- if (config.primitive === false) return type;
36
-
37
- const resolved: string = importer.external({
38
- type: true,
39
- library: "@nestia/fetcher",
40
- instance: "Resolved",
41
- });
42
- return `${resolved}<${type}>`;
43
- };
44
-
45
- export const input =
46
- (config: INestiaConfig) =>
47
- (importer: ImportDictionary) =>
48
- (param: IRoute.IParameter): string => {
49
- const type: string = name(config)(importer)(param);
50
- if (config.primitive === false) return type;
51
-
52
- const primitive: string = importer.external({
53
- type: true,
54
- library: "@nestia/fetcher",
55
- instance: "Primitive",
56
- });
57
- return `${primitive}<${type}>`;
58
- };
59
-
60
- export const output =
61
- (config: INestiaConfig) =>
62
- (importer: ImportDictionary) =>
63
- (route: IRoute): string => {
64
- if (config.propagate !== true) {
65
- const type: string = name(config)(importer)(route.output);
66
- if (type === "void" || config.primitive === false) return type;
67
-
68
- const wrapper: string = importer.external({
69
- type: true,
70
- library: "@nestia/fetcher",
71
- instance:
72
- route.output.contentType === "application/x-www-form-urlencoded"
73
- ? "Resolved"
74
- : "Primitive",
75
- });
76
- return `${wrapper}<${type}>`;
77
- }
78
-
79
- const propagation: string = importer.external({
80
- type: true,
81
- library: "@nestia/fetcher",
82
- instance: "IPropagation",
83
- });
84
- const branches: IBranch[] = [
85
- {
86
- status: String(route.status ?? (route.method === "POST" ? 201 : 200)),
87
- type: name(config)(importer)(route.output),
88
- },
89
- ...Object.entries(route.exceptions).map(([status, value]) => ({
90
- status,
91
- type: name(config)(importer)(value),
92
- })),
93
- ];
94
- return (
95
- `${propagation}<{\n` +
96
- branches
97
- .map(
98
- (b) =>
99
- ` ${
100
- b.status.endsWith("XX") ? `"${b.status}"` : b.status
101
- }: ${b.type};`,
102
- )
103
- .join("\n") +
104
- "\n" +
105
- ` }${route.status ? `, ${route.status}` : ""}>`
106
- );
107
- };
108
-
109
- export const responseBody =
110
- (config: INestiaConfig) =>
111
- (importer: ImportDictionary) =>
112
- (route: IRoute): string =>
113
- output({ ...config, propagate: false })(importer)(route);
114
- }
115
-
116
- interface IBranch {
117
- status: string;
118
- type: string;
119
- }
1
+ import { INestiaConfig } from "../../INestiaConfig";
2
+ import { IRoute } from "../../structures/IRoute";
3
+ import { ImportDictionary } from "../../utils/ImportDictionary";
4
+ import { SdkDtoGenerator } from "./SdkDtoGenerator";
5
+
6
+ export namespace SdkTypeDefiner {
7
+ export const name =
8
+ (config: INestiaConfig) =>
9
+ (importer: ImportDictionary) =>
10
+ (p: IRoute.IParameter | IRoute.IOutput): string =>
11
+ p.metadata
12
+ ? SdkDtoGenerator.decode(config)(importer)(p.metadata)
13
+ : p.typeName;
14
+
15
+ export const headers =
16
+ (config: INestiaConfig) =>
17
+ (importer: ImportDictionary) =>
18
+ (param: IRoute.IParameter): string => {
19
+ const type: string = name(config)(importer)(param);
20
+ if (config.primitive === false) return type;
21
+
22
+ const resolved: string = importer.external({
23
+ type: true,
24
+ library: "@nestia/fetcher",
25
+ instance: "Resolved",
26
+ });
27
+ return `${resolved}<${type}>`;
28
+ };
29
+
30
+ export const query =
31
+ (config: INestiaConfig) =>
32
+ (importer: ImportDictionary) =>
33
+ (param: IRoute.IParameter): string => {
34
+ const type: string = name(config)(importer)(param);
35
+ if (config.primitive === false) return type;
36
+
37
+ const resolved: string = importer.external({
38
+ type: true,
39
+ library: "@nestia/fetcher",
40
+ instance: "Resolved",
41
+ });
42
+ return `${resolved}<${type}>`;
43
+ };
44
+
45
+ export const input =
46
+ (config: INestiaConfig) =>
47
+ (importer: ImportDictionary) =>
48
+ (param: IRoute.IParameter): string => {
49
+ const type: string = name(config)(importer)(param);
50
+ if (config.primitive === false) return type;
51
+
52
+ const primitive: string = importer.external({
53
+ type: true,
54
+ library: "@nestia/fetcher",
55
+ instance: "Primitive",
56
+ });
57
+ return `${primitive}<${type}>`;
58
+ };
59
+
60
+ export const output =
61
+ (config: INestiaConfig) =>
62
+ (importer: ImportDictionary) =>
63
+ (route: IRoute): string => {
64
+ if (config.propagate !== true) {
65
+ const type: string = name(config)(importer)(route.output);
66
+ if (type === "void" || config.primitive === false) return type;
67
+
68
+ const wrapper: string = importer.external({
69
+ type: true,
70
+ library: "@nestia/fetcher",
71
+ instance:
72
+ route.output.contentType === "application/x-www-form-urlencoded"
73
+ ? "Resolved"
74
+ : "Primitive",
75
+ });
76
+ return `${wrapper}<${type}>`;
77
+ }
78
+
79
+ const propagation: string = importer.external({
80
+ type: true,
81
+ library: "@nestia/fetcher",
82
+ instance: "IPropagation",
83
+ });
84
+ const branches: IBranch[] = [
85
+ {
86
+ status: String(route.status ?? (route.method === "POST" ? 201 : 200)),
87
+ type: name(config)(importer)(route.output),
88
+ },
89
+ ...Object.entries(route.exceptions).map(([status, value]) => ({
90
+ status,
91
+ type: name(config)(importer)(value),
92
+ })),
93
+ ];
94
+ return (
95
+ `${propagation}<{\n` +
96
+ branches
97
+ .map(
98
+ (b) =>
99
+ ` ${
100
+ b.status.endsWith("XX") ? `"${b.status}"` : b.status
101
+ }: ${b.type};`,
102
+ )
103
+ .join("\n") +
104
+ "\n" +
105
+ ` }${route.status ? `, ${route.status}` : ""}>`
106
+ );
107
+ };
108
+
109
+ export const responseBody =
110
+ (config: INestiaConfig) =>
111
+ (importer: ImportDictionary) =>
112
+ (route: IRoute): string =>
113
+ output({ ...config, propagate: false })(importer)(route);
114
+ }
115
+
116
+ interface IBranch {
117
+ status: string;
118
+ type: string;
119
+ }
@@ -151,7 +151,15 @@ export namespace SwaggerSchemaGenerator {
151
151
  schema: coalesce(props)(result),
152
152
  },
153
153
  },
154
- "x-nestia-encrypted": route.encrypted,
154
+ ...(props.config.additional === true
155
+ ? {
156
+ "x-nestia-encrypted": route.encrypted,
157
+ }
158
+ : route.encrypted === true
159
+ ? {
160
+ "x-nestia-encrypted": true,
161
+ }
162
+ : {}),
155
163
  };
156
164
  return output;
157
165
  };
@@ -206,7 +214,15 @@ export namespace SwaggerSchemaGenerator {
206
214
  },
207
215
  },
208
216
  required: true,
209
- "x-nestia-encrypted": encrypted,
217
+ ...(props.config.additional === true
218
+ ? {
219
+ "x-nestia-encrypted": encrypted,
220
+ }
221
+ : encrypted === true
222
+ ? {
223
+ "x-nestia-encrypted": true,
224
+ }
225
+ : {}),
210
226
  };
211
227
  };
212
228
 
@@ -1,198 +1,198 @@
1
- import { MetadataFactory } from "typia/lib/factories/MetadataFactory";
2
- import { Metadata } from "typia/lib/schemas/metadata/Metadata";
3
- import { MetadataArray } from "typia/lib/schemas/metadata/MetadataArray";
4
-
5
- export namespace SwaggerSchemaValidator {
6
- export const path = (meta: Metadata): string[] => {
7
- const errors: string[] = [];
8
- const insert = (msg: string) => errors.push(msg);
9
-
10
- if (meta.any) insert("do not allow any type");
11
- if (meta.isRequired() === false) insert("do not allow undefindable type");
12
-
13
- const atomics = CoreMetadataUtil.atomics(meta);
14
- const expected: number =
15
- meta.atomics.length +
16
- meta.templates.length +
17
- meta.constants.map((c) => c.values.length).reduce((a, b) => a + b, 0);
18
- if (meta.size() !== expected || atomics.size === 0)
19
- insert("only atomic or constant types are allowed");
20
- if (atomics.size > 1) insert("do not allow union type");
21
-
22
- return errors;
23
- };
24
-
25
- export const query = (
26
- meta: Metadata,
27
- explore: MetadataFactory.IExplore,
28
- ): string[] => {
29
- const errors: string[] = [];
30
- const insert = (msg: string) => errors.push(msg);
31
-
32
- if (explore.top === true) {
33
- // TOP MUST BE ONLY OBJECT
34
- if (meta.objects.length !== 1 || meta.bucket() !== 1)
35
- insert("only one object type is allowed.");
36
- if (meta.nullable === true) insert("query parameters cannot be null.");
37
- if (meta.isRequired() === false)
38
- insert("query parameters cannot be undefined.");
39
- } else if (
40
- explore.nested !== null &&
41
- explore.nested instanceof MetadataArray
42
- ) {
43
- const atomics = CoreMetadataUtil.atomics(meta);
44
- const expected: number =
45
- meta.atomics.length +
46
- meta.templates.length +
47
- meta.constants.map((c) => c.values.length).reduce((a, b) => a + b, 0);
48
- if (atomics.size > 1) insert("union type is not allowed in array.");
49
- if (meta.nullable) insert("nullable type is not allowed in array.");
50
- if (meta.isRequired() === false)
51
- insert("optional type is not allowed in array.");
52
- if (meta.size() !== expected)
53
- insert("only atomic or constant types are allowed in array.");
54
- } else if (explore.object && explore.property !== null) {
55
- //----
56
- // COMMON
57
- //----
58
- // PROPERTY MUST BE SOLE
59
- if (typeof explore.property === "object")
60
- insert("dynamic property is not allowed.");
61
- // DO NOT ALLOW TUPLE TYPE
62
- if (meta.tuples.length) insert("tuple type is not allowed.");
63
- // DO NOT ALLOW UNION TYPE
64
- if (CoreMetadataUtil.isUnion(meta)) insert("union type is not allowed.");
65
- // DO NOT ALLOW NESTED OBJECT
66
- if (
67
- meta.objects.length ||
68
- meta.sets.length ||
69
- meta.maps.length ||
70
- meta.natives.length
71
- )
72
- insert("nested object type is not allowed.");
73
-
74
- //----
75
- // ARRAY CASES
76
- //----
77
- const isArray: boolean = meta.arrays.length > 1 || meta.tuples.length > 1;
78
- // ARRAY TYPE MUST BE REQUIRED
79
- if (isArray && meta.isRequired() === false)
80
- insert("optional type is not allowed when array.");
81
- // SET-COOKIE MUST BE ARRAY
82
- if (explore.property === "set-cookie" && !isArray)
83
- insert("set-cookie property must be array.");
84
- }
85
- return errors;
86
- };
87
-
88
- export const headers = (
89
- meta: Metadata,
90
- explore: MetadataFactory.IExplore,
91
- ): string[] => {
92
- const errors: string[] = [];
93
- const insert = (msg: string) => errors.push(msg);
94
-
95
- if (explore.top === true) {
96
- // TOP MUST BE ONLY OBJECT
97
- if (meta.objects.length !== 1 || meta.bucket() !== 1)
98
- insert("only one object type is allowed.");
99
- if (meta.nullable === true) insert("headers cannot be null.");
100
- if (meta.isRequired() === false) insert("headers cannot be null.");
101
- } else if (
102
- explore.nested !== null &&
103
- explore.nested instanceof MetadataArray
104
- ) {
105
- const atomics = CoreMetadataUtil.atomics(meta);
106
- const expected: number =
107
- meta.atomics.length +
108
- meta.templates.length +
109
- meta.constants.map((c) => c.values.length).reduce((a, b) => a + b, 0);
110
- if (atomics.size > 1) insert("union type is not allowed in array.");
111
- if (meta.nullable) insert("nullable type is not allowed in array.");
112
- if (meta.isRequired() === false) insert("optional type is not allowed.");
113
- if (meta.size() !== expected)
114
- insert("only atomic or constant types are allowed in array.");
115
- } else if (explore.object && explore.property !== null) {
116
- //----
117
- // COMMON
118
- //----
119
- // PROPERTY MUST BE SOLE
120
- if (typeof explore.property === "object")
121
- insert("dynamic property is not allowed.");
122
- // DO NOT ALLOW TUPLE TYPE
123
- if (meta.tuples.length) insert("tuple type is not allowed.");
124
- // DO NOT ALLOW UNION TYPE
125
- if (CoreMetadataUtil.isUnion(meta)) insert("union type is not allowed.");
126
- // DO NOT ALLOW NESTED OBJECT
127
- if (
128
- meta.objects.length ||
129
- meta.sets.length ||
130
- meta.maps.length ||
131
- meta.natives.length
132
- )
133
- insert("nested object type is not allowed.");
134
- // DO NOT ALLOW NULLABLE
135
- if (meta.nullable) insert("nullable type is not allowed.");
136
-
137
- //----
138
- // ARRAY CASES
139
- //----
140
- const isArray: boolean = meta.arrays.length > 1;
141
- // ARRAY TYPE MUST BE REQUIRED
142
- if (isArray && meta.isRequired() === false)
143
- insert("optional type is not allowed when array.");
144
- // SET-COOKIE MUST BE ARRAY
145
- if (explore.property === "set-cookie" && !isArray)
146
- insert("set-cookie property must be array.");
147
- // MUST BE SINGULAR CASE
148
- if (
149
- typeof explore.property === "string" &&
150
- SINGULAR.has(explore.property) &&
151
- isArray
152
- )
153
- insert("property cannot be array.");
154
- }
155
- return errors;
156
- };
157
- }
158
-
159
- namespace CoreMetadataUtil {
160
- export const atomics = (
161
- meta: Metadata,
162
- ): Set<"boolean" | "bigint" | "number" | "string"> =>
163
- new Set([
164
- ...meta.atomics.map((a) => a.type),
165
- ...meta.constants.map((c) => c.type),
166
- ...(meta.templates.length ? (["string"] as const) : []),
167
- ]);
168
-
169
- export const isUnion = (meta: Metadata): boolean =>
170
- atomics(meta).size +
171
- meta.arrays.length +
172
- meta.tuples.length +
173
- meta.natives.length +
174
- meta.maps.length +
175
- meta.objects.length >
176
- 1;
177
- }
178
-
179
- const SINGULAR: Set<string> = new Set([
180
- "age",
181
- "authorization",
182
- "content-length",
183
- "content-type",
184
- "etag",
185
- "expires",
186
- "from",
187
- "host",
188
- "if-modified-since",
189
- "if-unmodified-since",
190
- "last-modified",
191
- "location",
192
- "max-forwards",
193
- "proxy-authorization",
194
- "referer",
195
- "retry-after",
196
- "server",
197
- "user-agent",
198
- ]);
1
+ import { MetadataFactory } from "typia/lib/factories/MetadataFactory";
2
+ import { Metadata } from "typia/lib/schemas/metadata/Metadata";
3
+ import { MetadataArray } from "typia/lib/schemas/metadata/MetadataArray";
4
+
5
+ export namespace SwaggerSchemaValidator {
6
+ export const path = (meta: Metadata): string[] => {
7
+ const errors: string[] = [];
8
+ const insert = (msg: string) => errors.push(msg);
9
+
10
+ if (meta.any) insert("do not allow any type");
11
+ if (meta.isRequired() === false) insert("do not allow undefindable type");
12
+
13
+ const atomics = CoreMetadataUtil.atomics(meta);
14
+ const expected: number =
15
+ meta.atomics.length +
16
+ meta.templates.length +
17
+ meta.constants.map((c) => c.values.length).reduce((a, b) => a + b, 0);
18
+ if (meta.size() !== expected || atomics.size === 0)
19
+ insert("only atomic or constant types are allowed");
20
+ if (atomics.size > 1) insert("do not allow union type");
21
+
22
+ return errors;
23
+ };
24
+
25
+ export const query = (
26
+ meta: Metadata,
27
+ explore: MetadataFactory.IExplore,
28
+ ): string[] => {
29
+ const errors: string[] = [];
30
+ const insert = (msg: string) => errors.push(msg);
31
+
32
+ if (explore.top === true) {
33
+ // TOP MUST BE ONLY OBJECT
34
+ if (meta.objects.length !== 1 || meta.bucket() !== 1)
35
+ insert("only one object type is allowed.");
36
+ if (meta.nullable === true) insert("query parameters cannot be null.");
37
+ if (meta.isRequired() === false)
38
+ insert("query parameters cannot be undefined.");
39
+ } else if (
40
+ explore.nested !== null &&
41
+ explore.nested instanceof MetadataArray
42
+ ) {
43
+ const atomics = CoreMetadataUtil.atomics(meta);
44
+ const expected: number =
45
+ meta.atomics.length +
46
+ meta.templates.length +
47
+ meta.constants.map((c) => c.values.length).reduce((a, b) => a + b, 0);
48
+ if (atomics.size > 1) insert("union type is not allowed in array.");
49
+ if (meta.nullable) insert("nullable type is not allowed in array.");
50
+ if (meta.isRequired() === false)
51
+ insert("optional type is not allowed in array.");
52
+ if (meta.size() !== expected)
53
+ insert("only atomic or constant types are allowed in array.");
54
+ } else if (explore.object && explore.property !== null) {
55
+ //----
56
+ // COMMON
57
+ //----
58
+ // PROPERTY MUST BE SOLE
59
+ if (typeof explore.property === "object")
60
+ insert("dynamic property is not allowed.");
61
+ // DO NOT ALLOW TUPLE TYPE
62
+ if (meta.tuples.length) insert("tuple type is not allowed.");
63
+ // DO NOT ALLOW UNION TYPE
64
+ if (CoreMetadataUtil.isUnion(meta)) insert("union type is not allowed.");
65
+ // DO NOT ALLOW NESTED OBJECT
66
+ if (
67
+ meta.objects.length ||
68
+ meta.sets.length ||
69
+ meta.maps.length ||
70
+ meta.natives.length
71
+ )
72
+ insert("nested object type is not allowed.");
73
+
74
+ //----
75
+ // ARRAY CASES
76
+ //----
77
+ const isArray: boolean = meta.arrays.length > 1 || meta.tuples.length > 1;
78
+ // ARRAY TYPE MUST BE REQUIRED
79
+ if (isArray && meta.isRequired() === false)
80
+ insert("optional type is not allowed when array.");
81
+ // SET-COOKIE MUST BE ARRAY
82
+ if (explore.property === "set-cookie" && !isArray)
83
+ insert("set-cookie property must be array.");
84
+ }
85
+ return errors;
86
+ };
87
+
88
+ export const headers = (
89
+ meta: Metadata,
90
+ explore: MetadataFactory.IExplore,
91
+ ): string[] => {
92
+ const errors: string[] = [];
93
+ const insert = (msg: string) => errors.push(msg);
94
+
95
+ if (explore.top === true) {
96
+ // TOP MUST BE ONLY OBJECT
97
+ if (meta.objects.length !== 1 || meta.bucket() !== 1)
98
+ insert("only one object type is allowed.");
99
+ if (meta.nullable === true) insert("headers cannot be null.");
100
+ if (meta.isRequired() === false) insert("headers cannot be null.");
101
+ } else if (
102
+ explore.nested !== null &&
103
+ explore.nested instanceof MetadataArray
104
+ ) {
105
+ const atomics = CoreMetadataUtil.atomics(meta);
106
+ const expected: number =
107
+ meta.atomics.length +
108
+ meta.templates.length +
109
+ meta.constants.map((c) => c.values.length).reduce((a, b) => a + b, 0);
110
+ if (atomics.size > 1) insert("union type is not allowed in array.");
111
+ if (meta.nullable) insert("nullable type is not allowed in array.");
112
+ if (meta.isRequired() === false) insert("optional type is not allowed.");
113
+ if (meta.size() !== expected)
114
+ insert("only atomic or constant types are allowed in array.");
115
+ } else if (explore.object && explore.property !== null) {
116
+ //----
117
+ // COMMON
118
+ //----
119
+ // PROPERTY MUST BE SOLE
120
+ if (typeof explore.property === "object")
121
+ insert("dynamic property is not allowed.");
122
+ // DO NOT ALLOW TUPLE TYPE
123
+ if (meta.tuples.length) insert("tuple type is not allowed.");
124
+ // DO NOT ALLOW UNION TYPE
125
+ if (CoreMetadataUtil.isUnion(meta)) insert("union type is not allowed.");
126
+ // DO NOT ALLOW NESTED OBJECT
127
+ if (
128
+ meta.objects.length ||
129
+ meta.sets.length ||
130
+ meta.maps.length ||
131
+ meta.natives.length
132
+ )
133
+ insert("nested object type is not allowed.");
134
+ // DO NOT ALLOW NULLABLE
135
+ if (meta.nullable) insert("nullable type is not allowed.");
136
+
137
+ //----
138
+ // ARRAY CASES
139
+ //----
140
+ const isArray: boolean = meta.arrays.length > 1;
141
+ // ARRAY TYPE MUST BE REQUIRED
142
+ if (isArray && meta.isRequired() === false)
143
+ insert("optional type is not allowed when array.");
144
+ // SET-COOKIE MUST BE ARRAY
145
+ if (explore.property === "set-cookie" && !isArray)
146
+ insert("set-cookie property must be array.");
147
+ // MUST BE SINGULAR CASE
148
+ if (
149
+ typeof explore.property === "string" &&
150
+ SINGULAR.has(explore.property) &&
151
+ isArray
152
+ )
153
+ insert("property cannot be array.");
154
+ }
155
+ return errors;
156
+ };
157
+ }
158
+
159
+ namespace CoreMetadataUtil {
160
+ export const atomics = (
161
+ meta: Metadata,
162
+ ): Set<"boolean" | "bigint" | "number" | "string"> =>
163
+ new Set([
164
+ ...meta.atomics.map((a) => a.type),
165
+ ...meta.constants.map((c) => c.type),
166
+ ...(meta.templates.length ? (["string"] as const) : []),
167
+ ]);
168
+
169
+ export const isUnion = (meta: Metadata): boolean =>
170
+ atomics(meta).size +
171
+ meta.arrays.length +
172
+ meta.tuples.length +
173
+ meta.natives.length +
174
+ meta.maps.length +
175
+ meta.objects.length >
176
+ 1;
177
+ }
178
+
179
+ const SINGULAR: Set<string> = new Set([
180
+ "age",
181
+ "authorization",
182
+ "content-length",
183
+ "content-type",
184
+ "etag",
185
+ "expires",
186
+ "from",
187
+ "host",
188
+ "if-modified-since",
189
+ "if-unmodified-since",
190
+ "last-modified",
191
+ "location",
192
+ "max-forwards",
193
+ "proxy-authorization",
194
+ "referer",
195
+ "retry-after",
196
+ "server",
197
+ "user-agent",
198
+ ]);