@nestia/sdk 5.0.0 → 6.0.0-dev.20250406

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 (112) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +92 -92
  3. package/assets/bundle/api/HttpError.ts +1 -1
  4. package/assets/bundle/api/IConnection.ts +1 -1
  5. package/assets/bundle/api/Primitive.ts +1 -1
  6. package/assets/bundle/api/Resolved.ts +1 -1
  7. package/assets/bundle/api/index.ts +4 -4
  8. package/assets/bundle/api/module.ts +6 -6
  9. package/assets/bundle/distribute/README.md +37 -37
  10. package/assets/bundle/distribute/package.json +28 -28
  11. package/assets/bundle/distribute/tsconfig.json +109 -109
  12. package/assets/bundle/e2e/index.ts +42 -42
  13. package/assets/config/nestia.config.ts +97 -97
  14. package/lib/INestiaConfig.d.ts +1 -1
  15. package/lib/executable/internal/NestiaConfigLoader.js +4 -4
  16. package/lib/executable/sdk.js +12 -12
  17. package/lib/generates/SwaggerGenerator.js +1 -1
  18. package/lib/generates/SwaggerGenerator.js.map +1 -1
  19. package/package.json +9 -9
  20. package/src/INestiaConfig.ts +271 -271
  21. package/src/NestiaSdkApplication.ts +307 -307
  22. package/src/NestiaSwaggerComposer.ts +138 -138
  23. package/src/analyses/AccessorAnalyzer.ts +67 -67
  24. package/src/analyses/ConfigAnalyzer.ts +155 -155
  25. package/src/analyses/ExceptionAnalyzer.ts +154 -154
  26. package/src/analyses/GenericAnalyzer.ts +49 -49
  27. package/src/analyses/ImportAnalyzer.ts +171 -171
  28. package/src/analyses/PathAnalyzer.ts +69 -69
  29. package/src/analyses/ReflectControllerAnalyzer.ts +105 -105
  30. package/src/analyses/ReflectHttpOperationAnalyzer.ts +183 -183
  31. package/src/analyses/ReflectHttpOperationExceptionAnalyzer.ts +71 -71
  32. package/src/analyses/ReflectHttpOperationParameterAnalyzer.ts +348 -348
  33. package/src/analyses/ReflectHttpOperationResponseAnalyzer.ts +127 -127
  34. package/src/analyses/ReflectMetadataAnalyzer.ts +44 -44
  35. package/src/analyses/ReflectWebSocketOperationAnalyzer.ts +172 -172
  36. package/src/analyses/SecurityAnalyzer.ts +25 -25
  37. package/src/analyses/TypedHttpRouteAnalyzer.ts +186 -186
  38. package/src/analyses/TypedWebSocketRouteAnalyzer.ts +18 -18
  39. package/src/decorators/OperationMetadata.ts +15 -15
  40. package/src/executable/internal/CommandParser.ts +15 -15
  41. package/src/executable/internal/NestiaConfigLoader.ts +78 -78
  42. package/src/executable/internal/NestiaSdkCommand.ts +103 -103
  43. package/src/executable/sdk.ts +75 -75
  44. package/src/generates/CloneGenerator.ts +66 -66
  45. package/src/generates/E2eGenerator.ts +32 -32
  46. package/src/generates/SdkGenerator.ts +159 -159
  47. package/src/generates/SwaggerGenerator.ts +292 -292
  48. package/src/generates/internal/E2eFileProgrammer.ts +183 -183
  49. package/src/generates/internal/FilePrinter.ts +53 -53
  50. package/src/generates/internal/ImportDictionary.ts +147 -147
  51. package/src/generates/internal/SdkAliasCollection.ts +185 -185
  52. package/src/generates/internal/SdkDistributionComposer.ts +103 -103
  53. package/src/generates/internal/SdkFileProgrammer.ts +116 -116
  54. package/src/generates/internal/SdkHttpCloneProgrammer.ts +124 -124
  55. package/src/generates/internal/SdkHttpCloneReferencer.ts +71 -71
  56. package/src/generates/internal/SdkHttpFunctionProgrammer.ts +301 -301
  57. package/src/generates/internal/SdkHttpNamespaceProgrammer.ts +529 -529
  58. package/src/generates/internal/SdkHttpRouteProgrammer.ts +117 -117
  59. package/src/generates/internal/SdkHttpSimulationProgrammer.ts +362 -362
  60. package/src/generates/internal/SdkImportWizard.ts +55 -55
  61. package/src/generates/internal/SdkRouteDirectory.ts +18 -18
  62. package/src/generates/internal/SdkTypeProgrammer.ts +377 -377
  63. package/src/generates/internal/SdkTypeTagProgrammer.ts +102 -102
  64. package/src/generates/internal/SdkWebSocketNamespaceProgrammer.ts +362 -362
  65. package/src/generates/internal/SdkWebSocketRouteProgrammer.ts +265 -265
  66. package/src/generates/internal/SwaggerDescriptionComposer.ts +64 -64
  67. package/src/generates/internal/SwaggerOperationComposer.ts +119 -119
  68. package/src/generates/internal/SwaggerOperationParameterComposer.ts +177 -177
  69. package/src/generates/internal/SwaggerOperationResponseComposer.ts +110 -110
  70. package/src/index.ts +4 -4
  71. package/src/module.ts +3 -3
  72. package/src/structures/INestiaProject.ts +13 -13
  73. package/src/structures/INestiaSdkInput.ts +20 -20
  74. package/src/structures/IReflectApplication.ts +8 -8
  75. package/src/structures/IReflectController.ts +15 -15
  76. package/src/structures/IReflectHttpOperation.ts +26 -26
  77. package/src/structures/IReflectHttpOperationException.ts +19 -19
  78. package/src/structures/IReflectHttpOperationParameter.ts +81 -81
  79. package/src/structures/IReflectHttpOperationSuccess.ts +22 -22
  80. package/src/structures/IReflectOperationError.ts +26 -26
  81. package/src/structures/IReflectType.ts +4 -4
  82. package/src/structures/IReflectTypeImport.ts +4 -4
  83. package/src/structures/IReflectWebSocketOperation.ts +17 -17
  84. package/src/structures/IReflectWebSocketOperationParameter.ts +38 -38
  85. package/src/structures/ITypedApplication.ts +11 -11
  86. package/src/structures/ITypedHttpRoute.ts +30 -30
  87. package/src/structures/ITypedHttpRouteException.ts +15 -15
  88. package/src/structures/ITypedHttpRouteParameter.ts +41 -41
  89. package/src/structures/ITypedHttpRouteSuccess.ts +22 -22
  90. package/src/structures/ITypedWebSocketRoute.ts +20 -20
  91. package/src/structures/ITypedWebSocketRouteParameter.ts +3 -3
  92. package/src/structures/MethodType.ts +5 -5
  93. package/src/structures/ParamCategory.ts +1 -1
  94. package/src/structures/TypeEntry.ts +22 -22
  95. package/src/transform.ts +9 -9
  96. package/src/transformers/IOperationMetadata.ts +44 -44
  97. package/src/transformers/ISdkOperationTransformerContext.ts +8 -8
  98. package/src/transformers/SdkOperationProgrammer.ts +209 -209
  99. package/src/transformers/SdkOperationTransformer.ts +253 -253
  100. package/src/transformers/TextPlainValidator.ts +17 -17
  101. package/src/typings/get-function-location.d.ts +7 -7
  102. package/src/utils/ArrayUtil.ts +26 -26
  103. package/src/utils/FileRetriever.ts +22 -22
  104. package/src/utils/MapUtil.ts +14 -14
  105. package/src/utils/MetadataUtil.ts +26 -26
  106. package/src/utils/PathUtil.ts +10 -10
  107. package/src/utils/SourceFinder.ts +66 -66
  108. package/src/utils/StringUtil.ts +17 -17
  109. package/src/utils/StripEnums.ts +5 -5
  110. package/src/utils/VersioningStrategy.ts +28 -28
  111. package/src/validators/HttpHeadersValidator.ts +34 -34
  112. package/src/validators/HttpQueryValidator.ts +34 -34
