Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

TypeScript SDK

This is the README that tungsten generates for the TypeScript target, shown for a small example API (Swagger's Petstore). Names change with your API; the structure does not.

TypeScript SDK for the openapi API (1.0.0), generated by tungsten.

Every call returns a Result and never throws for API or transport errors. Arguments are validated before any request is sent.

Install

npm install openapi-sdk

Create a client

import { OpenapiClient } from "openapi-sdk";

const client = new OpenapiClient({
  baseUrl: "http://petstore.swagger.io/v1",
});

Read

const res = await client.pets.list();
if (res.ok) console.log(res.value);
else console.error(res.error.category, res.error.remediation);

Change something, safely

Mutations are never retried blindly. When the operation takes an idempotency key, generate it once, persist it with your intent, and reuse it on every retry.

const idempotencyKey = crypto.randomUUID();
const res = await client.pets.create({}, { idempotencyKey });

Errors

A failed call returns { ok: false, error }, where error is the diagnostic envelope:

{
  "status": "error",
  "category": "VALIDATION_FAILED",
  "operation": "<operation id>",
  "http_status": null,
  "code": null,
  "failed_parameter": "<args path>",
  "received_value": null,
  "expected": "<what was expected>",
  "remediation": "<what to do next>",
  "retryable": "never",
  "retry_after_ms": null,
  "next_action": null,
  "request_id": null,
  "trace": { "attempts": 0 }
}

Branch on category and retryable: same_key_only means retry only with the same idempotency key, after_remediation means act first (fix the input, or check whether an unknown outcome took effect: without a key a repeat can apply it twice), and OUTCOME_UNKNOWN means the request may have been applied. When a mutation or macro already took effect, the failed result's partial holds what it produced (store one-time secrets from it). A success response without a body has value: undefined.

Extending

src/custom/index.ts is yours: it is created once, never overwritten, and exported as custom.