@lenne.tech/nest-server 11.44.2 → 11.45.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.
@@ -0,0 +1,111 @@
1
+ # Migration Guide: 11.44.x → 11.45.x
2
+
3
+ ## 11.45.0 changes behaviour
4
+
5
+ The version number moves only the MINOR digit (the MAJOR follows NestJS). This is the 11.x counterpart of the Swagger
6
+ default from 12.7.0 (DEV-4086):
7
+
8
+ - `setupSwagger()` serves the API documentation only in `local`, `ci` and `e2e` unless the new server option
9
+ `swagger` is `true`. On every other stage (`develop`, `test`, `production`, …) `/swagger`, `/api-docs-json` and the
10
+ API-token view answer 404 after the update (§1).
11
+
12
+ Nothing else changes.
13
+
14
+ ## Overview
15
+
16
+ | Category | Details |
17
+ |---|---|
18
+ | **Breaking Changes** | Swagger is off outside `local` / `ci` / `e2e` unless `swagger: true` (§1) |
19
+ | **New Features** | `swagger` server option (`NSC__SWAGGER`); `isSwaggerEnabled()` exported; `setupSwagger()` takes the configuration as an optional third argument |
20
+ | **Bugfixes** | The full API description is no longer public on deployed stages (§1) |
21
+ | **Migration Effort** | Set `swagger: true` on the stages that must keep showing the API documentation (§1); check that every stage loads its own block (§2) |
22
+
23
+ ## Quick Migration
24
+
25
+ ```bash
26
+ pnpm add @lenne.tech/nest-server@11.45.0
27
+ pnpm run update # nest-server-starter based projects (release/11.x)
28
+ pnpm run build
29
+ pnpm test
30
+ ```
31
+
32
+ **Vendor mode**: run `/lt-dev:backend:update-nest-server-core` as usual. A vendored core that closed Swagger in
33
+ `main.ts` or ported the switch by hand can drop its own version: the core now carries it.
34
+
35
+ ## 1. Swagger only where it is switched on
36
+
37
+ **Why:** `setupSwagger()` mounted the Swagger UI and the JSON document on every stage a project called it on,
38
+ production included. The full API description — every route, the roles it requires, every input shape — was public
39
+ there.
40
+
41
+ **What changed:** `setupSwagger()` asks `isSwaggerEnabled()` first (new server option `swagger`):
42
+
43
+ | `swagger` | `local` / `ci` / `e2e` | every other environment (`develop`, `test`, `production`, …) |
44
+ |---|---|---|
45
+ | not set | served | **off** |
46
+ | `true` | served | served |
47
+ | `false` | off | off |
48
+
49
+ Off mounts nothing: neither `/swagger` and `/swagger-api-tokens` nor `/api-docs-json` and `/api-docs-api-tokens-json`
50
+ (or the paths you gave `setupSwagger()`); they answer 404. One line in the boot log says Swagger is off. A value that
51
+ is not a boolean counts as not set, so the string `'true'` from `NEST_SERVER_CONFIG` does not switch it on;
52
+ `NSC__SWAGGER=true` arrives as a boolean.
53
+
54
+ **Your project:** nothing to do if the documentation should stay local. To keep it on a stage, set it in that
55
+ stage's block of `config.env.ts`, or through the environment:
56
+
57
+ ```typescript
58
+ // config.env.ts — only the stage that should show the documentation
59
+ develop: {
60
+ // …
61
+ swagger: true,
62
+ },
63
+ ```
64
+
65
+ ```bash
66
+ NSC__SWAGGER=true
67
+ ```
68
+
69
+ A client generator that reads `/api-docs-json` from a deployed stage (`openapi-typescript`, `orval`, …) needs the
70
+ switch on that stage, or reads the document from a local or CI run instead.
71
+
72
+ A test that calls `setupSwagger()` under the unit runner (`NODE_ENV=test`, which is no local environment) passes the
73
+ switch itself: `setupSwagger(app, options, { swagger: true })`.
74
+
75
+ ## 2. The environment is the one the loaded block names
76
+
77
+ `isSwaggerEnabled()` reads `env` of the loaded configuration, else `NODE_ENV`. On 11.x a `NODE_ENV` that names no
78
+ block in `config.env.ts` still loads the `local` block (12.7.0 refuses that start; 11.x keeps the old behaviour), and
79
+ the `local` block counts as local — such a stage keeps serving Swagger. Check the boot line of every stage:
80
+
81
+ ```
82
+ Configured for: local (requested: staging)
83
+ ```
84
+
85
+ A `(requested: …)` part means the stage runs on another block than it asked for. Add a block for that `NODE_ENV`, or
86
+ set `swagger: false` in the block it falls back to.
87
+
88
+ ## Compatibility Notes
89
+
90
+ | Pattern | Status |
91
+ |---|---|
92
+ | `setupSwagger(app, options)` in `main.ts` | Unchanged call; serves only where the switch allows it |
93
+ | A `main.ts` that calls `setupSwagger()` only outside production by its own check | Keeps working; the switch closes the other deployed stages too |
94
+ | `buildSwaggerDocument()` / `buildApiTokenSwaggerDocument()` used directly | Unchanged — the switch applies to `setupSwagger()` only |
95
+ | `SwaggerModule.setup()` called directly by the project | Not covered — gate it with `isSwaggerEnabled()` |
96
+ | The Hub's Swagger link on a stage with Swagger off | Answers 404 — set `swagger: true` there if the link should work |
97
+
98
+ ## Troubleshooting
99
+
100
+ | Symptom | Cause | Fix |
101
+ |---|---|---|
102
+ | `/swagger` or `/api-docs-json` answers 404 on a deployed stage after the update | Swagger is off outside `local` / `ci` / `e2e` | `swagger: true` in that stage's block, or `NSC__SWAGGER=true` (§1) |
103
+ | Swagger still answers on a deployed stage | The stage runs on the `local` block (boot line `Configured for: local (requested: …)`) or sets `swagger: true` | Add the missing block, or `swagger: false` (§2) |
104
+ | A unit test that serves Swagger gets 404 | The unit runner is no local environment | Pass `{ swagger: true }` as the third argument of `setupSwagger()` (§1) |
105
+
106
+ ## References
107
+
108
+ - `src/core/common/helpers/swagger-enabled.helper.ts` — `isSwaggerEnabled()`
109
+ - `src/core/common/helpers/swagger.helper.ts` — `setupSwagger()`
110
+ - `tests/unit/swagger-switch.spec.ts`
111
+ - `.claude/rules/configurable-features.md` — row "Swagger"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lenne.tech/nest-server",
3
- "version": "11.44.2",
3
+ "version": "11.45.0",
4
4
  "description": "Modern, fast, powerful Node.js web framework in TypeScript based on Nest with a GraphQL API and a connection to MongoDB (or other databases).",
