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

next-openapi-gen

v0.0.19

Published

Super fast and easy way to generate OpenAPI documentation automatically from API routes in NextJS 14

Downloads

990

Readme

next-openapi-gen

next-openapi-gen super fast and easy way to generate OpenAPI 3.0 documentation automatically from API routes in a Next.js 14.

With support for multiple user interfaces next-openapi-gen makes documenting your API a breeze!

Prerequisites

  • Nextjs >= 14
  • Node >= 18

Supported interfaces

  • Swagger
  • Redoc
  • Stoplight Elements
  • RapiDoc

Features

  • Automatic OpenAPI Generation: Generate OpenAPI 3.0 documentation from your Next.js routes, automatically parsing TypeScript types for parameters, request bodies and responses.
  • TypeScript Type Scanning: Automatically resolve TypeScript types for params, body, and responses based on your API endpoint's TypeScript definitions. Field comments in TypeScript types are reflected as descriptions in the OpenAPI schema.
  • JSDoc-Based Documentation (Optional): Document API routes with JSDoc comments, including tags like @openapi, @auth, @desc, @params, @body, and @response to easily define route metadata.
  • UI Interface Options: Choose between Swagger UI, Redoc, Stoplight Elements or RapiDoc to visualize your API documentation. Customize the interface to fit your preferences.
  • Real-time Documentation: As your API evolves, regenerate the OpenAPI documentation with a single command, ensuring your documentation is always up to date.
  • Easy configuration: Customize generator behavior using the next.openapi.json configuration file, allowing for quick adjustments without modifying the code.

Demo File

Installation

yarn add next-openapi-gen

Usage

Step 1: Initialize Configuration and Setup

Run the following command to generate the next.openapi.json configuration file and automatically set up Swagger UI with /api-docs routes:

npx next-openapi-gen init --ui swagger --docs-url api-docs

Parameters:

  • ui: swagger | redoc | stoplight | rapidoc
  • docs-url: url on which api docs will be visible

This command does the following:

  • Generates a next.openapi.json file, which stores the OpenAPI configuration for your project.
  • Installs Swagger UI to provide an API documentation interface.
  • Adds an /api-docs route in the Next.js app for visualizing the generated OpenAPI documentation.

Step 2: Add JSDoc Comments to Your API Routes

Annotate your API routes using JSDoc comments. Here's an example:

//app/api/auth/reset-password/route.ts

import { type NextRequest } from "next/server";

type ResetPasswordParams = {
  token: string; // Token for resetting the password
};

type ResetPasswordBody = {
  password: string; // The new password for the user
};

type ResetPasswordResponse = {
  message: string; // Confirmation message that password has been reset
};

/**
 * Reset the user's password.
 * @auth: bearer
 * @desc: Allows users to reset their password using a reset token.
 * @params: ResetPasswordParams
 * @body: ResetPasswordBody
 * @response: ResetPasswordResponse
 */
export async function POST(req: Request) {
  const searchParams = req.nextUrl.searchParams;

  const token = searchParams.get("token"); // Token from query params
  const { password } = await req.json(); // New password from request body

  // Logic to reset the user's password

  return Response.json({ message: "Password has been reset" });
}
  • @openapi: Marks the route for inclusion in the OpenAPI specification.
  • @auth: Specifies authentication type used for API route (basic, bearer, apikey)
  • @desc: Provides a detailed description of the API route.
  • @params: Specifies the TypeScript interface for the query parameters.
  • @body: Specifies the TypeScript interface for the request body.
  • @response: Specifies the TypeScript interface for the response.

Step 3: Generate the OpenAPI Specification

Run the following command to generate the OpenAPI schema based on your API routes:

npx next-openapi-gen generate

This command processes all your API routes, extracts the necessary information from JSDoc comments, and generates the OpenAPI schema, typically saved to a swagger.json file in the public folder.

Step 4: View API Documentation

With the /api-docs route generated from the init command, you can now access your API documentation through Swagger UI by navigating to http://localhost:3000/api-docs.

Configuration Options

The next.openapi.json file allows you to configure the behavior of the OpenAPI generator, including options such as:

  • apiDir: (default: ./src/app/api) The directory where your API routes are stored.
  • schemaDir: (default: ./src) The directory where your schema definitions are stored.
  • docsUrl: (default: ./api-docs) Route where OpenAPI UI is available.
  • ui: (default: swagger) OpenAPI UI interface.
  • outputFile: (default: ./swagger.json) The file where the generated OpenAPI specification will be saved in public folder.
  • includeOpenApiRoutes: (default: false) When true, the generator will only include routes that have the @openapi tag in their JSDoc comments.

Interface providers