openapi-fetch-client-gen
v0.0.1
Published
Generate a typed fetch client from an OpenAPI spec
Readme
openapi-fetch-client-gen
Generate a JSDoc-typed fetch client from an OpenAPI JSON specification.
Install
npm install -g openapi-fetch-client-genUsage
openapi-fetch-client-gen <openapi.json|url> [output.mjs]If output.mjs is omitted, the client is written to client.mjs in the current directory.
Example
openapi-fetch-client-gen ./openapi.json ./client.mjsYou can also generate from a URL:
openapi-fetch-client-gen https://api.example.com/openapi.json ./client.mjsThen import the generated client:
import { ApiClient } from "./client.mjs";
const client = new ApiClient("https://api.example.com");You can provide default headers when constructing the client:
const client = new ApiClient("https://api.example.com", {
Authorization: `Bearer ${token}`,
});Headers can also be updated after construction:
client.setHeader("Authorization", `Bearer ${newToken}`);
client.updateHeaders({ "X-Request-ID": requestId });Requirements
Node.js 18 or newer.
