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

express-swaggerify

v1.2.6

Published

Auto-generate OpenAPI 3.0 documentation from Express.js routes with intelligent field detection

Downloads

254

Readme

Express Swaggerify

🚀 Auto-generate OpenAPI 3.0 documentation from Express.js routes with intelligent field detection

npm version License: MIT

✨ Features

  • 🔍 Smart Route Detection - Parses both single-line and multi-line Express.js route definitions
  • 🎯 Joi Schema Extraction - Automatically extracts request body schemas from Joi validators (validate(authSchemas.register))
  • 🧠 Intelligent Field Inference - Analyzes controller code to extract request body fields and types
  • 📝 Auto-Generated Summaries - Creates meaningful endpoint summaries from route paths and methods
  • 🏷️ Organized by Tags - Groups endpoints by route file for better organization
  • 🔒 Auth Detection - Automatically detects authentication middleware
  • 📊 Status Code Analysis - Extracts HTTP status codes from controller implementations
  • 🎯 Smart Defaults - Generates appropriate fields based on common patterns (login, payment, etc.)
  • 📋 OpenAPI 3.0 Compliant - Generates proper OpenAPI 3.0 specification format
  • 🚫 No Manual Documentation - Zero manual Swagger documentation required!

🚀 Quick Start

Installation

npm install express-swaggerify
# or
yarn add express-swaggerify

CLI Usage

# Generate documentation
npx swaggerify generate

# With custom options
npx swaggerify generate --routes-dir ./api/routes --output ./docs/swagger.ts --validators-dir ./src/api/v1/validators

# Validate routes without generating docs
npx swaggerify validate

Programmatic Usage

import { swaggerifyRoutes, generateSwaggerDocs } from 'express-swaggerify';

// Generate endpoints from routes
const endpoints = await swaggerifyRoutes({
  routesDir: './src/routes',
  controllersDir: './src/controllers',
  basePath: '/api/v1'
});

// Generate OpenAPI spec
const swaggerSpec = await generateSwaggerDocs(endpoints, {
  title: 'My API',
  version: '1.0.0'
});

📁 Project Structure

your-project/
├── src/
│   ├── routes/
│   │   ├── auth.ts      # → Auth endpoints
│   │   ├── users.ts     # → Users endpoints
│   │   └── payments.ts  # → Payments endpoints
│   └── controllers/
│       ├── AuthController.ts
│       ├── UserController.ts
│       └── PaymentController.ts
└── swagger-docs.ts      # Generated documentation

🎯 How It Works

1. Route Detection

Swaggerify Routes automatically detects Express.js routes:

// Single-line routes
router.get('/users', userController.getAllUsers);

// Multi-line routes
router.post(
  '/users',
  authenticate,
  validate(userSchema),
  userController.createUser
);

2. Joi Schema Extraction (NEW!)

Automatically extracts request body schemas from Joi validators:

  • Detects validate(authSchemas.register) middleware calls
  • Loads and converts Joi schemas to OpenAPI JSON Schema format
  • Extracts all validation rules (required, min/max length, patterns, etc.)
  • Provides accurate request body documentation from your validation schemas

3. Controller Analysis

Analyzes controller methods to extract:

  • Request body fields from destructuring: const { email, password } = req.body
  • HTTP status codes from responses: res.status(400).json(...)
  • Field types based on usage patterns

4. Smart Field Generation

Generates intelligent defaults based on route patterns:

// /api/v1/auth/login → email, password fields
// /api/v1/payments/create → amount, currency, description fields
// /api/v1/users → email, username, firstName, lastName fields

5. OpenAPI 3.0 Generation

Creates proper OpenAPI 3.0 specification with:

  • Correct content.application/json structure
  • Meaningful operation IDs
  • Organized tags by route file
  • Proper authentication requirements

📖 Configuration Options

