@buka/nestjs-kit 4.6.0 → 4.7.1
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/CHANGELOG.md +14 -0
- package/dist/index.d.mts +73 -8
- package/dist/index.d.ts +73 -8
- package/dist/index.js +1421 -1346
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1445 -1372
- package/dist/index.mjs.map +1 -1
- package/docs/swagger-patcher.md +29 -1
- package/package.json +1 -1
- package/skills/nestjs-kit-swagger/SKILL.md +23 -1
- package/skills/nestjs-kit-urn/SKILL.md +34 -2
package/docs/swagger-patcher.md
CHANGED
|
@@ -28,7 +28,8 @@ SwaggerModule.setup("/swagger/ui", app, document);
|
|
|
28
28
|
| 选项 | 类型 | 默认值 | 说明 |
|
|
29
29
|
| --- | --- | --- | --- |
|
|
30
30
|
| `overwrite` | `('4xx' \| '5xx' \| 'default' \| number)[]` | `[]` | 覆写已有的哪些响应 |
|
|
31
|
-
| `insert` | `(number \| 'default')[]` | `[]` |
|
|
31
|
+
| `insert` | `(number \| 'default')[]` | `[]` | 插入状态码响应(已存在也覆盖) |
|
|
32
|
+
| `filter` | `(context: { pathname, method, operation }) => boolean` | `undefined` | 返回 `false` 的 operation 完全跳过(既不覆写也不插入) |
|
|
32
33
|
|
|
33
34
|
```typescript
|
|
34
35
|
import { SwaggerPatcher } from "@buka/nestjs-kit";
|
|
@@ -58,3 +59,30 @@ SwaggerPatcher.unifyExceptionResponses(document, {
|
|
|
58
59
|
|
|
59
60
|
SwaggerModule.setup("/swagger/ui", app, document);
|
|
60
61
|
```
|
|
62
|
+
|
|
63
|
+
### 跳过统一异常响应
|
|
64
|
+
|
|
65
|
+
错误体遵循其他规范的端点(如 OAuth2 token、健康检查)不应被统一为 Buka 信封。在 controller 方法上添加 `@SkipUnifiedExceptionResponse()`,该 operation 就会完全跳过:
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { SkipUnifiedExceptionResponse } from "@buka/nestjs-kit";
|
|
69
|
+
import { ApiResponse } from "@nestjs/swagger";
|
|
70
|
+
|
|
71
|
+
@Post("token")
|
|
72
|
+
@SkipUnifiedExceptionResponse()
|
|
73
|
+
@ApiResponse({ status: 400, type: OAuth2ErrorResponseDto })
|
|
74
|
+
@ApiResponse({ status: 401, type: OAuth2ErrorResponseDto })
|
|
75
|
+
exchangeToken() {}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
- 装饰器写入 `x-buka-skip-unified-exception-response: true` 扩展;标记在 controller 类上时对该类所有 operation 生效。
|
|
79
|
+
- 被跳过的 operation 不会被 `overwrite` 或 `insert` 修改,需自行用 `@ApiResponse` 声明错误响应。
|
|
80
|
+
- `components.schemas.ExceptionResponse` 与 `components.responses.ExceptionResponse` 仍会注册(全局组件,与具体 operation 无关)。
|
|
81
|
+
- 无法修改 controller 的 operation(第三方模块等)用 `filter` 豁免:
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
SwaggerPatcher.unifyExceptionResponses(document, {
|
|
85
|
+
overwrite: ["4xx", "5xx"],
|
|
86
|
+
filter: ({ pathname }) => pathname !== "/health",
|
|
87
|
+
});
|
|
88
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@buka/nestjs-kit",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.7.1",
|
|
4
4
|
"description": "Buka Nestjs Kit",
|
|
5
5
|
"packageManager": "pnpm@10.33.4+sha512.1c67b3b359b2d408119ba1ed289f34b8fc3c6873412bec6fd264fbdc82489e510fcbecb9ce9d22dae7f3b76269d8441046014bdca53b9979cd7a561ad631b800",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -4,7 +4,7 @@ description: >
|
|
|
4
4
|
本 skill 覆盖 @buka/nestjs-kit 的 Swagger 文档修补。当用户遇到 Swagger UI 中 object 型 query 参数
|
|
5
5
|
渲染成手敲 JSON 文本框、需要统一异常响应文档(unifyExceptionResponses)、
|
|
6
6
|
配置 SwaggerPatcher(deepObjectifyQueries/unifyExceptionResponses)、
|
|
7
|
-
|
|
7
|
+
按项目实际暴露状态码裁剪 insert 列表,或某端点错误体不符合统一信封需要豁免时使用本 skill。
|
|
8
8
|
SwaggerPatcher 只做文档层后处理、不改变运行时校验;运行时错误响应来自 nestjs-kit-exception,
|
|
9
9
|
@FilterQuery/@PageQuery 的 Swagger 声明随 nestjs-kit-coding-standards 生成。
|
|
10
10
|
---
|
|
@@ -52,6 +52,7 @@ SwaggerModule.setup('/swagger/ui', app, document)
|
|
|
52
52
|
|---|---|---|---|
|
|
53
53
|
| `overwrite` | `('4xx' \| '5xx' \| 'default' \| number)[]` | `[]` | 覆写已有响应为 ExceptionResponse |
|
|
54
54
|
| `insert` | `(number \| 'default')[]` | `[]` | 插入新状态码响应(已存在也覆盖) |
|
|
55
|
+
| `filter` | `(context: { pathname, method, operation }) => boolean` | `undefined` | 返回 `false` 的 operation 完全跳过(既不覆写也不插入) |
|
|
55
56
|
|
|
56
57
|
```typescript
|
|
57
58
|
SwaggerPatcher.unifyExceptionResponses(document) // 仅注册组件
|
|
@@ -61,9 +62,30 @@ SwaggerPatcher.unifyExceptionResponses(document, { overwrite: ['4xx', '5xx'] })
|
|
|
61
62
|
SwaggerPatcher.unifyExceptionResponses(document, { overwrite: ['5xx'], insert: [401, 403] })
|
|
62
63
|
```
|
|
63
64
|
|
|
65
|
+
### 协议端点豁免
|
|
66
|
+
|
|
67
|
+
错误体遵循其他规范的端点(OAuth2 token、健康检查等)不应被统一为 Buka 信封。在 controller 方法上添加 `@SkipUnifiedExceptionResponse()`:
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import { SkipUnifiedExceptionResponse } from '@buka/nestjs-kit'
|
|
71
|
+
import { ApiResponse } from '@nestjs/swagger'
|
|
72
|
+
|
|
73
|
+
@Post('token')
|
|
74
|
+
@SkipUnifiedExceptionResponse()
|
|
75
|
+
@ApiResponse({ status: 400, type: OAuth2ErrorResponseDto })
|
|
76
|
+
@ApiResponse({ status: 401, type: OAuth2ErrorResponseDto })
|
|
77
|
+
exchangeToken() {}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
- 装饰器写入 `x-buka-skip-unified-exception-response: true`;标记在 controller 类上时对该类所有 operation 生效
|
|
81
|
+
- 被跳过的 operation 不会被 `overwrite`/`insert` 修改,需自行用 `@ApiResponse` 声明错误响应
|
|
82
|
+
- `components` 中的 `ExceptionResponse` 仍会注册(全局组件,与具体 operation 无关)
|
|
83
|
+
- 无法修改 controller 的 operation(第三方模块等)用 `filter` 豁免:`filter: ({ pathname }) => pathname !== '/health'`
|
|
84
|
+
|
|
64
85
|
## 注意事项
|
|
65
86
|
|
|
66
87
|
- 只影响**文档**,不改变运行时——运行时校验还是全局验证管道的职责
|
|
88
|
+
- 被豁免的 operation 在文档里没有错误响应,必须补 `@ApiResponse`,否则客户端生成器拿不到错误类型
|
|
67
89
|
- 服务多了以后文档体积会大,`unifyExceptionResponses` 的 `insert` 列表按项目真实暴露的状态码裁剪(如认证豁免的接口可不插 401)
|
|
68
90
|
- CLI 插件的 `dtoFileNameSuffix` 配置(见 [coding-standards 的 references/entity.md](../nestjs-kit-coding-standards/references/entity.md))作用于编译期 schema 生成;`SwaggerPatcher` 作用于 `createDocument` 之后的文档后处理,两者位于不同阶段
|
|
69
91
|
|
|
@@ -25,8 +25,37 @@ urn:buka:<domain>[:<resource>]
|
|
|
25
25
|
|
|
26
26
|
概念域按**资源类型的定义权归属**划分,非按部署单元。一个概念域可以有多个服务实现。
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
| 类型 | 定义权归属 | 名称 | 示例 |
|
|
29
|
+
| --- | --- | --- | --- |
|
|
30
|
+
| 系统域 | 某个系统 | 系统名 | `galaxy`、`openbao`、`kafka` |
|
|
31
|
+
| 虚拟域 | 公司级跨系统约定,不属于任何单一系统实现 | 规范登记的保留名 | `application`、`service` |
|
|
32
|
+
|
|
33
|
+
#### 系统域
|
|
34
|
+
|
|
35
|
+
绝大多数资源归属某个系统,使用系统名作为概念域;系统域的名称与该系统一致。系统域级 URN 表示该系统本身(如 `urn:buka:galaxy`)。
|
|
36
|
+
|
|
37
|
+
#### 虚拟域
|
|
38
|
+
|
|
39
|
+
**虚拟域(virtual domain)**:资源类型的定义权不属于任何单一系统、而属于公司级跨系统约定的概念域。虚拟域不是系统名,也不对应任何部署单元;它通过资源级 URN 被跨系统引用。
|
|
40
|
+
|
|
41
|
+
设立虚拟域须同时满足:
|
|
42
|
+
|
|
43
|
+
1. 该资源类型被多个系统共同引用(如应用授权单位、服务身份与发现);
|
|
44
|
+
2. 定义权若归属某个系统,会因该系统改名、拆分或替换而使标识失效;
|
|
45
|
+
3. 存在唯一的公司级注册表定义其格式与实例,并已在本规范登记。
|
|
46
|
+
|
|
47
|
+
约束:
|
|
48
|
+
|
|
49
|
+
- 虚拟域名称不得与系统名相同,也不得再作为系统名注册;
|
|
50
|
+
- 实现侧不得自行约定虚拟域;新增虚拟域必须先登记在本规范;
|
|
51
|
+
- 虚拟域的域级 URN(`urn:buka:application`、`urn:buka:service`)只是命名空间,不指向任何对象,不能作为标识符使用。
|
|
52
|
+
|
|
53
|
+
##### 虚拟域登记
|
|
54
|
+
|
|
55
|
+
| 虚拟域 | 格式 | 定义方 | 语义 |
|
|
56
|
+
| --- | --- | --- | --- |
|
|
57
|
+
| `application` | `urn:buka:application:{handle}` | 应用注册表 | 应用身份:授权单位与 token audience;`handle` 全局唯一、创建后不可变 |
|
|
58
|
+
| `service` | `urn:buka:service:{system}:{name}` | 服务注册表 | 服务身份与发现句柄;`system` 为 L1 系统,`name` 为 L2 服务;服务不是授权单位,不作为 token audience |
|
|
30
59
|
|
|
31
60
|
### 字符集
|
|
32
61
|
|
|
@@ -62,6 +91,9 @@ URN 支持两种粒度:
|
|
|
62
91
|
| `openbao` | `dev:kv:galaxy:database` | `urn:buka:openbao:dev:kv:galaxy:database` | OpenBao 开发环境 KV |
|
|
63
92
|
| `kafka` | `prod:topic:galaxy-events` | `urn:buka:kafka:prod:topic:galaxy-events` | Kafka 生产环境 Topic |
|
|
64
93
|
| `kubernetes` | `prod:deployment:galaxy:api` | `urn:buka:kubernetes:prod:deployment:galaxy:api` | K8s 生产环境 Deployment |
|
|
94
|
+
| `galaxy` | — | `urn:buka:galaxy` | Galaxy 系统(系统域级) |
|
|
95
|
+
| `application` | `galaxy` | `urn:buka:application:galaxy` | Galaxy 应用(虚拟域,授权单位 / aud) |
|
|
96
|
+
| `service` | `galaxy:backend` | `urn:buka:service:galaxy:backend` | Galaxy 后端服务(虚拟域,发现句柄) |
|
|
65
97
|
|
|
66
98
|
## 通配匹配
|
|
67
99
|
|