@seshuk/payload-plugin-openapi 0.1.0

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.
Files changed (151) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +595 -0
  3. package/dist/bin/generateSpec.d.ts +25 -0
  4. package/dist/bin/generateSpec.js +61 -0
  5. package/dist/constants.d.ts +1 -0
  6. package/dist/constants.js +2 -0
  7. package/dist/endpoints/interactiveAuth.d.ts +4 -0
  8. package/dist/endpoints/interactiveAuth.js +27 -0
  9. package/dist/endpoints/spec.d.ts +5 -0
  10. package/dist/endpoints/spec.js +39 -0
  11. package/dist/index.d.ts +9 -0
  12. package/dist/index.js +67 -0
  13. package/dist/options.d.ts +2 -0
  14. package/dist/options.js +44 -0
  15. package/dist/spec/build.d.ts +14 -0
  16. package/dist/spec/build.js +36 -0
  17. package/dist/spec/buildDocument.d.ts +15 -0
  18. package/dist/spec/buildDocument.js +234 -0
  19. package/dist/spec/components.d.ts +46 -0
  20. package/dist/spec/components.js +280 -0
  21. package/dist/spec/downconvert.d.ts +3 -0
  22. package/dist/spec/downconvert.js +55 -0
  23. package/dist/spec/entitySchemas.d.ts +15 -0
  24. package/dist/spec/entitySchemas.js +246 -0
  25. package/dist/spec/fields.d.ts +5 -0
  26. package/dist/spec/fields.js +44 -0
  27. package/dist/spec/filters.d.ts +11 -0
  28. package/dist/spec/filters.js +74 -0
  29. package/dist/spec/names.d.ts +14 -0
  30. package/dist/spec/names.js +37 -0
  31. package/dist/spec/params.d.ts +47 -0
  32. package/dist/spec/params.js +344 -0
  33. package/dist/spec/paths/auth.d.ts +9 -0
  34. package/dist/spec/paths/auth.js +285 -0
  35. package/dist/spec/paths/collections.d.ts +7 -0
  36. package/dist/spec/paths/collections.js +261 -0
  37. package/dist/spec/paths/custom.d.ts +9 -0
  38. package/dist/spec/paths/custom.js +41 -0
  39. package/dist/spec/paths/globals.d.ts +7 -0
  40. package/dist/spec/paths/globals.js +68 -0
  41. package/dist/spec/paths/jobs.d.ts +7 -0
  42. package/dist/spec/paths/jobs.js +130 -0
  43. package/dist/spec/paths/versions.d.ts +18 -0
  44. package/dist/spec/paths/versions.js +243 -0
  45. package/dist/spec/tags.d.ts +27 -0
  46. package/dist/spec/tags.js +79 -0
  47. package/dist/translations/index.d.ts +6 -0
  48. package/dist/translations/index.js +96 -0
  49. package/dist/translations/locales/ar.d.ts +2 -0
  50. package/dist/translations/locales/ar.js +76 -0
  51. package/dist/translations/locales/az.d.ts +2 -0
  52. package/dist/translations/locales/az.js +76 -0
  53. package/dist/translations/locales/bg.d.ts +2 -0
  54. package/dist/translations/locales/bg.js +76 -0
  55. package/dist/translations/locales/bnBd.d.ts +2 -0
  56. package/dist/translations/locales/bnBd.js +76 -0
  57. package/dist/translations/locales/bnIn.d.ts +2 -0
  58. package/dist/translations/locales/bnIn.js +76 -0
  59. package/dist/translations/locales/ca.d.ts +2 -0
  60. package/dist/translations/locales/ca.js +76 -0
  61. package/dist/translations/locales/cs.d.ts +2 -0
  62. package/dist/translations/locales/cs.js +76 -0
  63. package/dist/translations/locales/da.d.ts +2 -0
  64. package/dist/translations/locales/da.js +76 -0
  65. package/dist/translations/locales/de.d.ts +2 -0
  66. package/dist/translations/locales/de.js +76 -0
  67. package/dist/translations/locales/en.d.ts +2 -0
  68. package/dist/translations/locales/en.js +76 -0
  69. package/dist/translations/locales/es.d.ts +2 -0
  70. package/dist/translations/locales/es.js +76 -0
  71. package/dist/translations/locales/et.d.ts +2 -0
  72. package/dist/translations/locales/et.js +76 -0
  73. package/dist/translations/locales/fa.d.ts +2 -0
  74. package/dist/translations/locales/fa.js +76 -0
  75. package/dist/translations/locales/fr.d.ts +2 -0
  76. package/dist/translations/locales/fr.js +76 -0
  77. package/dist/translations/locales/he.d.ts +2 -0
  78. package/dist/translations/locales/he.js +76 -0
  79. package/dist/translations/locales/hr.d.ts +2 -0
  80. package/dist/translations/locales/hr.js +76 -0
  81. package/dist/translations/locales/hu.d.ts +2 -0
  82. package/dist/translations/locales/hu.js +76 -0
  83. package/dist/translations/locales/hy.d.ts +2 -0
  84. package/dist/translations/locales/hy.js +76 -0
  85. package/dist/translations/locales/id.d.ts +2 -0
  86. package/dist/translations/locales/id.js +76 -0
  87. package/dist/translations/locales/is.d.ts +2 -0
  88. package/dist/translations/locales/is.js +76 -0
  89. package/dist/translations/locales/it.d.ts +2 -0
  90. package/dist/translations/locales/it.js +76 -0
  91. package/dist/translations/locales/ja.d.ts +2 -0
  92. package/dist/translations/locales/ja.js +76 -0
  93. package/dist/translations/locales/ko.d.ts +2 -0
  94. package/dist/translations/locales/ko.js +76 -0
  95. package/dist/translations/locales/lt.d.ts +2 -0
  96. package/dist/translations/locales/lt.js +76 -0
  97. package/dist/translations/locales/lv.d.ts +2 -0
  98. package/dist/translations/locales/lv.js +76 -0
  99. package/dist/translations/locales/my.d.ts +2 -0
  100. package/dist/translations/locales/my.js +76 -0
  101. package/dist/translations/locales/nb.d.ts +2 -0
  102. package/dist/translations/locales/nb.js +76 -0
  103. package/dist/translations/locales/nl.d.ts +2 -0
  104. package/dist/translations/locales/nl.js +76 -0
  105. package/dist/translations/locales/pl.d.ts +2 -0
  106. package/dist/translations/locales/pl.js +76 -0
  107. package/dist/translations/locales/pt.d.ts +2 -0
  108. package/dist/translations/locales/pt.js +76 -0
  109. package/dist/translations/locales/ro.d.ts +2 -0
  110. package/dist/translations/locales/ro.js +76 -0
  111. package/dist/translations/locales/rs.d.ts +2 -0
  112. package/dist/translations/locales/rs.js +76 -0
  113. package/dist/translations/locales/rsLatin.d.ts +2 -0
  114. package/dist/translations/locales/rsLatin.js +76 -0
  115. package/dist/translations/locales/ru.d.ts +2 -0
  116. package/dist/translations/locales/ru.js +76 -0
  117. package/dist/translations/locales/sk.d.ts +2 -0
  118. package/dist/translations/locales/sk.js +76 -0
  119. package/dist/translations/locales/sl.d.ts +2 -0
  120. package/dist/translations/locales/sl.js +76 -0
  121. package/dist/translations/locales/sv.d.ts +2 -0
  122. package/dist/translations/locales/sv.js +76 -0
  123. package/dist/translations/locales/ta.d.ts +2 -0
  124. package/dist/translations/locales/ta.js +76 -0
  125. package/dist/translations/locales/th.d.ts +2 -0
  126. package/dist/translations/locales/th.js +76 -0
  127. package/dist/translations/locales/tr.d.ts +2 -0
  128. package/dist/translations/locales/tr.js +76 -0
  129. package/dist/translations/locales/uk.d.ts +2 -0
  130. package/dist/translations/locales/uk.js +76 -0
  131. package/dist/translations/locales/vi.d.ts +2 -0
  132. package/dist/translations/locales/vi.js +76 -0
  133. package/dist/translations/locales/zh.d.ts +2 -0
  134. package/dist/translations/locales/zh.js +76 -0
  135. package/dist/translations/locales/zhTw.d.ts +2 -0
  136. package/dist/translations/locales/zhTw.js +76 -0
  137. package/dist/translations/types.d.ts +82 -0
  138. package/dist/translations/types.js +1 -0
  139. package/dist/types.d.ts +183 -0
  140. package/dist/types.js +1 -0
  141. package/dist/ui/createUiPlugin.d.ts +14 -0
  142. package/dist/ui/createUiPlugin.js +28 -0
  143. package/dist/ui/html.d.ts +8 -0
  144. package/dist/ui/html.js +8 -0
  145. package/dist/ui/scalar.d.ts +1 -0
  146. package/dist/ui/scalar.js +12 -0
  147. package/dist/ui/swagger.d.ts +1 -0
  148. package/dist/ui/swagger.js +12 -0
  149. package/dist/utils.d.ts +3 -0
  150. package/dist/utils.js +13 -0
  151. package/package.json +84 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Maxim Seshuk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,595 @@