@@ -1,147 +1,147 @@
1
- import path from "path";
2
- import { HashMap, HashSet, Pair } from "tstl";
3
- import ts from "typescript";
4
-
5
- import { FilePrinter } from "./FilePrinter";
6
-
7
- export class ImportDictionary {
8
- private readonly components_: HashMap<Pair<string, boolean>, IComposition> =
9
- new HashMap();
10
-
11
- public constructor(public readonly file: string) {}
12
-
13
- public empty(): boolean {
14
- return this.components_.empty();
15
- }
16
-
17
- public external(props: ImportDictionary.IExternalProps): string {
18
- const composition: IComposition = this.components_.take(
19
- new Pair(`node_modules/${props.library}`, props.type),
20
- () => ({
21
- location: `node_modules/${props.library}`,
22
- elements: new HashSet(),
23
- default: false,
24
- type: props.type,
25
- }),
26
- );
27
- if (props.instance === null) composition.default = true;
28
- else composition.elements.insert(props.instance);
29
- return props.instance ?? props.library;
30
- }
31
-
32
- public internal(props: ImportDictionary.IInternalProps): string {
33
- const file: string = (() => {
34
- if (props.file.substring(props.file.length - 5) === ".d.ts")
35
- return props.file.substring(0, props.file.length - 5);
36
- else if (props.file.substring(props.file.length - 3) === ".ts")
37
- return props.file.substring(0, props.file.length - 3);
38
- return props.file;
39
- })();
40
- const composition: IComposition = this.components_.take(
41
- new Pair(file, props.type),
42
- () => ({
43
- location: file,
44
- elements: new HashSet(),
45
- default: false,
46
- type: props.type,
47
- }),
48
- );
49
- if (props.instance === null) {
50
- composition.default = true;
51
- if (props.name) composition.name = props.name;
52
- } else composition.elements.insert(props.instance);
53
- return props.instance ?? file;
54
- }
55
-
56
- public toStatements(outDir: string): ts.Statement[] {
57
- const external: ts.ImportDeclaration[] = [];
58
- const internal: ts.ImportDeclaration[] = [];
59
-
60
- const locator = (str: string) => {
61
- const location: string = path.relative(outDir, str).split("\\").join("/");
62
- const index: number = location.lastIndexOf(NODE_MODULES);
63
- return index === -1
64
- ? location.startsWith("..")
65
- ? location
66
- : `./${location}`
67
- : location.substring(index + NODE_MODULES.length);
68
- };
69
- const enroll =
70
- (filter: (str: string) => boolean) =>
71
- (container: ts.ImportDeclaration[]) => {
72
- const compositions: IComposition[] = this.components_
73
- .toJSON()
74
- .filter((c) => filter(c.second.location))
75
- .map((e) => ({
76
- ...e.second,
77
- location: locator(e.second.location),
78
- }))
79
- .sort((a, b) => a.location.localeCompare(b.location));
80
- for (const c of compositions) {
81
- const brackets: string[] = [];
82
- if (c.default) brackets.push(c.name ?? c.location);
83
- if (c.elements.empty() === false)
84
- brackets.push(
85
- `{ ${c.elements
86
- .toJSON()
87
- .sort((a, b) => a.localeCompare(b))
88
- .join(", ")} }`,
89
- );
90
- container.push(
91
- ts.factory.createImportDeclaration(
92
- undefined,
93
- ts.factory.createImportClause(
94
- c.type,
95
- c.default
96
- ? ts.factory.createIdentifier(c.name ?? c.location)
97
- : undefined,
98
- c.elements.empty() === false
99
- ? ts.factory.createNamedImports(
100
- [...c.elements].map((elem) =>
101
- ts.factory.createImportSpecifier(
102
- false,
103
- undefined,
104
- ts.factory.createIdentifier(elem),
105
- ),
106
- ),
107
- )
108
- : undefined,
109
- ),
110
- ts.factory.createStringLiteral(c.location),
111
- ),
112
- );
113
- }
114
- };
115
-
116
- enroll((str) => str.indexOf(NODE_MODULES) !== -1)(external);
117
- enroll((str) => str.indexOf(NODE_MODULES) === -1)(internal);
118
- return [
119
- ...external,
120
- ...(external.length && internal.length ? [FilePrinter.enter()] : []),
121
- ...internal,
122
- ];
123
- }
124
- }
125
- export namespace ImportDictionary {
126
- export interface IExternalProps {
127
- type: boolean;
128
- library: string;
129
- instance: string | null;
130
- }
131
- export interface IInternalProps {
132
- type: boolean;
133
- file: string;
134
- instance: string | null;
135
- name?: string | null;
136
- }
137
- }
138
-
139
- interface IComposition {
140
- location: string;
141
- type: boolean;
142
- default: boolean;
143
- name?: string;
144
- elements: HashSet<string>;
145
- }
146
-
147
- const NODE_MODULES = "node_modules/";
1
+ import path from "path";
2
+ import { HashMap, HashSet, Pair } from "tstl";
3
+ import ts from "typescript";
4
+
5
+ import { FilePrinter } from "./FilePrinter";
6
+
7
+ export class ImportDictionary {
8
+ private readonly components_: HashMap<Pair<string, boolean>, IComposition> =
9
+ new HashMap();
10
+
11
+ public constructor(public readonly file: string) {}
12
+
13
+ public empty(): boolean {
14
+ return this.components_.empty();
15
+ }
16
+
17
+ public external(props: ImportDictionary.IExternalProps): string {
18
+ const composition: IComposition = this.components_.take(
19
+ new Pair(`node_modules/${props.library}`, props.type),
20
+ () => ({
21
+ location: `node_modules/${props.library}`,
22
+ elements: new HashSet(),
23
+ default: false,
24
+ type: props.type,
25
+ }),
26
+ );
27
+ if (props.instance === null) composition.default = true;
28
+ else composition.elements.insert(props.instance);
29
+ return props.instance ?? props.library;
30
+ }
31
+
32
+ public internal(props: ImportDictionary.IInternalProps): string {
33
+ const file: string = (() => {
34
+ if (props.file.substring(props.file.length - 5) === ".d.ts")
35
+ return props.file.substring(0, props.file.length - 5);
36
+ else if (props.file.substring(props.file.length - 3) === ".ts")
37
+ return props.file.substring(0, props.file.length - 3);
38
+ return props.file;
39
+ })();
40
+ const composition: IComposition = this.components_.take(
41
+ new Pair(file, props.type),
42
+ () => ({
43
+ location: file,
44
+ elements: new HashSet(),
45
+ default: false,
46
+ type: props.type,
47
+ }),
48
+ );
49
+ if (props.instance === null) {
50
+ composition.default = true;
51
+ if (props.name) composition.name = props.name;
52
+ } else composition.elements.insert(props.instance);
53
+ return props.instance ?? file;
54
+ }
55
+
56
+ public toStatements(outDir: string): ts.Statement[] {
57
+ const external: ts.ImportDeclaration[] = [];
58
+ const internal: ts.ImportDeclaration[] = [];
59
+
60
+ const locator = (str: string) => {
61
+ const location: string = path.relative(outDir, str).split("\\").join("/");
62
+ const index: number = location.lastIndexOf(NODE_MODULES);
63
+ return index === -1
64
+ ? location.startsWith("..")
65
+ ? location
66
+ : `./${location}`
67
+ : location.substring(index + NODE_MODULES.length);
68
+ };
69
+ const enroll =
70
+ (filter: (str: string) => boolean) =>
71
+ (container: ts.ImportDeclaration[]) => {
72
+ const compositions: IComposition[] = this.components_
73
+ .toJSON()
74
+ .filter((c) => filter(c.second.location))
75
+ .map((e) => ({
76
+ ...e.second,
77
+ location: locator(e.second.location),
78
+ }))
79
+ .sort((a, b) => a.location.localeCompare(b.location));
80
+ for (const c of compositions) {
81
+ const brackets: string[] = [];
82
+ if (c.default) brackets.push(c.name ?? c.location);
83
+ if (c.elements.empty() === false)
84
+ brackets.push(
85
+ `{ ${c.elements
86
+ .toJSON()
87
+ .sort((a, b) => a.localeCompare(b))
88
+ .join(", ")} }`,
89
+ );
90
+ container.push(
91
+ ts.factory.createImportDeclaration(
92
+ undefined,
93
+ ts.factory.createImportClause(
94
+ c.type,
95
+ c.default
96
+ ? ts.factory.createIdentifier(c.name ?? c.location)
97
+ : undefined,
98
+ c.elements.empty() === false
99
+ ? ts.factory.createNamedImports(
100
+ [...c.elements].map((elem) =>
101
+ ts.factory.createImportSpecifier(
102
+ false,
103
+ undefined,
104
+ ts.factory.createIdentifier(elem),
105
+ ),
106
+ ),
107
+ )
108
+ : undefined,
109
+ ),
110
+ ts.factory.createStringLiteral(c.location),
111
+ ),
112
+ );
113
+ }
114
+ };
115
+
116
+ enroll((str) => str.indexOf(NODE_MODULES) !== -1)(external);
117
+ enroll((str) => str.indexOf(NODE_MODULES) === -1)(internal);
118
+ return [
119
+ ...external,
120
+ ...(external.length && internal.length ? [FilePrinter.enter()] : []),
121
+ ...internal,
122
+ ];
123
+ }
124
+ }
125
+ export namespace ImportDictionary {
126
+ export interface IExternalProps {
127
+ type: boolean;
128
+ library: string;
129
+ instance: string | null;
130
+ }
131
+ export interface IInternalProps {
132
+ type: boolean;
133
+ file: string;
134
+ instance: string | null;
135
+ name?: string | null;
136
+ }
137
+ }
138
+
139
+ interface IComposition {
140
+ location: string;
141
+ type: boolean;
142
+ default: boolean;
143
+ name?: string;
144
+ elements: HashSet<string>;
145
+ }
146
+
147
+ const NODE_MODULES = "node_modules/";
@@ -1,185 +1,185 @@
1
- import ts from "typescript";
2
- import typia from "typia";
3
- import { TypeFactory } from "typia/lib/factories/TypeFactory";
4
- import { Metadata } from "typia/lib/schemas/metadata/Metadata";
5
-
6
- import { INestiaProject } from "../../structures/INestiaProject";
7
- import { IReflectHttpOperationParameter } from "../../structures/IReflectHttpOperationParameter";
8
- import { IReflectType } from "../../structures/IReflectType";
9
- import { ITypedHttpRoute } from "../../structures/ITypedHttpRoute";
10
- import { ITypedHttpRouteParameter } from "../../structures/ITypedHttpRouteParameter";
11
- import { ImportDictionary } from "./ImportDictionary";
12
- import { SdkTypeProgrammer } from "./SdkTypeProgrammer";
13
-
14
- export namespace SdkAliasCollection {
15
- export const name = ({ type }: { type: IReflectType }): ts.TypeNode =>
16
- ts.factory.createTypeReferenceNode(
17
- type.name,
18
- type.typeArguments
19
- ? type.typeArguments.map((a) => name({ type: a }))
20
- : undefined,
21
- );
22
-
23
- export const from =
24
- (project: INestiaProject) =>
25
- (importer: ImportDictionary) =>
26
- (metadata: Metadata) =>
27
- SdkTypeProgrammer.write(project)(importer)(metadata);
28
-
29
- export const headers =
30
- (project: INestiaProject) =>
31
- (importer: ImportDictionary) =>
32
- (param: ITypedHttpRouteParameter.IHeaders): ts.TypeNode => {
33
- if (project.config.clone === true)
34
- return from(project)(importer)(param.metadata);
35
- const type: ts.TypeNode = name(param);
36
- if (project.config.primitive === false) return type;
37
- return ts.factory.createTypeReferenceNode(
38
- importer.external({
39
- type: true,
40
- library: "typia",
41
- instance: "Resolved",
42
- }),
43
- [type],
44
- );
45
- };
46
-
47
- export const query =
48
- (project: INestiaProject) =>
49
- (importer: ImportDictionary) =>
50
- (param: ITypedHttpRouteParameter.IQuery): ts.TypeNode => {
51
- if (project.config.clone === true)
52
- return from(project)(importer)(param.metadata);
53
- const type: ts.TypeNode = name(param);
54
- if (project.config.primitive === false) return type;
55
- return ts.factory.createTypeReferenceNode(
56
- importer.external({
57
- type: true,
58
- library: "typia",
59
- instance: "Resolved",
60
- }),
61
- [type],
62
- );
63
- };
64
-
65
- export const input =
66
- (project: INestiaProject) =>
67
- (importer: ImportDictionary) =>
68
- (param: ITypedHttpRouteParameter.IBody): ts.TypeNode => {
69
- if (project.config.clone === true) {
70
- const type: ts.TypeNode = from(project)(importer)(param.metadata);
71
- return param.contentType === "multipart/form-data"
72
- ? formDataInput(importer)(type)
73
- : type;
74
- }
75
- const type: ts.TypeNode = name(param);
76
- if (param.contentType === "multipart/form-data")
77
- return formDataInput(importer)(type);
78
- else if (project.config.primitive === false) return type;
79
- return ts.factory.createTypeReferenceNode(
80
- importer.external({
81
- type: true,
82
- library: "typia",
83
- instance:
84
- typia.is<IReflectHttpOperationParameter.IBody>(param) &&
85
- (param.contentType === "application/json" ||
86
- param.encrypted === true)
87
- ? "Primitive"
88
- : "Resolved",
89
- }),
90
- [type],
91
- );
92
- };
93
-
94
- export const output =
95
- (project: INestiaProject) =>
96
- (importer: ImportDictionary) =>
97
- (route: ITypedHttpRoute): ts.TypeNode => {
98
- const schema = (p: { metadata: Metadata; type: IReflectType }) =>
99
- p.metadata.size() === 0
100
- ? TypeFactory.keyword("void")
101
- : project.config.clone === true
102
- ? from(project)(importer)(p.metadata)
103
- : project.config.primitive !== false
104
- ? ts.factory.createTypeReferenceNode(
105
- importer.external({
106
- type: true,
107
- library: "typia",
108
- instance:
109
- route.success.contentType === "application/json" ||
110
- route.success.encrypted === true
111
- ? "Primitive"
112
- : "Resolved",
113
- }),
114
- [name(p)],
115
- )
116
- : name(p);
117
- if (project.config.propagate !== true) return schema(route.success);
118
-
119
- const branches: IBranch[] = [
120
- {
121
- status: String(
122
- route.success.status ?? (route.method === "POST" ? 201 : 200),
123
- ),
124
- type: schema(route.success),
125
- },
126
- ...Object.entries(route.exceptions).map(([status, value]) => ({
127
- status,
128
- type: schema(value),
129
- })),
130
- ];
131
- return ts.factory.createTypeReferenceNode(
132
- importer.external({
133
- type: true,
134
- library: "@nestia/fetcher",
135
- instance: "IPropagation",
136
- }),
137
- [
138
- ts.factory.createTypeLiteralNode(
139
- branches.map((b) =>
140
- ts.factory.createPropertySignature(
141
- undefined,
142
- ts.factory.createNumericLiteral(b.status),
143
- undefined,
144
- b.type,
145
- ),
146
- ),
147
- ),
148
- ...(route.success.status
149
- ? [
150
- ts.factory.createLiteralTypeNode(
151
- ts.factory.createNumericLiteral(route.success.status),
152
- ),
153
- ]
154
- : []),
155
- ],
156
- );
157
- };
158
-
159
- export const responseBody =
160
- (project: INestiaProject) =>
161
- (importer: ImportDictionary) =>
162
- (route: ITypedHttpRoute): ts.TypeNode =>
163
- output({
164
- ...project,
165
- config: {
166
- ...project.config,
167
- propagate: false,
168
- },
169
- })(importer)(route);
170
-
171
- const formDataInput = (importer: ImportDictionary) => (type: ts.TypeNode) =>
172
- ts.factory.createTypeReferenceNode(
173
- importer.external({
174
- type: true,
175
- library: "@nestia/fetcher",
176
- instance: "FormDataInput",
177
- }),
178
- [type],
179
- );
180
- }
181
-
182
- interface IBranch {
183
- status: string;
184
- type: ts.TypeNode;
185
- }
1
+ import ts from "typescript";
2
+ import typia from "typia";
3
+ import { TypeFactory } from "typia/lib/factories/TypeFactory";
4
+ import { Metadata } from "typia/lib/schemas/metadata/Metadata";
5
+
6
+ import { INestiaProject } from "../../structures/INestiaProject";
7
+ import { IReflectHttpOperationParameter } from "../../structures/IReflectHttpOperationParameter";
8
+ import { IReflectType } from "../../structures/IReflectType";
9
+ import { ITypedHttpRoute } from "../../structures/ITypedHttpRoute";
10
+ import { ITypedHttpRouteParameter } from "../../structures/ITypedHttpRouteParameter";
11
+ import { ImportDictionary } from "./ImportDictionary";
12
+ import { SdkTypeProgrammer } from "./SdkTypeProgrammer";
13
+
14
+ export namespace SdkAliasCollection {
15
+ export const name = ({ type }: { type: IReflectType }): ts.TypeNode =>
16
+ ts.factory.createTypeReferenceNode(
17
+ type.name,
18
+ type.typeArguments
19
+ ? type.typeArguments.map((a) => name({ type: a }))
20
+ : undefined,
21
+ );
22
+
23
+ export const from =
24
+ (project: INestiaProject) =>
25
+ (importer: ImportDictionary) =>
26
+ (metadata: Metadata) =>
27
+ SdkTypeProgrammer.write(project)(importer)(metadata);
28
+
29
+ export const headers =
30
+ (project: INestiaProject) =>
31
+ (importer: ImportDictionary) =>
32
+ (param: ITypedHttpRouteParameter.IHeaders): ts.TypeNode => {
33
+ if (project.config.clone === true)
34
+ return from(project)(importer)(param.metadata);
35
+ const type: ts.TypeNode = name(param);
36
+ if (project.config.primitive === false) return type;
37
+ return ts.factory.createTypeReferenceNode(
38
+ importer.external({
39
+ type: true,
40
+ library: "typia",
41
+ instance: "Resolved",
42
+ }),
43
+ [type],
44
+ );
45
+ };
46
+
47
+ export const query =
48
+ (project: INestiaProject) =>
49
+ (importer: ImportDictionary) =>
50
+ (param: ITypedHttpRouteParameter.IQuery): ts.TypeNode => {
51
+ if (project.config.clone === true)
52
+ return from(project)(importer)(param.metadata);
53
+ const type: ts.TypeNode = name(param);
54
+ if (project.config.primitive === false) return type;
55
+ return ts.factory.createTypeReferenceNode(
56
+ importer.external({
57
+ type: true,
58
+ library: "typia",
59
+ instance: "Resolved",
60
+ }),
61
+ [type],
62
+ );
63
+ };
64
+
65
+ export const input =
66
+ (project: INestiaProject) =>
67
+ (importer: ImportDictionary) =>
68
+ (param: ITypedHttpRouteParameter.IBody): ts.TypeNode => {
69
+ if (project.config.clone === true) {
70
+ const type: ts.TypeNode = from(project)(importer)(param.metadata);
71
+ return param.contentType === "multipart/form-data"
72
+ ? formDataInput(importer)(type)
73
+ : type;
74
+ }
75
+ const type: ts.TypeNode = name(param);
76
+ if (param.contentType === "multipart/form-data")
77
+ return formDataInput(importer)(type);
78
+ else if (project.config.primitive === false) return type;
79
+ return ts.factory.createTypeReferenceNode(
80
+ importer.external({
81
+ type: true,
82
+ library: "typia",
83
+ instance:
84
+ typia.is<IReflectHttpOperationParameter.IBody>(param) &&
85
+ (param.contentType === "application/json" ||
86
+ param.encrypted === true)
87
+ ? "Primitive"
88
+ : "Resolved",
89
+ }),
90
+ [type],
91
+ );
92
+ };
93
+
94
+ export const output =
95
+ (project: INestiaProject) =>
96
+ (importer: ImportDictionary) =>
97
+ (route: ITypedHttpRoute): ts.TypeNode => {
98
+ const schema = (p: { metadata: Metadata; type: IReflectType }) =>
99
+ p.metadata.size() === 0
100
+ ? TypeFactory.keyword("void")
101
+ : project.config.clone === true
102
+ ? from(project)(importer)(p.metadata)
103
+ : project.config.primitive !== false
104
+ ? ts.factory.createTypeReferenceNode(
105
+ importer.external({
106
+ type: true,
107
+ library: "typia",
108
+ instance:
109
+ route.success.contentType === "application/json" ||
110
+ route.success.encrypted === true
111
+ ? "Primitive"
112
+ : "Resolved",
113
+ }),
114
+ [name(p)],
115
+ )
116
+ : name(p);
117
+ if (project.config.propagate !== true) return schema(route.success);
118
+
119
+ const branches: IBranch[] = [
120
+ {
121
+ status: String(
122
+ route.success.status ?? (route.method === "POST" ? 201 : 200),
123
+ ),
124
+ type: schema(route.success),
125
+ },
126
+ ...Object.entries(route.exceptions).map(([status, value]) => ({
127
+ status,
128
+ type: schema(value),
129
+ })),
130
+ ];
131
+ return ts.factory.createTypeReferenceNode(
132
+ importer.external({
133
+ type: true,
134
+ library: "@nestia/fetcher",
135
+ instance: "IPropagation",
136
+ }),
137
+ [
138
+ ts.factory.createTypeLiteralNode(
139
+ branches.map((b) =>
140
+ ts.factory.createPropertySignature(
141
+ undefined,
142
+ ts.factory.createNumericLiteral(b.status),
143
+ undefined,
144
+ b.type,
145
+ ),
146
+ ),
147
+ ),
148
+ ...(route.success.status
149
+ ? [
150
+ ts.factory.createLiteralTypeNode(
151
+ ts.factory.createNumericLiteral(route.success.status),
152
+ ),
153
+ ]
154
+ : []),
155
+ ],
156
+ );
157
+ };
158
+
159
+ export const responseBody =
160
+ (project: INestiaProject) =>
161
+ (importer: ImportDictionary) =>
162
+ (route: ITypedHttpRoute): ts.TypeNode =>
163
+ output({
164
+ ...project,
165
+ config: {
166
+ ...project.config,
167
+ propagate: false,
168
+ },
169
+ })(importer)(route);
170
+
171
+ const formDataInput = (importer: ImportDictionary) => (type: ts.TypeNode) =>
172
+ ts.factory.createTypeReferenceNode(
173
+ importer.external({
174
+ type: true,
175
+ library: "@nestia/fetcher",
176
+ instance: "FormDataInput",
177
+ }),
178
+ [type],
179
+ );
180
+ }
181
+
182
+ interface IBranch {
183
+ status: string;
184
+ type: ts.TypeNode;
185
+ }