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

wpapi-to-swagger

v0.1.0

Published

_description_

Readme

wpapi-to-swagger

npm version npm downloads bundle JSDocs License

Convert WordPress REST API schema to OpenAPI/Swagger v2 specification document

Installation

# Using npm
npm install wpapi-to-swagger

# Using pnpm
pnpm add wpapi-to-swagger

# Using yarn
yarn add wpapi-to-swagger

Usage

import { transform } from 'wpapi-to-swagger'

// Get WordPress REST API schema
const wpApiSchema = {
  routes: {
    '/wp/v2/posts': {
      namespace: 'wp/v2',
      endpoints: [
        {
          methods: ['get'],
          description: 'Get list of posts',
          args: {
            page: {
              type: 'integer',
              description: 'Page number',
              required: false
            },
            per_page: {
              type: 'integer',
              description: 'Number of items per page',
              required: false
            }
          }
        }
      ]
    },
    '/wp/v2/posts/(?P<id>[\\d]+)': {
      namespace: 'wp/v2',
      endpoints: [
        {
          methods: ['get'],
          description: 'Get a single post',
          args: {
            context: {
              type: 'string',
              description: 'Request context',
              required: false,
              enum: ['view', 'embed', 'edit']
            }
          }
        }
      ]
    }
  }
}

// Transform to Swagger specification
const swaggerSpec = transform(wpApiSchema)

// Output result
console.log(JSON.stringify(swaggerSpec, null, 2))

Features

  • Convert WordPress REST API schema to OpenAPI/Swagger v2 specification
  • Automatically handle WordPress-style path parameters (e.g. /wp/v2/posts/(?P<id>[\d]+)/wp/v2/posts/{id})
  • Automatically determine parameter locations based on HTTP methods (path, query, formData)
  • Support parameter type conversion and validation rules
  • Generate complete API documentation including paths, methods, parameters, and responses

API

transform(source)

Convert WordPress REST API schema to OpenAPI/Swagger v2 specification document.

Parameters:

  • source (WordPressAPISchema): WordPress REST API schema object

Returns:

  • (OpenAPISpecificationV2): Document object conforming to OpenAPI/Swagger v2 specification

Type Definitions

// WordPress API schema
interface WordPressAPISchema {
  routes: Record<string, WordPressRoute>
}

// WordPress route
interface WordPressRoute {
  namespace: string
  endpoints: WordPressEndpoint[]
}

// WordPress endpoint
interface WordPressEndpoint {
  methods: ('get' | 'post' | 'put' | 'delete')[]
  description?: string
  args?: Record<string, WordPressArgument>
}

// WordPress argument
interface WordPressArgument {
  type?: string | string[]
  description?: string
  required?: boolean
  enum?: string[]
  default?: any
  items?: {
    type?: string
  }
  maximum?: number
  minimum?: number
  format?: string
  schema?: any
}

Utility Functions

The package also provides some useful utility functions:

  • convertEndpoint(endpoint): Convert WordPress-style endpoint paths to Swagger style
  • getParametersFromEndpoint(endpoint): Extract path parameters from endpoint paths
  • getParametersFromArgs(endpoint, args, method): Extract Swagger parameters from argument definitions

Example Output

{
  "swagger": "2.0",
  "basePath": "",
  "host": "",
  "info": {
    "title": "WordPress REST API",
    "version": "1.0",
    "description": "Using the WordPress REST API you can create a plugin to provide an entirely new admin experience for WordPress, build a brand new interactive front-end experience, or bring your WordPress content into completely separate applications."
  },
  "paths": {
    "/wp/v2/posts": {
      "get": {
        "tags": ["wp/v2"],
        "summary": "Get list of posts",
        "description": "Get list of posts",
        "operationId": "get_wp_v2_posts",
        "parameters": [
          {
            "name": "page",
            "in": "query",
            "description": "Page number",
            "required": false,
            "type": "integer",
            "format": "int64"
          },
          {
            "name": "per_page",
            "in": "query",
            "description": "Number of items per page",
            "required": false,
            "type": "integer",
            "format": "int64"
          }
        ],
        "responses": {
          "200": { "description": "OK" },
          "400": { "description": "Bad Request" },
          "404": { "description": "Not Found" }
        }
      }
    },
    "/wp/v2/posts/{id}": {
      "get": {
        "tags": ["wp/v2"],
        "summary": "Get a single post",
        "description": "Get a single post",
        "operationId": "get_wp_v2_posts_id",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "",
            "required": true,
            "type": "integer",
            "format": "int64"
          },
          {
            "name": "context",
            "in": "query",
            "description": "Request context",
            "required": false,
            "type": "array",
            "items": {
              "type": "string",
              "enum": ["view", "embed", "edit"]
            },
            "collectionFormat": "multi"
          }
        ],
        "responses": {
          "200": { "description": "OK" },
          "400": { "description": "Bad Request" },
          "404": { "description": "Not Found" }
        }
      }
    }
  }
}

Use Cases

  • Generate API documentation for WordPress sites
  • Create client applications based on WordPress API
  • Provide interactive API documentation using Swagger UI
  • Integrate with API development tools like Postman, Swagger Editor, etc.

License

MIT License © 2025 Hairyf