teunmooij

swagger

by teunmooij
payload-tools
APIs & SDKs
v1
^1.6.22
MIT

Overview

Summary generated from the README

Builds an OpenAPI 3 document from the Payload config and adds routes for the document, a Swagger UI page and the license file. It is registered through the plugins array and covers collection, global, custom, authentication and preferences endpoints. Written for Payload 1.x.

Features

  • Generates an OpenAPI 3 document with typed schemas for requests, parameters and responses
  • Adds routes for Swagger UI, the specs document and the LICENSE file with configurable paths
  • Documents custom endpoints with a defineEndpoint helper or a custom.openapi property
  • Analyses access functions to determine endpoint authentication requirements
  • Merges extra OpenAPI data from package.json and an optional .openapi file
  • Supports examples on collections and globals and per-language descriptions for docs

Use cases

  • Publish interactive API documentation for a Payload backend
  • Document custom endpoints with typed request and response schemas
  • Hide unused endpoints from API docs by blocking access with payload-rbac

From the author

Swagger plugin for payload cms

Installation

npm i payload-swagger

143

Stars

15

Forks

13

Open Issues

2 years ago

Last Updated

1.4k

Weekly Downloads

v1.4.0

npm Version

24.3 KB

Unpacked Size

2

Dependencies

Repository stars

143+2 since Sep 6

Show data
Repository stars by day (UTC)
DateStars
Dec 5, 2026143
Dec 4, 2026143
Dec 3, 2026143
Dec 2, 2026143
Dec 1, 2026143
Nov 30, 2026143
Nov 29, 2026143
Nov 28, 2026143
Nov 27, 2026143
Nov 26, 2026143
Nov 25, 2026143
Nov 24, 2026143
Nov 23, 2026143
Nov 22, 2026143
Nov 21, 2026143
Nov 20, 2026143
Nov 19, 2026143
Nov 18, 2026143
Nov 17, 2026143
Nov 16, 2026143
Nov 15, 2026143
Nov 14, 2026143
Nov 13, 2026143
Nov 12, 2026143
Nov 11, 2026143
Nov 10, 2026143
Nov 9, 2026143
Nov 8, 2026143
Nov 7, 2026142
Nov 6, 2026142
Nov 5, 2026142
Nov 4, 2026142
Nov 3, 2026142
Nov 2, 2026142
Nov 1, 2026142
Oct 31, 2026142
Oct 30, 2026142
Oct 29, 2026142
Oct 28, 2026142
Oct 27, 2026142
Oct 26, 2026142
Oct 25, 2026142
Oct 24, 2026142
Oct 23, 2026142
Oct 22, 2026142
Oct 21, 2026142
Oct 20, 2026142
Oct 19, 2026142
Oct 18, 2026142
Oct 17, 2026142
Oct 16, 2026142
Oct 15, 2026142
Oct 14, 2026142
Oct 13, 2026142
Oct 12, 2026142
Oct 11, 2026142
Oct 10, 2026142
Oct 9, 2026142
Oct 8, 2026142
Oct 7, 2026142
Oct 6, 2026142
Oct 5, 2026142
Oct 4, 2026142
Oct 3, 2026142
Oct 2, 2026142
Oct 1, 2026142
Sep 30, 2026142
Sep 29, 2026142
Sep 28, 2026142
Sep 27, 2026142
Sep 26, 2026142
Sep 25, 2026142
Sep 24, 2026142
Sep 23, 2026142
Sep 22, 2026142
Sep 21, 2026142
Sep 20, 2026142
Sep 19, 2026142
Sep 18, 2026142
Sep 17, 2026142
Sep 16, 2026141
Sep 15, 2026141
Sep 14, 2026141
Sep 13, 2026141
Sep 12, 2026141
Sep 11, 2026141
Sep 10, 2026141
Sep 9, 2026141
Sep 8, 2026141
Sep 7, 2026141
Sep 6, 2026141

Shared by 5 plugins in teunmooij/payload-tools.

npm weekly downloads

