Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,26 @@ VANTA_VERSION=0.2.0 pnpm build:binaries
```

The typed API client is generated from [`api-spec.json`](api-spec.json) with [`@hey-api/openapi-ts`](https://heyapi.dev/).

Experimental APIs appear under `vanta experimental <operation-name>`. Names come
from the OpenAPI operation ID converted to kebab-case, so `trySomethingNew` becomes
`vanta experimental try-something-new`. Experimental APIs do not need to follow
the stable API's resource and verb naming conventions.

Run `vanta experimental --help` to discover available commands, then use a
command's `--help` to discover its flags. Experimental endpoints may change or be
removed without notice, and commands may require a CLI update. Warnings go to
stderr so stdout remains available for response data.
If an experimental endpoint fails or returns an incompatible response, the CLI
includes the HTTP status and original error, explains that the endpoint may have
changed or been removed, and suggests updating the CLI. Authentication, rate
limits, network failures, and local input or file errors keep their specific
errors.

`pnpm generate` also generates the experimental command catalog from operations
marked `x-vanta-experimental: true` or paths under `/experimental/`. The request
path is preserved; the API base stays at `/v1`. Path, query, header, and multipart
inputs become flags using their original names in kebab-case. JSON bodies use
`--json` or `--file`; downloads support `--output`. Stable commands are maintained
separately. A build whose spec has no experimental operations still exposes
`vanta experimental --help` and explains that no endpoints are included.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"type": "module",
"scripts": {
"generate": "openapi-ts",
"generate": "openapi-ts && tsx scripts/generate-experimental.ts",
"dev": "tsx src/cli.ts",
"build": "pnpm run generate && tsup",
"typecheck": "tsc --noEmit",
Expand Down
13 changes: 13 additions & 0 deletions scripts/generate-experimental.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { readFile, writeFile } from "node:fs/promises";
import { generateExperimentalOperations } from "../src/codegen/experimental.js";

const spec = JSON.parse(
await readFile(process.argv[2] ?? "api-spec.json", "utf8"),
);
const operations = generateExperimentalOperations(spec);
await writeFile(
"src/generated/experimental.gen.ts",
`// Generated by scripts/generate-experimental.ts. Do not edit.\n` +
`import type { ExperimentalOperation } from "../codegen/experimental.js";\n` +
`export const experimentalOperations: ExperimentalOperation[] = ${JSON.stringify(operations, null, 2)};\n`,
);
91 changes: 91 additions & 0 deletions src/codegen/experimental.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { readFileSync } from "node:fs";
import { generateExperimentalOperations } from "./experimental.js";

const fixture = () =>
JSON.parse(
readFileSync(
new URL("./fixtures/experimental.json", import.meta.url),
"utf8",
),
);

describe("experimental codegen", () => {
it("uses operation IDs regardless of path conventions and keeps stable operations out", () => {
const ops = generateExperimentalOperations(fixture());
assert.deepEqual(
ops.map((op) => op.command),
["attach-evidence", "fetch-report", "ping", "try-something-new"],
);
const op = ops.find((op) => op.command === "try-something-new")!;
assert.equal(op.path, "/experimental/odd:route/{subjectId}/doSomething");
assert.equal(op.method, "POST");
assert.equal(op.description, "Try a new operation");
assert.deepEqual(op.body, { type: "json", required: true });
assert.equal(op.inputs.filter((p) => p.name === "mode").length, 1);
assert.match(
op.inputs.find((p) => p.name === "mode")!.description,
/Processing mode.*FAST, THOROUGH/,
);
assert.equal(op.inputs.find((p) => p.name === "subjectId")!.required, true);
assert.equal(
op.inputs.find((p) => p.name === "subjectId")!.placeholder,
"id",
);
assert.match(
op.inputs.find((p) => p.name === "after")!.description,
/ISO 8601 timestamp/,
);
assert.equal(op.inputs.find((p) => p.name === "tag")!.array, true);
assert.equal(op.inputs.find((p) => p.name === "enabled")!.kind, "boolean");
});

it("recognizes multipart files and downloads", () => {
const ops = generateExperimentalOperations(fixture());
const upload = ops.find((op) => op.command === "attach-evidence")!;
assert.deepEqual(upload.body, { type: "multipart", required: true });
assert.equal(upload.inputs[0].kind, "file");
assert.equal(upload.inputs[0].required, true);
assert.equal(
ops.find((op) => op.command === "fetch-report")!.download,
true,
);
});

it("handles promotion by removing the experimental path and marker", () => {
const spec = fixture();
spec.paths["/ping"] = spec.paths["/experimental/ping"];
delete spec.paths["/experimental/ping"];
assert(
!generateExperimentalOperations(spec).some((op) => op.command === "ping"),
);
});

it("rejects ambiguous names and flags during generation", () => {
const missing = fixture();
delete missing.paths["/experimental/ping"].get.operationId;
assert.throws(() => generateExperimentalOperations(missing), /operationId/);
const duplicate = fixture();
duplicate.paths["/experimental/ping"].get.operationId = "try-something-new";
assert.throws(() => generateExperimentalOperations(duplicate), /duplicate/);
const collision = fixture();
collision.paths["/experimental/ping"].get.parameters = [
{ name: "fooBar", in: "query" },
{ name: "foo-bar", in: "header" },
];
assert.throws(
() => generateExperimentalOperations(collision),
/Conflicting.*--foo-bar/,
);
});

it("rejects a marked operation outside the experimental namespace", () => {
const spec = fixture();
spec.paths["/controls"].get["x-vanta-experimental"] = true;
assert.throws(
() => generateExperimentalOperations(spec),
/must use \/experimental\//,
);
});
});
Loading