Registry / devops / fetch-openapi

fetch-openapi

JSON →
library12.1.3jsnpmunverified

Client SDK generator for OpenAPI (Swagger) 2.0 documents, producing JavaScript code that uses the Fetch API. Currently at version 12.1.3. It generates a client with methods for each operationId, supporting Node.js and ES6 presets. Unlike other OpenAPI generators, it outputs lightweight, dependency-free ES6 code instead of complex wrappers. The package also includes a CLI tool for quick generation. It has been around since 2016, with stable maintenance releases.

npm install fetch-openapi
INSTALL
IMPORT
SIG · FETCH-OPENAPI
F
fetch-openapi
devopsjavascriptv12.1.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fetch-openapi
const fetchOpenapi = require('fetch-openapi')
import fetchOpenapi from 'fetch-openapi'
CommonJS only; the package does not ship ESM exports. Use require().
CLI tool
npx fetch-openapi --api-doc-url ... --output-file-path ... --preset es6
fetch-openapi --help
The CLI is invoked directly, not via a programmatic API. Ensure the package is installed globally or use npx.
createApi
const { createApi } = require('./generated-client.js')
import { createApi } from './generated-client.js'
The generated client is CommonJS by default (node preset). For ES6 preset, use import.

Generates a client from an OpenAPI 2.0 document and calls an endpoint.

const fetchOpenapi = require('fetch-openapi'); const fs = require('fs'); // Example OpenAPI doc const apiDoc = { swagger: '2.0', info: { title: 'Pet Store', version: '1.0.0' }, host: 'petstore.swagger.io', basePath: '/v2', paths: { '/pet': { post: { operationId: 'addPet', parameters: [ { in: 'body', name: 'body', required: true, schema: { type: 'object' } } ], responses: { '200': { description: 'success' } } } } } }; const generator = fetchOpenapi(apiDoc, { preset: 'node' }); fs.writeFileSync('./petStore.js', generator, 'utf8'); // Use the generated client const petStore = require('./petStore'); petStore.addPet({ name: 'fluffy' }).then(response => console.log(response));
Debug
Known issues
gotchaThe generated client uses the global fetch API, which is only available in modern browsers and Node.js v18+. For older environments, a polyfill is required.
fix
Use a fetch polyfill like 'node-fetch' or 'whatwg-fetch' and assign to global scope before requiring the generated client.
affects: >=1.0.0
gotchaThe package only supports OpenAPI (Swagger) 2.0, not OpenAPI 3.x. Using a 3.x document may produce unexpected results.
fix
Convert your OpenAPI 3.x document to 2.0 using a tool like swagger-converter, or use a different library.
affects: >=1.0.0 <=12.1.3
gotchaThe generate function takes an options object but does not validate it. Passing undefined or malformed options may cause runtime errors.
fix
Always provide an options object with at least a preset property.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: fetch is not defined
The generated client uses fetch which is not available in the runtime (e.g., older Node.js).
fix
Install a fetch polyfill like node-fetch and set it globally: global.fetch = require('node-fetch');
Cannot find module './api'
The output file path specified in CLI or programmatic usage does not exist or is incorrect.
fix
Ensure the path is correct and the directory exists. Use an absolute path if needed.
Missing required parameter: body
The API definition has required parameters but the client call omitted them.
fix
Pass the required parameters as an object to the generated method, e.g., petStore.addPet({name: 'fluffy'});
Upgrade
Version history
12.1.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
fetch-openapi — npm install fetch-openapi · libregistry