1.3K−67 since Sep 6

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 5, 20261,313
Dec 4, 20261,084
Dec 3, 20261,079
Dec 2, 20261,181
Dec 1, 20261,179
Nov 30, 20261,159
Nov 29, 20261,266
Nov 28, 20261,281
Nov 27, 20261,464
Nov 26, 20261,572
Nov 25, 20261,497
Nov 24, 20261,568
Nov 23, 20261,577
Nov 22, 20261,401
Nov 21, 20261,530
Nov 20, 20261,242
Nov 19, 20261,004
Nov 18, 2026809
Nov 17, 2026782
Nov 16, 2026824
Nov 15, 2026755
Nov 14, 2026574
Nov 13, 2026581
Nov 12, 2026665
Nov 11, 2026700
Nov 10, 2026694
Nov 9, 2026660
Nov 8, 2026655
Nov 7, 2026629
Nov 6, 2026603
Nov 5, 2026573
Nov 4, 2026599
Nov 3, 2026623
Nov 2, 2026626
Nov 1, 2026653
Oct 31, 2026614
Oct 30, 2026620
Oct 29, 2026473
Oct 28, 2026356
Oct 27, 2026307
Oct 26, 2026454
Oct 25, 2026378
Oct 24, 2026371
Oct 23, 2026287
Oct 22, 2026386
Oct 21, 2026444
Oct 20, 2026449
Oct 19, 2026298
Oct 18, 2026283
Oct 17, 2026284
Oct 16, 2026391
Oct 15, 2026346
Oct 14, 2026391
Oct 13, 2026372
Oct 12, 2026360
Oct 11, 2026357
Oct 10, 2026546
Oct 9, 2026717
Oct 8, 20261,007
Oct 7, 20261,084
Oct 6, 20261,131
Oct 5, 20261,227
Oct 4, 20261,591
Oct 3, 20261,612
Oct 2, 20261,663
Oct 1, 20261,509
Sep 30, 20261,543
Sep 29, 20261,533
Sep 28, 20261,449
Sep 27, 20261,302
Sep 26, 20261,303
Sep 25, 20261,294
Sep 24, 20261,346
Sep 23, 20261,169
Sep 22, 20261,171
Sep 21, 20261,158
Sep 20, 2026989
Sep 19, 2026780
Sep 18, 2026506
Sep 17, 2026340
Sep 16, 2026391
Sep 15, 2026370
Sep 14, 2026367
Sep 13, 2026327
Sep 12, 2026343
Sep 11, 2026456
Sep 10, 2026669
Sep 9, 2026794
Sep 8, 2026938
Sep 7, 20261,128
Sep 6, 20261,380

Each point is npm's rolling 7-day download count.

Health Score

