npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

swagger2types

v0.1.0

Published

Convert Swagger Spec to Typescript Types

Readme

swagger2types

A TypeScript utility that converts Swagger/OpenAPI specifications into pure TypeScript types for type-safe API route handling. The generated types are completely erased at runtime, adding zero overhead to your JavaScript bundle.

Overview

swagger2types transforms your Swagger/OpenAPI specs into a structured TypeScript interface that provides:

  • Request/Response type mapping for each endpoint
  • Type-safe route definitions with body, query, header, and parameter types
  • Zero runtime cost - all types are erased during compilation

Before You Start

This is an advanced TypeScript tool that requires some understanding of:

  • TypeScript generics and mapped types
  • API client patterns and HTTP libraries
  • OpenAPI/Swagger specifications

Not a traditional API client library - swagger2types generates only TypeScript types. You'll need to implement your own API calling logic or use the provided helper utilities.

Best suited for developers who:

  • Want maximum control over their API client implementation
  • Value minimal bundle size and runtime overhead
  • Are comfortable with advanced TypeScript patterns
  • Prefer explicit API structure over abstracted method names

New to TypeScript or API clients? Consider starting with a full-featured library like swagger-typescript-api or openapi-generator before exploring this minimal approach.

Ready to proceed? Continue reading to understand how swagger2types works and see the examples for complete implementations.

Installation

npm install swagger2types

Usage

CLI Usage

# From a local file
swagger2types ./api-spec.json > generated/my-api.ts

# From a URL
swagger2types https://api.example.com/swagger.json > generated/my-api.ts

# From stdin
cat api-spec.json | swagger2types > generated/my-api.ts

Programmatic Usage

import { generate } from 'swagger2types';
import { writeFileSync } from 'fs';

const spec = {
  // Your Swagger/OpenAPI spec
  openapi: '3.0.0',
  // ... rest of spec
};

const generated = await generate(spec);
writeFileSync('./generated/my-api.ts', generated);

Generated Type Structure

The tool generates a Routes type that maps each API endpoint to its request and response types:

export type Routes = {
  /** Get a list of users with optional pagination */
  ['GET /users']: {
    Request: {
      query?: { page?: number; limit?: number };
    };
    Response: User[];
  };

  /** Create a new post for a specific user */
  ['POST /users/${userId}/posts']: {
    Request: {
      params: { userId: string };
      body: CreatePostRequest;
    };
    Response: Post;
  };

  // ... more routes
};

Each route includes JSDoc comments from your OpenAPI spec's description or summary fields, plus request types with only the properties it actually uses:

  • params - Path parameters (e.g., ${userId})
  • query - Query string parameters
  • headers - Required headers
  • body - Request body for POST/PUT/PATCH operations

But Why?

Preserves Your API Structure

Other generators create method-based clients (client.getUser(id)) that obscure the actual API structure. You end up hunting through documentation to figure out which method maps to GET /api/users/{userId}.

swagger2types keeps it simple: you work directly with routes as they appear in your API documentation. No abstraction layer, no guessing.

Minimal Bundle Impact

Traditional generators produce massive amounts of runtime code. For example, swagger-typescript-api generates over 1MB of TypeScript code for the GitHub API, resulting in 200KB+ of minified JavaScript that ships to users.

swagger2types generates only type definitions that disappear at compile time. Your bundle impact stays under 1KB regardless of API size - whether you have 10 endpoints or 1,000.

Real-world comparison using our vite-project example:

dist/assets/swagger2types-BkBWp9Hm.js             1.64 kB │ gzip:  0.87 kB
dist/assets/swagger-typescript-api-t86z52Ap.js  183.91 kB │ gzip: 19.89 kB

Maximum Flexibility

Choose your own HTTP client, error handling, response processing, and integration approach. Works seamlessly with any framework (React, Vue, Angular, Express, Next.js) and module system (ESM, CJS).

Integrate gradually with existing codebases - no need to refactor everything to use a specific client library. The examples show different approaches you can adapt.

Complete Example

Here's the typical workflow from spec to type-safe API calls:

1. Generate Types

swagger2types https://petstore.swagger.io/v2/swagger.json > petstore-types.ts

2. Create Your Client

Copy the helper utilities from swagger-utils.ts and create a client:

import { clientFromFetch } from './swagger-utils.ts';
import type { Routes } from './petstore-types.ts';

export const petstore = clientFromFetch<Routes>({
  baseUrl: 'https://petstore.swagger.io/v2/',
});

3. Make Type-Safe API Calls

// Full type safety with autocomplete
const pet = await petstore('GET /pet/${petId}', {
  params: { petId: 1 }
});

const newPet = await petstore('POST /pet', {
  body: {
    name: 'Fluffy',
    category: { id: 1, name: 'cats' },
    status: 'available'
  }
});

Demo

Examples & Resources

Working Examples:

  • ESM Example - Modern ES modules with GitHub, Petstore, and Star Wars APIs
  • CJS Example - CommonJS usage with GitHub API
  • Vite Project - React app with bundle size comparison
  • Client Types - Ready-to-use clients for fetch, axios, got, and Node.js HTTP

Try it now: Interactive example on StackBlitz

How It Works

  1. Spec Processing - Accepts OpenAPI/Swagger specifications with full TypeScript typing
  2. Code Generation - Uses swagger-typescript-api with a custom template to generate pure types
  3. Route Mapping - Creates a structured Routes type mapping each endpoint to request/response types
  4. Type Extraction - Strips runtime code and exports only the type definitions

Development

# Install dependencies
npm install

# Build the project
npm run build

# Run tests
npm test

# Test individual module systems
npm run test:esm
npm run test:cjs

License

MIT

Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.