oazapfts OpenAPI TypeScript Client Generator
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
-
Error: Cannot find module '@oazapfts/runtime'
cause The `@oazapfts/runtime` package, which provides the necessary fetch utilities for generated clients, has not been installed.fixInstall the runtime dependency: `npm install @oazapfts/runtime` or `yarn add @oazapfts/runtime`. -
TypeError: (0, _my_generated_api_ts__WEBPACK_IMPORTED_MODULE_0__.getPetById) is not a function
cause This typically occurs in a CommonJS environment or when bundlers incorrectly resolve ESM named exports, or when a default import is attempted for a file that only provides named exports.fixEnsure your project is configured for ES modules or that your bundler correctly handles named exports. Verify that you are using named imports like `import { getPetById } from './my-generated-api';` instead of default imports or `require`. -
Invalid `spec` argument: must be a URL or a path to a local OpenAPI/Swagger spec file.
cause The `oazapfts` CLI or programmatic `generate` function was invoked without a valid OpenAPI/Swagger specification path or URL.fixProvide a correct path to a local JSON/YAML spec file, a URL to a remote spec, or pass a valid OpenAPI object directly to the `generate` function.
Warnings
- breaking With version 3.0.0, oazapfts transitioned from embedding all fetch logic directly into the generated code to becoming a runtime dependency. This means generated clients now rely on `oazapfts` to be available in the runtime environment.
- breaking As of version 6.0.0, the core runtime logic for oazapfts was extracted into a separate peer dependency, `@oazapfts/runtime`. Generated client code depends on this package for its fetching capabilities.
- gotcha The `--futureStripLegacyMethods` CLI option, introduced to remove deprecated HTTP verb/path-based aliases for operation IDs containing special characters, will become the default behavior in a future major version. Relying on legacy method names is discouraged.
- gotcha When using `npm` with `oazapfts`, be mindful of `@oazapfts/*` internal package version ranges (e.g., between `oazapfts` and `@oazapfts/runtime`). Mismatched versions can lead to unexpected behavior or build errors.
Install
-
npm install oazapfts -
yarn add oazapfts -
pnpm add oazapfts
Imports
- getPetById
import api from './my-generated-api'; const res = api.getPetById(1);
import { getPetById } from './my-generated-api'; - api
const api = require('./my-generated-api');import * as api from './my-generated-api';
- generate
import { generate } from 'oazapfts';import { generate } from 'oazapfts/generate'; - RequestOpts
import { RequestOpts } from 'oazapfts';import type { RequestOpts } from '@oazapfts/runtime';
Quickstart
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);