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

@richie-rpc/openapi

v1.1.1

Published

OpenAPI 3.1 specification generator for Richie RPC contracts

Readme

@richie-rpc/openapi

OpenAPI 3.1 specification generator for Richie RPC contracts.

Installation

bun add @richie-rpc/openapi @richie-rpc/core zod@^4

Usage

Generate OpenAPI Spec

import { generateOpenAPISpec } from '@richie-rpc/openapi';
import { contract } from './contract';

const spec = generateOpenAPISpec(contract, {
  info: {
    title: 'My API',
    version: '1.0.0',
    description: 'API description',
    contact: {
      name: 'API Support',
      email: '[email protected]'
    }
  },
  servers: [
    {
      url: 'https://api.example.com',
      description: 'Production server'
    },
    {
      url: 'http://localhost:3000',
      description: 'Development server'
    }
  ]
});

Generate OpenAPI Spec with basePath

If your API is served under a path prefix, use the basePath option to prefix all paths in the spec:

const spec = generateOpenAPISpec(contract, {
  info: {
    title: 'My API',
    version: '1.0.0'
  },
  basePath: '/api'  // All paths will be prefixed with /api
});

// If contract defines /users, the OpenAPI spec will show /api/users

Serve OpenAPI Spec

import { createOpenAPIResponse } from '@richie-rpc/openapi';

Bun.serve({
  fetch(request) {
    const url = new URL(request.url);
    
    if (url.pathname === '/openapi.json') {
      return createOpenAPIResponse(contract, {
        info: { title: 'My API', version: '1.0.0' }
      });
    }
    
    // ... other routes
  }
});

Serve API Documentation UI

The package includes support for Scalar API documentation UI:

import { createDocsResponse } from '@richie-rpc/openapi';

Bun.serve({
  fetch(request) {
    const url = new URL(request.url);
    
    if (url.pathname === '/docs') {
      return createDocsResponse('/openapi.json', {
        title: 'My API Documentation',
        layout: 'modern'
      });
    }
    
    // ... other routes
  }
});

Features

  • ✅ OpenAPI 3.1 specification generation
  • ✅ Automatic Zod to JSON Schema conversion using Zod v4's built-in z.toJSONSchema()
  • ✅ No external JSON Schema conversion dependencies
  • ✅ Path parameters, query parameters, request bodies
  • ✅ Multiple response types per endpoint
  • ✅ BasePath support for prefixing all paths
  • ✅ Server and info metadata support
  • ✅ Scalar API documentation UI integration
  • ✅ Modern, interactive documentation

API Reference

generateOpenAPISpec(contract, options)

Generates an OpenAPI 3.1 specification from a contract.

Parameters:

  • contract: The Richie RPC contract
  • options.info: OpenAPI info object (required)
    • title: API title
    • version: API version
    • description: API description (optional)
    • contact: Contact information (optional)
    • license: License information (optional)
  • options.servers: Array of server objects (optional)
  • options.basePath: Path prefix for all endpoints (optional, e.g., /api)

Returns: OpenAPI specification object

createOpenAPIResponse(contract, options)

Creates a Response object with the OpenAPI spec as JSON.

createDocsResponse(openAPIUrl, options)

Creates a Response object with HTML for Scalar API documentation.

Parameters:

  • openAPIUrl: Path to OpenAPI spec JSON (default: /openapi.json)
  • options.title: Documentation title
  • options.layout: UI layout ('modern' or 'classic')
  • options.showToolbar: Show toolbar ('always', 'never', or 'auto')
  • options.hideClientButton: Hide client button (boolean)

OpenAPI Spec Structure

The generated spec includes:

  • Paths: All endpoints with their operations
  • Parameters: Path and query parameters with schemas
  • Request Bodies: JSON schemas for request bodies
  • Responses: Status codes mapped to response schemas
  • Schemas: Automatically generated from Zod schemas

Example Output

{
  "openapi": "3.1.0",
  "info": {
    "title": "My API",
    "version": "1.0.0"
  },
  "paths": {
    "/users/{id}": {
      "get": {
        "operationId": "getUser",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": { ... }
              }
            }
          }
        }
      }
    }
  }
}

Links

  • npm: https://www.npmjs.com/package/@richie-rpc/openapi
  • Repository: https://github.com/ricsam/richie-rpc

License

MIT