@nestia/sdk 12.0.0-dev.20260601.1 → 12.0.0-dev.20260612.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 (209) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +93 -93
  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/NestiaSdkApplication.js +29 -7
  15. package/lib/NestiaSdkApplication.js.map +1 -1
  16. package/lib/NestiaSwaggerComposer.js +21 -13
  17. package/lib/NestiaSwaggerComposer.js.map +1 -1
  18. package/lib/analyses/AccessorAnalyzer.d.ts +4 -1
  19. package/lib/analyses/AccessorAnalyzer.js.map +1 -1
  20. package/lib/analyses/ConfigAnalyzer.js +1 -1
  21. package/lib/analyses/PathAnalyzer.d.ts +18 -3
  22. package/lib/analyses/PathAnalyzer.js +32 -0
  23. package/lib/analyses/PathAnalyzer.js.map +1 -1
  24. package/lib/analyses/ReflectControllerAnalyzer.js +3 -2
  25. package/lib/analyses/ReflectControllerAnalyzer.js.map +1 -1
  26. package/lib/analyses/ReflectHttpOperationAnalyzer.d.ts +1 -1
  27. package/lib/analyses/ReflectHttpOperationAnalyzer.js +1 -1
  28. package/lib/analyses/ReflectHttpOperationAnalyzer.js.map +1 -1
  29. package/lib/analyses/ReflectHttpOperationResponseAnalyzer.d.ts +1 -1
  30. package/lib/analyses/ReflectHttpOperationResponseAnalyzer.js +53 -20
  31. package/lib/analyses/ReflectHttpOperationResponseAnalyzer.js.map +1 -1
  32. package/lib/analyses/ReflectMcpOperationAnalyzer.d.ts +14 -0
  33. package/lib/analyses/ReflectMcpOperationAnalyzer.js +79 -0
  34. package/lib/analyses/ReflectMcpOperationAnalyzer.js.map +1 -0
  35. package/lib/analyses/TypedMcpRouteAnalyzer.d.ts +9 -0
  36. package/lib/analyses/TypedMcpRouteAnalyzer.js +31 -0
  37. package/lib/analyses/TypedMcpRouteAnalyzer.js.map +1 -0
  38. package/lib/executable/internal/NestiaConfigLoader.js +5 -1
  39. package/lib/executable/internal/NestiaConfigLoader.js.map +1 -1
  40. package/lib/executable/internal/NestiaSdkCommand.js +30 -14
  41. package/lib/executable/internal/NestiaSdkCommand.js.map +1 -1
  42. package/lib/executable/internal/NestiaSdkWatcher.d.ts +10 -0
  43. package/lib/executable/internal/NestiaSdkWatcher.js +322 -0
  44. package/lib/executable/internal/NestiaSdkWatcher.js.map +1 -0
  45. package/lib/executable/sdk.js +12 -12
  46. package/lib/executable/sdk.js.map +1 -1
  47. package/lib/generates/CloneGenerator.js +4 -2
  48. package/lib/generates/CloneGenerator.js.map +1 -1
  49. package/lib/generates/SdkGenerator.js +50 -1
  50. package/lib/generates/SdkGenerator.js.map +1 -1
  51. package/lib/generates/SwaggerGenerator.js +18 -2
  52. package/lib/generates/SwaggerGenerator.js.map +1 -1
  53. package/lib/generates/internal/E2eFileProgrammer.js +3 -1
  54. package/lib/generates/internal/E2eFileProgrammer.js.map +1 -1
  55. package/lib/generates/internal/ImportDictionary.d.ts +1 -0
  56. package/lib/generates/internal/ImportDictionary.js +9 -4
  57. package/lib/generates/internal/ImportDictionary.js.map +1 -1
  58. package/lib/generates/internal/SdkAliasCollection.d.ts +2 -0
  59. package/lib/generates/internal/SdkAliasCollection.js +11 -2
  60. package/lib/generates/internal/SdkAliasCollection.js.map +1 -1
  61. package/lib/generates/internal/SdkDistributionComposer.d.ts +1 -0
  62. package/lib/generates/internal/SdkDistributionComposer.js +3 -0
  63. package/lib/generates/internal/SdkDistributionComposer.js.map +1 -1
  64. package/lib/generates/internal/SdkFileProgrammer.js +4 -1
  65. package/lib/generates/internal/SdkFileProgrammer.js.map +1 -1
  66. package/lib/generates/internal/SdkHttpCloneReferencer.d.ts +1 -1
  67. package/lib/generates/internal/SdkHttpCloneReferencer.js +42 -9
  68. package/lib/generates/internal/SdkHttpCloneReferencer.js.map +1 -1
  69. package/lib/generates/internal/SdkHttpFunctionProgrammer.js +3 -4
  70. package/lib/generates/internal/SdkHttpFunctionProgrammer.js.map +1 -1
  71. package/lib/generates/internal/SdkHttpNamespaceProgrammer.js +2 -1
  72. package/lib/generates/internal/SdkHttpNamespaceProgrammer.js.map +1 -1
  73. package/lib/generates/internal/SdkHttpSimulationProgrammer.js +6 -3
  74. package/lib/generates/internal/SdkHttpSimulationProgrammer.js.map +1 -1
  75. package/lib/generates/internal/SdkMcpRouteProgrammer.d.ts +15 -0
  76. package/lib/generates/internal/SdkMcpRouteProgrammer.js +148 -0
  77. package/lib/generates/internal/SdkMcpRouteProgrammer.js.map +1 -0
  78. package/lib/generates/internal/SdkRouteDirectory.d.ts +2 -1
  79. package/lib/generates/internal/SdkRouteDirectory.js.map +1 -1
  80. package/lib/generates/internal/SdkWebSocketCloneProgrammer.d.ts +6 -0
  81. package/lib/generates/internal/SdkWebSocketCloneProgrammer.js +283 -0
  82. package/lib/generates/internal/SdkWebSocketCloneProgrammer.js.map +1 -0
  83. package/lib/generates/internal/SdkWebSocketRouteProgrammer.js +11 -9
  84. package/lib/generates/internal/SdkWebSocketRouteProgrammer.js.map +1 -1
  85. package/lib/generates/internal/SwaggerOperationParameterComposer.js +10 -2
  86. package/lib/generates/internal/SwaggerOperationParameterComposer.js.map +1 -1
  87. package/lib/generates/internal/SwaggerOperationResponseComposer.d.ts +1 -1
  88. package/lib/generates/internal/SwaggerOperationResponseComposer.js +6 -1
  89. package/lib/generates/internal/SwaggerOperationResponseComposer.js.map +1 -1
  90. package/lib/generates/internal/SwaggerReadonlyArrayEmender.d.ts +9 -0
  91. package/lib/generates/internal/SwaggerReadonlyArrayEmender.js +174 -0
  92. package/lib/generates/internal/SwaggerReadonlyArrayEmender.js.map +1 -0
  93. package/lib/structures/INestiaSdkInput.d.ts +9 -2
  94. package/lib/structures/IReflectController.d.ts +2 -1
  95. package/lib/structures/IReflectHttpOperationSuccess.d.ts +4 -2
  96. package/lib/structures/IReflectMcpOperation.d.ts +35 -0
  97. package/lib/structures/IReflectMcpOperation.js +3 -0
  98. package/lib/structures/IReflectMcpOperation.js.map +1 -0
  99. package/lib/structures/IReflectMcpOperationParameter.d.ts +19 -0
  100. package/lib/structures/IReflectMcpOperationParameter.js +3 -0
  101. package/lib/structures/IReflectMcpOperationParameter.js.map +1 -0
  102. package/lib/structures/ITypedApplication.d.ts +2 -1
  103. package/lib/structures/ITypedHttpRouteSuccess.d.ts +3 -1
  104. package/lib/structures/ITypedMcpRoute.d.ts +31 -0
  105. package/lib/structures/ITypedMcpRoute.js +3 -0
  106. package/lib/structures/ITypedMcpRoute.js.map +1 -0
  107. package/lib/utils/HttpResponseContentTypeUtil.d.ts +5 -0
  108. package/lib/utils/HttpResponseContentTypeUtil.js +22 -0
  109. package/lib/utils/HttpResponseContentTypeUtil.js.map +1 -0
  110. package/native/go.mod +52 -52
  111. package/native/go.sum +84 -54
  112. package/native/sdk/register.go +322 -165
  113. package/native/sdk/sdk.go +17 -17
  114. package/native/sdk/sdk_metadata_json.go +327 -327
  115. package/native/sdk/sdk_transform.go +1879 -1549
  116. package/package.json +11 -9
  117. package/src/INestiaConfig.ts +267 -267
  118. package/src/NestiaSdkApplication.ts +39 -8
  119. package/src/NestiaSwaggerComposer.ts +153 -142
  120. package/src/analyses/AccessorAnalyzer.ts +64 -67
  121. package/src/analyses/ConfigAnalyzer.ts +330 -330
  122. package/src/analyses/ImportAnalyzer.ts +92 -92
  123. package/src/analyses/PathAnalyzer.ts +130 -69
  124. package/src/analyses/ReflectControllerAnalyzer.ts +112 -105
  125. package/src/analyses/ReflectHttpOperationAnalyzer.ts +183 -183
  126. package/src/analyses/ReflectHttpOperationExceptionAnalyzer.ts +90 -90
  127. package/src/analyses/ReflectHttpOperationParameterAnalyzer.ts +350 -350
  128. package/src/analyses/ReflectHttpOperationResponseAnalyzer.ts +163 -130
  129. package/src/analyses/ReflectMcpOperationAnalyzer.ts +124 -0
  130. package/src/analyses/ReflectMetadataAnalyzer.ts +44 -44
  131. package/src/analyses/SecurityAnalyzer.ts +25 -25
  132. package/src/analyses/TypedMcpRouteAnalyzer.ts +34 -0
  133. package/src/decorators/OperationMetadata.ts +29 -29
  134. package/src/executable/internal/CommandParser.ts +15 -15
  135. package/src/executable/internal/NestiaConfigLoader.ts +451 -446
  136. package/src/executable/internal/NestiaSdkCommand.ts +124 -106
  137. package/src/executable/internal/NestiaSdkWatcher.ts +342 -0
  138. package/src/executable/sdk.ts +90 -88
  139. package/src/generates/CloneGenerator.ts +73 -66
  140. package/src/generates/E2eGenerator.ts +32 -32
  141. package/src/generates/SdkGenerator.ts +176 -118
  142. package/src/generates/SwaggerGenerator.ts +342 -310
  143. package/src/generates/internal/E2eFileProgrammer.ts +240 -233
  144. package/src/generates/internal/FilePrinter.ts +65 -65
  145. package/src/generates/internal/ImportDictionary.ts +209 -204
  146. package/src/generates/internal/SdkAliasCollection.ts +274 -261
  147. package/src/generates/internal/SdkDistributionComposer.ts +123 -116
  148. package/src/generates/internal/SdkFileProgrammer.ts +116 -112
  149. package/src/generates/internal/SdkHttpCloneProgrammer.ts +126 -126
  150. package/src/generates/internal/SdkHttpCloneReferencer.ts +131 -77
  151. package/src/generates/internal/SdkHttpFunctionProgrammer.ts +301 -301
  152. package/src/generates/internal/SdkHttpNamespaceProgrammer.ts +520 -510
  153. package/src/generates/internal/SdkHttpParameterProgrammer.ts +165 -165
  154. package/src/generates/internal/SdkHttpRouteProgrammer.ts +109 -109
  155. package/src/generates/internal/SdkHttpSimulationProgrammer.ts +331 -314
  156. package/src/generates/internal/SdkImportWizard.ts +62 -62
  157. package/src/generates/internal/SdkMcpRouteProgrammer.ts +452 -0
  158. package/src/generates/internal/SdkRouteDirectory.ts +21 -18
  159. package/src/generates/internal/SdkTypeTagProgrammer.ts +114 -114
  160. package/src/generates/internal/SdkWebSocketCloneProgrammer.ts +319 -0
  161. package/src/generates/internal/SdkWebSocketNamespaceProgrammer.ts +389 -389
  162. package/src/generates/internal/SdkWebSocketParameterProgrammer.ts +89 -89
  163. package/src/generates/internal/SdkWebSocketRouteProgrammer.ts +331 -323
  164. package/src/generates/internal/SwaggerDescriptionComposer.ts +64 -64
  165. package/src/generates/internal/SwaggerOperationComposer.ts +119 -119
  166. package/src/generates/internal/SwaggerOperationParameterComposer.ts +175 -162
  167. package/src/generates/internal/SwaggerOperationResponseComposer.ts +115 -110
  168. package/src/generates/internal/SwaggerReadonlyArrayEmender.ts +262 -0
  169. package/src/index.ts +4 -4
  170. package/src/internal/legacy.ts +492 -492
  171. package/src/module.ts +4 -4
  172. package/src/structures/INestiaProject.ts +10 -10
  173. package/src/structures/INestiaSdkInput.ts +27 -20
  174. package/src/structures/IOperationMetadata.ts +41 -41
  175. package/src/structures/IReflectController.ts +18 -15
  176. package/src/structures/IReflectHttpOperation.ts +26 -26
  177. package/src/structures/IReflectHttpOperationException.ts +18 -18
  178. package/src/structures/IReflectHttpOperationParameter.ts +79 -79
  179. package/src/structures/IReflectHttpOperationSuccess.ts +18 -21
  180. package/src/structures/IReflectImport.ts +6 -6
  181. package/src/structures/IReflectMcpOperation.ts +38 -0
  182. package/src/structures/IReflectMcpOperationParameter.ts +27 -0
  183. package/src/structures/IReflectOperationError.ts +26 -26
  184. package/src/structures/IReflectType.ts +4 -4
  185. package/src/structures/IReflectWebSocketOperation.ts +17 -17
  186. package/src/structures/ITypedApplication.ts +12 -11
  187. package/src/structures/ITypedHttpRoute.ts +41 -41
  188. package/src/structures/ITypedHttpRouteException.ts +15 -15
  189. package/src/structures/ITypedHttpRouteParameter.ts +41 -41
  190. package/src/structures/ITypedHttpRouteSuccess.ts +18 -22
  191. package/src/structures/ITypedMcpRoute.ts +33 -0
  192. package/src/structures/ITypedWebSocketRoute.ts +24 -24
  193. package/src/structures/ITypedWebSocketRouteParameter.ts +3 -3
  194. package/src/transform.ts +59 -59
  195. package/src/typings/get-function-location.d.ts +7 -7
  196. package/src/utils/ArrayUtil.ts +26 -26
  197. package/src/utils/EmittedJavaScriptPatcher.ts +88 -88
  198. package/src/utils/FileRetriever.ts +22 -22
  199. package/src/utils/HttpResponseContentTypeUtil.ts +30 -0
  200. package/src/utils/MapUtil.ts +14 -14
  201. package/src/utils/PathUtil.ts +10 -10
  202. package/src/utils/SourceFinder.ts +63 -63
  203. package/src/utils/StringUtil.ts +17 -17
  204. package/src/utils/TsConfigReader.ts +108 -108
  205. package/src/utils/TtscExecutor.ts +68 -68
  206. package/src/utils/VersioningStrategy.ts +28 -28
  207. package/src/validators/HttpHeadersValidator.ts +11 -11
  208. package/src/validators/HttpQueryValidator.ts +11 -11
  209. package/src/validators/TextPlainValidator.ts +17 -17
