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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@emdgroup/tapir

v1.0.0-beta.20

Published

Generate TypeScript assertions and type guards from an OpenAPI 3.0 definition

Downloads

241

Readme

tapir

Generate TypeScript assertions and type guards from an OpenAPI 3.0 definition

example

Features

  • Validates a schema against the OpenAPI 3.0 specification
  • Validators generated for request bodies, path parameters, query parameters and response bodies
  • Generates TypeScript assertions and type guards for all components
  • Type guards include validators for OpenAPI formats and types
  • Translates schema descriptions into JSDoc comments
  • Tree-shakable ESM output

Check out the examples directory to see the generated types and assertions for the petstore example.

Synopsis

yarn add --dev @emdgroup/tapir
yarn tapir openapi.yml dir/to/lib/

# or use - to read from stdin
cat openapi.yml | yarn tapir - dist/
curl -s https://petstore3.swagger.io/api/v3/openapi.json | yarn tapir - petstore/

This will create the following files:

dist/
  types.d.ts
  types.js

The generated files are self-contained and tree-shakable. There are no runtime dependencies on tapir or any other package.

The generated files use ESM syntax, so you'll need to use a bundler like Rollup or Webpack to bundle them for use in the browser.

Example

With the following example schema we will illustrate the code that tapir is generating. You can open this example at https://codesandbox.io/p/sandbox/tapir-uffo1n and test the generated types and type guards.

openapi: 3.0.2

info: { title: Synopsis, version: 1.0.0 }

paths:
  /pet/{petId}:
    parameters:
      - $ref: '#/components/parameters/PetId'
    post:
      operationId: UpdatePet
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Pet'
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
        404:
          description: Not Found

components:
  parameters:
    PetId:
      required: true
      in: path
      name: petId
      schema:
        $ref: '#/components/schemas/PetId'

  schemas:
    PetId:
      type: string
      pattern: ^pet-[0-9a-f]{8}$

    Pet:
      type: object
      required: [name]
      additionalProperties: false
      properties:
        name:
          type: string
          minLength: 1
          maxLength: 64
        status:
          $ref: '#/components/schemas/PetStatus'

    PetStatus:
      type: string
      enum:
        - AVAILABLE
        - PENDING
        - SOLD

Tapir will generate a type, type guard and type assertion for every request, response and schema. In our example, this means the following resources are created:

import type {
  Pet,
  PetId,
  PetStatus,
  UpdatePetRequest,
  UpdatePetResponse,
} from './types';

import {
  isPet,
  isPetId,
  isPetStatus,
  isUpdatePetRequest,
  isUpdatePetResponse,
} from './types';

In your server code, you would use these functions like this:

import type { Pet } from './types';
import { isUpdatePetRequest, isUpdatePetResponse } from './types';

import type { APIGatewayProxyHandlerV2 } from 'aws-lambda';

export const handler: APIGatewayProxyHandlerV2 = async (event) => {
  const request: unknown = {
      pathParameters: event.pathParameters,
      json: event.body ? JSON.parse(event.body) : {},
  }

  if (!isUpdatePetRequest(request)) throw new Error('Bad request');

  // request is now of type UpdatePetRequest, request.json is a Pet
  const pet = await updatePet(request.pathParameters.petId, request.json);

  const response: unknown = { json: pet, statusCode: 200 };

  if (!isUpdatePetResponse(response)) throw new Error('Internal Server Error');

  // response is now of type UpdatePetResponse
  return response;
};

On the client, we recommend to skip the type guards since the client should be able to trust the server response. This has the added benefit that your bundle size will not increase since the types are all stripped out.

const req = await fetch('/pet/pet-00000000', { method: 'PUT' });
const { json, statusCode } = { json: await req.json(), statusCode: req.status } as UpdatePetResponse;
if (status === 200) {
  // json is of type Pet
} else {
  // handle Not Found.
}

Note that every type guard is also exported as type assertion.

isPetStatus(status); // -> returns true/false
assertPetStatus(status); // -> throws if status is not a valid PetStatus