54/100Good

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/611765581-swagger.svg)](https://payloaddirectory.dev/plugins/611765581-swagger)
HTML
<a href="https://payloaddirectory.dev/plugins/611765581-swagger"><img src="https://payloaddirectory.dev/badge/611765581-swagger.svg" alt="Payload Directory"></a>
Updated Jul 17, 2024Created Mar 9, 2023Published Jul 15, 2023

README

created by snyk downloads npm version license

payload-swagger

Swagger plugin for payload cms:

  • generate openAPI 3 documentation from your payload config
  • adds routes for:
    • openAPI document
    • Swagger UI
    • LICENSE file, if available

payload-swagger uses payload-openapi, which documents ALL your payload openapi endpoints and includes:

  • collection endpoints
  • global endpoints
  • custom endpoints
  • authentication endpoints
  • preferences endpoints
  • fully typed schema definitions for all requests, parameters and responses
  • authentication requirements for all your endpoints
  • extension points to merge your custom openapi definitions into the schema

Alternatives:

Installation

With yarn:

yarn add payload-swagger

With npm:

npm install payload-swagger

Usage

import { buildConfig } from 'payload/config';
import path from 'path';
import { swagger } from 'payload-swagger';

export default buildConfig({
  plugins: [
    swagger({
      /* see options section */
    }),
  ],
  // The rest of your config goes here
});

Extend the openAPI document

The openAPI document generated by payload-swagger includes information from the following external sources:

  • package.json: payload-swagger uses the information in the fields shown below. In openapi a partial openapi document can be included;
{
  "name": "my payload cms",
  "version": "1.2.3",
  "description": "My awesome cms",
  "license": "MIT",
  "openapi": {}
}
  • .openapi: optional file with partial openapi document in JSON format.

The data from all these source is recursively merged into the final document.

Options

payload-swagger has the following options:

interface Options {
  /**
   * By default the access functions on all collections in the config are called to determine
   * the access level of the operations.
   * Provide an array of collection slugs to disable this for the given collections,
   * or `true` to disable for all.
   */
  disableAccessAnalysis?: boolean | string[];

  /**
   * Exclude parts of the payload config from the openapi document
   */
  exclude?: {
    authPaths?: boolean;
    authCollection?: boolean;
    passwordRecovery: boolean; // default true, set to `false` to include
    preferences?: boolean; // default true, set to `false` to include
    custom?: boolean;
  };

  /**
   * Customize the payload-swagger routes
   */
  routes?: {
    /**
     * Swagger ui route
     * @default /api-docs
     */
    swagger?: string;
    /**
     * Openapi specs route
     * @default /api-docs/specs
     */
    specs?: string;
    /**
     * License route (requires LICENSE file in root of repository or explicit license url in openapi document)
     * @default /api-docs/license
     */
    license?: string;
  };

  /**
   * Swagger ui options (see swagger-ui documentation)
   */
  ui?: Omit<SwaggerUiOptions, 'swaggerUrl' | 'swaggerUrls'>;

  /**
   * Throw on error
   * @default false
   * @description If set to true, the plugin will throw the error if any error occurs while generating the openapi document, causing Payload to fail to start.
   */
  throwOnError?: boolean;
}

Descriptions

Endpoint descriptions and summaries are generated from the description and label/labels.{singular/plural} fields of your globals and collections. If a multilanguage (`Record<string, string>) value is found, the order of priority for picking the language is as follows:

  • docs
  • en
  • first value found that is a string

This makes it possible to set a custom description for the docs:

import { CollectionConfig } from 'payload/types';

const Media: CollectionConfig = {
  slug: 'media',
  admin: {
    description: {
      docs: 'Description used in openapi docs',
      en: 'Description used in the admin panel',
    },
  },
  labels: {
    singular: 'Single value used everywhere',
    plural: {
      docs: 'Plural value used in docs',
      en: 'Plural value used in admin panel',
    },
  },
  // ... Rest of collection config
};

Documentation of custom endpoints

Custom endpoints on all levels can be fully documented, either by using the defineEndpoint helper:

import { CollectionConfig } from 'payload/types';
import { defineEndpoint } from 'payload-swagger';

const Post: CollectionConfig = {
  slug: 'posts',
  endpoints: [
    defineEndpoint({
      summary: 'media by category',
      description: 'gets the media of the given category',
      path: '/category/:category',
      method: 'get',
      responseSchema: 'posts',
      errorResponseSchemas: {
        404: 'error',
      },
      queryParameters: {
        limit: { schema: { type: 'number' } },
        page: { schema: { type: 'number' } },
        sort: { scema: { type: 'string' } },
      },
      handler: (req, res) => {
        // ... handler implementation
      },
    }),
  ],
  // ... Rest of collection config
};

or by setting the custom.openapi property:

import { CollectionConfig } from 'payload/types';
import type { EndpointDocumentation } from 'payload-swagger';

const documentation: EndpointDocumentation = {
  summary: 'media by category',
  description: 'gets the media of the given category',
  responseSchema: 'posts',
  errorResponseSchemas: {
    404: { type: 'object', properties: { message: { type: 'string' } }, required: ['message'] },
  },
  queryParameters: {
    limit: { schema: { type: 'number' } },
    page: { schema: { type: 'number' } },
    sort: { scema: { type: 'string' } },
  },
};

const Post: CollectionConfig = {
  slug: 'posts',
  endpoints: [
    {
      path: '/category/:category',
      method: 'get',
      handler: (req, res) => {
        // ... handler implementation
      },
      custom: {
        openapi: documentation,
      },
    },
  ],
  // ... Rest of collection config
};

Response schemas can be defined as openapi schema objects or as string, referencing any of the schemas defined in the schema section of the openapi document.

To exclude a custom endpoint from the documentation, set the custom.openapi property to false.

Adding examples

Examples can be added on collections and globals using the defineCollection or defineGlobal helper:

import { defineCollection } from 'payload-openapi';

const Categories = defineCollection({
  slug: 'categories',
  example: {
    name: 'Example Category',
    archived: false,
  },
  // ... Rest of collection config
});

Or by setting the custom.openapi property:

import { CollectionConfig } from 'payload/types';

const Categories: CollectionConfig = {
  slug: 'categories',
  custom: {
    openapi: {
      example: {
        name: 'Example Category',
        archived: false,
      },
    },
  },
  // ... Rest of collection config
};

It's also possible to set multiple examples:

import { defineCollection } from 'payload-openapi';

const Categories = defineCollection({
  slug: 'categories',
  examples: {
    active: {
      value: {
        name: 'Active Category',
        archived: false,
      },
      summary: 'Example of an active category',
    },
    archive: {
      value: {
        name: 'Archived Category',
        archived: true,
      },
      summary: 'Example of an archived category',
    },
  },
  // ... Rest of collection config
});

Openapi supports examples at lots of different levels. To add examples add other levels, you can define theme in a separate partial openapi document, as described here.

Excluding unused endpoints

In Payload collections and globals have a standard set of available endpoints. In some situations you might not want to use some of these endpoints. In those situations you probably have used an access method that looks something like this: () => false;. This blocks all traffic, but the endpoint is still part of the openapi documentation.

To also remove the endpoint from the openapi documentation, you can use payload-rbac:

import { CollectionConfig } from 'payload/types';
import { blockAll } from 'payload-rbac';

const Media: CollectionConfig: {
  slug: 'media',
  access: {
    delete: blockAll(), // Use block all to exclude endpoint from docs
  },
  // ... Rest of collection config
}

Version history

See changelog

Similar plugins