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

koa-okapi-router

v0.1.4

Published

Koa Router-wrapper adding type-safety, schema validation and openapi generation.

Readme

koa-okapi-router

Zod-aware router abstraction for Koa, providing a declarative Fastify-style schema declaration and type-safe middleware input/output

npm version GitHub License: ISC Node

Features

  • Declare routes with Zod schemas
  • Type-safe ctx: ParameterizedContext in route handling middleware
  • Support for zod v3 and v4
  • Generate openapi.json
  • Can co-exist with vanilla KoaRouter and e.g, swagger-jsdoc in the same application

Usage

Creating an OkapiRouter

Create a router without asking any questions or taking down any names.

const router = makeOkapiRouter(new KoaRouter(), {
  openapi: {
    info: { title: `ONECore ${config.applicationName}` },
  },
})

Create a router with openapi metadata

const router = makeOkapiRouter(new KoaRouter(), {
  openapi: {
    info: { 
      title: `Exotic Animal Spotting API` 
      version: '1.2.3'
    },
  },
})

Create a router by bringing your own zod instance. Useful when you rely on importing zod/v3 or zod/v4 and cannot use the default zod import.

const router = makeOkapiRouter(new KoaRouter(), {
  schema: {
    zod: z
  },
})

Declaring routes with schemas

  router.get(
    '/contacts/by-phone-number/:phoneNumber',
    {
      summary: 'Get a single contact by phone number',
      description: 'A somewhat more long-winded description',
      tags: ['Contacts'],
      params: {
        phoneNumber: {
          description: 'Phone number to search for',
          schema: z.string(),
      },
      response: {
        200: z.object({
          content: ContactSchema,
          meta: z.object({}),
        }),
        404: z.null(),
      },
    },
    async (ctx) => {
      // ...
    }
  )
  
  app.use(router.routes())

Registering schemas as components/entities


  router.addEntity('Contact', ContactSchema)

  router.get(
    '/contacts/by-phone-number/:phoneNumber',
    {
      ...
      response: {
        200: {
          // Providing a canonical name for the response body registers it as a component entity
          name: 'ContactResponse',
          schema: z.object({
            content: ContactSchema,
            meta: z.object({})
          })
        }
        ...
      }
    ...
  )

Serving Swagger with koa2-swagger-ui

app.use(
  new KoaRouter()
    .get(okapiRouter.openapiJsonUrl, async (ctx) => {
      ctx.body = api.openapiJson()
    })
    .routes()
)

app.use(
  koaSwagger({
    routePrefix: '/swagger',
    swaggerOptions: { url: api.openapiJsonUrl },
  })
)

Extending an existing OpenAPI json schema with Okapi routes and components

Adopting koa-okapi-router doesn't have to be all in, in terms of producing a single openapi.json either. Existing projects looking to migrate can do so gradually by generating the json schema with the current tools as long as you have access to the schema model object.

okapiRouter.extendOpenApiJson takes your existing schema as input, and generates the routes and components from an okapiRouter into that schema.


// OpenAPI schema generated by swaggerJsDoc (or by other means)
const swaggerJsDocSchema = swaggerJsdoc(swaggerSpec) as any

// Combined schema to serve and in place of `swaggerJsDocSchema`
const openApiJson = okapiRouter.extendOpenApiJson(swaggerJsDocSchema)

Version History

See CHANGELOG.md for version history and unreleased changes.

License

© 2025 Sven Johansson. ISC Licensed