@distilled.cloud/core 1.0.0-rc.12 → 1.0.0-rc.14

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 (276) hide show
  1. package/lib/api.d.ts +4 -4
  2. package/lib/api.d.ts.map +1 -1
  3. package/lib/api.js +3 -5
  4. package/lib/api.js.map +1 -1
  5. package/lib/category.d.ts.map +1 -1
  6. package/lib/category.js +3 -5
  7. package/lib/category.js.map +1 -1
  8. package/lib/codegen/boolean-string-enums.d.ts +8 -8
  9. package/lib/codegen/boolean-string-enums.d.ts.map +1 -1
  10. package/lib/codegen/boolean-string-enums.js +83 -30
  11. package/lib/codegen/boolean-string-enums.js.map +1 -1
  12. package/lib/codegen/boolean-string-enums.test.js +130 -2
  13. package/lib/codegen/boolean-string-enums.test.js.map +1 -1
  14. package/lib/codegen/cli.d.ts +5 -4
  15. package/lib/codegen/cli.d.ts.map +1 -1
  16. package/lib/codegen/cli.js +12 -9
  17. package/lib/codegen/cli.js.map +1 -1
  18. package/lib/codegen/defer-naming.test.d.ts +2 -0
  19. package/lib/codegen/defer-naming.test.d.ts.map +1 -0
  20. package/lib/codegen/defer-naming.test.js +105 -0
  21. package/lib/codegen/defer-naming.test.js.map +1 -0
  22. package/lib/codegen/emit.d.ts.map +1 -1
  23. package/lib/codegen/emit.js +1 -3
  24. package/lib/codegen/emit.js.map +1 -1
  25. package/lib/codegen/format.d.ts +0 -12
  26. package/lib/codegen/format.d.ts.map +1 -1
  27. package/lib/codegen/format.js +10 -6
  28. package/lib/codegen/format.js.map +1 -1
  29. package/lib/codegen/generated-at.d.ts +29 -0
  30. package/lib/codegen/generated-at.d.ts.map +1 -0
  31. package/lib/codegen/generated-at.js +78 -0
  32. package/lib/codegen/generated-at.js.map +1 -0
  33. package/lib/codegen/generated-at.test.d.ts +2 -0
  34. package/lib/codegen/generated-at.test.d.ts.map +1 -0
  35. package/lib/codegen/generated-at.test.js +81 -0
  36. package/lib/codegen/generated-at.test.js.map +1 -0
  37. package/lib/codegen/generator.d.ts +24 -5
  38. package/lib/codegen/generator.d.ts.map +1 -1
  39. package/lib/codegen/generator.js +69 -80
  40. package/lib/codegen/generator.js.map +1 -1
  41. package/lib/codegen/generator.test.d.ts +2 -0
  42. package/lib/codegen/generator.test.d.ts.map +1 -0
  43. package/lib/codegen/generator.test.js +121 -0
  44. package/lib/codegen/generator.test.js.map +1 -0
  45. package/lib/codegen/graphql-client.d.ts +7 -1
  46. package/lib/codegen/graphql-client.d.ts.map +1 -1
  47. package/lib/codegen/graphql-client.js +264 -148
  48. package/lib/codegen/graphql-client.js.map +1 -1
  49. package/lib/codegen/graphql-client.test.js +98 -238
  50. package/lib/codegen/graphql-client.test.js.map +1 -1
  51. package/lib/codegen/graphql.d.ts.map +1 -1
  52. package/lib/codegen/graphql.js +7 -20
  53. package/lib/codegen/graphql.js.map +1 -1
  54. package/lib/codegen/model-diff.d.ts +10 -0
  55. package/lib/codegen/model-diff.d.ts.map +1 -0
  56. package/lib/codegen/model-diff.js +67 -0
  57. package/lib/codegen/model-diff.js.map +1 -0
  58. package/lib/codegen/naming.d.ts.map +1 -1
  59. package/lib/codegen/naming.js.map +1 -1
  60. package/lib/codegen/openapi-binary.test.d.ts +2 -0
  61. package/lib/codegen/openapi-binary.test.d.ts.map +1 -0
  62. package/lib/codegen/openapi-binary.test.js +67 -0
  63. package/lib/codegen/openapi-binary.test.js.map +1 -0
  64. package/lib/codegen/openapi-cli.d.ts +11 -11
  65. package/lib/codegen/openapi-cli.d.ts.map +1 -1
  66. package/lib/codegen/openapi-cli.js +26 -51
  67. package/lib/codegen/openapi-cli.js.map +1 -1
  68. package/lib/codegen/openapi.d.ts +16 -2
  69. package/lib/codegen/openapi.d.ts.map +1 -1
  70. package/lib/codegen/openapi.js +56 -39
  71. package/lib/codegen/openapi.js.map +1 -1
  72. package/lib/codegen/operations.d.ts.map +1 -1
  73. package/lib/codegen/operations.js.map +1 -1
  74. package/lib/codegen/pagination.d.ts.map +1 -1
  75. package/lib/codegen/pagination.js +2 -4
  76. package/lib/codegen/pagination.js.map +1 -1
  77. package/lib/codegen/patch-audit.d.ts +84 -0
  78. package/lib/codegen/patch-audit.d.ts.map +1 -0
  79. package/lib/codegen/patch-audit.js +366 -0
  80. package/lib/codegen/patch-audit.js.map +1 -0
  81. package/lib/codegen/patch-audit.test.d.ts +2 -0
  82. package/lib/codegen/patch-audit.test.d.ts.map +1 -0
  83. package/lib/codegen/patch-audit.test.js +141 -0
  84. package/lib/codegen/patch-audit.test.js.map +1 -0
  85. package/lib/codegen/patches.d.ts +75 -11
  86. package/lib/codegen/patches.d.ts.map +1 -1
  87. package/lib/codegen/patches.js +301 -65
  88. package/lib/codegen/patches.js.map +1 -1
  89. package/lib/codegen/patches.test.js +55 -5
  90. package/lib/codegen/patches.test.js.map +1 -1
  91. package/lib/codegen/prelude.js +1 -1
  92. package/lib/codegen/prelude.js.map +1 -1
  93. package/lib/codegen/proto.d.ts.map +1 -1
  94. package/lib/codegen/proto.js +2 -4
  95. package/lib/codegen/proto.js.map +1 -1
  96. package/lib/codegen/protocol-imports.test.d.ts +2 -0
  97. package/lib/codegen/protocol-imports.test.d.ts.map +1 -0
  98. package/lib/codegen/protocol-imports.test.js +36 -0
  99. package/lib/codegen/protocol-imports.test.js.map +1 -0
  100. package/lib/codegen/rewrite-operation-ids.d.ts +23 -0
  101. package/lib/codegen/rewrite-operation-ids.d.ts.map +1 -1
  102. package/lib/codegen/rewrite-operation-ids.js +97 -59
  103. package/lib/codegen/rewrite-operation-ids.js.map +1 -1
  104. package/lib/codegen/rewrite-operation-ids.test.js +19 -101
  105. package/lib/codegen/rewrite-operation-ids.test.js.map +1 -1
  106. package/lib/codegen/spec-path.d.ts +1 -1
  107. package/lib/codegen/spec-path.d.ts.map +1 -1
  108. package/lib/codegen/spec-path.js +1 -1
  109. package/lib/codegen/spec-path.js.map +1 -1
  110. package/lib/codegen/untagged-unions.test.d.ts +2 -0
  111. package/lib/codegen/untagged-unions.test.d.ts.map +1 -0
  112. package/lib/codegen/untagged-unions.test.js +80 -0
  113. package/lib/codegen/untagged-unions.test.js.map +1 -0
  114. package/lib/error-category.d.ts.map +1 -1
  115. package/lib/error-category.js.map +1 -1
  116. package/lib/errors.d.ts +2 -1
  117. package/lib/errors.d.ts.map +1 -1
  118. package/lib/errors.js +9 -4
  119. package/lib/errors.js.map +1 -1
  120. package/lib/fixtures/xml/upstream-validator.json +397 -0
  121. package/lib/graphql.d.ts +292 -205
  122. package/lib/graphql.d.ts.map +1 -1
  123. package/lib/graphql.js +945 -636
  124. package/lib/graphql.js.map +1 -1
  125. package/lib/graphql.test.js +135 -764
  126. package/lib/graphql.test.js.map +1 -1
  127. package/lib/graphql.types.d.ts +10 -0
  128. package/lib/graphql.types.d.ts.map +1 -1
  129. package/lib/graphql.types.js +4 -44
  130. package/lib/graphql.types.js.map +1 -1
  131. package/lib/index.d.ts +27 -0
  132. package/lib/index.d.ts.map +1 -0
  133. package/lib/index.js +27 -0
  134. package/lib/index.js.map +1 -0
  135. package/lib/json-patch.d.ts +13 -6
  136. package/lib/json-patch.d.ts.map +1 -1
  137. package/lib/json-patch.js +34 -19
  138. package/lib/json-patch.js.map +1 -1
  139. package/lib/json-patch.test.d.ts +2 -0
  140. package/lib/json-patch.test.d.ts.map +1 -0
  141. package/lib/json-patch.test.js +65 -0
  142. package/lib/json-patch.test.js.map +1 -0
  143. package/lib/pagination.d.ts +2 -1
  144. package/lib/pagination.d.ts.map +1 -1
  145. package/lib/pagination.js +28 -30
  146. package/lib/pagination.js.map +1 -1
  147. package/lib/pagination.test.d.ts +2 -0
  148. package/lib/pagination.test.d.ts.map +1 -0
  149. package/lib/pagination.test.js +131 -0
  150. package/lib/pagination.test.js.map +1 -0
  151. package/lib/protocol-http.d.ts +30 -16
  152. package/lib/protocol-http.d.ts.map +1 -1
  153. package/lib/protocol-http.js +133 -84
  154. package/lib/protocol-http.js.map +1 -1
  155. package/lib/protocol-http.test.js +141 -1
  156. package/lib/protocol-http.test.js.map +1 -1
  157. package/lib/protocol-rest.d.ts +18 -1
  158. package/lib/protocol-rest.d.ts.map +1 -1
  159. package/lib/protocol-rest.js +32 -21
  160. package/lib/protocol-rest.js.map +1 -1
  161. package/lib/protocol-rest.test.d.ts +2 -0
  162. package/lib/protocol-rest.test.d.ts.map +1 -0
  163. package/lib/protocol-rest.test.js +498 -0
  164. package/lib/protocol-rest.test.js.map +1 -0
  165. package/lib/query.d.ts +29 -0
  166. package/lib/query.d.ts.map +1 -0
  167. package/lib/query.js +27 -0
  168. package/lib/query.js.map +1 -0
  169. package/lib/response-validation.d.ts +67 -0
  170. package/lib/response-validation.d.ts.map +1 -0
  171. package/lib/response-validation.js +76 -0
  172. package/lib/response-validation.js.map +1 -0
  173. package/lib/response-validation.test.d.ts +2 -0
  174. package/lib/response-validation.test.d.ts.map +1 -0
  175. package/lib/response-validation.test.js +72 -0
  176. package/lib/response-validation.test.js.map +1 -0
  177. package/lib/retry-after.d.ts.map +1 -1
  178. package/lib/retry-after.js +1 -1
  179. package/lib/retry-after.js.map +1 -1
  180. package/lib/retry-after.test.d.ts +2 -0
  181. package/lib/retry-after.test.d.ts.map +1 -0
  182. package/lib/retry-after.test.js +204 -0
  183. package/lib/retry-after.test.js.map +1 -0
  184. package/lib/retry.d.ts.map +1 -1
  185. package/lib/retry.js +1 -2
  186. package/lib/retry.js.map +1 -1
  187. package/lib/retry.test.d.ts +2 -0
  188. package/lib/retry.test.d.ts.map +1 -0
  189. package/lib/retry.test.js +407 -0
  190. package/lib/retry.test.js.map +1 -0
  191. package/lib/sdks.test.d.ts +2 -0
  192. package/lib/sdks.test.d.ts.map +1 -0
  193. package/lib/sdks.test.js +76 -0
  194. package/lib/sdks.test.js.map +1 -0
  195. package/lib/testing.d.ts +39 -0
  196. package/lib/testing.d.ts.map +1 -0
  197. package/lib/testing.js +41 -0
  198. package/lib/testing.js.map +1 -0
  199. package/lib/trait.d.ts +29 -9
  200. package/lib/trait.d.ts.map +1 -1
  201. package/lib/trait.js +8 -0
  202. package/lib/trait.js.map +1 -1
  203. package/lib/xml.d.ts +44 -0
  204. package/lib/xml.d.ts.map +1 -0
  205. package/lib/xml.js +311 -0
  206. package/lib/xml.js.map +1 -0
  207. package/lib/xml.test.d.ts +2 -0
  208. package/lib/xml.test.d.ts.map +1 -0
  209. package/lib/xml.test.js +210 -0
  210. package/lib/xml.test.js.map +1 -0
  211. package/package.json +8 -11
  212. package/src/api.ts +38 -55
  213. package/src/category.ts +21 -50
  214. package/src/codegen/boolean-string-enums.test.ts +148 -17
  215. package/src/codegen/boolean-string-enums.ts +81 -32
  216. package/src/codegen/cli.ts +29 -47
  217. package/src/codegen/defer-naming.test.ts +113 -0
  218. package/src/codegen/emit.ts +2 -5
  219. package/src/codegen/format.ts +12 -10
  220. package/src/codegen/generated-at.test.ts +89 -0
  221. package/src/codegen/generated-at.ts +88 -0
  222. package/src/codegen/generator.test.ts +137 -0
  223. package/src/codegen/generator.ts +132 -193
  224. package/src/codegen/graphql-client.test.ts +100 -297
  225. package/src/codegen/graphql-client.ts +302 -210
  226. package/src/codegen/graphql.ts +22 -94
  227. package/src/codegen/model-diff.ts +67 -0
  228. package/src/codegen/naming.ts +1 -2
  229. package/src/codegen/openapi-binary.test.ts +91 -0
  230. package/src/codegen/openapi-cli.ts +38 -79
  231. package/src/codegen/openapi.ts +98 -133
  232. package/src/codegen/operations.ts +2 -5
  233. package/src/codegen/pagination.ts +4 -11
  234. package/src/codegen/patch-audit.test.ts +160 -0
  235. package/src/codegen/patch-audit.ts +480 -0
  236. package/src/codegen/patches.test.ts +73 -15
  237. package/src/codegen/patches.ts +389 -99
  238. package/src/codegen/prelude.ts +1 -1
  239. package/src/codegen/proto.ts +18 -54
  240. package/src/codegen/protocol-imports.test.ts +39 -0
  241. package/src/codegen/rewrite-operation-ids.test.ts +23 -114
  242. package/src/codegen/rewrite-operation-ids.ts +111 -98
  243. package/src/codegen/spec-path.ts +3 -6
  244. package/src/codegen/untagged-unions.test.ts +85 -0
  245. package/src/error-category.ts +5 -17
  246. package/src/errors.ts +18 -30
  247. package/src/fixtures/xml/upstream-validator.json +397 -0
  248. package/src/graphql.test.ts +173 -949
  249. package/src/graphql.ts +1468 -1186
  250. package/src/graphql.types.ts +78 -164
  251. package/src/index.ts +26 -0
  252. package/src/json-patch.test.ts +77 -0
  253. package/src/json-patch.ts +54 -60
  254. package/src/pagination.test.ts +169 -0
  255. package/src/pagination.ts +46 -103
  256. package/src/protocol-http.test.ts +178 -9
  257. package/src/protocol-http.ts +174 -163
  258. package/src/protocol-rest.test.ts +703 -0
  259. package/src/protocol-rest.ts +76 -65
  260. package/src/query.ts +39 -0
  261. package/src/response-validation.test.ts +89 -0
  262. package/src/response-validation.ts +107 -0
  263. package/src/retry-after.test.ts +267 -0
  264. package/src/retry-after.ts +4 -10
  265. package/src/retry.test.ts +566 -0
  266. package/src/retry.ts +6 -21
  267. package/src/sdks.test.ts +87 -0
  268. package/src/testing.ts +72 -0
  269. package/src/trait.ts +48 -63
  270. package/src/xml.test.ts +251 -0
  271. package/src/xml.ts +323 -0
  272. package/lib/graphql.fixture.d.ts +0 -249
  273. package/lib/graphql.fixture.d.ts.map +0 -1
  274. package/lib/graphql.fixture.js +0 -240
  275. package/lib/graphql.fixture.js.map +0 -1
  276. package/src/graphql.fixture.ts +0 -371
