@silkweave/nestjs 1.12.0 → 2.0.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.
@@ -1,74 +0,0 @@
1
- import { type INestApplication } from '@nestjs/common'
2
- import type { OpenAPIObject } from '@nestjs/swagger'
3
- import { type Action, createContext } from '@silkweave/core'
4
- import { ActionDiscovery } from './discovery.js'
5
- import { buildActionPaths } from './openapi.js'
6
- import { SILKWEAVE_MODULE_OPTIONS, type SilkweaveModuleOptions } from './types.js'
7
-
8
- export interface SilkweaveSwaggerOptions {
9
- /**
10
- * URL prefix the `rest()` adapter mounts on. Defaults to the configured
11
- * `rest()` adapter's `basePath`, falling back to `'/api'`.
12
- */
13
- basePath?: string
14
- /** OpenAPI tag the actions are grouped under. Default `'Actions'`. */
15
- tag?: string
16
- /**
17
- * Include actions that are *not* enabled on the REST transport (gated out via
18
- * `transports` / `isEnabled`). Default `false` - the document mirrors the
19
- * routes the `rest()` adapter actually registers.
20
- */
21
- includeDisabled?: boolean
22
- }
23
-
24
- /**
25
- * Merge every REST-exposed Silkweave `@Action` into a NestJS Swagger
26
- * `OpenAPIObject`.
27
- *
28
- * `@nestjs/swagger` builds its document by scanning **controllers**, but
29
- * Silkweave registers action routes directly on the HTTP adapter (so they sit
30
- * ahead of controllers in the request pipeline) - which means the scanner never
31
- * sees them. This helper closes that gap: it discovers the actions through the
32
- * same `ActionDiscovery` provider the `rest()` adapter uses, builds OpenAPI
33
- * paths with the same routing logic (`buildActionPaths`), and merges them into
34
- * the document. The result stays in sync with the live routes without any
35
- * dynamic controllers.
36
- *
37
- * Call it between `SwaggerModule.createDocument()` and `SwaggerModule.setup()`:
38
- *
39
- * @example
40
- * ```ts
41
- * const document = SwaggerModule.createDocument(app, config)
42
- * addSilkweaveActions(app, document)
43
- * SwaggerModule.setup('api/docs', app, document)
44
- * ```
45
- */
46
- export function addSilkweaveActions(
47
- app: INestApplication,
48
- document: OpenAPIObject,
49
- options: SilkweaveSwaggerOptions = {}
50
- ): OpenAPIObject {
51
- const discovery = app.get(ActionDiscovery)
52
- const moduleOptions = app.get<SilkweaveModuleOptions>(SILKWEAVE_MODULE_OPTIONS)
53
- const restAdapter = moduleOptions.adapters.find((adapter) => adapter.name === 'rest')
54
- const basePath = options.basePath ?? restAdapter?.basePath ?? '/api'
55
-
56
- const allActions = discovery.discover()
57
- const actions = options.includeDisabled
58
- ? allActions
59
- : allActions.filter((action) => isRestEnabled(action, moduleOptions))
60
-
61
- const paths = buildActionPaths(actions, { basePath, tag: options.tag })
62
-
63
- document.paths ??= {}
64
- for (const [route, item] of Object.entries(paths)) {
65
- document.paths[route] = { ...(document.paths[route] ?? {}), ...item }
66
- }
67
- return document
68
- }
69
-
70
- /** Whether an action would be registered by the `rest()` adapter (adapter: 'rest'). */
71
- function isRestEnabled(action: Action, moduleOptions: SilkweaveModuleOptions): boolean {
72
- if (!action.isEnabled) { return true }
73
- return action.isEnabled(createContext({ ...(moduleOptions.context ?? {}), adapter: 'rest' }))
74
- }