@nestia/sdk 4.6.1-dev.20250117 → 4.6.2

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 (122) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +92 -87
  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 +2 -2
  15. package/lib/analyses/GenericAnalyzer.js +1 -1
  16. package/lib/executable/internal/NestiaConfigLoader.js +4 -4
  17. package/lib/executable/sdk.js +12 -12
  18. package/lib/generates/SdkGenerator.js +2 -2
  19. package/lib/generates/SdkGenerator.js.map +1 -1
  20. package/lib/generates/internal/SdkHttpNamespaceProgrammer.js +3 -3
  21. package/lib/generates/internal/SdkHttpNamespaceProgrammer.js.map +1 -1
  22. package/lib/generates/internal/SdkHttpRouteProgrammer.js +1 -1
  23. package/lib/generates/internal/SdkTypeTagProgrammer.js +3 -16
  24. package/lib/generates/internal/SdkTypeTagProgrammer.js.map +1 -1
  25. package/lib/generates/internal/SdkWebSocketNamespaceProgrammer.js +3 -3
  26. package/lib/generates/internal/SdkWebSocketNamespaceProgrammer.js.map +1 -1
  27. package/lib/generates/internal/SwaggerOperationResponseComposer.js +1 -1
  28. package/lib/generates/internal/SwaggerOperationResponseComposer.js.map +1 -1
  29. package/package.json +7 -7
  30. package/src/INestiaConfig.ts +271 -271
  31. package/src/NestiaSdkApplication.ts +307 -307
  32. package/src/NestiaSwaggerComposer.ts +138 -138
  33. package/src/analyses/AccessorAnalyzer.ts +67 -67
  34. package/src/analyses/ConfigAnalyzer.ts +155 -155
  35. package/src/analyses/ExceptionAnalyzer.ts +154 -154
  36. package/src/analyses/GenericAnalyzer.ts +49 -49
  37. package/src/analyses/ImportAnalyzer.ts +171 -171
  38. package/src/analyses/PathAnalyzer.ts +69 -69
  39. package/src/analyses/ReflectControllerAnalyzer.ts +105 -105
  40. package/src/analyses/ReflectHttpOperationAnalyzer.ts +183 -183
  41. package/src/analyses/ReflectHttpOperationExceptionAnalyzer.ts +71 -71
  42. package/src/analyses/ReflectHttpOperationParameterAnalyzer.ts +348 -348
  43. package/src/analyses/ReflectHttpOperationResponseAnalyzer.ts +127 -127
  44. package/src/analyses/ReflectMetadataAnalyzer.ts +44 -44
  45. package/src/analyses/ReflectWebSocketOperationAnalyzer.ts +172 -172
  46. package/src/analyses/SecurityAnalyzer.ts +25 -25
  47. package/src/analyses/TypedHttpRouteAnalyzer.ts +186 -186
  48. package/src/analyses/TypedWebSocketRouteAnalyzer.ts +18 -18
  49. package/src/decorators/OperationMetadata.ts +15 -15
  50. package/src/executable/internal/CommandParser.ts +15 -15
  51. package/src/executable/internal/NestiaConfigLoader.ts +78 -78
  52. package/src/executable/internal/NestiaSdkCommand.ts +103 -103
  53. package/src/executable/sdk.ts +75 -75
  54. package/src/generates/CloneGenerator.ts +66 -66
  55. package/src/generates/E2eGenerator.ts +32 -32
  56. package/src/generates/SdkGenerator.ts +159 -159
  57. package/src/generates/SwaggerGenerator.ts +292 -292
  58. package/src/generates/internal/E2eFileProgrammer.ts +183 -183
  59. package/src/generates/internal/FilePrinter.ts +53 -53
  60. package/src/generates/internal/ImportDictionary.ts +147 -147
  61. package/src/generates/internal/SdkAliasCollection.ts +185 -185
  62. package/src/generates/internal/SdkDistributionComposer.ts +103 -103
  63. package/src/generates/internal/SdkFileProgrammer.ts +116 -116
  64. package/src/generates/internal/SdkHttpCloneProgrammer.ts +124 -124
  65. package/src/generates/internal/SdkHttpCloneReferencer.ts +71 -71
  66. package/src/generates/internal/SdkHttpFunctionProgrammer.ts +301 -301
  67. package/src/generates/internal/SdkHttpNamespaceProgrammer.ts +529 -529
  68. package/src/generates/internal/SdkHttpRouteProgrammer.ts +117 -117
  69. package/src/generates/internal/SdkHttpSimulationProgrammer.ts +362 -362
  70. package/src/generates/internal/SdkImportWizard.ts +55 -55
  71. package/src/generates/internal/SdkRouteDirectory.ts +18 -18
  72. package/src/generates/internal/SdkTypeProgrammer.ts +377 -377
  73. package/src/generates/internal/SdkTypeTagProgrammer.ts +102 -120
  74. package/src/generates/internal/SdkWebSocketNamespaceProgrammer.ts +362 -363
  75. package/src/generates/internal/SdkWebSocketRouteProgrammer.ts +265 -265
  76. package/src/generates/internal/SwaggerDescriptionComposer.ts +64 -64
  77. package/src/generates/internal/SwaggerOperationComposer.ts +119 -119
  78. package/src/generates/internal/SwaggerOperationParameterComposer.ts +177 -177
  79. package/src/generates/internal/SwaggerOperationResponseComposer.ts +110 -110
  80. package/src/index.ts +4 -4
  81. package/src/module.ts +3 -3
  82. package/src/structures/INestiaProject.ts +13 -13
  83. package/src/structures/INestiaSdkInput.ts +20 -20
  84. package/src/structures/IReflectApplication.ts +8 -8
  85. package/src/structures/IReflectController.ts +15 -15
  86. package/src/structures/IReflectHttpOperation.ts +26 -26
  87. package/src/structures/IReflectHttpOperationException.ts +19 -19
  88. package/src/structures/IReflectHttpOperationParameter.ts +81 -81
  89. package/src/structures/IReflectHttpOperationSuccess.ts +22 -22
  90. package/src/structures/IReflectOperationError.ts +26 -26
  91. package/src/structures/IReflectType.ts +4 -4
  92. package/src/structures/IReflectTypeImport.ts +4 -4
  93. package/src/structures/IReflectWebSocketOperation.ts +17 -17
  94. package/src/structures/IReflectWebSocketOperationParameter.ts +38 -38
  95. package/src/structures/ITypedApplication.ts +11 -11
  96. package/src/structures/ITypedHttpRoute.ts +30 -30
  97. package/src/structures/ITypedHttpRouteException.ts +15 -15
  98. package/src/structures/ITypedHttpRouteParameter.ts +41 -41
  99. package/src/structures/ITypedHttpRouteSuccess.ts +22 -22
  100. package/src/structures/ITypedWebSocketRoute.ts +20 -20
  101. package/src/structures/ITypedWebSocketRouteParameter.ts +3 -3
  102. package/src/structures/MethodType.ts +5 -5
  103. package/src/structures/ParamCategory.ts +1 -1
  104. package/src/structures/TypeEntry.ts +22 -22
  105. package/src/transform.ts +9 -9
  106. package/src/transformers/IOperationMetadata.ts +44 -44
  107. package/src/transformers/ISdkOperationTransformerContext.ts +8 -8
  108. package/src/transformers/SdkOperationProgrammer.ts +209 -209
  109. package/src/transformers/SdkOperationTransformer.ts +253 -253
  110. package/src/transformers/TextPlainValidator.ts +17 -17
  111. package/src/typings/get-function-location.d.ts +7 -7
  112. package/src/utils/ArrayUtil.ts +26 -26
  113. package/src/utils/FileRetriever.ts +22 -22
  114. package/src/utils/MapUtil.ts +14 -14
  115. package/src/utils/MetadataUtil.ts +26 -26
  116. package/src/utils/PathUtil.ts +10 -10
  117. package/src/utils/SourceFinder.ts +66 -66
  118. package/src/utils/StringUtil.ts +17 -17
  119. package/src/utils/StripEnums.ts +5 -5
  120. package/src/utils/VersioningStrategy.ts +28 -28
  121. package/src/validators/HttpHeadersValidator.ts +34 -34
  122. 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
+ }