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

swagger-ts-mapper

v0.4.5

Published

mapping swagger schemas to typescript types

Readme

Swagger Type Mapper 🔄

A zero-runtime, type-only library to generate precise TypeScript types directly from your OpenAPI v2/v3 schema.

Overview

The Swagger Type Mapper is a simple TypeScript type library that provides a robust solution for converting Swagger/OpenAPI JSON schema definitions into corresponding TypeScript types. This allows developers to gain strong type-checking for their API request and response bodies directly from their API documentation, eliminating manual type declaration and reducing potential runtime errors.

Features

  • Zero Runtime, No Build Step: Purely a devDependency. All type generation happens at compile time.
  • $ref Resolution: Automatically resolves internal references for schemas and parameters.
  • Complete Coverage: Maps responses, requestBody, and parameters (query, path, header, cookie). -️ Accurate Types: Correctly handles required and optional properties, enum, allOf/oneOf/anyOf combiners, and nullable fields.
  • Content-Aware: Creates types for different content types (e.g., application/json, application/xml) and maps empty responses to void.
  • Strongly Typed: Leverages your OpenAPI schema as const to infer the most precise types possible.

Installation

npm install --save-dev swagger-ts-mapper

Usage

Using this library is a simple, three-step process.

Step 1: Prepare Your OpenAPI Schema

The most important step is to import your OpenAPI/Swagger JSON object and export it from a TypeScript file with an as const assertion. This is critical because it preserves the literal types that the mappers need to work.

  1. Get your swagger JSON file.
  2. Create a new file, for example src/api-spec.ts.
  3. Import or paste the JSON content and export it as const.
// src/api-spec.ts

// IMPORTANT: Use `as const` to preserve all literal types from your schema.
export const myApiSpec = {
  openapi: '3.0.0',
  info: {
    title: 'Simple Pet Store API',
    version: '1.0.0',
  },
  paths: {
    '/pets/{petId}': {
      get: {
        summary: 'Find pet by ID',
        parameters: [
          {
            name: 'petId',
            in: 'path',
            required: true,
            schema: { type: 'integer', format: 'int64' },
          },
        ],
        responses: {
          '200': {
            description: 'successful operation',
            content: {
              'application/json': {
                schema: { $ref: '#/components/schemas/Pet' },
              },
            },
          },
          '404': {
            description: 'Pet not found',
          },
        },
      },
    },
  },
  components: {
    schemas: {
      Pet: {
        type: 'object',
        required: ['id', 'name'],
        properties: {
          id: { type: 'integer', format: 'int64' },
          name: { type: 'string' },
          tag: { type: 'string' },
        },
      },
    },
  },
} as const;

Step 2: Generate Your API Types

Now, use the main SwaggerMapper type to generate a complete type map for your entire API.

// src/api-types.ts
import type { SwaggerMapper } from 'swagger-ts-mapper';
import { myApiSpec } from './api-spec';

// This one line generates types for your entire API!
export type MyApi = SwaggerMapper<typeof myApiSpec>;

Step 3: Use the Generated Types

You can now import MyApi anywhere in your project and access the precise types for any endpoint, response, or parameter.

import type { MyApi } from './api-types';

// Get the type for a successful response body
type Pet = MyApi['/pets/{petId}']['get']['responses']['200']['application/json'];
// => type Pet = { readonly id: number; readonly name: string; readonly tag?: string; }

// Get the type for a response with no content
type NotFoundResponse = MyApi['/pets/{petId}']['get']['responses']['404'];
// => void

// Get the type for path parameters
type PathParams = MyApi['/pets/{petId}']['get']['parameters']['path'];
// => type PathParams = { readonly petId: number; }

// Use it in your API client
async function getPetById(params: PathParams): Promise<Pet> {
  const response = await fetch(`/pets/${params.petId}`);
  if (!response.ok) {
    if (response.status === 404) {
      // The type is `void`, so we don't expect a body
      throw new Error('Pet not found');
    }
    throw new Error('An unknown error occurred');
  }
  const data: Pet = await response.json();
  return data;
}

Utility Mappers

While SwaggerMapper is the main entry point, the package also exposes the underlying utility types if you need to map only a specific part of a schema.

  • ResponseMapper<S, T>: Maps an OpenAPI responses object.
  • RequestBodyMapper<S, T>: Maps an OpenAPI requestBody object.
  • ParametersMapper<S, T>: Maps an OpenAPI parameters array.
  • BodyMapper<S, T>: Maps a single OpenAPI schema object to its corresponding TypeScript type.

How It Works

This library contains no runtime code. It leverages advanced TypeScript features like Conditional Types, the infer keyword, and Mapped Types with Key Remapping to recursively parse the structure of your schema object at the type level. The as const assertion provides the necessary literal type information for this process to be possible.

Contributing

This project is a self-contained type utility. If you have suggestions for improvements or find edge cases, please consider sharing them.

License

MIT