@seshuk/payload-plugin-openapi 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -539
- package/dist/index.d.ts +1 -1
- package/dist/options.js +1 -0
- package/dist/spec/buildDocument.js +39 -5
- package/dist/spec/components.d.ts +0 -2
- package/dist/spec/components.js +1 -16
- package/dist/spec/downconvert.d.ts +1 -1
- package/dist/spec/downconvert.js +27 -8
- package/dist/spec/entitySchemas.js +36 -32
- package/dist/spec/filters.js +12 -3
- package/dist/spec/params.d.ts +9 -1
- package/dist/spec/params.js +93 -1
- package/dist/spec/paths/auth.js +52 -63
- package/dist/spec/paths/collections.d.ts +3 -1
- package/dist/spec/paths/collections.js +42 -25
- package/dist/spec/paths/globals.d.ts +3 -1
- package/dist/spec/paths/globals.js +23 -13
- package/dist/spec/paths/versions.d.ts +2 -1
- package/dist/spec/paths/versions.js +14 -5
- package/dist/spec/security.d.ts +19 -0
- package/dist/spec/security.js +96 -0
- package/dist/translations/locales/ar.js +5 -0
- package/dist/translations/locales/az.js +5 -0
- package/dist/translations/locales/bg.js +5 -0
- package/dist/translations/locales/bnBd.js +5 -0
- package/dist/translations/locales/bnIn.js +5 -0
- package/dist/translations/locales/ca.js +5 -0
- package/dist/translations/locales/cs.js +5 -0
- package/dist/translations/locales/da.js +5 -0
- package/dist/translations/locales/de.js +5 -0
- package/dist/translations/locales/en.js +5 -0
- package/dist/translations/locales/es.js +5 -0
- package/dist/translations/locales/et.js +5 -0
- package/dist/translations/locales/fa.js +5 -0
- package/dist/translations/locales/fr.js +5 -0
- package/dist/translations/locales/he.js +5 -0
- package/dist/translations/locales/hr.js +5 -0
- package/dist/translations/locales/hu.js +5 -0
- package/dist/translations/locales/hy.js +5 -0
- package/dist/translations/locales/id.js +5 -0
- package/dist/translations/locales/is.js +5 -0
- package/dist/translations/locales/it.js +5 -0
- package/dist/translations/locales/ja.js +5 -0
- package/dist/translations/locales/ko.js +5 -0
- package/dist/translations/locales/lt.js +5 -0
- package/dist/translations/locales/lv.js +5 -0
- package/dist/translations/locales/my.js +5 -0
- package/dist/translations/locales/nb.js +5 -0
- package/dist/translations/locales/nl.js +5 -0
- package/dist/translations/locales/pl.js +5 -0
- package/dist/translations/locales/pt.js +5 -0
- package/dist/translations/locales/ro.js +5 -0
- package/dist/translations/locales/rs.js +5 -0
- package/dist/translations/locales/rsLatin.js +5 -0
- package/dist/translations/locales/ru.js +5 -0
- package/dist/translations/locales/sk.js +5 -0
- package/dist/translations/locales/sl.js +5 -0
- package/dist/translations/locales/sv.js +5 -0
- package/dist/translations/locales/ta.js +5 -0
- package/dist/translations/locales/th.js +5 -0
- package/dist/translations/locales/tr.js +5 -0
- package/dist/translations/locales/uk.js +5 -0
- package/dist/translations/locales/vi.js +5 -0
- package/dist/translations/locales/zh.js +5 -0
- package/dist/translations/locales/zhTw.js +5 -0
- package/dist/translations/types.d.ts +5 -0
- package/dist/types.d.ts +9 -0
- package/package.json +39 -24
package/README.md
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
<div align="center">
|
|
2
2
|
|
|
3
|
+
<picture>
|
|
4
|
+
<img src="docs/logo.svg" alt="OpenAPI Plugin for Payload CMS" height="80" />
|
|
5
|
+
</picture>
|
|
6
|
+
|
|
3
7
|
<h1>OpenAPI Plugin for Payload CMS</h1>
|
|
4
8
|
|
|
5
|
-
<p>OpenAPI 3.0/3.1/3.2
|
|
9
|
+
<p>Generate an OpenAPI 3.0/3.1/3.2 specification from your Payload config and serve it with Scalar or Swagger UI.</p>
|
|
6
10
|
|
|
7
|
-
<a href="https://www.npmjs.com/package/@seshuk/payload-plugin-openapi"><img src="https://img.shields.io/npm/v/@seshuk/payload-plugin-openapi?style=flat-square&logo=npm" alt="npm version" /></a>
|
|
8
|
-
<a href="https://www.npmjs.com/package/@seshuk/payload-plugin-openapi"><img src="https://img.shields.io/npm/dm/@seshuk/payload-plugin-openapi?style=flat-square" alt="npm downloads" /></a>
|
|
9
11
|
<a href="https://github.com/maximseshuk/payload-plugin-openapi/releases/"><img src="https://img.shields.io/github/v/release/maximseshuk/payload-plugin-openapi?style=flat-square&logo=github" alt="GitHub release" /></a>
|
|
12
|
+
<a href="https://www.npmjs.com/package/@seshuk/payload-plugin-openapi"><img src="https://img.shields.io/npm/v/@seshuk/payload-plugin-openapi?style=flat-square&logo=npm" alt="npm version" /></a>
|
|
13
|
+
<a href="https://www.npmjs.com/package/@seshuk/payload-plugin-openapi"><img src="https://img.shields.io/npm/dm/@seshuk/payload-plugin-openapi?style=flat-square&logo=npm" alt="npm downloads" /></a>
|
|
14
|
+
<a href="https://github.com/maximseshuk/payload-plugin-openapi/actions/workflows/ci.yml"><img src="https://img.shields.io/github/actions/workflow/status/maximseshuk/payload-plugin-openapi/ci.yml?style=flat-square&logo=github" alt="CI" /></a>
|
|
15
|
+
<a href="https://payload-plugin-openapi.seshuk.im/"><img src="https://img.shields.io/badge/docs-payload--plugin--openapi.seshuk.im-blue?style=flat-square&logo=readthedocs&logoColor=white" alt="Documentation" /></a>
|
|
10
16
|
<a href="https://github.com/maximseshuk/payload-plugin-openapi/blob/main/LICENSE"><img src="https://img.shields.io/github/license/maximseshuk/payload-plugin-openapi?style=flat-square" alt="license" /></a>
|
|
11
17
|
<a href="https://ko-fi.com/V7V61UCT39"><img src="https://img.shields.io/badge/Ko--fi-Buy_me_a_coffee-ff5f5f?style=flat-square&logo=ko-fi&logoColor=white" alt="Ko-fi" /></a>
|
|
12
18
|
|
|
@@ -14,63 +20,36 @@
|
|
|
14
20
|
|
|
15
21
|
## Features
|
|
16
22
|
|
|
17
|
-
- Full spec from your config — collections, globals, auth, versions, and jobs
|
|
18
|
-
-
|
|
19
|
-
-
|
|
20
|
-
-
|
|
21
|
-
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
- [Plugin Options](#plugin-options)
|
|
32
|
-
- [Metadata](#metadata)
|
|
33
|
-
- [OpenAPI Version](#openapi-version)
|
|
34
|
-
- [Filters](#filters)
|
|
35
|
-
- [Interactive Auth](#interactive-auth)
|
|
36
|
-
- [Caching](#caching)
|
|
37
|
-
- [Docs UI](#docs-ui)
|
|
38
|
-
- [Documenting Custom Endpoints](#documenting-custom-endpoints)
|
|
39
|
-
- [Annotating fields](#annotating-fields)
|
|
40
|
-
- [For plugin authors](#for-plugin-authors)
|
|
41
|
-
- [Extensions](#extensions)
|
|
42
|
-
- [Writing to a File](#writing-to-a-file)
|
|
43
|
-
- [Generate only, no runtime endpoint](#generate-only-no-runtime-endpoint)
|
|
44
|
-
- [Serving a pre-generated file in the docs UI](#serving-a-pre-generated-file-in-the-docs-ui)
|
|
45
|
-
- [Programmatic Use](#programmatic-use)
|
|
46
|
-
- [Internationalization](#internationalization)
|
|
47
|
-
- [Exports](#exports)
|
|
48
|
-
- [License](#license)
|
|
49
|
-
- [Related Plugins](#related-plugins)
|
|
50
|
-
|
|
51
|
-
## Requirements
|
|
52
|
-
|
|
53
|
-
- Payload `^3.53.0`
|
|
54
|
-
- Node.js `>=20`
|
|
55
|
-
|
|
56
|
-
## Installation
|
|
23
|
+
- **Full spec from your config** — collections, globals, auth, versions, and jobs are documented with zero annotation.
|
|
24
|
+
- **Interactive docs included** — mount Scalar or Swagger UI, or both on different paths.
|
|
25
|
+
- **Native metadata** — document custom endpoints and refine field schemas through Payload's own `custom.openapi` key. No wrapper, no separate registry.
|
|
26
|
+
- **Precise filtering** — choose exactly which entities and operations end up in the spec.
|
|
27
|
+
- **Security marking** — every operation is marked public or secured by probing your access functions, with per-entity and document-wide overrides.
|
|
28
|
+
- **Localized** — descriptions resolve through your Payload i18n, and the UI ships translations for 44 locales.
|
|
29
|
+
- **File generation** — write the spec to disk with `payload openapi:generate` for CI, schema diffs, or client codegen.
|
|
30
|
+
- **One option for the spec version** — serve OpenAPI 3.0, 3.1, or 3.2 from the same config.
|
|
31
|
+
|
|
32
|
+
## Quick start
|
|
33
|
+
|
|
34
|
+
Requires **Payload CMS 3.53.0 or later** and **Node.js 20 or later**.
|
|
35
|
+
|
|
36
|
+
### Install
|
|
57
37
|
|
|
58
38
|
```bash
|
|
59
|
-
pnpm add @seshuk/payload-plugin-openapi
|
|
60
|
-
# or
|
|
61
39
|
npm install @seshuk/payload-plugin-openapi
|
|
62
|
-
# or
|
|
63
40
|
yarn add @seshuk/payload-plugin-openapi
|
|
41
|
+
pnpm add @seshuk/payload-plugin-openapi
|
|
64
42
|
```
|
|
65
43
|
|
|
66
|
-
|
|
44
|
+
### Configure
|
|
67
45
|
|
|
68
|
-
|
|
69
|
-
|
|
46
|
+
Add the plugin to your Payload config, plus a docs UI renderer:
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
70
49
|
import { buildConfig } from 'payload'
|
|
50
|
+
import { openapi, scalar } from '@seshuk/payload-plugin-openapi'
|
|
71
51
|
|
|
72
52
|
export default buildConfig({
|
|
73
|
-
// ...
|
|
74
53
|
plugins: [
|
|
75
54
|
openapi({
|
|
76
55
|
metadata: {
|
|
@@ -83,513 +62,50 @@ export default buildConfig({
|
|
|
83
62
|
})
|
|
84
63
|
```
|
|
85
64
|
|
|
86
|
-
|
|
65
|
+
Two endpoints are now live, both relative to your API route (`/api` by default):
|
|
87
66
|
|
|
88
67
|
- `GET /api/openapi.json` — the generated OpenAPI document
|
|
89
|
-
- `GET /api/docs` — interactive API reference
|
|
90
|
-
|
|
91
|
-
Both paths are mounted under your Payload API route (`routes.api`, `/api` by default), since the plugin registers them as Payload endpoints. The `path` and `specEndpoint` options you pass are relative to that route.
|
|
92
|
-
|
|
93
|
-
Prefer Swagger UI? Swap `scalar()` for `swaggerUi()` — or mount both on different paths.
|
|
94
|
-
|
|
95
|
-
> [!NOTE]
|
|
96
|
-
> `metadata.title` and `metadata.version` are required. The plugin throws on boot if either is missing.
|
|
97
|
-
|
|
98
|
-
## Configuration
|
|
99
|
-
|
|
100
|
-
### Plugin Options
|
|
101
|
-
|
|
102
|
-
| Option | Type | Default | Description |
|
|
103
|
-
| ----------------- | ------------------------- | ----------------- | -------------------------------------------------------------------------------------------------------------------------- |
|
|
104
|
-
| `metadata` | `OpenApiMetadata` | — | API title, version, and description. **Required.** |
|
|
105
|
-
| `openapiVersion` | `'3.0' \| '3.1' \| '3.2'` | `'3.2'` | Spec version to serve |
|
|
106
|
-
| `specEndpoint` | `string` | `'/openapi.json'` | Path the spec is served from (relative to the API route) |
|
|
107
|
-
| `enabled` | `boolean` | `true` | Set `false` to disable the plugin entirely |
|
|
108
|
-
| `serve` | `boolean` | `true` | Set `false` to register only the CLI generator and serve nothing over HTTP ([details](#generate-only-no-runtime-endpoint)) |
|
|
109
|
-
| `filters` | `FilterOptions` | see below | Which entities and operations to document ([details](#filters)) |
|
|
110
|
-
| `interactiveAuth` | `boolean \| { endpoint }` | `false` | Username/password login for the docs UI |
|
|
111
|
-
| `nestedTags` | `boolean` | `false` | Emit an OpenAPI 3.2 nested tag hierarchy (see below) |
|
|
112
|
-
| `cache` | `boolean` | `true` | Cache the built document for the life of the process |
|
|
113
|
-
| `extensions` | `OpenApiExtension[]` | `[]` | Inject paths, components, tags, or transform the document |
|
|
114
|
-
|
|
115
|
-
### Metadata
|
|
116
|
-
|
|
117
|
-
```ts
|
|
118
|
-
openapi({
|
|
119
|
-
metadata: {
|
|
120
|
-
title: 'My API',
|
|
121
|
-
version: '1.0.0',
|
|
122
|
-
description: 'Public API for the example app.',
|
|
123
|
-
},
|
|
124
|
-
})
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
### OpenAPI Version
|
|
128
|
-
|
|
129
|
-
The document is always built as 3.2. The `openapiVersion` option controls what gets served:
|
|
130
|
-
|
|
131
|
-
| Version | Behavior |
|
|
132
|
-
| ------- | ----------------------------------------------------------------------------- |
|
|
133
|
-
| `'3.2'` | Served as built (default) |
|
|
134
|
-
| `'3.1'` | A downconversion pass strips 3.2-only fields |
|
|
135
|
-
| `'3.0'` | A downconversion pass to 3.0 (e.g. nullable handling, `examples` → `example`) |
|
|
136
|
-
|
|
137
|
-
> [!NOTE]
|
|
138
|
-
> Scalar and Swagger UI don't render OpenAPI 3.2 nested tags yet. The `nestedTags` option is **off by default**, so `doc.tags` is a flat list of per-entity tags with descriptions. Turn it on only if your consumer understands the 3.2 `kind`/`parent` tag tree.
|
|
139
|
-
|
|
140
|
-
### Filters
|
|
141
|
-
|
|
142
|
-
`filters` decides what ends up in the spec: which entities are documented, which Payload-internal collections show up, which endpoint groups are generated, and which operations are dropped. It's one flat object.
|
|
143
|
-
|
|
144
|
-
| Option | Type | Default | Description |
|
|
145
|
-
| ------------------- | ------------------ | ------- | ------------------------------------------------------------------------------- |
|
|
146
|
-
| `include` | `EntityMatcher[]` | `[]` | Allowlist. When non-empty, only matching entities are documented |
|
|
147
|
-
| `exclude` | `EntityMatcher[]` | `[]` | Entities to leave out entirely (schemas and paths) |
|
|
148
|
-
| `includeHidden` | `boolean` | `false` | Include collections flagged `hidden` / `admin.hidden` |
|
|
149
|
-
| `includeSystem` | `boolean` | `false` | Include Payload-internal collections (`payload-jobs`, `payload-preferences`, …) |
|
|
150
|
-
| `includeCustom` | `boolean` | `true` | Document endpoints carrying `custom.openapi` metadata |
|
|
151
|
-
| `includeAuth` | `boolean` | `true` | Document auth operations (login / logout / me / …) |
|
|
152
|
-
| `includeAdminAuth` | `boolean` | `false` | Document admin/bootstrap auth endpoints (`/init`, `/access`, first-register) |
|
|
153
|
-
| `includeVersions` | `boolean` | `true` | Document version operations (`/versions`, `/versions/{id}`) |
|
|
154
|
-
| `includeJobs` | `boolean` | `true` | Document jobs endpoints (`/payload-jobs/run`, `/handle-schedules`) |
|
|
155
|
-
| `excludeOperations` | `OperationRule[]` | `[]` | Per-operation removal rules |
|
|
156
|
-
| `excludeWhen` | `(ctx) => boolean` | — | Escape hatch: return `true` to drop an operation |
|
|
157
|
-
|
|
158
|
-
#### Choosing entities with `include` and `exclude`
|
|
159
|
-
|
|
160
|
-
These two lists decide which collections and globals are documented. Each entry is one of three things:
|
|
161
|
-
|
|
162
|
-
| Matcher | Matches | Example |
|
|
163
|
-
| ---------------- | -------------------------------------------------------------- | --------------------------------- |
|
|
164
|
-
| a string | one entity by its exact slug | `'posts'` |
|
|
165
|
-
| a `RegExp` | every entity whose slug matches the pattern | `/^marketing-/` |
|
|
166
|
-
| `{ kind, slug }` | one entity, when a collection and a global share the same slug | `{ kind: 'global', slug: 'nav' }` |
|
|
167
|
-
|
|
168
|
-
The rules are:
|
|
169
|
-
|
|
170
|
-
- `include` is an allowlist. Leave it empty and everything is documented; add anything and only matching entities survive.
|
|
171
|
-
- `exclude` always wins. If an entity matches both lists, it's dropped.
|
|
172
|
-
|
|
173
|
-
**Document only a few collections.** With `include` set, nothing else makes it into the spec:
|
|
174
|
-
|
|
175
|
-
```ts
|
|
176
|
-
filters: {
|
|
177
|
-
include: ['posts', 'media', 'categories'],
|
|
178
|
-
}
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
**Document everything except a couple of entities:**
|
|
182
|
-
|
|
183
|
-
```ts
|
|
184
|
-
filters: {
|
|
185
|
-
exclude: ['audit-log', 'internal-settings'],
|
|
186
|
-
}
|
|
187
|
-
```
|
|
188
|
-
|
|
189
|
-
**Match a group of slugs with a regular expression.** The pattern is tested against the slug, so this keeps every collection whose slug starts with `public-`:
|
|
190
|
-
|
|
191
|
-
```ts
|
|
192
|
-
filters: {
|
|
193
|
-
// public-posts, public-media, public-authors … all kept; everything else dropped.
|
|
194
|
-
include: [/^public-/],
|
|
195
|
-
}
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
A few more patterns, for reference:
|
|
199
|
-
|
|
200
|
-
- `/^public-/` — slug starts with `public-`
|
|
201
|
-
- `/-draft$/` — slug ends with `-draft`
|
|
202
|
-
- `/^(posts|pages)$/` — slug is exactly `posts` or `pages`
|
|
203
|
-
- `/internal/i` — slug contains `internal`, case-insensitive
|
|
204
|
-
|
|
205
|
-
**Disambiguate a collection from a global.** If you have a collection and a global that share a slug, a plain string would match both. Use `{ kind, slug }` to target one:
|
|
206
|
-
|
|
207
|
-
```ts
|
|
208
|
-
filters: {
|
|
209
|
-
// Keep the `settings` collection, drop the `settings` global.
|
|
210
|
-
exclude: [{ kind: 'global', slug: 'settings' }],
|
|
211
|
-
}
|
|
212
|
-
```
|
|
213
|
-
|
|
214
|
-
**Mix them.** `include` narrows the set first, then `exclude` removes from what's left:
|
|
215
|
-
|
|
216
|
-
```ts
|
|
217
|
-
filters: {
|
|
218
|
-
// Document every `public-*` collection, but never the drafts one.
|
|
219
|
-
include: [/^public-/],
|
|
220
|
-
exclude: ['public-drafts'],
|
|
221
|
-
}
|
|
222
|
-
```
|
|
223
|
-
|
|
224
|
-
`include` and `exclude` only apply to your own collections and globals. Hidden and Payload-internal collections are governed by `includeHidden` and `includeSystem`, which run first — adding `payload-jobs` to `include` won't surface it unless `includeSystem` is on.
|
|
225
|
-
|
|
226
|
-
#### Dropping individual operations
|
|
227
|
-
|
|
228
|
-
`include` / `exclude` work at the entity level. To remove specific operations (a single method on one collection, every `DELETE`, anything under a path prefix), use `excludeOperations` or `excludeWhen`.
|
|
229
|
-
|
|
230
|
-
Each rule in `excludeOperations` is a set of conditions. Within one rule every field you set must match (AND). Across rules, an operation is dropped if any rule matches it. Leave a field out and it matches anything.
|
|
231
|
-
|
|
232
|
-
```ts
|
|
233
|
-
filters: {
|
|
234
|
-
excludeOperations: [
|
|
235
|
-
// Drop every DELETE, on any entity.
|
|
236
|
-
{ method: 'delete' },
|
|
237
|
-
// Drop writes to `posts`, but leave its reads alone.
|
|
238
|
-
{ slug: 'posts', method: ['post', 'patch', 'put'] },
|
|
239
|
-
// Drop anything matching a path, regardless of method or entity.
|
|
240
|
-
{ path: /\/preview$/ },
|
|
241
|
-
],
|
|
242
|
-
}
|
|
243
|
-
```
|
|
244
|
-
|
|
245
|
-
`excludeWhen` is the escape hatch for logic that doesn't fit a rule. It runs after `excludeOperations` and gets the method, path, slug, and kind for each operation:
|
|
246
|
-
|
|
247
|
-
```ts
|
|
248
|
-
filters: {
|
|
249
|
-
excludeWhen: ({ path, method }) => path.includes('/internal/') || method === 'put',
|
|
250
|
-
}
|
|
251
|
-
```
|
|
252
|
-
|
|
253
|
-
### Interactive Auth
|
|
254
|
-
|
|
255
|
-
By default the docs UI expects you to paste a bearer token. Turn on `interactiveAuth` to add a token endpoint and a matching security scheme, so Scalar/Swagger can show an **Authorize** dialog where users log in with their Payload credentials:
|
|
256
|
-
|
|
257
|
-
```ts
|
|
258
|
-
openapi({
|
|
259
|
-
metadata: { title: 'My API', version: '1.0.0' },
|
|
260
|
-
interactiveAuth: true, // token endpoint at /api/openapi-auth
|
|
261
|
-
// interactiveAuth: { endpoint: '/login' }, // or a custom path → /api/login
|
|
262
|
-
})
|
|
263
|
-
```
|
|
264
|
-
|
|
265
|
-
The endpoint logs in against your first auth-enabled collection (falling back to `users`) and returns the JWT. It accepts the username as either `email` or `username`, matching how your collection is configured.
|
|
266
|
-
|
|
267
|
-
> [!WARNING]
|
|
268
|
-
> The interactive auth endpoint exchanges credentials for a live JWT. Only enable it on docs you intend real users to authenticate against, and serve them over HTTPS.
|
|
269
|
-
|
|
270
|
-
### Caching
|
|
271
|
-
|
|
272
|
-
The Payload config is static after boot, so the document is identical on every request apart from the server URL (which is always filled in fresh). Caching is on by default. Disable it in development so edits to your config show up without a restart:
|
|
273
|
-
|
|
274
|
-
```ts
|
|
275
|
-
openapi({
|
|
276
|
-
metadata: { title: 'My API', version: '1.0.0' },
|
|
277
|
-
cache: process.env.NODE_ENV === 'production',
|
|
278
|
-
})
|
|
279
|
-
```
|
|
280
|
-
|
|
281
|
-
## Docs UI
|
|
282
|
-
|
|
283
|
-
The plugin ships two UI renderers. Each is a separate Payload plugin you add alongside `openapi()` — mount one, or both on different paths:
|
|
284
|
-
|
|
285
|
-
```ts
|
|
286
|
-
import { openapi, scalar, swaggerUi } from '@seshuk/payload-plugin-openapi'
|
|
287
|
-
|
|
288
|
-
plugins: [
|
|
289
|
-
openapi({ metadata: { title: 'My API', version: '1.0.0' } }),
|
|
290
|
-
scalar(), // Scalar at /api/docs
|
|
291
|
-
swaggerUi({ path: '/swagger' }), // Swagger UI at /api/swagger
|
|
292
|
-
]
|
|
293
|
-
```
|
|
294
|
-
|
|
295
|
-
Both renderers accept the same options:
|
|
68
|
+
- `GET /api/docs` — the interactive API reference
|
|
296
69
|
|
|
297
|
-
|
|
298
|
-
| --------------- | ------------------------- | --------------------------- | --------------------------------------------------------------------------------- |
|
|
299
|
-
| `path` | `string` | `'/docs'` | Where the docs UI is served, relative to the API route (so `/docs` → `/api/docs`) |
|
|
300
|
-
| `specEndpoint` | `string` | `'<apiRoute>/openapi.json'` | URL the UI fetches the document from |
|
|
301
|
-
| `cdnBase` | `string` | official jsDelivr package | CDN base for the UI's assets |
|
|
302
|
-
| `configuration` | `Record<string, unknown>` | `{}` | Extra config merged into the library's init options |
|
|
303
|
-
| `enabled` | `boolean` | `true` | Set `false` to skip mounting the UI |
|
|
70
|
+
`metadata.title` and `metadata.version` are required; the plugin throws at boot without them. Prefer Swagger UI? Swap `scalar()` for `swaggerUi()`, or mount both on different paths.
|
|
304
71
|
|
|
305
|
-
|
|
72
|
+
The document is built lazily from the fully sanitized config, so collections, fields, and endpoints added by other plugins are picked up regardless of plugin order.
|
|
306
73
|
|
|
307
|
-
|
|
308
|
-
scalar({
|
|
309
|
-
configuration: {
|
|
310
|
-
theme: 'purple',
|
|
311
|
-
hideDownloadButton: true,
|
|
312
|
-
},
|
|
313
|
-
})
|
|
314
|
-
```
|
|
315
|
-
|
|
316
|
-
## Documenting Custom Endpoints
|
|
317
|
-
|
|
318
|
-
Any custom Payload endpoint with `custom.openapi` metadata is picked up automatically (as long as `filters.includeCustom` stays on). The metadata is a standard OpenAPI [Operation Object](https://spec.openapis.org/oas/v3.1.0#operation-object):
|
|
319
|
-
|
|
320
|
-
```ts
|
|
321
|
-
import type { Endpoint } from 'payload'
|
|
322
|
-
|
|
323
|
-
const healthEndpoint: Endpoint = {
|
|
324
|
-
path: '/health',
|
|
325
|
-
method: 'get',
|
|
326
|
-
handler: () => Response.json({ ok: true }),
|
|
327
|
-
custom: {
|
|
328
|
-
openapi: {
|
|
329
|
-
summary: 'Health check',
|
|
330
|
-
tags: ['System'],
|
|
331
|
-
responses: {
|
|
332
|
-
'200': { description: 'Service is healthy' },
|
|
333
|
-
},
|
|
334
|
-
},
|
|
335
|
-
},
|
|
336
|
-
}
|
|
337
|
-
```
|
|
338
|
-
|
|
339
|
-
Endpoints are collected from the top-level config (`/api/health`), from collections (`/api/<slug>/...`), and from globals (`/api/globals/<slug>/...`). Path params written `:id` are normalized to `{id}` for you.
|
|
340
|
-
|
|
341
|
-
`custom.openapi` is the convention shown in Payload's own [agent skills](https://github.com/payloadcms/payload/blob/main/tools/claude-plugin/skills/payload/reference/ENDPOINTS.md#openapi-documentation). The plugin reads exactly that shape, so the metadata you write stays a plain OpenAPI Operation Object on the endpoint where it belongs — no plugin-specific wrapper, no separate registry to keep in sync. Endpoints already documented this way are picked up with zero changes.
|
|
342
|
-
|
|
343
|
-
### Annotating fields
|
|
344
|
-
|
|
345
|
-
The same `custom.openapi` convention works on individual fields. The plugin already infers each field's schema from its Payload type; anything you put under `custom.openapi` is merged on top of that schema, so you can add a `description`, an `example`, `format`, constraints, or override what was inferred:
|
|
346
|
-
|
|
347
|
-
```ts
|
|
348
|
-
import type { Field } from 'payload'
|
|
349
|
-
|
|
350
|
-
const slug: Field = {
|
|
351
|
-
name: 'slug',
|
|
352
|
-
type: 'text',
|
|
353
|
-
custom: {
|
|
354
|
-
openapi: {
|
|
355
|
-
description: 'URL-safe identifier, lowercase with dashes.',
|
|
356
|
-
pattern: '^[a-z0-9-]+$',
|
|
357
|
-
example: 'hello-world',
|
|
358
|
-
},
|
|
359
|
-
},
|
|
360
|
-
}
|
|
361
|
-
```
|
|
362
|
-
|
|
363
|
-
The merge is deep and your keys win, so `custom.openapi` only changes what you name and leaves the rest of the inferred schema intact. It applies wherever the field appears — read, create, and update schemas alike.
|
|
364
|
-
|
|
365
|
-
`description`, `title`, and `summary` are localizable. Give them a function or a locale-keyed object and they're resolved against the request language, the same way Payload labels are:
|
|
366
|
-
|
|
367
|
-
```ts
|
|
368
|
-
const slug: Field = {
|
|
369
|
-
name: 'slug',
|
|
370
|
-
type: 'text',
|
|
371
|
-
custom: {
|
|
372
|
-
openapi: {
|
|
373
|
-
// a function…
|
|
374
|
-
description: ({ t }) => t('fields:slugHelp'),
|
|
375
|
-
// …or a locale map:
|
|
376
|
-
// description: { en: 'URL-safe identifier', ru: 'URL-совместимый идентификатор' },
|
|
377
|
-
},
|
|
378
|
-
},
|
|
379
|
-
}
|
|
380
|
-
```
|
|
381
|
-
|
|
382
|
-
### For plugin authors
|
|
383
|
-
|
|
384
|
-
If you maintain a Payload plugin that adds its own endpoints, you can make them show up in the spec **without depending on this package** — and without your users having to wire anything up. Attach a `custom.openapi` Operation Object to each endpoint you add, exactly as above:
|
|
385
|
-
|
|
386
|
-
```ts
|
|
387
|
-
import type { Config, Plugin } from 'payload'
|
|
388
|
-
|
|
389
|
-
export const myPlugin =
|
|
390
|
-
(): Plugin =>
|
|
391
|
-
(config: Config): Config => ({
|
|
392
|
-
...config,
|
|
393
|
-
endpoints: [
|
|
394
|
-
...(config.endpoints ?? []),
|
|
395
|
-
{
|
|
396
|
-
path: '/my-feature/sync',
|
|
397
|
-
method: 'post',
|
|
398
|
-
handler: syncHandler,
|
|
399
|
-
custom: {
|
|
400
|
-
// Picked up automatically if the OpenAPI plugin is installed; ignored otherwise.
|
|
401
|
-
openapi: {
|
|
402
|
-
summary: 'Trigger a sync',
|
|
403
|
-
tags: ['My Feature'],
|
|
404
|
-
responses: { '202': { description: 'Sync queued' } },
|
|
405
|
-
},
|
|
406
|
-
},
|
|
407
|
-
},
|
|
408
|
-
],
|
|
409
|
-
})
|
|
410
|
-
```
|
|
411
|
-
|
|
412
|
-
If your plugin adds collections, globals, or fields, the field-level `custom.openapi` from [above](#annotating-fields) works there too — annotate the fields you add and they carry their own documentation into the spec.
|
|
413
|
-
|
|
414
|
-
This works regardless of plugin order. The OpenAPI plugin doesn't read your config when it runs — it builds the document lazily, on request, from the fully sanitized config. By then every plugin has run, so it sees your endpoints and fields no matter where either plugin sits in the `plugins` array. The `custom.openapi` key is a plain convention: if the OpenAPI plugin isn't installed, the metadata is inert and harmless.
|
|
415
|
-
|
|
416
|
-
For documentation that isn't tied to a single endpoint or field — shared components, extra tags, or a post-build transform — your users can pass an [extension](#extensions). That's an opt-in your users wire into their own `openapi({ extensions: [...] })` call; there's no implicit cross-plugin channel to maintain.
|
|
417
|
-
|
|
418
|
-
## Extensions
|
|
419
|
-
|
|
420
|
-
For anything the generator doesn't produce on its own, pass `extensions`. Each extension can add paths, components, and tags, and/or transform the finished document before it's served:
|
|
421
|
-
|
|
422
|
-
```ts
|
|
423
|
-
import type { OpenApiExtension } from '@seshuk/payload-plugin-openapi'
|
|
424
|
-
|
|
425
|
-
const myExtension: OpenApiExtension = {
|
|
426
|
-
paths: {
|
|
427
|
-
'/webhooks/stripe': {
|
|
428
|
-
post: { summary: 'Stripe webhook', responses: { '200': { description: 'OK' } } },
|
|
429
|
-
},
|
|
430
|
-
},
|
|
431
|
-
components: {
|
|
432
|
-
securitySchemes: {
|
|
433
|
-
apiKey: { type: 'apiKey', in: 'header', name: 'X-API-Key' },
|
|
434
|
-
},
|
|
435
|
-
},
|
|
436
|
-
tags: [{ name: 'Webhooks', description: 'Inbound webhooks' }],
|
|
437
|
-
transform: (doc, ctx) => {
|
|
438
|
-
doc.info.termsOfService = 'https://example.com/terms'
|
|
439
|
-
return doc
|
|
440
|
-
},
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
openapi({
|
|
444
|
-
metadata: { title: 'My API', version: '1.0.0' },
|
|
445
|
-
extensions: [myExtension],
|
|
446
|
-
})
|
|
447
|
-
```
|
|
448
|
-
|
|
449
|
-
The `transform` hook receives the full document and a `BuildContext` (locales, API route, active i18n), and must return the document to serve.
|
|
450
|
-
|
|
451
|
-
## Writing to a File
|
|
452
|
-
|
|
453
|
-
The plugin registers a Payload `bin` script under the `openapi:generate` key, so you can write the document to a file without making an HTTP request — handy for committing the spec, running schema diffs in CI, or feeding a client-code generator.
|
|
454
|
-
|
|
455
|
-
```bash
|
|
456
|
-
# Writes openapi.json using the i18n fallback language.
|
|
457
|
-
payload openapi:generate
|
|
458
|
-
|
|
459
|
-
# Pick a language and an output path.
|
|
460
|
-
payload openapi:generate --lang ru --out ./spec/openapi.ru.json
|
|
461
|
-
|
|
462
|
-
# Set the base URL written to the spec's `servers`.
|
|
463
|
-
payload openapi:generate --server https://api.example.com
|
|
464
|
-
|
|
465
|
-
# Write one file per supported language: openapi.<lang>.json
|
|
466
|
-
payload openapi:generate --lang all
|
|
467
|
-
```
|
|
74
|
+
Add `filters` to control what is exposed, `interactiveAuth` for a login dialog in the docs UI, `extensions` for anything the generator doesn't produce on its own — see the [configuration reference](https://payload-plugin-openapi.seshuk.im/configuration/overview).
|
|
468
75
|
|
|
469
|
-
|
|
470
|
-
| ---------- | ------------------------------------------------------------------------------------- |
|
|
471
|
-
| `--lang` | Locale for translated descriptions, or `all` to write one file per supported language |
|
|
472
|
-
| `--out` | Output path for a single-language run (default `openapi.json`) |
|
|
473
|
-
| `--server` | Base URL written to the spec's `servers`. Omit it to leave `servers` empty |
|
|
76
|
+
## Documentation
|
|
474
77
|
|
|
475
|
-
|
|
476
|
-
> The generated spec reflects public-access marking only — it does not run per-user access checks. This matches the HTTP endpoint exactly.
|
|
78
|
+
Full docs are at **<https://payload-plugin-openapi.seshuk.im/>**:
|
|
477
79
|
|
|
478
|
-
|
|
80
|
+
- [Quick start](https://payload-plugin-openapi.seshuk.im/quick-start)
|
|
81
|
+
- [Configuration reference](https://payload-plugin-openapi.seshuk.im/configuration/overview)
|
|
82
|
+
- [Filters](https://payload-plugin-openapi.seshuk.im/configuration/filters)
|
|
83
|
+
- [Security marking](https://payload-plugin-openapi.seshuk.im/configuration/security-marking)
|
|
84
|
+
- [Docs UI — Scalar & Swagger](https://payload-plugin-openapi.seshuk.im/configuration/docs-ui)
|
|
85
|
+
- [Documenting custom endpoints](https://payload-plugin-openapi.seshuk.im/guides/custom-endpoints)
|
|
86
|
+
- [Field metadata](https://payload-plugin-openapi.seshuk.im/guides/field-metadata)
|
|
87
|
+
- [CLI — `openapi:generate`](https://payload-plugin-openapi.seshuk.im/cli/generate)
|
|
88
|
+
- [Examples](https://payload-plugin-openapi.seshuk.im/guides/examples)
|
|
479
89
|
|
|
480
|
-
|
|
90
|
+
## For plugin authors
|
|
481
91
|
|
|
482
|
-
|
|
92
|
+
If you maintain a Payload plugin, attach a `custom.openapi` Operation Object to the endpoints you add and they show up in the generated spec — no dependency on this package, and nothing for your users to wire up. The same key works on fields. If this plugin isn't installed, the metadata is inert. See [For plugin authors](https://payload-plugin-openapi.seshuk.im/guides/plugin-authors).
|
|
483
93
|
|
|
484
|
-
|
|
485
|
-
openapi({
|
|
486
|
-
metadata: { title: 'My API', version: '1.0.0' },
|
|
487
|
-
serve: false, // register the CLI generator only; nothing is served over HTTP
|
|
488
|
-
})
|
|
489
|
-
```
|
|
490
|
-
|
|
491
|
-
The plugin still needs to be in your `plugins` array (that's what registers the `bin` script and the resolved options it reads), but it won't mount the spec or interactive-auth endpoints. Then generate the file wherever you keep static assets:
|
|
492
|
-
|
|
493
|
-
```bash
|
|
494
|
-
payload openapi:generate --server https://api.example.com --out ./public/openapi.json
|
|
495
|
-
```
|
|
496
|
-
|
|
497
|
-
Now serve `public/openapi.json` like any other static file — through Next.js, a CDN, nginx, or by committing it to the repo. There's no per-request work and no way to hit a stale or unauthenticated spec at runtime.
|
|
498
|
-
|
|
499
|
-
### Serving a pre-generated file in the docs UI
|
|
500
|
-
|
|
501
|
-
The docs UI plugins (`scalar`/`swaggerUi`) load the spec from a URL, and that URL doesn't have to be the plugin's own endpoint. Point `specEndpoint` at your static file and the UI renders it directly — no runtime generation involved:
|
|
502
|
-
|
|
503
|
-
```ts
|
|
504
|
-
plugins: [
|
|
505
|
-
openapi({ metadata: { title: 'My API', version: '1.0.0' }, serve: false }),
|
|
506
|
-
scalar({ specEndpoint: '/openapi.json' }), // loads public/openapi.json
|
|
507
|
-
]
|
|
508
|
-
```
|
|
509
|
-
|
|
510
|
-
For multiple languages, generate one file per language (`--lang all`) and wire up a switcher exactly as in [Internationalization](#internationalization) — just point the `sources` at your static files (`/openapi.en.json`, …) instead of the runtime endpoint.
|
|
511
|
-
|
|
512
|
-
> [!TIP]
|
|
513
|
-
> Even with the default runtime endpoint, you're not re-generating on every request. With `cache: true` (the default) the document is built once on first hit and reused for the life of the process — only the server URL is refreshed per request. `serve: false` is for when you want **no** runtime endpoint at all, not merely to avoid rebuild cost.
|
|
514
|
-
|
|
515
|
-
## Programmatic Use
|
|
516
|
-
|
|
517
|
-
Need the document in your own code? `buildOpenApiDocument` builds it from a live Payload instance, and the downconverters are exported too:
|
|
518
|
-
|
|
519
|
-
```ts
|
|
520
|
-
import { buildOpenApiDocument, toOpenApi30 } from '@seshuk/payload-plugin-openapi'
|
|
521
|
-
|
|
522
|
-
const doc = await buildOpenApiDocument({
|
|
523
|
-
payload, // a BasePayload instance
|
|
524
|
-
options, // resolved plugin options
|
|
525
|
-
language: 'en', // optional
|
|
526
|
-
servers: [{ url: 'https://api.example.com' }],
|
|
527
|
-
})
|
|
528
|
-
|
|
529
|
-
const v30 = toOpenApi30(doc)
|
|
530
|
-
```
|
|
94
|
+
## Related plugins
|
|
531
95
|
|
|
532
|
-
|
|
96
|
+
- **[@seshuk/payload-storage-bunny](https://github.com/maximseshuk/payload-storage-bunny)** — store files and stream video from Payload CMS on Bunny's global CDN.
|
|
97
|
+
- **[@seshuk/payload-plugin-media-preview](https://github.com/maximseshuk/payload-plugin-media-preview)** — preview images, video, audio, and documents directly in the Payload admin panel.
|
|
533
98
|
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
Entity titles and descriptions in the generated spec are resolved through the active request locale. The spec endpoint also honors an explicit `?lang=` query param (when supported), so `GET /api/openapi.json?lang=ru` returns Russian descriptions.
|
|
537
|
-
|
|
538
|
-
To put a language switcher in the docs UI, point Scalar's `sources` at the same runtime endpoint with different `?lang=` values — nothing is pre-generated, each language resolves on request:
|
|
539
|
-
|
|
540
|
-
```ts
|
|
541
|
-
scalar({
|
|
542
|
-
configuration: {
|
|
543
|
-
sources: [
|
|
544
|
-
{ title: 'English', url: '/api/openapi.json?lang=en', default: true },
|
|
545
|
-
{ title: 'Русский', url: '/api/openapi.json?lang=ru' },
|
|
546
|
-
],
|
|
547
|
-
},
|
|
548
|
-
})
|
|
549
|
-
```
|
|
550
|
-
|
|
551
|
-
When `sources` is set, the renderer's own `specEndpoint` is ignored, and the first entry is the default unless another sets `default: true`. Swagger UI has no built-in switcher — for it, mount one instance per language on its own `path`:
|
|
552
|
-
|
|
553
|
-
```ts
|
|
554
|
-
swaggerUi({ path: '/docs/en', specEndpoint: '/api/openapi.json?lang=en' }),
|
|
555
|
-
swaggerUi({ path: '/docs/ru', specEndpoint: '/api/openapi.json?lang=ru' }),
|
|
556
|
-
```
|
|
557
|
-
|
|
558
|
-
The same switcher works against static files instead of the runtime endpoint — see [Serving a pre-generated file in the docs UI](#serving-a-pre-generated-file-in-the-docs-ui).
|
|
559
|
-
|
|
560
|
-
Supported locales: `ar`, `az`, `bg`, `bn` (BD/IN), `ca`, `cs`, `da`, `de`, `en`, `es`, `et`, `fa`, `fr`, `he`, `hr`, `hu`, `hy`, `id`, `is`, `it`, `ja`, `ko`, `lt`, `lv`, `my`, `nb`, `nl`, `pl`, `pt`, `ro`, `rs` (Cyrillic/Latin), `ru`, `sk`, `sl`, `sv`, `ta`, `th`, `tr`, `uk`, `vi`, `zh`, `zhTw`.
|
|
561
|
-
|
|
562
|
-
## Exports
|
|
563
|
-
|
|
564
|
-
Everything is exported from the single main entry point:
|
|
565
|
-
|
|
566
|
-
| Export | Kind | Description |
|
|
567
|
-
| ---------------------- | -------- | ----------------------------------------------- |
|
|
568
|
-
| `openapi` | plugin | The main plugin — generates and serves the spec |
|
|
569
|
-
| `scalar` | plugin | Mounts a Scalar API reference UI |
|
|
570
|
-
| `swaggerUi` | plugin | Mounts a Swagger UI |
|
|
571
|
-
| `buildOpenApiDocument` | function | Build the document from a live Payload instance |
|
|
572
|
-
| `toOpenApi30` | function | Downconvert a 3.2 document to 3.0 |
|
|
573
|
-
| `toOpenApi31` | function | Downconvert a 3.2 document to 3.1 |
|
|
99
|
+
## Support
|
|
574
100
|
|
|
575
|
-
|
|
101
|
+
Bug reports, feature requests, and questions go to [GitHub Issues](https://github.com/maximseshuk/payload-plugin-openapi/issues). For Payload itself, see the [Payload CMS docs](https://payloadcms.com/docs) and [Discord](https://discord.gg/payloadcms).
|
|
576
102
|
|
|
577
103
|
## License
|
|
578
104
|
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
## Related Plugins
|
|
582
|
-
|
|
583
|
-
- **[@seshuk/payload-storage-bunny](https://github.com/maximseshuk/payload-storage-bunny)** — Bunny.net storage adapter for Payload CMS
|
|
584
|
-
- **[@seshuk/payload-plugin-media-preview](https://github.com/maximseshuk/payload-plugin-media-preview)** — Preview images, video, audio, and documents directly in the Payload admin panel
|
|
585
|
-
|
|
586
|
-
## Support
|
|
587
|
-
|
|
588
|
-
- **Bug Reports**: [GitHub Issues](https://github.com/maximseshuk/payload-plugin-openapi/issues)
|
|
589
|
-
- **Questions**: Open a [GitHub Issue](https://github.com/maximseshuk/payload-plugin-openapi/issues) or ask in the [Payload CMS Discord](https://discord.gg/payloadcms)
|
|
105
|
+
MIT — see [LICENSE](LICENSE).
|
|
590
106
|
|
|
591
107
|
## Credits
|
|
592
108
|
|
|
593
|
-
Built
|
|
109
|
+
Built by [Maxim Seshuk](https://github.com/maximseshuk) for the Payload CMS community.
|
|
594
110
|
|
|
595
|
-
If
|
|
111
|
+
If this plugin saves you time, you can [buy me a coffee](https://ko-fi.com/seshuk) ☕
|
package/dist/index.d.ts
CHANGED
|
@@ -6,4 +6,4 @@ export type { BuildOpenApiInput } from './spec/build.js';
|
|
|
6
6
|
export { toOpenApi30, toOpenApi31 } from './spec/downconvert.js';
|
|
7
7
|
export { scalar } from './ui/scalar.js';
|
|
8
8
|
export { swaggerUi } from './ui/swagger.js';
|
|
9
|
-
export type { BuildContext, FilterOptions, OpenApiExtension, OpenApiMetadata, OpenApiPluginOptions, OpenApiVersion, UiPluginOptions, } from './types.js';
|
|
9
|
+
export type { BuildContext, EntityOperation, EntitySecurityOverride, FilterOptions, OpenApiExtension, OpenApiMetadata, OpenApiPluginOptions, OpenApiVersion, OperationContext, UiPluginOptions, } from './types.js';
|