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.fixUse 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.fixConvert 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.fixAlways 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).
fixInstall 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.
fixEnsure 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.
fixPass the required parameters as an object to the generated method, e.g., petStore.addPet({name: 'fluffy'}); Audit
Dependencies
No dependency data recorded yet.