interface SwaggerifyOptions {
  routesDir?: string;           // './src/routes'
  controllersDir?: string;      // './src/controllers'
  outputFile?: string;          // './swagger-docs.ts'
  basePath?: string;            // '/api/v1'
  title?: string;               // 'API Documentation'
  version?: string;             // '1.0.0'
  description?: string;         // 'Auto-generated API documentation'
  servers?: Array<{             // [{ url: 'http://localhost:3000', description: 'Dev' }]
    url: string;
    description: string;
  }>;
  validatorsDir?: string;        // './src/api/v1/validators' - Directory where Joi validators are located
  customSchemas?: Record<string, any>;
  smartDefaults?: boolean;      // true
  fieldTypeInference?: boolean; // true
}

🎨 Example Output

Generated Endpoint

{
  method: 'POST',
  path: '/api/v1/auth/login',
  summary: 'Login',
  operationId: 'login',
  tags: ['Auth'],
  requestBody: {
    required: true,
    content: {
      'application/json': {
        schema: {
          type: 'object',
          properties: {
            email: { type: 'string', description: 'string (email)' },
            password: { type: 'string', description: 'string (password)' }
          },
          required: ['email', 'password']
        }
      }
    }
  },
  responses: {
    '200': {
      description: 'Success',
      content: {
        'application/json': {
          schema: { $ref: '#/components/schemas/ApiResponse' }
        }
      }
    }
  }
}

Generated OpenAPI Spec

openapi: 3.0.0
info:
  title: API Documentation
  version: 1.0.0
paths:
  /api/v1/auth/login:
    post:
      summary: Login
      operationId: login
      tags: [Auth]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                email:
                  type: string
                  description: string (email)
                password:
                  type: string
                  description: string (password)
              required: [email, password]
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ApiResponse'

🔧 Integration Examples

Express.js + Swagger UI

import express from 'express';
import swaggerUi from 'swagger-ui-express';
import { swaggerSpec } from './swagger-docs';

const app = express();

app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));

Next.js API Routes

// pages/api/docs.ts
import { swaggerSpec } from '../../../swagger-docs';

export default function handler(req: NextApiRequest, res: NextApiResponse) {
  res.json(swaggerSpec);
}

Package.json Scripts

{
  "scripts": {
    "docs:generate": "swaggerify generate",
    "docs:validate": "swaggerify validate",
    "dev": "npm run docs:generate && nodemon src/index.ts"
  }
}

🎯 Supported Patterns

Route Definitions

  • ✅ router.get('/path', handler)
  • ✅ router.post('/path', middleware, handler)
  • ✅ Multi-line route definitions
  • ✅ Path parameters: /users/:id
  • ✅ Authentication middleware detection

Controller Methods

  • ✅ async functionName(req, res) { ... }
  • ✅ const methodName = async (req, res) => { ... }
  • ✅ Request body destructuring: const { email, password } = req.body
  • ✅ Status code extraction: res.status(400).json(...)

Field Type Inference

  • ✅ Email fields: email → string (email)
  • ✅ Password fields: password → string (password)
  • ✅ ID fields: userId → string (uuid)
  • ✅ Numeric fields: amount, price → number
  • ✅ Boolean fields: isActive, hasPermission → boolean

⚠️ Important Notes

What Works

Express Swaggerify works best with static route definitions that follow standard Express.js patterns. It analyzes your TypeScript/JavaScript files using AST parsing to extract route information.

Current Limitations

  • ❌ Dynamic route registration (routes registered in loops or from configuration)
  • ❌ Routes without clear controller references (inline handlers with complex logic)
  • ❌ Heavily obfuscated or minified code

Best Practices

  1. Keep route definitions in dedicated route files
  2. Use named controller methods (not inline anonymous functions)
  3. Use destructuring for request body fields: const { email } = req.body
  4. Follow consistent naming conventions

For detailed examples, see the examples/basic-express-app directory.

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

  • Built for the Express.js community
  • Inspired by the need for automated API documentation
  • Thanks to all contributors and users!

Made with ❤️ by Ahmad Sadek