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 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-openapi-typer

v0.0.2

Published

Code-generation-free conversion of OpenAPI schema into typed Express request handlers

Downloads

5

Readme

express-openapi-typer

npm version

Caution! Alpha-level software ahead. Use at your own peril

Code-generation-free conversion of OpenAPI v3.1 schema into type-checked Express request handlers.

Derive Express handler types from an OpenAPI schema to get

  • type errors when a handler doesn't match the schema,
  • and auto-completion on handler path, req.param, req.query, req.body, res.send(), res.json() etc.

Note that the library does not perform runtime validation against the OpenAPI schema: add something like https://github.com/Hilzu/express-openapi-validate for that purpose.

Requires OpenAPI v3.1. This library relies heavily on existing JSON Schema tooling whereas earlier OpenAPI versions use the OpenAPI Schema Object instead of pure JSON Schema. OpenAPI v3.1 is yet unpublished; track progress here. Read more about the OpenAPI/JSON Schema divergence at https://apisyouwonthate.com/blog/openapi-and-json-schema-divergence-part-1 and how v3.1 solves it at https://phil.tech/2019/09/07/update-openapi-json-schema/.

Install

yarn add express-openapi-typer

Usage

First define your OpenAPI schema as a TypeScript type:

interface PetStoreSchema {
  openapi: '3.1.0'
  info: { ... }
  paths: {
    '/pets': {
        get: { ...}
    },
    ...
  }
}

And then override your Express router's type from

const router = express.Router()

into the following:

import { OpenAPIRouter } from 'express-openapi-typer'

const router = (express.Router() as unknown) as OpenAPIRouter<PetStoreSchema>

Handler functions in router now get type-checked as per PetStoreSchema! For example when using the full sample PetStore schema we end up with the following:

Usage sample

Access OpenAPI schema type from a runtime value

It can be useful to instantiate the OpenAPI schema as a runtime value instead of a plain type. For example when serving the schema as documentation or handling validation we need to access the schema at runtime. In cases like these combine typeof and as const to access the schema type:

const petStoreSchema = {
  openapi: '3.1.0',
  info: { ... },
  paths: {
    '/pets': {
        get: { ... }
    },
    ...
  }
} as const // <-- important!

type PetStoreSchema = typeof petStoreSchema

Allow additional paths

By default OpenAPIRouter doesn't allow any additional handlers not defined in the OpenAPI schema. To loosen this restriction you can expand the type as follows:

import * as express from 'express'

const router = express.Router() as OpenAPIRouter<PetStoreSchema> & express.Router

You can also select a subset of express.Router with Pick/Omit when allowing additional methods only for a specific HTTP method, for example.

TODO

  • All the limitations from json-schema-type-mapper apply here as well
  • Handle request header parameters?
  • API client type checking, using Axios?
  • Figure a way out of the unfortunate as unknown cast
  • Support path-based $refs, not just $id-based ones
    • requires some sort of manual mapping as we can't take "#/components/schemas/NewUser" apart at type-level

Related projects

  • https://github.com/rawrmaan/restyped
  • https://github.com/hmil/rest.ts
  • https://stoplight.io/open-source/prism/ Prism is an open source mock server that can mimic your API’s behavior as if you already built it. Servers are generated from your OpenAPI v2/v3 (formerly known as Swagger) documents.
  • apiaryio/dredd