Registry / devops / rest2ts

rest2ts

JSON →
library6.3.5jsnpmunverified

A CLI tool and library that generates modern TypeScript types and strongly-typed HTTP clients from REST API schemas (Swagger/OpenAPI 2.x/3.x). v6.3.5 is current stable; releases weekly. Key differentiators: supports both Swagger and OpenAPI spec versions, produces zero-dependency TypeScript output with full type safety, configurable client generation for fetch/axios/ky, and includes a watch mode for incremental codegen. Alternatives (e.g., openapi-generator) often produce larger output files with more boilerplate or require heavier runtimes.

npm install rest2ts
INSTALL
IMPORT
SIG · REST2TS
R
rest2ts
devopsjavascriptv6.3.5
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.

generateTypes
✓ import { generateTypes } from 'rest2ts'
✗ const generateTypes = require('rest2ts').generateTypes
ESM-only since v6.1; use named export.
generateClient
✓ import { generateClient } from 'rest2ts'
✗ import generateClient from 'rest2ts'
Named export, not default. Added in v5.
Rest2tsConfig
✓ import { Rest2tsConfig } from 'rest2ts'
✗ interface Rest2tsConfig { ... }
Type only; use import type if you want elision.

Demonstrates programmatic usage to fetch an OpenAPI spec and generate TypeScript types and a fetch-based client.

import { generateTypes } from 'rest2ts'; import { writeFileSync } from 'fs'; const config = { spec: 'https://petstore.swagger.io/v2/swagger.json', output: './types', client: 'fetch', // or 'axios', 'ky' prettier: true, }; async function main() { const { files } = await generateTypes(config); for (const file of files) { writeFileSync(file.path, file.content); } console.log(`Generated ${files.length} files`); } main().catch(console.error);
Debug
Known issues
breakingDefault export removed in v6.0 in favor of named exports.
fix
Change import from 'rest2ts' to named imports like `import { generateTypes } from 'rest2ts'`
affects: >=6.0
deprecatedSpec URL must be a valid JSON document; YAML support was experimental and is removed in v6.0.
fix
Convert your YAML spec to JSON before passing it to rest2ts.
affects: >=6.0
gotchaThe CLI uses `-s` for spec URL; `-f` for spec file is ambiguous in some shells.
fix
Always quote the URL/ path: `npx rest2ts -s 'https://example.com/openapi.json'`
affects: >=5.0
gotchaGenerated files may contain circular references if your OpenAPI spec has recursive schemas. These will compile only with `--strictNullChecks` off.
fix
Use the `--maxDepth` option to limit recursion depth, or manually edit generated types.
affects: >=4.0
breakingNode.js 16+ required; v6 dropped support for Node 14.
fix
Upgrade Node.js to 16 or later.
affects: >=6.0
deprecatedThe `--client` option value `superagent` is deprecated and will be removed in v7.
fix
Switch to `fetch`, `axios`, or `ky`.
affects: >=6.2
Errors
Common errors & fixes
Error: Cannot find module 'rest2ts'
ESM-only package referenced via require() or not installed.
fix
Install the package: `npm install rest2ts` and use ESM import syntax.
SyntaxError: Invalid or unexpected token when parsing spec
Spec file is YAML or malformed JSON.
fix
Convert spec to valid JSON. rest2ts does not accept YAML since v6.
TypeError: generateTypes is not a function
Attempted default import in v6+.
fix
Use named import: `import { generateTypes } from 'rest2ts'`.
Error: No such file or directory: './types'
Output directory does not exist.
fix
Create the output directory first with `mkdir -p ./types` or use the `--mkdir` flag.
Upgrade
Version history
6.3.5latest on npm
Audit
Dependencies
commanderrequiredCLI argument parsing
openapi-typesrequiredTypeScript types for OpenAPI spec objects
prettieroptionalCode formatting for generated output
Agent activity
6 hits · last 30 days
node
6
Resources
rest2ts — npm install rest2ts · libregistry