1
+ <div align="center">
2
+
3
+ <h1>OpenAPI Plugin for Payload CMS</h1>
4
+
5
+ <p>OpenAPI 3.0/3.1/3.2 specs from your Payload CMS instance, with Scalar or Swagger UI.</p>
6
+
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
+ <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>
10
+ <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
+ <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
+
13
+ </div>
14
+
15
+ ## Features
16
+
17
+ - Full spec from your config — collections, globals, auth, versions, and jobs, with zero annotation needed.
18
+ - Document custom endpoints and refine field schemas and descriptions with native `custom.openapi` metadata.
19
+ - Localized descriptions and 44 UI locales, resolved through your Payload i18n.
20
+ - Interactive docs out of the box with **Scalar** or **Swagger UI**.
21
+ - Filter exactly which entities and operations are exposed.
22
+ - Generate the spec to a file for CI, schema diffs, or client codegen.
23
+ - OpenAPI 3.0, 3.1, or 3.2 — one option switches the output.
24
+
25
+ ## Table of Contents
26
+
27
+ - [Requirements](#requirements)
28
+ - [Installation](#installation)
29
+ - [Quick Start](#quick-start)
30
+ - [Configuration](#configuration)
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
57
+
58
+ ```bash
59
+ pnpm add @seshuk/payload-plugin-openapi
60
+ # or
61
+ npm install @seshuk/payload-plugin-openapi
62
+ # or
63
+ yarn add @seshuk/payload-plugin-openapi
64
+ ```
65
+
66
+ ## Quick Start
67
+
68
+ ```ts
69
+ import { openapi, scalar } from '@seshuk/payload-plugin-openapi'
70
+ import { buildConfig } from 'payload'
71
+
72
+ export default buildConfig({
73
+ // ...
74
+ plugins: [
75
+ openapi({
76
+ metadata: {
77
+ title: 'My API',
78
+ version: '1.0.0',
79
+ },
80
+ }),
81
+ scalar(),
82
+ ],
83
+ })
84
+ ```
85
+
86
+ That's it. Two endpoints are now live:
87
+
88
+ - `GET /api/openapi.json` — the generated OpenAPI document
89
+ - `GET /api/docs` — interactive API reference (Scalar)
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:
296
+
297
+ | Option | Type | Default | Description |
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 |
304
+
305
+ `configuration` is passed straight through to the underlying library — Scalar's `createApiReference` config or Swagger's `SwaggerUIBundle` options. It must be JSON-serializable; functions aren't supported there.
306
+
307
+ ```ts
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
+ ```
468
+
469
+ | Flag | Description |
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 |
474
+
475
+ > [!NOTE]
476
+ > The generated spec reflects public-access marking only — it does not run per-user access checks. This matches the HTTP endpoint exactly.
477
+
478
+ **Setting the server URL.** Over HTTP the `servers` field is filled in per request from the incoming `Host` header, so there's nothing to configure. A file on disk has no request to read the host from, so you set it yourself with `--server` (or, when building the document in your own code, an explicit `servers` — see [Programmatic Use](#programmatic-use)). `metadata` carries the API's title, version, and description only, not its URL — keeping the base URL out of the spec is what lets the same document work behind any host, proxy, or environment.
479
+
480
+ ### Generate only, no runtime endpoint
481
+
482
+ By default the plugin both registers the `openapi:generate` CLI **and** serves the spec at `/api/openapi.json`. If you only want the CLI — generate the file at build time and host it yourself, without exposing a live endpoint — set `serve: false`:
483
+
484
+ ```ts
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
+ ```
531
+
532
+ ## Internationalization
533
+
534
+ The plugin includes UI translations for 44 locales, automatically merged into your Payload i18n configuration under the `@seshuk/payload-plugin-openapi` namespace. Only the languages present in your project's `i18n.supportedLanguages` are merged in.
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 |
574
+
575
+ All public types are exported from the same entry point, so editor autocomplete picks them up on `import type { … } from '@seshuk/payload-plugin-openapi'`.
576
+
577
+ ## License
578
+
579
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
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)
590
+
591
+ ## Credits
592
+
593
+ Built with ❤️ for the Payload CMS community.
594
+
595
+ If you find this plugin useful, [buy me a coffee](https://ko-fi.com/V7V61UCT39).
@@ -0,0 +1,25 @@
1
+ import type { SanitizedConfig } from 'payload';
2
+ interface CliArgs {
3
+ lang?: string;
4
+ out?: string;
5
+ server?: string;
6
+ }
7
+ export declare const parseArgs: (argv: string[]) => CliArgs;
8
+ /**
9
+ * Bin entrypoint registered automatically by the OpenAPI plugin. Reads resolved
10
+ * plugin options from `config.custom[PLUGIN_NAME]` (stashed during config build),
11
+ * then writes the OpenAPI document(s) to disk — no HTTP request required.
12
+ *
13
+ * Flags:
14
+ * --lang <code|all> locale for translated descriptions; `all` writes one file
15
+ * per supported language. Defaults to the i18n fallback.
16
+ * --out <path> output file for a single-language run (default openapi.json).
17
+ * --server <url> base URL written to the document's `servers`. The HTTP
18
+ * endpoint derives this per request; on disk there's no
19
+ * request, so pass it here or leave `servers` empty.
20
+ *
21
+ * Note: the spec reflects public-access marking only (no per-user access),
22
+ * identical to the HTTP endpoint.
23
+ */
24
+ export declare const script: (config: SanitizedConfig) => Promise<void>;
25
+ export {};