oazapfts OpenAPI TypeScript Client Generator

7.5.0 · active · verified Sun Apr 19

oazapfts is a specialized tool for generating TypeScript API clients directly from OpenAPI or Swagger specifications. Unlike template-based generators, it leverages TypeScript's Abstract Syntax Tree (AST) API, resulting in faster code generation and highly optimized, tree-shakeable client libraries. The current stable version is 7.5.0, with frequent alpha and minor releases demonstrating active development. Key differentiators include its AST-driven approach, which avoids HTTP-specific implementation details in method signatures, grouping all optional parameters into a single object for improved ergonomics. Generated clients are self-contained in a single file and provide individually exported functions, promoting efficient tree-shaking for modern bundlers. It is primarily consumed via a command-line interface but also exposes a programmatic generation API.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates programmatically generating a TypeScript API client from a mock OpenAPI specification and outlines how to consume the generated functions with named imports, highlighting the necessary runtime dependency.

import { mkdir, writeFile } from 'node:fs/promises';
import { generate } from 'oazapfts/generate';

// A minimal OpenAPI spec for demonstration
const openApiSpec = {
  openapi: '3.0.0',
  info: { title: 'Test API', version: '1.0.0' },
  paths: {
    '/items': {
      get: {
        operationId: 'getItems',
        summary: 'Retrieve a list of items',
        responses: { '200': { description: 'A list of items', content: { 'application/json': { schema: { type: 'array', items: { type: 'string' } } } } } }
      },
      post: {
        operationId: 'createItem',
        summary: 'Create a new item',
        requestBody: {
          required: true,
          content: { 'application/json': { schema: { type: 'object', properties: { name: { type: 'string' } } } } }
        },
        responses: { '201': { description: 'Item created' } }
      }
    }
  }
};

async function main() {
  const outputDir = './generated-client';
  await mkdir(outputDir, { recursive: true });
  const outputPath = `${outputDir}/api.ts`;

  // Generate the client code
  const clientCode = await generate({
    spec: openApiSpec,
    url: 'https://example.com/api-docs',
    plugins: [] // You can add custom plugins here
  });

  await writeFile(outputPath, clientCode);
  console.log(`OpenAPI client generated to ${outputPath}`);

  // Example of consuming the generated API (conceptually)
  console.log('\n// To use the generated client (after installing @oazapfts/runtime):');
  console.log(`// import { getItems, createItem } from './generated-client/api';`);
  console.log(`// import { fetch } from '@oazapfts/runtime';`);
  console.log(`// const items = await getItems();`);
  console.log(`// await createItem({ name: 'New Item' });`);
}

main().catch(console.error);

view raw JSON →