@@ -10,21 +10,23 @@
10
10
  * Lives here so the shared {@link runGeneratorCli} and the providers with
11
11
  * their own pipelines run the identical step.
12
12
  */
13
+ import { spawn } from "node:child_process";
13
14
  import { Console, Effect } from "effect";
14
15
 
15
16
  /** Run a dev-time tool, failing the generate run if it does. */
16
- export const runTool = (
17
- argv: readonly string[],
18
- ): Effect.Effect<void, never, never> =>
17
+ export const runTool = (argv: readonly string[]): Effect.Effect<void, never, never> =>
19
18
  Effect.tryPromise({
20
19
  try: () =>
21
- Bun.spawn([...argv], { stdout: "inherit", stderr: "inherit" }).exited,
20
+ new Promise<number | null>((resolve, reject) => {
21
+ const [command, ...args] = argv;
22
+ spawn(command!, args, { stdio: ["ignore", "inherit", "inherit"] })
23
+ .on("error", reject)
24
+ .on("exit", resolve);
25
+ }),
22
26
  catch: (cause) => new Error(`${argv[0]} failed to start: ${cause}`),
23
27
  }).pipe(
24
28
  Effect.flatMap((code) =>
25
- code === 0
26
- ? Effect.void
27
- : Effect.die(new Error(`${argv.join(" ")} exited with ${code}`)),
29
+ code === 0 ? Effect.void : Effect.die(new Error(`${argv.join(" ")} exited with ${code}`)),
28
30
  ),
29
31
  Effect.catchCause((cause) => Effect.die(cause)),
30
32
  );