@@ -1,114 +1,114 @@
1
- import { TypeScriptFactory } from "@nestia/factory";
2
- import { LiteralFactory } from "@nestia/factory";
3
- import { IMetadataTypeTag } from "@typia/interface";
4
-
5
- import { ImportDictionary } from "./ImportDictionary";
6
-
7
- export namespace SdkTypeTagProgrammer {
8
- export const write = (
9
- importer: ImportDictionary,
10
- from: "object" | "array" | "boolean" | "number" | "bigint" | "string",
11
- tag: IMetadataTypeTag,
12
- ) => {
13
- const name: string = tag.name.split("<")[0]!;
14
- if (PREDEFINED[from]?.has(name) === true)
15
- return TypeScriptFactory.createTypeReferenceNode(
16
- TypeScriptFactory.createQualifiedName(
17
- TypeScriptFactory.createIdentifier(
18
- importer.external({
19
- declaration: true,
20
- file: `typia`,
21
- type: "element",
22
- name: "tags",
23
- }),
24
- ),
25
- TypeScriptFactory.createIdentifier(name),
26
- ),
27
- [
28
- TypeScriptFactory.createLiteralTypeNode(
29
- LiteralFactory.write(tag.value) as any,
30
- ),
31
- ],
32
- );
33
- return TypeScriptFactory.createTypeReferenceNode(
34
- TypeScriptFactory.createQualifiedName(
35
- TypeScriptFactory.createIdentifier(
36
- importer.external({
37
- declaration: true,
38
- file: `typia`,
39
- type: "element",
40
- name: "tags",
41
- }),
42
- ),
43
- TypeScriptFactory.createIdentifier("TagBase"),
44
- ),
45
- [
46
- TypeScriptFactory.createLiteralTypeNode(
47
- LiteralFactory.write({
48
- target: from,
49
- kind: tag.kind,
50
- value: tag.value,
51
- validate: tag.validate,
52
- exclusive: tag.exclusive,
53
- schema: tag.schema,
54
- }) as any,
55
- ),
56
- ],
57
- );
58
- };
59
- }
60
-
61
- const COMMON_KINDS = ["Default", "Example", "Examples", "Sequence"];
62
- const PREDEFINED = {
63
- object: new Set([...COMMON_KINDS]),
64
- array: new Set([...COMMON_KINDS, "MinItems", "MaxItems", "UniqueItems"]),
65
- boolean: new Set([...COMMON_KINDS]),
66
- number: new Set([
67
- ...COMMON_KINDS,
68
- "Minimum",
69
- "Maximum",
70
- "ExclusiveMinimum",
71
- "ExclusiveMaximum",
72
- "MultipleOf",
73
- "Type",
74
- ]),
75
- bigint: new Set([
76
- ...COMMON_KINDS,
77
- "Minimum",
78
- "Maximum",
79
- "ExclusiveMinimum",
80
- "ExclusiveMaximum",
81
- "MultipleOf",
82
- "Type",
83
- ]),
84
- string: new Set([
85
- ...COMMON_KINDS,
86
- "ContentMediaType",
87
- "Format",
88
- "MaxLength",
89
- "MinLength",
90
- "Pattern",
91
- ]),
92
- };
93
-
94
- // export * from "./Constant";
95
- // export * from "./ContentMediaType";
96
- // export * from "./Default";
97
- // export * from "./Example";
98
- // export * from "./Examples";
99
- // export * from "./ExclusiveMaximum";
100
- // export * from "./ExclusiveMinimum";
101
- // export * from "./Format";
102
- // export * from "./JsonSchemaPlugin";
103
- // export * from "./Maximum";
104
- // export * from "./MaxItems";
105
- // export * from "./MaxLength";
106
- // export * from "./Minimum";
107
- // export * from "./MinItems";
108
- // export * from "./MinLength";
109
- // export * from "./MultipleOf";
110
- // export * from "./Pattern";
111
- // export * from "./Sequence";
112
- // export * from "./TagBase";
113
- // export * from "./Type";
114
- // export * from "./UniqueItems";
1
+ import { TypeScriptFactory } from "@nestia/factory";
2
+ import { LiteralFactory } from "@nestia/factory";
3
+ import { IMetadataTypeTag } from "@typia/interface";
4
+
5
+ import { ImportDictionary } from "./ImportDictionary";
6
+
7
+ export namespace SdkTypeTagProgrammer {
8
+ export const write = (
9
+ importer: ImportDictionary,
10
+ from: "object" | "array" | "boolean" | "number" | "bigint" | "string",
11
+ tag: IMetadataTypeTag,
12
+ ) => {
13
+ const name: string = tag.name.split("<")[0]!;
14
+ if (PREDEFINED[from]?.has(name) === true)
15
+ return TypeScriptFactory.createTypeReferenceNode(
16
+ TypeScriptFactory.createQualifiedName(
17
+ TypeScriptFactory.createIdentifier(
18
+ importer.external({
19
+ declaration: true,
20
+ file: `typia`,
21
+ type: "element",
22
+ name: "tags",
23
+ }),
24
+ ),
25
+ TypeScriptFactory.createIdentifier(name),
26
+ ),
27
+ [
28
+ TypeScriptFactory.createLiteralTypeNode(
29
+ LiteralFactory.write(tag.value) as any,
30
+ ),
31
+ ],
32
+ );
33
+ return TypeScriptFactory.createTypeReferenceNode(
34
+ TypeScriptFactory.createQualifiedName(
35
+ TypeScriptFactory.createIdentifier(
36
+ importer.external({
37
+ declaration: true,
38
+ file: `typia`,
39
+ type: "element",
40
+ name: "tags",
41
+ }),
42
+ ),
43
+ TypeScriptFactory.createIdentifier("TagBase"),
44
+ ),
45
+ [
46
+ TypeScriptFactory.createLiteralTypeNode(
47
+ LiteralFactory.write({
48
+ target: from,
49
+ kind: tag.kind,
50
+ value: tag.value,
51
+ validate: tag.validate,
52
+ exclusive: tag.exclusive,
53
+ schema: tag.schema,
54
+ }) as any,
55
+ ),
56
+ ],
57
+ );
58
+ };
59
+ }
60
+
61
+ const COMMON_KINDS = ["Default", "Example", "Examples", "Sequence"];
62
+ const PREDEFINED = {
63
+ object: new Set([...COMMON_KINDS]),
64
+ array: new Set([...COMMON_KINDS, "MinItems", "MaxItems", "UniqueItems"]),
65
+ boolean: new Set([...COMMON_KINDS]),
66
+ number: new Set([
67
+ ...COMMON_KINDS,
68
+ "Minimum",
69
+ "Maximum",
70
+ "ExclusiveMinimum",
71
+ "ExclusiveMaximum",
72
+ "MultipleOf",
73
+ "Type",
74
+ ]),
75
+ bigint: new Set([
76
+ ...COMMON_KINDS,
77
+ "Minimum",
78
+ "Maximum",
79
+ "ExclusiveMinimum",
80
+ "ExclusiveMaximum",
81
+ "MultipleOf",
82
+ "Type",
83
+ ]),
84
+ string: new Set([
85
+ ...COMMON_KINDS,
86
+ "ContentMediaType",
87
+ "Format",
88
+ "MaxLength",
89
+ "MinLength",
90
+ "Pattern",
91
+ ]),
92
+ };
93
+
94
+ // export * from "./Constant";
95
+ // export * from "./ContentMediaType";
96
+ // export * from "./Default";
97
+ // export * from "./Example";
98
+ // export * from "./Examples";
99
+ // export * from "./ExclusiveMaximum";
100
+ // export * from "./ExclusiveMinimum";
101
+ // export * from "./Format";
102
+ // export * from "./JsonSchemaPlugin";
103
+ // export * from "./Maximum";
104
+ // export * from "./MaxItems";
105
+ // export * from "./MaxLength";
106
+ // export * from "./Minimum";
107
+ // export * from "./MinItems";
108
+ // export * from "./MinLength";
109
+ // export * from "./MultipleOf";
110
+ // export * from "./Pattern";
111
+ // export * from "./Sequence";
112
+ // export * from "./TagBase";
113
+ // export * from "./Type";
114
+ // export * from "./UniqueItems";
@@ -0,0 +1,319 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+
4
+ import { ITypedApplication } from "../../structures/ITypedApplication";
5
+
6
+ export namespace SdkWebSocketCloneProgrammer {
7
+ export const write = async (app: ITypedApplication): Promise<Set<string>> => {
8
+ const ctx: IContext = {
9
+ output: `${app.project.config.output}/structures`,
10
+ outputs: new Map(),
11
+ visited: new Map(),
12
+ };
13
+ const cloned: Set<string> = new Set();
14
+ for (const route of app.routes)
15
+ if (route.protocol === "websocket")
16
+ for (const imp of route.imports)
17
+ for (const name of imp.elements) {
18
+ if (await clone(ctx)(imp.file, name))
19
+ cloned.add(importKey(imp.file, name));
20
+ }
21
+ return cloned;
22
+ };
23
+
24
+ const clone =
25
+ (ctx: IContext) =>
26
+ async (file: string, name: string): Promise<boolean> => {
27
+ const location: string | null = await resolveSourceFile(file);
28
+ if (location === null || isNodeModulesPath(location)) return false;
29
+
30
+ const key: string = `${location}#${name}`;
31
+ const status: CloneStatus | undefined = ctx.visited.get(key);
32
+ if (status === "written" || status === "pending") return true;
33
+ else if (status === "missing") return false;
34
+ ctx.visited.set(key, "pending");
35
+
36
+ const text: string = await fs.promises.readFile(location, "utf8");
37
+ const declarations: string[] = getDeclarations(text, name);
38
+ if (declarations.length === 0) {
39
+ ctx.visited.set(key, "missing");
40
+ return false;
41
+ }
42
+
43
+ const body: string = declarations.join("\n\n");
44
+ const imports: string[] = await collectImports(ctx)({
45
+ body,
46
+ location,
47
+ source: text,
48
+ target: name,
49
+ });
50
+
51
+ const content: string =
52
+ [...imports, body].filter((line) => line.length).join("\n\n") + "\n";
53
+ const oldbie: string | undefined = ctx.outputs.get(name);
54
+ if (oldbie !== undefined && oldbie !== content) {
55
+ ctx.visited.set(key, "missing");
56
+ return false;
57
+ }
58
+ ctx.outputs.set(name, content);
59
+
60
+ await fs.promises.mkdir(ctx.output, { recursive: true });
61
+ await fs.promises.writeFile(`${ctx.output}/${name}.ts`, content, "utf8");
62
+ ctx.visited.set(key, "written");
63
+ return true;
64
+ };
65
+
66
+ const collectImports =
67
+ (ctx: IContext) =>
68
+ async (props: {
69
+ body: string;
70
+ location: string;
71
+ source: string;
72
+ target: string;
73
+ }): Promise<string[]> => {
74
+ const imports: string[] = [];
75
+ const add = (line: string): void => {
76
+ if (imports.includes(line) === false) imports.push(line);
77
+ };
78
+
79
+ for (const imp of getImports(props.source)) {
80
+ const relative: boolean = imp.specifier.startsWith(".");
81
+ for (const elem of imp.elements) {
82
+ if (uses(props.body, elem.local) === false) continue;
83
+
84
+ if (relative === false) {
85
+ add(imp.text);
86
+ continue;
87
+ }
88
+
89
+ const sourceFile: string = path.resolve(
90
+ path.dirname(props.location),
91
+ imp.specifier,
92
+ );
93
+ if ((await clone(ctx)(sourceFile, elem.imported)) === false) {
94
+ add(imp.text);
95
+ continue;
96
+ }
97
+ add(
98
+ `import type { ${elem.imported}${elem.imported === elem.local ? "" : ` as ${elem.local}`} } from "./${elem.imported}";`,
99
+ );
100
+ }
101
+ for (const name of [imp.default, imp.namespace])
102
+ if (name !== null && uses(props.body, name)) add(imp.text);
103
+ }
104
+
105
+ for (const name of getExportedNames(props.source)) {
106
+ if (name === null || name === props.target) continue;
107
+ if (uses(props.body, name) === false) continue;
108
+ if ((await clone(ctx)(props.location, name)) === true)
109
+ add(`import type { ${name} } from "./${name}";`);
110
+ }
111
+ return imports.sort();
112
+ };
113
+
114
+ const resolveSourceFile = async (file: string): Promise<string | null> => {
115
+ const base: string = trimRuntimeExtension(file);
116
+ const candidates: string[] = [
117
+ file,
118
+ base,
119
+ `${base}.ts`,
120
+ `${base}.tsx`,
121
+ `${base}.d.ts`,
122
+ path.join(base, "index.ts"),
123
+ path.join(base, "index.d.ts"),
124
+ ];
125
+ for (const candidate of candidates) {
126
+ try {
127
+ const location: string = path.resolve(candidate);
128
+ const stat: fs.Stats = await fs.promises.stat(location);
129
+ if (stat.isFile()) return location;
130
+ } catch {}
131
+ }
132
+ return null;
133
+ };
134
+
135
+ const trimRuntimeExtension = (file: string): string => {
136
+ for (const ext of [".js", ".jsx", ".mjs", ".cjs"])
137
+ if (file.endsWith(ext)) return file.slice(0, -ext.length);
138
+ return file;
139
+ };
140
+
141
+ const getDeclarations = (source: string, name: string): string[] => {
142
+ const output: string[] = [];
143
+ const searchable: string = maskTrivia(source);
144
+ const regex: RegExp = declarationRegex(name);
145
+ for (const match of searchable.matchAll(regex)) {
146
+ if (isTopLevel(searchable, match.index!) === false) continue;
147
+ const end: number = declarationEnd(searchable, match.index!, match[1]!);
148
+ if (end !== -1) output.push(source.slice(match.index!, end).trim());
149
+ }
150
+ return output;
151
+ };
152
+
153
+ const getExportedNames = (source: string): string[] => {
154
+ const searchable: string = maskTrivia(source);
155
+ return Array.from(
156
+ searchable.matchAll(declarationRegex("[A-Za-z_$][\\w$]*")),
157
+ )
158
+ .filter((match) => isTopLevel(searchable, match.index!))
159
+ .map((match) => match[2]!);
160
+ };
161
+
162
+ const declarationRegex = (name: string): RegExp =>
163
+ new RegExp(
164
+ `export\\s+(?:declare\\s+)?(interface|type|enum|namespace|class)\\s+(${name})\\b`,
165
+ "g",
166
+ );
167
+
168
+ const declarationEnd = (
169
+ source: string,
170
+ start: number,
171
+ kind: string,
172
+ ): number => {
173
+ let depth: number = 0;
174
+ let block: boolean = false;
175
+ for (let i = start; i < source.length; ++i) {
176
+ const ch: string = source[i]!;
177
+ if (ch === "{") {
178
+ ++depth;
179
+ block = true;
180
+ } else if (ch === "}") {
181
+ --depth;
182
+ if (kind !== "type" && block && depth === 0) return i + 1;
183
+ } else if (ch === ";" && block === false && depth === 0) return i + 1;
184
+ else if (ch === ";" && kind === "type" && depth === 0) return i + 1;
185
+ }
186
+ return -1;
187
+ };
188
+
189
+ const isTopLevel = (source: string, index: number): boolean => {
190
+ let depth: number = 0;
191
+ for (let i = 0; i < index; ++i) {
192
+ const ch: string = source[i]!;
193
+ if (ch === "{") ++depth;
194
+ else if (ch === "}") --depth;
195
+ }
196
+ return depth === 0;
197
+ };
198
+
199
+ const maskTrivia = (source: string): string => {
200
+ const out: string[] = source.split("");
201
+ const mask = (index: number): void => {
202
+ if (out[index] !== "\n" && out[index] !== "\r") out[index] = " ";
203
+ };
204
+
205
+ let quote: string | null = null;
206
+ let escaped: boolean = false;
207
+ for (let i = 0; i < out.length; ++i) {
208
+ const ch: string = source[i]!;
209
+ const next: string | undefined = source[i + 1];
210
+ if (quote !== null) {
211
+ mask(i);
212
+ if (escaped) escaped = false;
213
+ else if (ch === "\\") escaped = true;
214
+ else if (ch === quote) quote = null;
215
+ } else if (ch === "/" && next === "/") {
216
+ mask(i++);
217
+ for (; i < out.length && source[i] !== "\n"; ++i) mask(i);
218
+ --i;
219
+ } else if (ch === "/" && next === "*") {
220
+ mask(i++);
221
+ mask(i);
222
+ for (++i; i < out.length; ++i) {
223
+ const current: string = source[i]!;
224
+ const following: string | undefined = source[i + 1];
225
+ mask(i);
226
+ if (current === "*" && following === "/") {
227
+ mask(++i);
228
+ break;
229
+ }
230
+ }
231
+ } else if (ch === '"' || ch === "'" || ch === "`") {
232
+ quote = ch;
233
+ mask(i);
234
+ }
235
+ }
236
+ return out.join("");
237
+ };
238
+
239
+ const getImports = (source: string): IImport[] =>
240
+ Array.from(
241
+ source.matchAll(/import\s+([\s\S]*?)\s+from\s+["']([^"']+)["'];?/g),
242
+ ).map((match) => {
243
+ const clause: string = match[1]!.trim();
244
+ const specifier: string = match[2]!;
245
+ return {
246
+ text: match[0]!.trim(),
247
+ specifier,
248
+ default: defaultImportName(clause),
249
+ namespace: namespaceImportName(clause),
250
+ elements: namedImportElements(clause),
251
+ };
252
+ });
253
+
254
+ const defaultImportName = (clause: string): string | null => {
255
+ const first: string = clause
256
+ .split(",")[0]!
257
+ .trim()
258
+ .replace(/^type\s+/, "");
259
+ return first.length && first.startsWith("{") === false ? first : null;
260
+ };
261
+
262
+ const namespaceImportName = (clause: string): string | null => {
263
+ const match: RegExpMatchArray | null = clause.match(/\*\s+as\s+(\w+)/);
264
+ return match?.[1] ?? null;
265
+ };
266
+
267
+ const namedImportElements = (clause: string): IImportElement[] => {
268
+ const match: RegExpMatchArray | null = clause.match(/\{([\s\S]*?)\}/);
269
+ if (match === null) return [];
270
+ return match[1]!
271
+ .split(",")
272
+ .map((part) => part.trim().replace(/^type\s+/, ""))
273
+ .filter((part) => part.length)
274
+ .map((part) => {
275
+ const pieces: string[] = part.split(/\s+as\s+/);
276
+ const imported: string = pieces[0]!.trim();
277
+ return {
278
+ imported,
279
+ local: (pieces[1] ?? imported).trim(),
280
+ };
281
+ });
282
+ };
283
+
284
+ const uses = (body: string, name: string): boolean =>
285
+ new RegExp(`\\b${escapeRegExp(name)}\\b`).test(body);
286
+
287
+ const escapeRegExp = (str: string): string =>
288
+ str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
289
+
290
+ export const importKey = (file: string, name: string): string =>
291
+ `${file}#${name}`;
292
+
293
+ export const isNodeModulesPath = (file: string): boolean =>
294
+ path
295
+ .resolve(file)
296
+ .split(/[\\/]+/)
297
+ .includes("node_modules");
298
+ }
299
+
300
+ interface IContext {
301
+ output: string;
302
+ outputs: Map<string, string>;
303
+ visited: Map<string, CloneStatus>;
304
+ }
305
+
306
+ type CloneStatus = "pending" | "written" | "missing";
307
+
308
+ interface IImport {
309
+ text: string;
310
+ specifier: string;
311
+ default: string | null;
312
+ namespace: string | null;
313
+ elements: IImportElement[];
314
+ }
315
+
316
+ interface IImportElement {
317
+ imported: string;
318
+ local: string;
319
+ }