From 451fcc8585f5785ac4ed65622afe26e9fca5e8ec Mon Sep 17 00:00:00 2001 From: Dennis Chen Date: Thu, 3 Dec 2026 22:56:48 +0000 Subject: [PATCH 1/2] feat: explain experimental endpoint compatibility failures Co-authored-by: Codex --- README.md | 4 + src/commands/experimental.test.ts | 160 ++++++++++++++++++++++++++++++ src/commands/helpers.ts | 50 ++++++++-- src/test-support.ts | 2 +- 4 files changed, 208 insertions(+), 8 deletions(-) create mode 100644 src/commands/experimental.test.ts diff --git a/README.md b/README.md index 40fad41..e33bd7d 100644 --- a/README.md +++ b/README.md @@ -168,3 +168,7 @@ 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 endpoints use `vanta experimental ` (the OpenAPI operation ID in kebab case). They may change or disappear without notice. Endpoint failures include compatibility guidance and retain the original API error; authentication, rate-limit, and transport failures keep their normal diagnostics. The namespace is included when the build's spec contains experimental endpoints. + +Command source is generated in [vanta-cli-codegen](https://github.com/VantaInc/vanta-cli-codegen); this repository contains the CLI runtime and generated command files. diff --git a/src/commands/experimental.test.ts b/src/commands/experimental.test.ts new file mode 100644 index 0000000..5a66247 --- /dev/null +++ b/src/commands/experimental.test.ts @@ -0,0 +1,160 @@ +import assert from "node:assert/strict"; +import { afterEach, beforeEach, describe, it, mock } from "node:test"; +import { jsonResponse, stubFetch, useTempHome } from "../test-support.js"; +import { experimentalRequest, runSdk } from "./helpers.js"; + +describe("experimental endpoint errors", () => { + let home: Awaited>; + let net: ReturnType; + let output: string; + let warning: string; + let dryRun: boolean; + + const run = () => + runSdk( + () => ({ + dryRun, + verbose: false, + pretty: false, + agentMode: false, + apiBase: "https://api.vanta.com/v1", + clientId: "test-client", + clientSecret: "test-secret", + }), + (api) => + experimentalRequest("try-something-new", () => + api.client.request({ + method: "GET", + url: "/experimental/odd:route", + security: [{ type: "http", scheme: "bearer" }], + }), + ), + ); + + beforeEach(async () => { + home = await useTempHome(); + output = ""; + warning = ""; + dryRun = false; + mock.method(process.stdout, "write", (chunk: string) => { + output += chunk; + return true; + }); + mock.method(process.stderr, "write", (chunk: string) => { + warning += chunk; + return true; + }); + net = stubFetch((req) => + req.url.endsWith("/oauth/token") + ? jsonResponse(200, { access_token: "test-token", expires_in: 3600 }) + : jsonResponse(200, { ok: true }), + ); + }); + afterEach(async () => { + net.restore(); + mock.restoreAll(); + await home.restore(); + }); + + function endpoint(response: () => Response): void { + net.restore(); + net = stubFetch((req) => + req.url.endsWith("/oauth/token") + ? jsonResponse(200, { access_token: "test-token", expires_in: 3600 }) + : response(), + ); + } + + it("warns on stderr and preserves successful output and authorization", async () => { + await run(); + assert.deepEqual(JSON.parse(output), { ok: true }); + assert.match(warning, /may change or be removed/); + assert.equal( + net.requests.at(-1)!.headers.get("authorization"), + "Bearer test-token", + ); + }); + + it("explains endpoint failures and retains full API details", async () => { + for (const status of [400, 404, 405, 410, 422, 500, 503]) { + const error = { + message: "Request no longer supported", + fields: { newField: "Required" }, + }; + endpoint(() => jsonResponse(status, error)); + await assert.rejects(run(), (err) => { + assert(err instanceof Error); + assert.match( + err.message, + new RegExp(`vanta experimental try-something-new.*HTTP ${status}`), + ); + if ([404, 405, 410].includes(status)) + assert.match(err.message, /is unavailable/); + assert.match(err.message, /endpoint may have changed or been removed/); + assert.match(err.message, /Update the Vanta CLI/); + assert.match(err.message, /newField/); + assert.deepEqual(err.cause, error); + return true; + }); + } + assert.equal(output, ""); + }); + + it("handles text, empty errors and changed JSON responses", async () => { + for (const [status, body, contentType, detail] of [ + [404, "Route was removed", "text/plain", /Route was removed/], + [410, "", "text/plain", /HTTP 410/], + [200, "{broken json", "application/json", /API error:/], + ] as const) { + endpoint( + () => + new Response(body, { + status, + headers: { "content-type": contentType }, + }), + ); + await assert.rejects(run(), (err) => { + assert(err instanceof Error); + assert.match(err.message, /endpoint may have changed or been removed/); + assert.match(err.message, detail); + return true; + }); + } + }); + + it("preserves authentication and rate-limit diagnostics", async () => { + for (const status of [401, 403, 429]) { + const error = { message: `Specific failure ${status}` }; + endpoint(() => jsonResponse(status, error)); + await assert.rejects(run(), (err) => { + assert.deepEqual(err, error); + return true; + }); + } + }); + + it("preserves transport and local failures", async () => { + const offline = new TypeError("Connection failed"); + endpoint(() => { + throw offline; + }); + await assert.rejects(run(), (err) => err === offline); + const local = new Error("Local input error"); + await assert.rejects( + experimentalRequest("test", async () => { + throw local; + }), + (err) => err === local, + ); + }); + + it("dry runs never send requests", async () => { + dryRun = true; + await run(); + assert.match( + output, + /DRY RUN GET https:\/\/api.vanta.com\/v1\/experimental\/odd:route/, + ); + assert.equal(net.requests.length, 0); + }); +}); diff --git a/src/commands/helpers.ts b/src/commands/helpers.ts index ca46c4a..81b1c85 100644 --- a/src/commands/helpers.ts +++ b/src/commands/helpers.ts @@ -18,13 +18,7 @@ export function collectString(value: string, previous: string[]): string[] { export function addPaginationOptions(cmd: Command): Command { return cmd - .option("--page-size ", "Number of results to return", (v) => { - const size = Number(v); - if (!/^[+-]?\d+$/.test(v) || !Number.isSafeInteger(size)) { - throw new Error("invalid --page-size: expected an integer"); - } - return size; - }) + .option("--page-size ", "Number of results to return", parsePageSize) .option("--page-cursor ", "Pagination cursor"); } @@ -232,3 +226,45 @@ export async function writeDownloadedMedia( `unsupported media response type: ${Object.prototype.toString.call(data)}`, ); } + +export function parsePageSize(value: string): number { + const size = Number(value); + if (!/^[+-]?\d+$/.test(value) || !Number.isSafeInteger(size)) { + throw new Error("invalid --page-size: expected an integer"); + } + return size; +} + +// Wrap only endpoint failures: local input, transport, auth and rate-limit +// errors retain their original diagnostics. +export async function experimentalRequest< + T extends { error?: unknown; response?: Response }, +>(command: string, call: () => Promise): Promise { + process.stderr.write( + "Experimental: these endpoints may change or be removed without prior notice. Commands may require a CLI update.\n", + ); + const result = await call(); + if ( + result.error && + result.response && + ![401, 403, 429].includes(result.response.status) + ) { + const error = result.error; + const details = + error instanceof Error + ? error.message + : typeof error === "string" + ? error + : JSON.stringify(error, null, 2); + const outcome = [404, 405, 410].includes(result.response.status) + ? "is unavailable" + : "failed"; + throw new Error( + `Experimental command "vanta experimental ${command}" ${outcome} (HTTP ${result.response.status}).\n` + + "The underlying experimental endpoint may have changed or been removed, so this command may no longer be available. Update the Vanta CLI and try again.\n" + + `API error: ${details || result.response.statusText || "No error details returned"}`, + { cause: error }, + ); + } + return result; +} diff --git a/src/test-support.ts b/src/test-support.ts index b738770..1babb22 100644 --- a/src/test-support.ts +++ b/src/test-support.ts @@ -51,7 +51,7 @@ export function stubFetch( globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { const req: RecordedRequest = { url: input instanceof Request ? input.url : String(input), - method: init?.method ?? "GET", + method: init?.method ?? (input instanceof Request ? input.method : "GET"), headers: new Headers(init?.headers), body: typeof init?.body === "string" From d93604ee632b5e137194f2a4631be96ba2e17374 Mon Sep 17 00:00:00 2001 From: Dennis Chen Date: Fri, 4 Dec 2026 17:04:43 +0000 Subject: [PATCH 2/2] fix: remove unrelated documentation and pagination changes Co-authored-by: Codex --- README.md | 4 ---- src/commands/helpers.ts | 16 +++++++--------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index e33bd7d..40fad41 100644 --- a/README.md +++ b/README.md @@ -168,7 +168,3 @@ 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 endpoints use `vanta experimental ` (the OpenAPI operation ID in kebab case). They may change or disappear without notice. Endpoint failures include compatibility guidance and retain the original API error; authentication, rate-limit, and transport failures keep their normal diagnostics. The namespace is included when the build's spec contains experimental endpoints. - -Command source is generated in [vanta-cli-codegen](https://github.com/VantaInc/vanta-cli-codegen); this repository contains the CLI runtime and generated command files. diff --git a/src/commands/helpers.ts b/src/commands/helpers.ts index 81b1c85..231ba77 100644 --- a/src/commands/helpers.ts +++ b/src/commands/helpers.ts @@ -18,7 +18,13 @@ export function collectString(value: string, previous: string[]): string[] { export function addPaginationOptions(cmd: Command): Command { return cmd - .option("--page-size ", "Number of results to return", parsePageSize) + .option("--page-size ", "Number of results to return", (v) => { + const size = Number(v); + if (!/^[+-]?\d+$/.test(v) || !Number.isSafeInteger(size)) { + throw new Error("invalid --page-size: expected an integer"); + } + return size; + }) .option("--page-cursor ", "Pagination cursor"); } @@ -227,14 +233,6 @@ export async function writeDownloadedMedia( ); } -export function parsePageSize(value: string): number { - const size = Number(value); - if (!/^[+-]?\d+$/.test(value) || !Number.isSafeInteger(size)) { - throw new Error("invalid --page-size: expected an integer"); - } - return size; -} - // Wrap only endpoint failures: local input, transport, auth and rate-limit // errors retain their original diagnostics. export async function experimentalRequest<