@edirect/documentation 11.0.59
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 +197 -0
- package/package.json +29 -0
package/README.md
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
# @edirect/documentation
|
|
2
|
+
|
|
3
|
+
Self-contained NestJS module that serves **code-first** OpenAPI documentation at `/docs`, with a
|
|
4
|
+
bolttech app bar and pluggable renderers.
|
|
5
|
+
|
|
6
|
+
- **Multi-renderer** — Redoc (default), Scalar, or Swagger UI, picked per service.
|
|
7
|
+
- **Version switching** — declare multiple API versions; the viewer renders a version dropdown.
|
|
8
|
+
- **Deprecated badge** — mark a version `deprecated` and it shows an amber badge in the app bar.
|
|
9
|
+
- **bolttech app bar** — one slim, sticky bar shared across renderers: logo, workspace breadcrumb,
|
|
10
|
+
environment badge, version switch, support link, and OpenAPI download.
|
|
11
|
+
- **Code-first** — specs are generated lazily from the live Nest application via `@nestjs/swagger`;
|
|
12
|
+
no hand-written OpenAPI files to maintain.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pnpm add @edirect/documentation
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Peer libraries used at runtime (already present in any Nest service): `@nestjs/common`,
|
|
21
|
+
`@nestjs/swagger`, `express`.
|
|
22
|
+
|
|
23
|
+
## Quick start
|
|
24
|
+
|
|
25
|
+
The module is `@Global`. Register it in your `AppModule`, then bind the running app once after
|
|
26
|
+
bootstrap so declarative versions can be generated lazily.
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
// app.module.ts
|
|
30
|
+
import { Module } from '@nestjs/common';
|
|
31
|
+
import { DocumentationModule } from '@edirect/documentation';
|
|
32
|
+
|
|
33
|
+
@Module({
|
|
34
|
+
imports: [
|
|
35
|
+
DocumentationModule.forRoot({
|
|
36
|
+
title: 'Messaging Service',
|
|
37
|
+
workspace: 'Shared Services',
|
|
38
|
+
support: 'shared-services@bolttech.io',
|
|
39
|
+
versions: [
|
|
40
|
+
{ slug: 'v2', label: 'v2', version: '2.0', isDefault: true, paths: ['/v2'] },
|
|
41
|
+
],
|
|
42
|
+
}),
|
|
43
|
+
],
|
|
44
|
+
})
|
|
45
|
+
export class AppModule {}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
// main.ts
|
|
50
|
+
import { NestFactory } from '@nestjs/core';
|
|
51
|
+
import { DocumentationService } from '@edirect/documentation';
|
|
52
|
+
import { AppModule } from './app.module';
|
|
53
|
+
|
|
54
|
+
async function bootstrap() {
|
|
55
|
+
const app = await NestFactory.create(AppModule);
|
|
56
|
+
|
|
57
|
+
// REQUIRED — without this the declarative versions cannot be generated.
|
|
58
|
+
app.get(DocumentationService).useApp(app);
|
|
59
|
+
|
|
60
|
+
await app.listen(3000);
|
|
61
|
+
}
|
|
62
|
+
bootstrap();
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Open `http://localhost:3000/docs`.
|
|
66
|
+
|
|
67
|
+
### Async configuration
|
|
68
|
+
|
|
69
|
+
Use `forRootAsync` when options depend on config/DI. Because the resolved `root` value is unknown at
|
|
70
|
+
module-build time, opt into the `/` route with `mountRoot: true`.
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
DocumentationModule.forRootAsync({
|
|
74
|
+
imports: [ConfigModule],
|
|
75
|
+
inject: [ConfigService],
|
|
76
|
+
mountRoot: true, // only needed if you want root: 'redirect' | 'serve'
|
|
77
|
+
useFactory: (config: ConfigService) => ({
|
|
78
|
+
enabled: config.get('DOCS_ENABLED') !== 'false',
|
|
79
|
+
title: config.get('SERVICE_NAME'),
|
|
80
|
+
root: 'redirect',
|
|
81
|
+
versions: [{ slug: 'v2', isDefault: true, paths: ['/v2'] }],
|
|
82
|
+
}),
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Routes
|
|
87
|
+
|
|
88
|
+
| Route | Description |
|
|
89
|
+
| --- | --- |
|
|
90
|
+
| `GET /docs` | HTML viewer (renderer of your choice). Honors `?spec=<slug>` to switch version. |
|
|
91
|
+
| `GET /docs/spec` | JSON index of registered specs (`{ default, specs: [{ slug, url, ... }] }`). |
|
|
92
|
+
| `GET /docs/spec/:slug` | OpenAPI JSON for a spec, with `ETag` + `Cache-Control` and `304` support. |
|
|
93
|
+
| `GET /` | Only when `root` is `redirect` or `serve` (and `mountRoot: true` for async). |
|
|
94
|
+
|
|
95
|
+
When `enabled: false`, every docs route responds `404`.
|
|
96
|
+
|
|
97
|
+
## Options
|
|
98
|
+
|
|
99
|
+
| Option | Type | Default | Description |
|
|
100
|
+
| --- | --- | --- | --- |
|
|
101
|
+
| `enabled` | `boolean` | `true` | Master switch. `false` → all docs routes return `404`. |
|
|
102
|
+
| `title` | `string` | `'API Documentation'` | Page title and OpenAPI `info.title`. |
|
|
103
|
+
| `description` | `string` | — | Default description for generated versions. |
|
|
104
|
+
| `workspace` | `string` | — | Domain/workspace shown before the title in the breadcrumb. |
|
|
105
|
+
| `support` | `string` | — | Support email or URL shown in the app bar. |
|
|
106
|
+
| `renderer` | `'redoc' \| 'scalar' \| 'swagger-ui'` | `'redoc'` | Which viewer to render. |
|
|
107
|
+
| `logo` | `{ url; altText? }` | bolttech logo | Brand logo in the app bar. |
|
|
108
|
+
| `brand.accent` | `string` (hex or CSS keyword) | bolttech cyan `#00bac7` | Accent color; validated, invalid values are ignored. |
|
|
109
|
+
| `bearerAuth` | `boolean` | `true` | Adds a bearer security scheme to every generated version. |
|
|
110
|
+
| `servers` | `string[]` | — | Servers advertised in every generated version. |
|
|
111
|
+
| `root` | `'off' \| 'redirect' \| 'serve'` | `'off'` | `/` behavior. Default never claims `/`. |
|
|
112
|
+
| `versions` | `DocumentationVersionOptions[]` | — | Declarative versions, generated lazily from the app. |
|
|
113
|
+
| `specs` | `DocumentationSpec[]` | — | Pre-built OpenAPI documents registered as-is. |
|
|
114
|
+
|
|
115
|
+
`DocumentationVersionOptions`: `slug` (required), `label`, `description`, `version`, `order`,
|
|
116
|
+
`isDefault`, `enabled`, `deprecated`, `paths` (keep only routes under these prefixes), `include`
|
|
117
|
+
(restrict generation to given Nest modules), `configure(builder)` (customize the `DocumentBuilder`).
|
|
118
|
+
|
|
119
|
+
> Note: the actual runtime defaults are `renderer: 'redoc'` and `root: 'off'`.
|
|
120
|
+
|
|
121
|
+
## Recipes
|
|
122
|
+
|
|
123
|
+
### Multiple versions
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
DocumentationModule.forRoot({
|
|
127
|
+
title: 'Policy Service',
|
|
128
|
+
versions: [
|
|
129
|
+
{ slug: 'v2', label: 'v2', version: '2.0', order: 1, isDefault: true, paths: ['/v2'] },
|
|
130
|
+
{ slug: 'v1', label: 'v1', version: '1.0', order: 2, deprecated: true, paths: ['/v1'] },
|
|
131
|
+
],
|
|
132
|
+
});
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Mark a version deprecated
|
|
136
|
+
|
|
137
|
+
Set `deprecated: true` on the version — the app bar shows an amber **Deprecated** badge when that
|
|
138
|
+
version is selected.
|
|
139
|
+
|
|
140
|
+
### Custom accent / logo / workspace
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
DocumentationModule.forRoot({
|
|
144
|
+
workspace: 'Payments',
|
|
145
|
+
brand: { accent: '#7c3aed' },
|
|
146
|
+
logo: { url: 'https://example.com/logo.svg', altText: 'Acme' },
|
|
147
|
+
versions: [{ slug: 'v2', isDefault: true }],
|
|
148
|
+
});
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Turn off in production
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
DocumentationModule.forRoot({
|
|
155
|
+
enabled: process.env.NODE_ENV !== 'production',
|
|
156
|
+
versions: [{ slug: 'v2', isDefault: true }],
|
|
157
|
+
});
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Redirect root to the docs
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
DocumentationModule.forRoot({ root: 'redirect', versions: [{ slug: 'v2', isDefault: true }] });
|
|
164
|
+
// forRootAsync: also pass `mountRoot: true`.
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Pre-built spec (escape hatch)
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
DocumentationModule.forRoot({
|
|
171
|
+
specs: [
|
|
172
|
+
{
|
|
173
|
+
slug: 'partner',
|
|
174
|
+
label: 'Partner API',
|
|
175
|
+
isDefault: true,
|
|
176
|
+
document: () => loadPartnerOpenApi(), // OpenAPIObject or a (possibly async) factory
|
|
177
|
+
},
|
|
178
|
+
],
|
|
179
|
+
});
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## Security notes
|
|
183
|
+
|
|
184
|
+
- **Pinned CDN + SRI** — Redoc `2.5.3`, Scalar `1.62.1`, and `swagger-ui-dist` `5.32.8` are loaded
|
|
185
|
+
from jsDelivr at fixed versions with Subresource Integrity hashes (see `renderers/shared.ts`,
|
|
186
|
+
`CDN`). Versions cannot drift to `latest`, and a compromised CDN cannot inject script. **When
|
|
187
|
+
bumping a renderer, change the version and the `integrity` hash together.**
|
|
188
|
+
- **`noindex`** — the viewer emits `<meta name="robots" content="noindex, nofollow">`.
|
|
189
|
+
- **`enabled` gate** — set `enabled: false` to make every docs route return `404` (e.g. production).
|
|
190
|
+
- **Auth** — the module does not gate `/docs` itself. If docs should require authentication, the
|
|
191
|
+
consumer scopes them in its own auth middleware/guard.
|
|
192
|
+
|
|
193
|
+
## Gotcha
|
|
194
|
+
|
|
195
|
+
`app.get(DocumentationService).useApp(app)` is **required** in `main.ts`. Declarative `versions`
|
|
196
|
+
are generated lazily from the live app — without `useApp`, they cannot be built and the service logs
|
|
197
|
+
a warning shortly after boot. (`specs` registered as pre-built documents do not need it.)
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@edirect/documentation",
|
|
3
|
+
"version": "11.0.59",
|
|
4
|
+
"main": "./dist/src/index.js",
|
|
5
|
+
"types": "./dist/src/index.d.ts",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./dist/src/index.js",
|
|
9
|
+
"default": "./dist/src/index.js",
|
|
10
|
+
"require": "./dist/src/index.js",
|
|
11
|
+
"types": "./dist/src/index.d.ts"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@nestjs/common": "^11.1.19",
|
|
20
|
+
"@nestjs/swagger": "11.4.2",
|
|
21
|
+
"express": "5.2.1",
|
|
22
|
+
"tslib": "^2.8.1"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/express": "^5.0.6",
|
|
26
|
+
"@types/node": "^25.6.2"
|
|
27
|
+
},
|
|
28
|
+
"type": "commonjs"
|
|
29
|
+
}
|