@buka/nestjs-kit 4.3.3 → 4.3.5

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/docs/core/urn.md CHANGED
@@ -5,21 +5,22 @@
5
5
  `Urn` 是 `urn:buka` 统一资源名称的解析与操作工具类,无需注册模块即可使用。
6
6
 
7
7
  ```typescript
8
- import { Urn, DomainUrn, ResourceTypeUrn, ResourceIdUrn } from "@buka/nestjs-kit";
8
+ import { Urn } from "@buka/nestjs-kit";
9
9
  ```
10
10
 
11
11
  ## 格式
12
12
 
13
13
  ```
14
- urn:buka:<domain>:<resource-type>[:<resource-id>]
14
+ urn:buka:<domain>:<resource...>
15
15
  ```
16
16
 
17
- 三段分别对应**概念域**(资源类型定义权归属)、**资源类型**(可多级,第一级推荐用于 `env`)、**资源ID**(可选)。
17
+ - **domain**:概念域,按资源类型定义权归属划分,通常为系统名
18
+ - **resource**:domain 之后的完整资源路径(`:` 分隔,零到多段)。parser 不区分"类型"和"实例",应用层自行约定各段语义
18
19
 
19
20
  ```typescript
20
- Urn.of("galaxy"); // 概念域级
21
- Urn.of("galaxy", "principal"); // 资源类型级
22
- Urn.of("galaxy", "principal", "550e8400-e29b-..."); // 实例级
21
+ Urn.of("galaxy"); // 概念域级
22
+ Urn.of("galaxy", "auth-client:galaxy:console"); // 资源级(字符串)
23
+ Urn.of("galaxy", ["auth-client", "galaxy", "console"]); // 资源级(数组)
23
24
  ```
24
25
 
