@buka/nestjs-kit 4.1.8 → 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.
- package/CHANGELOG.md +14 -0
- package/dist/index.d.mts +90 -66
- package/dist/index.d.ts +90 -66
- package/dist/index.js +139 -72
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +134 -67
- package/dist/index.mjs.map +1 -1
- package/docs/core/index.md +1 -1
- package/docs/core/urn.md +93 -99
- package/package.json +1 -1
package/docs/core/index.md
CHANGED
|
@@ -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`、`
|
|
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`
|
|
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
|
-
##
|
|
11
|
+
## 格式
|
|
17
12
|
|
|
18
13
|
```
|
|
19
|
-
urn:buka:<
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
31
|
+
根据参数数量创建对应层级的 URN,返回类型在编译期自动窄化。
|
|
50
32
|
|
|
51
33
|
```typescript
|
|
52
|
-
static of(
|
|
53
|
-
static of(
|
|
54
|
-
static of(
|
|
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"); // →
|
|
59
|
-
Urn.of("galaxy", "principal"); // →
|
|
60
|
-
Urn.of("galaxy", "principal", "550e8400-e29b-41d4-a716-..."); // →
|
|
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
|
-
|
|
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.
|
|
74
|
-
urn.
|
|
75
|
-
urn.
|
|
55
|
+
urn.domain; // → 'galaxy'
|
|
56
|
+
urn.resourceType; // → 'principal'
|
|
57
|
+
urn.resourceId; // → 'abc-123'
|
|
76
58
|
|
|
77
|
-
|
|
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
|
-
|
|
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(
|
|
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");
|
|
110
|
-
urn.is("galaxy", "principal");
|
|
111
|
-
urn.is("galaxy", "principal", "abc-123");
|
|
112
|
-
urn.is("galaxy", "oauth2-client");
|
|
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
|
-
###
|
|
88
|
+
### contains
|
|
117
89
|
|
|
118
|
-
|
|
90
|
+
判断当前 URN **是否包含**另一个 URN。含通配符时作为集合模式使用。
|
|
119
91
|
|
|
120
92
|
```typescript
|
|
121
|
-
|
|
93
|
+
contains(other: Urn): boolean
|
|
122
94
|
```
|
|
123
95
|
|
|
124
96
|
```typescript
|
|
125
|
-
const
|
|
126
|
-
|
|
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
|
-
|
|
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
|
-
###
|
|
112
|
+
### withResourceType
|
|
132
113
|
|
|
133
|
-
|
|
114
|
+
从概念域级 URN 衍生出资源类型级 URN。
|
|
134
115
|
|
|
135
116
|
```typescript
|
|
136
|
-
|
|
117
|
+
withResourceType(resourceType: string): ResourceTypeUrn
|
|
137
118
|
```
|
|
138
119
|
|
|
139
120
|
```typescript
|
|
140
|
-
|
|
141
|
-
|
|
121
|
+
Urn.of("galaxy").withResourceType("principal"); // → ResourceTypeUrn
|
|
122
|
+
```
|
|
142
123
|
|
|
143
|
-
|
|
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
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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.
|
|
160
|
-
urn.
|
|
162
|
+
if (urn.isResourceIdUrn()) {
|
|
163
|
+
urn.resourceId; // ✅ string,而非 string | undefined
|
|
161
164
|
}
|
|
162
165
|
|
|
163
|
-
if (urn.
|
|
164
|
-
|
|
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
|
-
// 签发
|
|
176
|
-
|
|
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
|
-
// 消费
|
|
182
|
+
// 消费
|
|
184
183
|
const subjectUrn = Urn.parse(payload.sub);
|
|
185
|
-
if (subjectUrn.
|
|
186
|
-
const principalId = subjectUrn.
|
|
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
|
|
195
|
+
// 处理 Principal 类型
|
|
199
196
|
} else if (urn.is("galaxy", "oauth2-client")) {
|
|
200
|
-
// 处理 OAuth2 Client
|
|
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").
|
|
204
|
+
const principalType = Urn.of("galaxy").withResourceType("principal");
|
|
210
205
|
|
|
211
|
-
|
|
212
|
-
const
|
|
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.
|
|
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",
|