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

@oselvar/openapi-routes

v0.0.4

Published

Supabase Realtime on Absinthe

Downloads

3

Readme

openapi-routes

OpenAPI Routes is a small library that validates HTTP requests and responses against an OpenAPI 3.0 specification.

It is designed to work with any web servers/framework that uses the Fetch API such as:

It also provides adapters for non-Fetch based web servers/frameworks such as:

OpenAPI Routes is built on top of Zod and zod-to-openapi. Rather than writing OpenAPI specifications by hand, you write Zod schemas and then generate OpenAPI specifications from them.

Installation

npm install --save @oselvar/openapi-routes

Usage

There are two or three steps, depending on your web server:

  1. Define an OpenAPI route
  2. Write a handler function
  3. Register the handler function (if you're using a web server that does not use the Fetch API)

Define an OpenAPI route

import { FetchOpenAPIHandler } from '@oselvar/openapi-routes';
import { RouteConfig } from '@asteasolutions/zod-to-openapi';

// Define an OpenAPI route using https://github.com/asteasolutions/zod-to-openapi
const routeConfig: RouteConfig = {
  method: 'post',
  path: '/things/{thingId}',
  // more properties...
}

Write a handler function

If you are using a framework that does not use the Fetch API Request and Response objects such as AWS Lambda, Express or Fastify, use the FetchRoute type to define your handler function:

import { FetchRoute } from '@oselvar/openapi-routes';

export const fetchRoute: FetchRoute = async ({ params, request }) => {
  // If the params are not valid, a 404 Response will be thrown
  // If the request body is not valid, a 422 Response will be thrown
  const { params, body, respond, response } = await validate<ThingParams, ThingBody>(
    routeConfig, 
    params, 
    request
  );
  if (response) {
    // There was a request validation error
    return response;
  }

  // Do something with params and body

  const responseBody = { message: 'Hello, world!' };
  // If the response body is not valid, a 500 Response will be returned
  return respond(responseBody, 200);
};

The FetchRoute handler can then be registered with your HTTP server (see below).

If you are using a framework that does use Request and Response such as Astro or Remix, you can write your handler using the framework's API and still use the validate function.

The following example uses Astro API Routes:

import type { APIRoute } from 'astro';

export const POST: APIRoute = async (context) => {
  const { params, body, respond } = await validate<ThingParams, ThingBody>(
    routeConfig, 
    context.params, 
    context.request
  );

  const responseBody = { message: 'Hello, world!' };
  return respond(responseBody, 200);
};

Register the handler function

This section is for web servers that do not use the Fetch API.

Convert the handler function to a function that can be registered with your HTTP server.

Note that the support for multiple HTTP servers can also simplify your developer experience. You can write your handler function once and then register it with multiple HTTP servers.

For example, you can register your handler functions with Express or Fastify during development and then register them with AWS Lambda during production.

AWS Lambda

import { FetchOpenAPIHandler } from '@oselvar/openapi-routes';
import { toProxyHandler } from '@oselvar/openapi-routes/aws-lambda';

export const handler = toProxyHandler(fetchRoute);

Express

Coming soon.

Fastify

Coming soon.