25
26
  > 格式规则详见 [URN 规范](https://github.com/buka-lib/docs.specifications/tree/main/urn)。
@@ -28,18 +29,17 @@ Urn.of("galaxy", "principal", "550e8400-e29b-..."); // 实例级
28
29
 
29
30
  ### Urn.of
30
31
 
31
- 根据参数数量创建对应层级的 URN,返回类型在编译期自动窄化。
32
+ 根据参数创建 URN。第二参数可为 `: ` 分隔的字符串或 `string[]`。
32
33
 
33
34
  ```typescript
34
- static of(domain: string): DomainUrn
35
- static of(domain: string, resourceType: string): ResourceTypeUrn
36
- static of(domain: string, resourceType: string, resourceId: string): ResourceIdUrn
35
+ static of(domain: string, resource?: string | string[]): Urn
37
36
  ```
38
37
 
39
38
  ```typescript
40
- Urn.of("galaxy"); // → DomainUrn
41
- Urn.of("galaxy", "principal"); // → ResourceTypeUrn
42
- Urn.of("galaxy", "principal", "550e8400-e29b-41d4-a716-..."); // → ResourceIdUrn
39
+ Urn.of("galaxy"); // 概念域级
40
+ Urn.of("galaxy", "principal:abc-123"); // 资源级
41
+ Urn.of("galaxy", ["principal", "abc-123"]); // 资源级(数组)
42
+ Urn.of("galaxy", ["auth-client", "galaxy", "console"]); // 多段资源路径
43
43
  ```
44
44
 
45
45
  ### Urn.parse
@@ -52,9 +52,9 @@ static parse(urn: string): Urn
52
52
 
53
53
  ```typescript
54
54
  const urn = Urn.parse("urn:buka:galaxy:principal:abc-123");
55
- urn.domain; // → 'galaxy'
56
- urn.resourceType; // → 'principal'
57
- urn.resourceId; // → 'abc-123'
55
+ urn.domain; // → 'galaxy'
56
+ urn.resource; // → 'principal:abc-123'
57
+ urn.resourceSegments; // → ['principal', 'abc-123']
58
58
 
59
59
  Urn.parse("urn:buka:galaxy:*:*").contains(urn); // true
60
60
  ```
@@ -64,25 +64,41 @@ Urn.parse("urn:buka:galaxy:*:*").contains(urn); // true
64
64
  序列化为 URN 字符串。
65
65
 
66
66
  ```typescript
67
- const urn = Urn.of("galaxy", "principal", "abc-123");
67
+ const urn = Urn.of("galaxy", ["principal", "abc-123"]);
68
68
  urn.toString(); // → 'urn:buka:galaxy:principal:abc-123'
69
69
  ```
70
70
 
71
71
  ### is
72
72
 
73
- 判断当前 URN 是否匹配指定条件。仅比较传入的参数,未传入的不参与比较。
73
+ 判断当前 URN 是否匹配指定的 domain 和 resource。仅比较传入的参数,未传入的不参与比较。
74
74
 
75
75
  ```typescript
76
- is(domain: string, resourceType?: string, resourceId?: string): boolean
76
+ is(domain: string, resource?: string): boolean
77
77
  ```
78
78
 
79
79
  ```typescript
80
80
  const urn = Urn.parse("urn:buka:galaxy:principal:abc-123");
81
81
 
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
82
+ urn.is("galaxy"); // true
83
+ urn.is("galaxy", "principal:abc-123"); // true(精确匹配 resource)
84
+ urn.is("galaxy", "principal"); // false(resource 不精确匹配)
85
+ ```
86
+
87
+ ### match
88
+
89
+ 判断当前 URN 是否匹配指定的 URN 模式(支持通配符)。
90
+
91
+ ```typescript
92
+ match(pattern: string): boolean
93
+ ```
94
+
95
+ ```typescript
96
+ const urn = Urn.parse("urn:buka:galaxy:principal:abc-123");
97
+
98
+ urn.match("urn:buka:galaxy:principal:*"); // true
99
+ urn.match("urn:buka:galaxy:principal:**"); // true
100
+ urn.match("urn:buka:galaxy:*:*"); // true
101
+ urn.match("urn:buka:galaxy:oauth2-client:*"); // false
86
102
  ```
87
103
 
88
104
  ### contains
@@ -109,62 +125,48 @@ concrete.contains(Urn.parse("urn:buka:galaxy:principal:123")); // true
109
125
  concrete.contains(Urn.parse("urn:buka:galaxy:principal:456")); // false
110
126
  ```
111
127
 
112
- ### withResourceType
128
+ ### withResource
113
129
 
114
- 从概念域级 URN 衍生出资源类型级 URN。
130
+ 从概念域级 URN 衍生出资源级 URN。
115
131
 
116
132
  ```typescript
117
- withResourceType(resourceType: string): ResourceTypeUrn
133
+ withResource(...segments: string[]): Urn
118
134
  ```
119
135
 
120
136
  ```typescript
121
- Urn.of("galaxy").withResourceType("principal"); // → ResourceTypeUrn
122
- ```
123
-
124
- ### withResourceId
125
-
126
- 从资源类型级 URN 衍生出实例级 URN。
137
+ Urn.of("galaxy").withResource("principal", "abc-123");
138
+ // → urn:buka:galaxy:principal:abc-123
127
139
 
128
- ```typescript
129
- withResourceId(resourceId: string): ResourceIdUrn
140
+ Urn.of("galaxy").withResource("auth-client", "galaxy", "console");
141
+ // → urn:buka:galaxy:auth-client:galaxy:console
130
142
  ```
131
143
 
132
- ```typescript
133
- Urn.of("galaxy", "principal").withResourceId("550e8400-..."); // → ResourceIdUrn
134
- ```
135
-
136
- ### resourceTypeSegments
144
+ ### resourceSegments
137
145
 
138
- 返回 `resourceType` 按 `:` 拆分后的层级数组。
146
+ 返回 `resource` 按 `:` 拆分后的段数组。
139
147
 
140
148
  ```typescript
141
- get resourceTypeSegments(): string[]
149
+ get resourceSegments(): string[]
142
150
  ```
143
151
 
144
152
  ```typescript
145
- Urn.parse("urn:buka:galaxy:principal:v1:table").resourceTypeSegments;
146
- // → ['principal', 'v1', 'table']
153
+ Urn.parse("urn:buka:galaxy:auth-client:galaxy:console").resourceSegments;
154
+ // → ['auth-client', 'galaxy', 'console']
147
155
  ```
148
156
 
149
- ### 类型守卫
157
+ ### isDomainUrn
150
158
 
151
- 用于 `Urn.parse()` 返回的宽类型 `Urn` 的层级窄化:
159
+ 类型守卫:判断是否为概念域级 URN(无 resource)。
152
160
 
153
161
  ```typescript
154
- isDomainUrn(): this is DomainUrn
155
- isResourceTypeUrn(): this is ResourceTypeUrn
156
- isResourceIdUrn(): this is ResourceIdUrn
162
+ isDomainUrn(): boolean
157
163
  ```
158
164
 
159
165
  ```typescript
160
- const urn = Urn.parse(token.sub);
161
-
162
- if (urn.isResourceIdUrn()) {
163
- urn.resourceId; // ✅ string,而非 string | undefined
164
- }
166
+ const urn = Urn.parse("urn:buka:galaxy");
165
167
 
166
- if (urn.isResourceTypeUrn()) {
167
- urn.withResourceId("id"); // ✅ 窄化后合法
168
+ if (urn.isDomainUrn()) {
169
+ // urn.resource 为 undefined
168
170
  }
169
171
  ```
170
172
 
@@ -175,25 +177,25 @@ if (urn.isResourceTypeUrn()) {
175
177
  ```typescript
176
178
  // 签发
177
179
  await sessionService.signAccessToken({
178
- subject: Urn.of("galaxy", "principal", principal.id).toString(),
180
+ subject: Urn.of("galaxy", ["principal", principal.id]).toString(),
179
181
  claims: { aud: Urn.of("galaxy").toString() },
180
182
  });
181
183
 
182
184
  // 消费
183
185
  const subjectUrn = Urn.parse(payload.sub);
184
- if (subjectUrn.isResourceIdUrn() && subjectUrn.is("galaxy", "principal")) {
185
- const principalId = subjectUrn.resourceId;
186
+ if (subjectUrn.match("urn:buka:galaxy:principal:*")) {
187
+ const principalId = subjectUrn.resourceSegments[1];
186
188
  }
187
189
  ```
188
190
 
189
- ### 资源类型判断
191
+ ### 资源路径判断
190
192
 
191
193
  ```typescript
192
194
  const urn = Urn.parse(session.subject);
193
195
 
194
- if (urn.is("galaxy", "principal")) {
196
+ if (urn.match("urn:buka:galaxy:principal:*")) {
195
197
  // 处理 Principal 类型
196
- } else if (urn.is("galaxy", "oauth2-client")) {
198
+ } else if (urn.match("urn:buka:galaxy:oauth2-client:*")) {
197
199
  // 处理 OAuth2 Client 类型
198
200
  }
199
201
  ```
@@ -201,8 +203,8 @@ if (urn.is("galaxy", "principal")) {
201
203
  ### 衍生复用
202
204
 
203
205
  ```typescript
204
- const principalType = Urn.of("galaxy").withResourceType("principal");
206
+ const domainUrn = Urn.of("galaxy");
205
207
 
206
- const urn1 = principalType.withResourceId(id1);
207
- const urn2 = principalType.withResourceId(id2);
208
+ const urn1 = domainUrn.withResource("principal", id1);
209
+ const urn2 = domainUrn.withResource("principal", id2);
208
210
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@buka/nestjs-kit",
3
- "version": "4.3.3",
3
+ "version": "4.3.5",
4
4
  "description": "Buka Nestjs Kit",
5
5
  "packageManager": "pnpm@10.33.4+sha512.1c67b3b359b2d408119ba1ed289f34b8fc3c6873412bec6fd264fbdc82489e510fcbecb9ce9d22dae7f3b76269d8441046014bdca53b9979cd7a561ad631b800",
6
6
  "types": "dist/index.d.ts",