5
5
  "keywords": [
6
6
  "node",
@@ -0,0 +1,33 @@
1
+ import type { IServerOptions } from '../interfaces/server-options.interface';
2
+ import { ConfigService } from '../services/config.service';
3
+ import { isLocalEnv } from './cookies.helper';
4
+
5
+ /**
6
+ * Whether `setupSwagger()` serves the API documentation (`swagger`, 11.45.0+ — the 12.7.0 rule, backported).
7
+ *
8
+ * | `swagger` | `local` / `ci` / `e2e` | every other environment (`develop`, `test`, `production`, …) |
9
+ * |-----------|------------------------|---------------------------------------------------------------|
10
+ * | not set | served | **off** — none of the four paths is mounted |
11
+ * | `true` | served | served |
12
+ * | `false` | off | off |
13
+ *
14
+ * Off means nothing is mounted: neither the UI (`/swagger`, `/swagger-api-tokens`) nor the JSON documents
15
+ * (`/api-docs-json`, `/api-docs-api-tokens-json`), so they answer 404 like any unknown route. Before 11.45.0 every
16
+ * project that called `setupSwagger()` published the full API description on every stage, production included.
17
+ *
18
+ * A value that is not a boolean (the string `'true'` from `NEST_SERVER_CONFIG`) counts as not set, so a string
19
+ * cannot switch the documentation on by truthiness; `NSC__SWAGGER=true` arrives as a boolean. The environment is the
20
+ * configured `env`, else `NODE_ENV`, compared with `isLocalEnv()`.
21
+ *
22
+ * @param config - defaults to the running configuration
23
+ * @since 11.45.0
24
+ */
25
+ export function isSwaggerEnabled(
26
+ config: Partial<IServerOptions> | undefined = ConfigService.configFastButReadOnly,
27
+ ): boolean {
28
+ const setting = config?.swagger;
29
+ if (setting === true || setting === false) {
30
+ return setting;
31
+ }
32
+ return isLocalEnv(config?.env ?? process.env.NODE_ENV);
33
+ }
@@ -28,6 +28,7 @@ import { mergeRolesMetadata } from '../../modules/tenant/core-tenant.helpers';
28
28
  import { RoleEnum } from '../enums/role.enum';
29
29
  import { ConfigService } from '../services/config.service';
30
30
  import { isCookiesEnabled } from './cookies.helper';
31
+ import { isSwaggerEnabled } from './swagger-enabled.helper';
31
32
 
32
33
  import type { INestApplication, Type } from '@nestjs/common';
33
34
  import type {
@@ -139,10 +140,28 @@ const logger = new Logger('Swagger');
139
140
  /**
140
141
  * Build the enriched document and serve it — plus the API-token view, when configured. Both documents are
141
142
  * built on the first request, when every module is initialised.
143
+ *
144
+ * Since 11.45.0 only where `swagger` switches it on ({@link isSwaggerEnabled}): by default in `local`, `ci` and `e2e`,
145
+ * everywhere with `swagger: true`. Otherwise nothing is mounted — no UI, no JSON document — and one line in the log
146
+ * says so.
147
+ *
148
+ * @param config - server configuration; the running configuration by default
142
149
  */
143
- export function setupSwagger(app: INestApplication, options: ISwaggerSetupOptions): void {
150
+ export function setupSwagger(
151
+ app: INestApplication,
152
+ options: ISwaggerSetupOptions,
153
+ config: Partial<IServerOptions> = ConfigService.configFastButReadOnly ?? {},
154
+ ): void {
155
+ if (!isSwaggerEnabled(config)) {
156
+ logger.log(
157
+ `Swagger is off in this environment (${config.env ?? process.env.NODE_ENV ?? 'unknown'}): nothing is served at ` +
158
+ `/${options.path ?? 'swagger'} or ${options.jsonDocumentUrl ?? '/api-docs-json'}. Set \`swagger: true\` ` +
159
+ `(NSC__SWAGGER=true) to serve the API documentation here.`,
160
+ );
161
+ return;
162
+ }
144
163
  let document: OpenAPIObject | undefined;
145
- const fullDocument = () => (document ??= buildSwaggerDocument(app, options));
164
+ const fullDocument = () => (document ??= buildSwaggerDocument(app, options, config));
146
165
 
147
166
  SwaggerModule.setup(options.path ?? 'swagger', app, fullDocument, {
148
167
  ...options.customOptions,
@@ -154,10 +173,15 @@ export function setupSwagger(app: INestApplication, options: ISwaggerSetupOption
154
173
  return;
155
174
  }
156
175
  let viewDocument: OpenAPIObject | undefined;
157
- SwaggerModule.setup(view.path, app, () => (viewDocument ??= buildApiTokenSwaggerDocument(fullDocument(), view)), {
158
- ...options.customOptions,
159
- jsonDocumentUrl: view.jsonDocumentUrl,
160
- });
176
+ SwaggerModule.setup(
177
+ view.path,
178
+ app,
179
+ () => (viewDocument ??= buildApiTokenSwaggerDocument(fullDocument(), view, config)),
180
+ {
181
+ ...options.customOptions,
182
+ jsonDocumentUrl: view.jsonDocumentUrl,
183
+ },
184
+ );
161
185
  }
162
186
 
163
187
  /**
@@ -3459,6 +3459,25 @@ export interface IServerOptions {
3459
3459
  path?: string;
3460
3460
  };
3461
3461
 
3462
+ /**
3463
+ * Whether `setupSwagger()` serves the API documentation (11.45.0+), decided by `isSwaggerEnabled()`.
3464
+ *
3465
+ * | Value | `local` / `ci` / `e2e` | every other environment (`develop`, `test`, `production`, …) |
3466
+ * |-------|------------------------|---------------------------------------------------------------|
3467
+ * | not set | served | **off** |
3468
+ * | `true` | served | served |
3469
+ * | `false` | off | off |
3470
+ *
3471
+ * Off mounts nothing: neither `/swagger` and `/swagger-api-tokens` nor `/api-docs-json` and
3472
+ * `/api-docs-api-tokens-json` (or the paths `setupSwagger()` was given) — they answer 404. Before 11.45.0 every
3473
+ * project that called `setupSwagger()` published its full API description on every stage. A value that is not a
3474
+ * boolean counts as not set. Environment variable: `NSC__SWAGGER=true`.
3475
+ *
3476
+ * @default undefined (served only in `local`, `ci` and `e2e`)
3477
+ * @since 11.45.0
3478
+ */
3479
+ swagger?: boolean;
3480
+
3462
3481
  /**
3463
3482
  * System setup configuration for initial admin creation.
3464
3483
  *
package/src/index.ts CHANGED
@@ -55,6 +55,7 @@ export * from './core/common/helpers/process-diagnostics.helper';
55
55
  export * from './core/common/helpers/register-enum.helper';
56
56
  export * from './core/common/helpers/scim.helper';
57
57
  export * from './core/common/helpers/service.helper';
58
+ export * from './core/common/helpers/swagger-enabled.helper';
58
59
  export * from './core/common/helpers/swagger.helper';
59
60
  export * from './core/common/helpers/table.helper';
60
61
  export * from './core/common/inputs/combined-filter.input';