@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.
- package/README.md +98 -165
- package/build/index.d.mts +218 -267
- package/build/index.d.mts.map +1 -1
- package/build/index.mjs +690 -509
- package/build/index.mjs.map +1 -1
- package/package.json +9 -10
- package/src/decorator/mcp.ts +39 -0
- package/src/index.ts +5 -9
- package/src/lib/controllerDiscovery.ts +201 -0
- package/src/lib/metadata.ts +32 -52
- package/src/lib/rebind.ts +117 -0
- package/src/lib/reflect/classValidator.ts +56 -0
- package/src/lib/reflect/openapi.ts +87 -0
- package/src/lib/reflect/params.ts +63 -0
- package/src/lib/reflect/route.ts +58 -0
- package/src/lib/reflect/schema.ts +270 -0
- package/src/lib/reflect/swagger.ts +39 -0
- package/src/lib/silkweave.module.ts +17 -17
- package/src/lib/types.ts +13 -10
- package/tsconfig.tsbuildinfo +1 -1
- package/src/adapter/rest.ts +0 -173
- package/src/adapter/trpc.ts +0 -58
- package/src/adapter/typegen.ts +0 -55
- package/src/decorator/action.ts +0 -38
- package/src/decorator/actions.ts +0 -25
- package/src/lib/discovery.ts +0 -131
- package/src/lib/filter.ts +0 -26
- package/src/lib/openapi.ts +0 -116
- package/src/lib/swagger.ts +0 -74
package/src/lib/swagger.ts
DELETED
|
@@ -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
|
-
}
|