@buka/nestjs-kit 4.1.9 → 4.2.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.
@@ -328,4 +328,4 @@ class ConfigDto {
328
328
 
329
329
  - [Model 装饰器体系](./models.md) — `@Model()`、`@Property()`、`@Composite()`、`@List()`、`@Dictionary()` 等
330
330
  - [类型转换工具](./converters.md) — `PickType`、`OmitType`、`PartialType`、`ResponseBodyType`、`@FilterQuery` 等
331
- - [URN 统一资源名称](./urn.md) — `Urn`、`NamespaceUrn`、`ResourceUrn`、`IdentifierUrn`、`@IsUrn()` 等
331
+ - [URN 统一资源名称](./urn.md) — `Urn`、`DomainUrn`、`ResourceTypeUrn`、`ResourceIdUrn`、`@IsUrn()`、通配符 `*`/`**`、`contains()` 等
package/docs/core/urn.md CHANGED
@@ -2,67 +2,49 @@
2
2
 
3
3
  ## 概述
4
4
 
5
- `Urn` 是公司级统一资源名称(Uniform Resource Name)工具类,用于在系统间以标准化格式标识任意资源。无需注册模块,直接从主入口导入即可使用。
5
+ `Urn` 是 `urn:buka` 统一资源名称的解析与操作工具类,无需注册模块即可使用。
6
6
 
7
7
  ```typescript
8
- import {
9
- Urn,
10
- NamespaceUrn,
11
- ResourceUrn,
12
- IdentifierUrn,
13
- } from "@buka/nestjs-kit";
8
+ import { Urn, DomainUrn, ResourceTypeUrn, ResourceIdUrn } from "@buka/nestjs-kit";
14
9
  ```
15
10
 
16
- ## URN 格式
11
+ ## 格式
17
12
 
18
13
  ```
19
- urn:buka:<namespace>[:<resource>[:<identifier>]]
14
+ urn:buka:<domain>:<resource-type>[:<resource-id>]
20
15
  ```
21
16
 
22
- | 层级 | 含义 | 规则 |
23
- | ------------ | ----------------------------- | ------------------------------------------------ |
24
- | `buka` | 组织前缀 | 固定值 |
25
- | `namespace` | 资源类型所有者(概念域/平台) | 小写 kebab-case,如 `galaxy`、`infra` |
26
- | `resource` | 资源类型 | 小写 kebab-case,如 `principal`、`oauth2-client` |
27
- | `identifier` | 资源唯一标识 | UUID、hex 或其他自定义格式 |
17
+ 三段分别对应**概念域**(资源类型定义权归属)、**资源类型**(可多级,第一级推荐用于 `env`)、**资源ID**(可选)。
28
18
 
29
- ### 三种粒度
30
-
31
- URN 支持三种粒度,`resource` 和 `identifier` 均为可选:
32
-
33
- | 粒度 | 类型 | 格式 | 示例 | 用途 |
34
- | ------------ | --------------- | ------------------------------- | ---------------------------------------- | ---------------------------------- |
35
- | namespace 级 | `NamespaceUrn` | `urn:buka:<ns>` | `urn:buka:galaxy` | 标识平台/系统整体,如 JWT audience |
36
- | 资源类型级 | `ResourceUrn` | `urn:buka:<ns>:<resource>` | `urn:buka:galaxy:principal` | 标识一类资源 |
37
- | 实例级 | `IdentifierUrn` | `urn:buka:<ns>:<resource>:<id>` | `urn:buka:galaxy:principal:550e8400-...` | 标识具体资源实例,如 JWT subject |
38
-
39
- ### namespace 划分原则
40
-
41
- namespace 按**资源类型的定义权归属**划分,而非按部署单元。一个 namespace 可以有多个服务实现。
19
+ ```typescript
20
+ Urn.of("galaxy"); // 概念域级
21
+ Urn.of("galaxy", "principal"); // 资源类型级
22
+ Urn.of("galaxy", "principal", "550e8400-e29b-..."); // 实例级
23
+ ```
42
24
 
43
- 例如,`principal` 和 `oauth2-client` 都由 Galaxy 平台定义,因此都属于 `galaxy` namespace,即使它们可能由不同的微服务管理。
25
+ > 格式规则详见 [URN 规范](https://github.com/buka-lib/docs.specifications/tree/main/urn)。
44
26
 
45
27
  ## API
46
28
 
47
29
  ### Urn.of
48
30
 
49
- 根据参数数量创建对应层级的 URN,返回类型在编译期自动窄化。构造函数为 `private`,`Urn.of` 是创建 URN 的主要入口。
31
+ 根据参数数量创建对应层级的 URN,返回类型在编译期自动窄化。
50
32
 
51
33
  ```typescript
52
- static of(namespace: string): NamespaceUrn
53
- static of(namespace: string, resource: string): ResourceUrn
54
- static of(namespace: string, resource: string, identifier: string): IdentifierUrn
34
+ static of(domain: string): DomainUrn
35
+ static of(domain: string, resourceType: string): ResourceTypeUrn
36
+ static of(domain: string, resourceType: string, resourceId: string): ResourceIdUrn
55
37
  ```
56
38
 
57
39
  ```typescript
58
- Urn.of("galaxy"); // → NamespaceUrn
59
- Urn.of("galaxy", "principal"); // → ResourceUrn
60
- Urn.of("galaxy", "principal", "550e8400-e29b-41d4-a716-..."); // → IdentifierUrn
40
+ Urn.of("galaxy"); // → DomainUrn
41
+ Urn.of("galaxy", "principal"); // → ResourceTypeUrn
42
+ Urn.of("galaxy", "principal", "550e8400-e29b-41d4-a716-..."); // → ResourceIdUrn
61
43
  ```
62
44
 
63
45
  ### Urn.parse
64
46
 
65
- 从 URN 字符串解析为 `Urn` 实例。格式不合法时抛出 `Error`。返回宽类型 `Urn`,可通过类型守卫进一步窄化。
47
+ 从字符串解析为 `Urn` 实例。支持通配符 `*`(匹配单个段)和 `**`(匹配尾部任意段)。
66
48
 
67
49
  ```typescript
68
50
  static parse(urn: string): Urn
@@ -70,77 +52,98 @@ static parse(urn: string): Urn
70
52
 
71
53
  ```typescript
72
54
  const urn = Urn.parse("urn:buka:galaxy:principal:abc-123");
73
- urn.namespace; // → 'galaxy'
74
- urn.resource; // → 'principal'
75
- urn.identifier; // → 'abc-123'
55
+ urn.domain; // → 'galaxy'
56
+ urn.resourceType; // → 'principal'
57
+ urn.resourceId; // → 'abc-123'
76
58
 
77
- const nsUrn = Urn.parse("urn:buka:galaxy");
78
- nsUrn.namespace; // → 'galaxy'
79
- nsUrn.resource; // → undefined
80
- nsUrn.identifier; // → undefined
59
+ Urn.parse("urn:buka:galaxy:*:*").contains(urn); // true
81
60
  ```
82
61
 
83
- > 格式不合法时抛出 `Error: Invalid URN: <input>`。
84
-
85
62
  ### toString
86
63
 
87
64
  序列化为 URN 字符串。
88
65
 
89
66
  ```typescript
90
- toString(): string
91
- ```
92
-
93
- ```typescript
94
- Urn.of("galaxy", "principal", "abc-123").toString();
95
- // → 'urn:buka:galaxy:principal:abc-123'
67
+ const urn = Urn.of("galaxy", "principal", "abc-123");
68
+ urn.toString(); // → 'urn:buka:galaxy:principal:abc-123'
96
69
  ```
97
70
 
98
71
  ### is
99
72
 
100
- 判断当前 URN 是否匹配指定条件。仅比较传入的参数,未传入的参数不参与比较。
73
+ 判断当前 URN 是否匹配指定条件。仅比较传入的参数,未传入的不参与比较。
101
74
 
102
75
  ```typescript
103
- is(namespace: string, resource?: string, identifier?: string): boolean
76
+ is(domain: string, resourceType?: string, resourceId?: string): boolean
104
77
  ```
105
78
 
106
79
  ```typescript
107
80
  const urn = Urn.parse("urn:buka:galaxy:principal:abc-123");
108
81
 
109
- urn.is("galaxy"); // → true(仅匹配 namespace)
110
- urn.is("galaxy", "principal"); // → true
111
- urn.is("galaxy", "principal", "abc-123"); // → true
112
- urn.is("galaxy", "oauth2-client"); // → false
113
- urn.is("infra"); // → false
82
+ urn.is("galaxy"); // true
83
+ urn.is("galaxy", "principal"); // true
84
+ urn.is("galaxy", "principal", "abc-123"); // true
85
+ urn.is("galaxy", "oauth2-client"); // false
114
86
  ```
115
87
 
116
- ### withResource
88
+ ### contains
117
89
 
118
- 从 `NamespaceUrn` 衍生出 `ResourceUrn`。当前 URN 已有 `resource` 时抛出 `Error`。
90
+ 判断当前 URN **是否包含**另一个 URN。含通配符时作为集合模式使用。
119
91
 
120
92
  ```typescript
121
- withResource(resource: string): ResourceUrn // 仅 NamespaceUrn 可调用
93
+ contains(other: Urn): boolean
122
94
  ```
123
95
 
124
96
  ```typescript
125
- const ns = Urn.of("galaxy"); // NamespaceUrn
126
- const resource = ns.withResource("principal"); // ResourceUrn
97
+ const pattern = Urn.parse("urn:buka:galaxy:*:*");
98
+ pattern.contains(Urn.parse("urn:buka:galaxy:principal:123")); // true
99
+ pattern.contains(Urn.parse("urn:buka:galaxy:org:456")); // true
100
+ pattern.contains(Urn.parse("urn:buka:infra:org:456")); // false
127
101
 
128
- resource.withResource("other"); // ❌ 编译错误:ResourceUrn 上此方法为 never
102
+ const tail = Urn.parse("urn:buka:galaxy:principal:**");
103
+ tail.contains(Urn.parse("urn:buka:galaxy:principal")); // true
104
+ tail.contains(Urn.parse("urn:buka:galaxy:principal:123")); // true
105
+
106
+ // 具体 URN 仅包含自身
107
+ const concrete = Urn.parse("urn:buka:galaxy:principal:123");
108
+ concrete.contains(Urn.parse("urn:buka:galaxy:principal:123")); // true
109
+ concrete.contains(Urn.parse("urn:buka:galaxy:principal:456")); // false
129
110
  ```
130
111
 
131
- ### withIdentifier
112
+ ### withResourceType
132
113
 
133
- 从 `ResourceUrn` 衍生出 `IdentifierUrn`。当前 URN 无 `resource` 或已有 `identifier` 时抛出 `Error`。
114
+ 从概念域级 URN 衍生出资源类型级 URN。
134
115
 
135
116
  ```typescript
136
- withIdentifier(identifier: string): IdentifierUrn // 仅 ResourceUrn 可调用
117
+ withResourceType(resourceType: string): ResourceTypeUrn
137
118
  ```
138
119
 
139
120
  ```typescript
140
- const resource = Urn.of("galaxy", "principal");
141
- const instance = resource.withIdentifier("550e8400-..."); // IdentifierUrn
121
+ Urn.of("galaxy").withResourceType("principal"); // → ResourceTypeUrn
122
+ ```
142
123
 
143
- Urn.of("galaxy").withIdentifier("id"); // ❌ 编译错误:NamespaceUrn 上此方法为 never
124
+ ### withResourceId
125
+
126
+ 从资源类型级 URN 衍生出实例级 URN。
127
+
128
+ ```typescript
129
+ withResourceId(resourceId: string): ResourceIdUrn
130
+ ```
131
+
132
+ ```typescript
133
+ Urn.of("galaxy", "principal").withResourceId("550e8400-..."); // → ResourceIdUrn
134
+ ```
135
+
136
+ ### resourceTypeSegments
137
+
138
+ 返回 `resourceType` 按 `:` 拆分后的层级数组。
139
+
140
+ ```typescript
141
+ get resourceTypeSegments(): string[]
142
+ ```
143
+
144
+ ```typescript
145
+ Urn.parse("urn:buka:galaxy:principal:v1:table").resourceTypeSegments;
146
+ // → ['principal', 'v1', 'table']
144
147
  ```
145
148
 
146
149
  ### 类型守卫
@@ -148,20 +151,20 @@ Urn.of("galaxy").withIdentifier("id"); // ❌ 编译错误:NamespaceUrn 上此
148
151
  用于 `Urn.parse()` 返回的宽类型 `Urn` 的层级窄化:
149
152
 
150
153
  ```typescript
151
- isNamespaceUrn(): this is NamespaceUrn
152
- isResourceUrn(): this is ResourceUrn
153
- isIdentifierUrn(): this is IdentifierUrn
154
+ isDomainUrn(): this is DomainUrn
155
+ isResourceTypeUrn(): this is ResourceTypeUrn
156
+ isResourceIdUrn(): this is ResourceIdUrn
154
157
  ```
155
158
 
156
159
  ```typescript
157
160
  const urn = Urn.parse(token.sub);
158
161
 
159
- if (urn.isResourceUrn()) {
160
- urn.withIdentifier("some-id"); // ✅ 窄化后合法
162
+ if (urn.isResourceIdUrn()) {
163
+ urn.resourceId; // ✅ string,而非 string | undefined
161
164
  }
162
165
 
163
- if (urn.isIdentifierUrn()) {
164
- const id = urn.identifier; // ✅ 类型为 string(非 string | undefined)
166
+ if (urn.isResourceTypeUrn()) {
167
+ urn.withResourceId("id"); // ✅ 窄化后合法
165
168
  }
166
169
  ```
167
170
 
@@ -169,46 +172,37 @@ if (urn.isIdentifierUrn()) {
169
172
 
170
173
  ### JWT subject / audience
171
174
 
172
- 将 URN 用于 JWT 的 `sub` 和 `aud` 字段,使 token 自带主体类型信息:
173
-
174
175
  ```typescript
175
- // 签发 token
176
- const accessToken = await sessionService.signAccessToken({
176
+ // 签发
177
+ await sessionService.signAccessToken({
177
178
  subject: Urn.of("galaxy", "principal", principal.id).toString(),
178
- claims: {
179
- aud: Urn.of("galaxy").toString(),
180
- },
179
+ claims: { aud: Urn.of("galaxy").toString() },
181
180
  });
182
181
 
183
- // 消费 token —— 从 sub 中提取 principal ID
182
+ // 消费
184
183
  const subjectUrn = Urn.parse(payload.sub);
185
- if (subjectUrn.isIdentifierUrn() && subjectUrn.is("galaxy", "principal")) {
186
- const principalId = subjectUrn.identifier; // string,无需可选链
184
+ if (subjectUrn.isResourceIdUrn() && subjectUrn.is("galaxy", "principal")) {
185
+ const principalId = subjectUrn.resourceId;
187
186
  }
188
187
  ```
189
188
 
190
189
  ### 资源类型判断
191
190
 
192
- 在不引入额外字段的情况下,通过 URN 判断资源类型并执行分支逻辑:
193
-
194
191
  ```typescript
195
192
  const urn = Urn.parse(session.subject);
196
193
 
197
194
  if (urn.is("galaxy", "principal")) {
198
- // 处理 Principal 类型的 subject
195
+ // 处理 Principal 类型
199
196
  } else if (urn.is("galaxy", "oauth2-client")) {
200
- // 处理 OAuth2 Client 类型的 subject
197
+ // 处理 OAuth2 Client 类型
201
198
  }
202
199
  ```
203
200
 
204
- ### 派生方法逐层构建
205
-
206
- 当需要在运行时动态组装 URN 时,可通过派生方法链式构建:
201
+ ### 衍生复用
207
202
 
208
203
  ```typescript
209
- const principalType = Urn.of("galaxy").withResource("principal"); // ResourceUrn,可复用
204
+ const principalType = Urn.of("galaxy").withResourceType("principal");
210
205
 
211
- // 为不同 ID 生成实例级 URN
212
- const urn1 = principalType.withIdentifier(id1);
213
- const urn2 = principalType.withIdentifier(id2);
206
+ const urn1 = principalType.withResourceId(id1);
207
+ const urn2 = principalType.withResourceId(id2);
214
208
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@buka/nestjs-kit",
3
- "version": "4.1.9",
3
+ "version": "4.2.0",
4
4
  "description": "Buka Nestjs Kit",
5
5
  "packageManager": "pnpm@10.33.4+sha512.1c67b3b359b2d408119ba1ed289f34b8fc3c6873412bec6fd264fbdc82489e510fcbecb9ce9d22dae7f3b76269d8441046014bdca53b9979cd7a561ad631b800",
6
6
  "types": "dist/index.d.ts",