docfy generate-client

Generates a typed TypeScript client from an OpenAPI document, a thin wrapper over openapi-typescript and openapi-fetch, not a from-scratch code generator.

Usage

bash
npx nestjs-docfy generate-client --spec <path-or-url> [options]

Rather than reimplementing a code generator, this wraps two well-established libraries: openapi-typescript (types) and openapi-fetch (the runtime client that consumes them).

Options

OptionDefaultDescription
--spec <path|url>(required)A local openapi.json, or a URL (e.g. a running app's /api-json)
--out <path>./generated-clientOutput directory for schema.d.ts and client.ts
--root <path>.Project root directory
--quietfalseSuppress all output except errors

What it writes

Two files are written to the output directory:

  • schema.d.ts: the generated types (paths, components, operations), via openapi-typescript.
  • client.ts: a small wrapper exporting createApiClient(baseUrl), typed against schema.d.ts.

Using the generated client

openapi-fetch is not a dependency of nestjs-docfy itself, since only the generated client.ts imports it, so install it in your own project:

bash
npm install openapi-fetch
ts
import { createApiClient } from './generated-client/client';

const api = createApiClient('https://api.example.com');
const { data, error } = await api.GET('/users/{id}', { params: { path: { id: '123' } } });