MCPcopy Index your code
hub / github.com/Moltivie/redoc-express-maintained

github.com/Moltivie/redoc-express-maintained @v2.1.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.1.1 ↗ · + Follow
68 symbols 134 edges 23 files 13 documented · 19%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

redoc-express-maintained

npm version npm downloads License: MIT Code Style: Prettier

Community-maintained Express middleware for serving OpenAPI/Swagger documentation using ReDoc.

This is a maintained fork of redoc-express, ensuring compatibility with modern Node.js/Express environments and providing active support.


🚀 What's New in v2.1.x

We've completely overhauled the package with powerful new features:

  • Extensible Plugin System: Hook into the lifecycle (beforeRender, afterRender, onRequest, onError) to customize behavior.
  • Built-in Plugins: Ready-to-use plugins for Authentication, Caching, and Metrics.
  • Full TypeScript Support: Full type definitions and a new named export redocExpressMiddleware for better ESM compatibility.
  • Improved Stability: Locked ReDoc version to prevent unexpected breaking changes.

Key Features

  • Drop-in Replacement: Works exactly like the original, but better.
  • Zero Configuration: Get started with just one line of code.
  • Highly Customizable: Configure ReDoc themes, options, and UI.
  • Production Ready: Built-in caching and error handling.
  • Type Safe: Written in TypeScript with comprehensive type definitions.

Installation

npm install redoc-express-maintained

Usage

Basic Usage (CommonJS)

const express = require('express');
const redoc = require('redoc-express-maintained');

const app = express();

// Serve your Swagger/OpenAPI spec
app.get('/docs/swagger.json', (req, res) => {
  res.sendFile('swagger.json', { root: '.' });
});

// Serve ReDoc
app.get(
  '/docs',
  redoc({
    title: 'API Documentation',
    specUrl: '/docs/swagger.json'
  })
);

app.listen(3000);

Usage with TypeScript / ESM

Recommended for v2+: Use the named export for better type inference.

import express, { Express } from 'express';
import {
  redocExpressMiddleware,
  type RedocExpressOptions
} from 'redoc-express-maintained';

const app: Express = express();

const options: RedocExpressOptions = {
  title: 'API Documentation',
  specUrl: '/docs/swagger.json'
};

app.get('/docs', redocExpressMiddleware(options));

app.listen(3000);

Plugin System

Extend functionality without modifying the core. v2.0 introduces a powerful plugin architecture. Plugins are configured only via the plugins option when creating the middleware—pass an array of plugin instances to enable them for that route.

Using Built-in Plugins

const { authPlugin, cachePlugin } = require('redoc-express-maintained');

app.get(
  '/docs',
  redoc({
    title: 'API Documentation',
    specUrl: '/docs/swagger.json',
    plugins: [
      // Protect docs with Basic Auth
      authPlugin({
        type: 'basic',
        users: { admin: 'password123' }
      }),
      // Cache rendered HTML for 1 hour
      cachePlugin({
        ttl: 3600
      })
    ]
  })
);

Available Built-in Plugins

Plugin Description
Auth Protect your docs with Basic, Bearer, or Custom authentication.
Cache Cache rendered HTML to improve performance.
Metrics Track documentation usage and performance.

Creating Custom Plugins

You can easily create your own plugins to inject scripts, modify HTML, or add logging.

import { createPlugin } from 'redoc-express-maintained';

const analyticsPlugin = createPlugin({
  name: 'analytics',
  hooks: {
    afterRender: (html) => {
      // Inject Google Analytics script
      return html.replace('</body>', '<script>/* GA Code */</script></body>');
    }
  }
});

👉 Read the full Plugin Documentation on our Wiki

Error handling

Plugins can define an onError hook to log, report, or customize error responses. Because Express error handlers run only when you pass an error to next(), you must attach the error middleware after the ReDoc route. Use the same plugins array you pass to the middleware:

const {
  redocExpressMiddleware,
  createOnErrorMiddleware
} = require('redoc-express-maintained');

const plugins = [
  /* your plugins */
];

app.get(
  '/docs',
  redocExpressMiddleware({ title: 'API Docs', specUrl: '/spec.json', plugins })
);
app.use(createOnErrorMiddleware(plugins)); // must come after the ReDoc route

In TypeScript/ESM, import createOnErrorMiddleware from the package and add app.use(createOnErrorMiddleware(plugins)) after your ReDoc route so onError hooks run when the route calls next(error).

Configuration Options

Option Type Description
title string Page title (required).
specUrl string URL to your OpenAPI spec (required).
nonce string Content Security Policy nonce.
redocOptions object ReDoc configuration object.
plugins Plugin[] Array of plugins to apply.

Contributing

Contributions are welcome! Please see our Contributing Guide.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'feat: add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.


Extension points exported contracts — how you extend this code

Core symbols most depended-on inside this repo

Shape

Function 36
Method 15
Interface 13
Class 4

Languages

TypeScript100%

Modules by API surface

src/utils/logger.ts12 symbols
src/plugins/cache/index.ts12 symbols
src/plugins/metrics/index.ts10 symbols
src/hooks.ts7 symbols
src/plugins/auth/index.ts5 symbols
src/types/plugin.ts4 symbols
tests/plugins/metrics.spec.ts3 symbols
tests/plugins/auth.spec.ts3 symbols
src/redoc-html-template.ts3 symbols
src/index.ts3 symbols
src/types/options.ts2 symbols
src/plugin-system.ts2 symbols

For agents

$ claude mcp add redoc-express-maintained \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page