@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.
- package/lib/api.d.ts +4 -4
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +3 -5
- package/lib/api.js.map +1 -1
- package/lib/category.d.ts.map +1 -1
- package/lib/category.js +3 -5
- package/lib/category.js.map +1 -1
- package/lib/codegen/boolean-string-enums.d.ts +8 -8
- package/lib/codegen/boolean-string-enums.d.ts.map +1 -1
- package/lib/codegen/boolean-string-enums.js +83 -30
- package/lib/codegen/boolean-string-enums.js.map +1 -1
- package/lib/codegen/boolean-string-enums.test.js +130 -2
- package/lib/codegen/boolean-string-enums.test.js.map +1 -1
- package/lib/codegen/cli.d.ts +5 -4
- package/lib/codegen/cli.d.ts.map +1 -1
- package/lib/codegen/cli.js +12 -9
- package/lib/codegen/cli.js.map +1 -1
- package/lib/codegen/defer-naming.test.d.ts +2 -0
- package/lib/codegen/defer-naming.test.d.ts.map +1 -0
- package/lib/codegen/defer-naming.test.js +105 -0
- package/lib/codegen/defer-naming.test.js.map +1 -0
- package/lib/codegen/emit.d.ts.map +1 -1
- package/lib/codegen/emit.js +1 -3
- package/lib/codegen/emit.js.map +1 -1
- package/lib/codegen/format.d.ts +0 -12
- package/lib/codegen/format.d.ts.map +1 -1
- package/lib/codegen/format.js +10 -6
- package/lib/codegen/format.js.map +1 -1
- package/lib/codegen/generated-at.d.ts +29 -0
- package/lib/codegen/generated-at.d.ts.map +1 -0
- package/lib/codegen/generated-at.js +78 -0
- package/lib/codegen/generated-at.js.map +1 -0
- package/lib/codegen/generated-at.test.d.ts +2 -0
- package/lib/codegen/generated-at.test.d.ts.map +1 -0
- package/lib/codegen/generated-at.test.js +81 -0
- package/lib/codegen/generated-at.test.js.map +1 -0
- package/lib/codegen/generator.d.ts +24 -5
- package/lib/codegen/generator.d.ts.map +1 -1
- package/lib/codegen/generator.js +69 -80
- package/lib/codegen/generator.js.map +1 -1
- package/lib/codegen/generator.test.d.ts +2 -0
- package/lib/codegen/generator.test.d.ts.map +1 -0
- package/lib/codegen/generator.test.js +121 -0
- package/lib/codegen/generator.test.js.map +1 -0
- package/lib/codegen/graphql-client.d.ts +7 -1
- package/lib/codegen/graphql-client.d.ts.map +1 -1
- package/lib/codegen/graphql-client.js +264 -148
- package/lib/codegen/graphql-client.js.map +1 -1
- package/lib/codegen/graphql-client.test.js +98 -238
- package/lib/codegen/graphql-client.test.js.map +1 -1
- package/lib/codegen/graphql.d.ts.map +1 -1
- package/lib/codegen/graphql.js +7 -20
- package/lib/codegen/graphql.js.map +1 -1
- package/lib/codegen/model-diff.d.ts +10 -0
- package/lib/codegen/model-diff.d.ts.map +1 -0
- package/lib/codegen/model-diff.js +67 -0
- package/lib/codegen/model-diff.js.map +1 -0
- package/lib/codegen/naming.d.ts.map +1 -1
- package/lib/codegen/naming.js.map +1 -1
- package/lib/codegen/openapi-binary.test.d.ts +2 -0
- package/lib/codegen/openapi-binary.test.d.ts.map +1 -0
- package/lib/codegen/openapi-binary.test.js +67 -0
- package/lib/codegen/openapi-binary.test.js.map +1 -0
- package/lib/codegen/openapi-cli.d.ts +11 -11
- package/lib/codegen/openapi-cli.d.ts.map +1 -1
- package/lib/codegen/openapi-cli.js +26 -51
- package/lib/codegen/openapi-cli.js.map +1 -1
- package/lib/codegen/openapi.d.ts +16 -2
- package/lib/codegen/openapi.d.ts.map +1 -1
- package/lib/codegen/openapi.js +56 -39
- package/lib/codegen/openapi.js.map +1 -1
- package/lib/codegen/operations.d.ts.map +1 -1
- package/lib/codegen/operations.js.map +1 -1
- package/lib/codegen/pagination.d.ts.map +1 -1
- package/lib/codegen/pagination.js +2 -4
- package/lib/codegen/pagination.js.map +1 -1
- package/lib/codegen/patch-audit.d.ts +84 -0
- package/lib/codegen/patch-audit.d.ts.map +1 -0
- package/lib/codegen/patch-audit.js +366 -0
- package/lib/codegen/patch-audit.js.map +1 -0
- package/lib/codegen/patch-audit.test.d.ts +2 -0
- package/lib/codegen/patch-audit.test.d.ts.map +1 -0
- package/lib/codegen/patch-audit.test.js +141 -0
- package/lib/codegen/patch-audit.test.js.map +1 -0
- package/lib/codegen/patches.d.ts +75 -11
- package/lib/codegen/patches.d.ts.map +1 -1
- package/lib/codegen/patches.js +301 -65
- package/lib/codegen/patches.js.map +1 -1
- package/lib/codegen/patches.test.js +55 -5
- package/lib/codegen/patches.test.js.map +1 -1
- package/lib/codegen/prelude.js +1 -1
- package/lib/codegen/prelude.js.map +1 -1
- package/lib/codegen/proto.d.ts.map +1 -1
- package/lib/codegen/proto.js +2 -4
- package/lib/codegen/proto.js.map +1 -1
- package/lib/codegen/protocol-imports.test.d.ts +2 -0
- package/lib/codegen/protocol-imports.test.d.ts.map +1 -0
- package/lib/codegen/protocol-imports.test.js +36 -0
- package/lib/codegen/protocol-imports.test.js.map +1 -0
- package/lib/codegen/rewrite-operation-ids.d.ts +23 -0
- package/lib/codegen/rewrite-operation-ids.d.ts.map +1 -1
- package/lib/codegen/rewrite-operation-ids.js +97 -59
- package/lib/codegen/rewrite-operation-ids.js.map +1 -1
- package/lib/codegen/rewrite-operation-ids.test.js +19 -101
- package/lib/codegen/rewrite-operation-ids.test.js.map +1 -1
- package/lib/codegen/spec-path.d.ts +1 -1
- package/lib/codegen/spec-path.d.ts.map +1 -1
- package/lib/codegen/spec-path.js +1 -1
- package/lib/codegen/spec-path.js.map +1 -1
- package/lib/codegen/untagged-unions.test.d.ts +2 -0
- package/lib/codegen/untagged-unions.test.d.ts.map +1 -0
- package/lib/codegen/untagged-unions.test.js +80 -0
- package/lib/codegen/untagged-unions.test.js.map +1 -0
- package/lib/error-category.d.ts.map +1 -1
- package/lib/error-category.js.map +1 -1
- package/lib/errors.d.ts +2 -1
- package/lib/errors.d.ts.map +1 -1
- package/lib/errors.js +9 -4
- package/lib/errors.js.map +1 -1
- package/lib/fixtures/xml/upstream-validator.json +397 -0
- package/lib/graphql.d.ts +292 -205
- package/lib/graphql.d.ts.map +1 -1
- package/lib/graphql.js +945 -636
- package/lib/graphql.js.map +1 -1
- package/lib/graphql.test.js +135 -764
- package/lib/graphql.test.js.map +1 -1
- package/lib/graphql.types.d.ts +10 -0
- package/lib/graphql.types.d.ts.map +1 -1
- package/lib/graphql.types.js +4 -44
- package/lib/graphql.types.js.map +1 -1
- package/lib/index.d.ts +27 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +27 -0
- package/lib/index.js.map +1 -0
- package/lib/json-patch.d.ts +13 -6
- package/lib/json-patch.d.ts.map +1 -1
- package/lib/json-patch.js +34 -19
- package/lib/json-patch.js.map +1 -1
- package/lib/json-patch.test.d.ts +2 -0
- package/lib/json-patch.test.d.ts.map +1 -0
- package/lib/json-patch.test.js +65 -0
- package/lib/json-patch.test.js.map +1 -0
- package/lib/pagination.d.ts +2 -1
- package/lib/pagination.d.ts.map +1 -1
- package/lib/pagination.js +28 -30
- package/lib/pagination.js.map +1 -1
- package/lib/pagination.test.d.ts +2 -0
- package/lib/pagination.test.d.ts.map +1 -0
- package/lib/pagination.test.js +131 -0
- package/lib/pagination.test.js.map +1 -0
- package/lib/protocol-http.d.ts +30 -16
- package/lib/protocol-http.d.ts.map +1 -1
- package/lib/protocol-http.js +133 -84
- package/lib/protocol-http.js.map +1 -1
- package/lib/protocol-http.test.js +141 -1
- package/lib/protocol-http.test.js.map +1 -1
- package/lib/protocol-rest.d.ts +18 -1
- package/lib/protocol-rest.d.ts.map +1 -1
- package/lib/protocol-rest.js +32 -21
- package/lib/protocol-rest.js.map +1 -1
- package/lib/protocol-rest.test.d.ts +2 -0
- package/lib/protocol-rest.test.d.ts.map +1 -0
- package/lib/protocol-rest.test.js +498 -0
- package/lib/protocol-rest.test.js.map +1 -0
- package/lib/query.d.ts +29 -0
- package/lib/query.d.ts.map +1 -0
- package/lib/query.js +27 -0
- package/lib/query.js.map +1 -0
- package/lib/response-validation.d.ts +67 -0
- package/lib/response-validation.d.ts.map +1 -0
- package/lib/response-validation.js +76 -0
- package/lib/response-validation.js.map +1 -0
- package/lib/response-validation.test.d.ts +2 -0
- package/lib/response-validation.test.d.ts.map +1 -0
- package/lib/response-validation.test.js +72 -0
- package/lib/response-validation.test.js.map +1 -0
- package/lib/retry-after.d.ts.map +1 -1
- package/lib/retry-after.js +1 -1
- package/lib/retry-after.js.map +1 -1
- package/lib/retry-after.test.d.ts +2 -0
- package/lib/retry-after.test.d.ts.map +1 -0
- package/lib/retry-after.test.js +204 -0
- package/lib/retry-after.test.js.map +1 -0
- package/lib/retry.d.ts.map +1 -1
- package/lib/retry.js +1 -2
- package/lib/retry.js.map +1 -1
- package/lib/retry.test.d.ts +2 -0
- package/lib/retry.test.d.ts.map +1 -0
- package/lib/retry.test.js +407 -0
- package/lib/retry.test.js.map +1 -0
- package/lib/sdks.test.d.ts +2 -0
- package/lib/sdks.test.d.ts.map +1 -0
- package/lib/sdks.test.js +76 -0
- package/lib/sdks.test.js.map +1 -0
- package/lib/testing.d.ts +39 -0
- package/lib/testing.d.ts.map +1 -0
- package/lib/testing.js +41 -0
- package/lib/testing.js.map +1 -0
- package/lib/trait.d.ts +29 -9
- package/lib/trait.d.ts.map +1 -1
- package/lib/trait.js +8 -0
- package/lib/trait.js.map +1 -1
- package/lib/xml.d.ts +44 -0
- package/lib/xml.d.ts.map +1 -0
- package/lib/xml.js +311 -0
- package/lib/xml.js.map +1 -0
- package/lib/xml.test.d.ts +2 -0
- package/lib/xml.test.d.ts.map +1 -0
- package/lib/xml.test.js +210 -0
- package/lib/xml.test.js.map +1 -0
- package/package.json +8 -11
- package/src/api.ts +38 -55
- package/src/category.ts +21 -50
- package/src/codegen/boolean-string-enums.test.ts +148 -17
- package/src/codegen/boolean-string-enums.ts +81 -32
- package/src/codegen/cli.ts +29 -47
- package/src/codegen/defer-naming.test.ts +113 -0
- package/src/codegen/emit.ts +2 -5
- package/src/codegen/format.ts +12 -10
- package/src/codegen/generated-at.test.ts +89 -0
- package/src/codegen/generated-at.ts +88 -0
- package/src/codegen/generator.test.ts +137 -0
- package/src/codegen/generator.ts +132 -193
- package/src/codegen/graphql-client.test.ts +100 -297
- package/src/codegen/graphql-client.ts +302 -210
- package/src/codegen/graphql.ts +22 -94
- package/src/codegen/model-diff.ts +67 -0
- package/src/codegen/naming.ts +1 -2
- package/src/codegen/openapi-binary.test.ts +91 -0
- package/src/codegen/openapi-cli.ts +38 -79
- package/src/codegen/openapi.ts +98 -133
- package/src/codegen/operations.ts +2 -5
- package/src/codegen/pagination.ts +4 -11
- package/src/codegen/patch-audit.test.ts +160 -0
- package/src/codegen/patch-audit.ts +480 -0
- package/src/codegen/patches.test.ts +73 -15
- package/src/codegen/patches.ts +389 -99
- package/src/codegen/prelude.ts +1 -1
- package/src/codegen/proto.ts +18 -54
- package/src/codegen/protocol-imports.test.ts +39 -0
- package/src/codegen/rewrite-operation-ids.test.ts +23 -114
- package/src/codegen/rewrite-operation-ids.ts +111 -98
- package/src/codegen/spec-path.ts +3 -6
- package/src/codegen/untagged-unions.test.ts +85 -0
- package/src/error-category.ts +5 -17
- package/src/errors.ts +18 -30
- package/src/fixtures/xml/upstream-validator.json +397 -0
- package/src/graphql.test.ts +173 -949
- package/src/graphql.ts +1468 -1186
- package/src/graphql.types.ts +78 -164
- package/src/index.ts +26 -0
- package/src/json-patch.test.ts +77 -0
- package/src/json-patch.ts +54 -60
- package/src/pagination.test.ts +169 -0
- package/src/pagination.ts +46 -103
- package/src/protocol-http.test.ts +178 -9
- package/src/protocol-http.ts +174 -163
- package/src/protocol-rest.test.ts +703 -0
- package/src/protocol-rest.ts +76 -65
- package/src/query.ts +39 -0
- package/src/response-validation.test.ts +89 -0
- package/src/response-validation.ts +107 -0
- package/src/retry-after.test.ts +267 -0
- package/src/retry-after.ts +4 -10
- package/src/retry.test.ts +566 -0
- package/src/retry.ts +6 -21
- package/src/sdks.test.ts +87 -0
- package/src/testing.ts +72 -0
- package/src/trait.ts +48 -63
- package/src/xml.test.ts +251 -0
- package/src/xml.ts +323 -0
- package/lib/graphql.fixture.d.ts +0 -249
- package/lib/graphql.fixture.d.ts.map +0 -1
- package/lib/graphql.fixture.js +0 -240
- package/lib/graphql.fixture.js.map +0 -1
- package/src/graphql.fixture.ts +0 -371
package/src/codegen/format.ts
CHANGED
|
@@ -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
|
-
|
|
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(["
|
|
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(["
|
|
45
|
-
runTool(["
|
|
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
|
+
});
|