@@ -32,7 +34,7 @@ export const runTool = (
32
34
  /** Format a generated directory in place. */
33
35
  export const formatGenerated = (dir: string) =>
34
36
  Effect.flatMap(Console.log(`\n🧹 Formatting ${dir}`), () =>
35
- runTool(["bunx", "oxfmt", dir]),
37
+ runTool(["pnpm", "exec", "oxfmt", dir]),
36
38
  );
37
39
 
38
40
  /**
@@ -41,7 +43,7 @@ export const formatGenerated = (dir: string) =>
41
43
  */
42
44
  export const lintAndFormatGenerated = (dir: string) =>
43
45
  Effect.flatMap(Console.log(`\n🧹 Linting and formatting ${dir}`), () =>
44
- Effect.flatMap(runTool(["bunx", "oxlint", "--fix", dir]), () =>
45
- runTool(["bunx", "oxfmt", dir]),
46
+ Effect.flatMap(runTool(["pnpm", "exec", "oxlint", "--fix", dir]), () =>
47
+ runTool(["pnpm", "exec", "oxfmt", dir]),
46
48
  ),
47
49
  );
@@ -0,0 +1,89 @@
1
+ import { execFileSync } from "node:child_process";
2
+ import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { dirname, join } from "node:path";
5
+ import { afterEach, describe, expect, it } from "vitest";
6
+ import { lastChangeDate, specUpdatedAt, stampGeneratedAt } from "./generated-at.ts";
7
+
8
+ describe("stampGeneratedAt", () => {
9
+ let dir: string;
10
+ afterEach(() => rmSync(dir, { recursive: true, force: true }));
11
+
12
+ const setup = (pkg: object) => {
13
+ dir = mkdtempSync(join(tmpdir(), "generated-at-"));
14
+ writeFileSync(join(dir, "package.json"), `${JSON.stringify(pkg, null, 2)}\n`);
15
+ };
16
+ const read = () => readFileSync(join(dir, "package.json"), "utf8");
17
+
18
+ it("adds distilled.generatedAt as a UTC date and keeps the other fields in order", () => {
19
+ setup({ name: "@distilled.cloud/x", version: "1.0.0", scripts: { generate: "g" } });
20
+ const stamp = stampGeneratedAt(dir, {
21
+ now: new Date("2026-10-08T23:30:00-05:00"),
22
+ specUpdatedAt: "2026-10-03",
23
+ });
24
+ expect(stamp).toEqual({ generatedAt: "2026-10-09", specUpdatedAt: "2026-10-03" });
25
+ const text = read();
26
+ expect(Object.keys(JSON.parse(text))).toEqual(["name", "version", "scripts", "distilled"]);
27
+ expect(JSON.parse(text).distilled).toEqual({
28
+ generatedAt: "2026-10-09",
29
+ specUpdatedAt: "2026-10-03",
30
+ });
31
+ expect(text.endsWith("}\n")).toBe(true);
32
+ });
33
+
34
+ it("updates existing dates and keeps other distilled fields", () => {
35
+ setup({ name: "x", distilled: { other: true, generatedAt: "2025-01-01" } });
36
+ stampGeneratedAt(dir, { now: new Date("2026-10-08T12:00:00Z"), specUpdatedAt: "2026-10-01" });
37
+ expect(JSON.parse(read()).distilled).toEqual({
38
+ other: true,
39
+ generatedAt: "2026-10-08",
40
+ specUpdatedAt: "2026-10-01",
41
+ });
42
+ });
43
+
44
+ it("keeps the existing spec date when the spec date is unknown", () => {
45
+ setup({ name: "x", distilled: { generatedAt: "2025-01-01", specUpdatedAt: "2024-12-31" } });
46
+ stampGeneratedAt(dir, { now: new Date("2026-10-08T12:00:00Z"), specUpdatedAt: undefined });
47
+ expect(JSON.parse(read()).distilled).toEqual({
48
+ generatedAt: "2026-10-08",
49
+ specUpdatedAt: "2024-12-31",
50
+ });
51
+ });
52
+
53
+ it("finds no spec date for a package without specs/", () => {
54
+ setup({ name: "x" });
55
+ expect(specUpdatedAt(dir)).toBeUndefined();
56
+ });
57
+ });
58
+
59
+ describe("lastChangeDate", () => {
60
+ let dir: string;
61
+ afterEach(() => rmSync(dir, { recursive: true, force: true }));
62
+
63
+ const sh = (cwd: string, ...args: string[]) => execFileSync("git", args, { cwd, stdio: "pipe" });
64
+ const commit = (cwd: string, file: string, date: string) => {
65
+ mkdirSync(join(cwd, dirname(file)), { recursive: true });
66
+ writeFileSync(join(cwd, file), date);
67
+ sh(cwd, "add", "-A");
68
+ execFileSync("git", ["commit", "-qm", file], {
69
+ cwd,
70
+ env: { ...process.env, GIT_COMMITTER_DATE: date, GIT_AUTHOR_DATE: date },
71
+ });
72
+ };
73
+
74
+ it("deepens a shallow clone past commits that do not touch the path", () => {
75
+ dir = mkdtempSync(join(tmpdir(), "spec-date-"));
76
+ const upstream = join(dir, "upstream");
77
+ mkdirSync(upstream);
78
+ sh(upstream, "init", "-q", "-b", "main");
79
+ sh(upstream, "config", "user.email", "t@t");
80
+ sh(upstream, "config", "user.name", "t");
81
+ sh(upstream, "config", "uploadpack.allowFilter", "true");
82
+ commit(upstream, "specs/a.json", "2026-09-30T10:00:00Z");
83
+ commit(upstream, "specs/a.json", "2026-10-03T23:30:00-05:00");
84
+ commit(upstream, ".meta/x.ts", "2026-10-05T10:00:00Z");
85
+ sh(dir, "clone", "-q", "--depth=1", `file://${upstream}`, "mirror");
86
+ // 23:30 at UTC-5 is the next day in UTC.
87
+ expect(lastChangeDate(join(dir, "mirror"), "specs")).toBe("2026-10-04");
88
+ });
89
+ });
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Records, in the package's `package.json`, two UTC `YYYY-MM-DD` dates:
3
+ *
4
+ * - `distilled.generatedAt` — when the SDK was last generated.
5
+ * - `distilled.specUpdatedAt` — when the spec it was generated from last
6
+ * changed: the newest commit at or before the pinned mirror commit that
7
+ * touched the mirror's `specs/` directory.
8
+ *
9
+ * Every successful generator run calls this, so both dates land in the same
10
+ * commit as the regenerated code and ship to npm with the package. Dates (not
11
+ * timestamps) keep repeat runs on the same day from churning the manifest.
12
+ */
13
+ import { execFileSync } from "node:child_process";
14
+ import { existsSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
15
+ import { join } from "node:path";
16
+ import { isLocalSpecs } from "./spec-path.ts";
17
+
18
+ /** A date in UTC, `YYYY-MM-DD`. */
19
+ export const generatedAtDate = (now: Date = new Date()): string => now.toISOString().slice(0, 10);
20
+
21
+ const git = (cwd: string, ...args: string[]): string =>
22
+ execFileSync("git", args, { cwd, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }).trim();
23
+
24
+ /** Commits at a shallow boundary; git shows them as adding every file. */
25
+ const shallowCommits = (cwd: string): Set<string> => {
26
+ const file = git(cwd, "rev-parse", "--path-format=absolute", "--git-path", "shallow");
27
+ return new Set(existsSync(file) ? readFileSync(file, "utf8").split("\n").filter(Boolean) : []);
28
+ };
29
+
30
+ /**
31
+ * Date of the newest commit at or before `HEAD` of the repository at `cwd`
32
+ * that changed `pathspec`. Mirrors are checked out `--depth=1`, so this
33
+ * deepens the history (commits and trees only, no file content) until the
34
+ * answer is a real commit and not the shallow boundary.
35
+ */
36
+ export const lastChangeDate = (cwd: string, pathspec: string): string => {
37
+ for (let round = 0; round < 20; round++) {
38
+ const [sha, date] = git(cwd, "log", "-1", "--format=%H %cI", "HEAD", "--", pathspec).split(" ");
39
+ if (!sha) throw new Error(`no commit in ${cwd} touches ${pathspec}`);
40
+ if (!shallowCommits(cwd).has(sha)) return generatedAtDate(new Date(date!));
41
+ git(cwd, "fetch", "--quiet", "--deepen=50", "--filter=blob:none", "origin");
42
+ }
43
+ throw new Error(`gave up deepening ${cwd} to find the last change to ${pathspec}`);
44
+ };
45
+
46
+ /**
47
+ * When the package's spec last changed, or `undefined` if that cannot be
48
+ * known here: no `specs/` directory (a hand-written SDK), the mirror is not
49
+ * checked out, or specs come from the untracked `specs/.local` copy.
50
+ */
51
+ export const specUpdatedAt = (root: string): string | undefined => {
52
+ const specs = join(root, "specs");
53
+ if (isLocalSpecs() || !existsSync(specs)) return undefined;
54
+ const mirror = readdirSync(specs).find((d) => d.startsWith("spec-mirror-"));
55
+ if (mirror === undefined) return lastChangeDate(root, "specs");
56
+ const dir = join(specs, mirror);
57
+ // An uninitialised submodule is an empty directory with no `.git`.
58
+ return existsSync(join(dir, ".git")) ? lastChangeDate(dir, "specs") : undefined;
59
+ };
60
+
61
+ export interface GeneratedAtStamp {
62
+ readonly generatedAt: string;
63
+ /** `undefined` when unknown; the existing value in `package.json` is kept. */
64
+ readonly specUpdatedAt: string | undefined;
65
+ }
66
+
67
+ /**
68
+ * Set `distilled.generatedAt` and `distilled.specUpdatedAt` in
69
+ * `<root>/package.json`; returns the dates written.
70
+ */
71
+ export const stampGeneratedAt = (
72
+ root: string,
73
+ options: { readonly now?: Date; readonly specUpdatedAt?: string | undefined } = {},
74
+ ): GeneratedAtStamp => {
75
+ const generatedAt = generatedAtDate(options.now);
76
+ const spec = "specUpdatedAt" in options ? options.specUpdatedAt : specUpdatedAt(root);
77
+ const file = join(root, "package.json");
78
+ const text = readFileSync(file, "utf8");
79
+ const pkg = JSON.parse(text);
80
+ pkg.distilled = {
81
+ ...pkg.distilled,
82
+ generatedAt,
83
+ ...(spec === undefined ? {} : { specUpdatedAt: spec }),
84
+ };
85
+ const next = `${JSON.stringify(pkg, null, 2)}\n`;
86
+ if (next !== text) writeFileSync(file, next);
87
+ return { generatedAt, specUpdatedAt: spec };
88
+ };
@@ -0,0 +1,137 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { generateService, type SdkSpec } from "./generator.ts";
3
+
4
+ const primitiveUnionModel = (kind: "enum" | "intEnum", request: boolean) => ({
5
+ smithy: "2.0",
6
+ shapes: {
7
+ "com.example.union#Example": {
8
+ type: "service",
9
+ version: "2024-01-01",
10
+ operations: [{ target: "com.example.union#GetValue" }],
11
+ },
12
+ "com.example.union#GetValue": {
13
+ type: "operation",
14
+ input: { target: "com.example.union#Request" },
15
+ output: { target: "com.example.union#Response" },
16
+ traits: { "smithy.api#http": { method: "POST", uri: "/value" } },
17
+ },
18
+ "com.example.union#Request": {
19
+ type: "structure",
20
+ traits: { "smithy.api#input": {} },
21
+ members: request ? { value: { target: "com.example.union#Value" } } : {},
22
+ },
23
+ "com.example.union#Response": {
24
+ type: "structure",
25
+ members: { value: { target: "com.example.union#Value" } },
26
+ },
27
+ "com.example.union#Mode": {
28
+ type: kind,
29
+ members: {
30
+ STOP: {
31
+ target: "smithy.api#Unit",
32
+ traits: { "smithy.api#enumValue": kind === "enum" ? "stop" : 1 },
33
+ },
34
+ },
35
+ },
36
+ "com.example.union#Value": {
37
+ type: "union",
38
+ members: {
39
+ mode: { target: "com.example.union#Mode" },
40
+ legacy: { target: "smithy.api#Boolean" },
41
+ },
42
+ },
43
+ },
44
+ });
45
+
46
+ const spec: SdkSpec = {
47
+ unionStyle: "primitive",
48
+ operationDecl: {
49
+ contextType: "ExampleContext",
50
+ commonErrorType: "ExampleError",
51
+ commonErrorClasses: [],
52
+ protocol: "ExampleProtocol",
53
+ retry: "Retry.Retry",
54
+ },
55
+ };
56
+
57
+ describe("operation protocol overrides", () => {
58
+ for (const retry of [undefined, "Retry.Retry"]) {
59
+ for (const overridden of [false, true]) {
60
+ test(`${overridden ? "overridden" : "default"} protocol with ${retry ? "configured" : "disabled"} retries`, () => {
61
+ const { code } = generateService(primitiveUnionModel("enum", false), {
62
+ ...spec,
63
+ operationDecl: {
64
+ contextType: "ExampleContext",
65
+ commonErrorType: "ExampleError",
66
+ commonErrorClasses: [],
67
+ protocol: "ExampleProtocol",
68
+ retry,
69
+ overrides: () =>
70
+ overridden ? { contextType: "PublicContext", protocol: "PublicProtocol" } : undefined,
71
+ },
72
+ });
73
+ expect(code).toContain(` protocol: ${overridden ? "PublicProtocol" : "ExampleProtocol"},`);
74
+ expect(code).toContain(` ${overridden ? "PublicContext" : "ExampleContext"}\n>`);
75
+ if (retry) expect(code).toContain(` retry: ${retry},`);
76
+ else expect(code).not.toContain(" retry:");
77
+ });
78
+ }
79
+ }
80
+ });
81
+
82
+ describe("primitive union generation", () => {
83
+ for (const kind of ["enum", "intEnum"] as const) {
84
+ for (const request of [false, true]) {
85
+ test(`${kind}/Boolean stays open when ${request ? "request-reachable" : "response-only"}`, () => {
86
+ const { code } = generateService(primitiveUnionModel(kind, request), spec);
87
+ const primitive = kind === "enum" ? "string" : "number";
88
+ expect(code).toContain(`export type Value = Mode | (${primitive} & {}) | boolean;`);
89
+ expect(code).toContain(
90
+ "export const Value: S.Codec<Value> = /*@__PURE__*/ S.Union([Mode, S.Boolean]);",
91
+ );
92
+ expect(code).toContain(
93
+ kind === "enum" ? "export const Mode = S.String;" : "export const Mode = S.Number;",
94
+ );
95
+ expect(code).not.toContain("export type Value = Mode | boolean;");
96
+ });
97
+ }
98
+ }
99
+
100
+ test("opaque-cases retains its existing primitive union codec", () => {
101
+ const { code } = generateService(primitiveUnionModel("enum", false), {
102
+ ...spec,
103
+ unionStyle: "opaque-cases",
104
+ });
105
+ expect(code).toContain("export type Value = Mode | boolean;");
106
+ expect(code).toContain(
107
+ "export const Value = /*@__PURE__*/ S.Unknown.pipe(T.UnionCases([[],[]]));",
108
+ );
109
+ expect(code).not.toContain("S.Union([Mode, S.Boolean])");
110
+ });
111
+
112
+ test("primitive union emission requires an explicit style", () => {
113
+ expect(() =>
114
+ generateService(primitiveUnionModel("enum", false), {
115
+ ...spec,
116
+ unionStyle: undefined,
117
+ }),
118
+ ).toThrow("no union emission configured for shape com.example.union#Value");
119
+ });
120
+
121
+ test("primitive style rejects object cases", () => {
122
+ const model = primitiveUnionModel("enum", false);
123
+ model.shapes["com.example.union#Value"].members.mode.target = "com.example.union#Request";
124
+ expect(() => generateService(model, spec)).toThrow(
125
+ "no union emission configured for shape com.example.union#Value",
126
+ );
127
+ });
128
+
129
+ test("custom union emission retains precedence", () => {
130
+ const { code } = generateService(primitiveUnionModel("enum", false), {
131
+ ...spec,
132
+ union: ({ name }) => [`export type ${name} = CustomUnion;`],
133
+ });
134
+ expect(code).toContain("export type Value = CustomUnion;");
135
+ expect(code).not.toContain("S.Union([Mode, S.Boolean])");
136
+ });
137
+ });