@buka/nestjs-kit 4.3.3 → 4.3.4
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 +7 -0
- package/dist/index.d.mts +35 -117
- package/dist/index.d.ts +35 -117
- package/dist/index.js +48 -151
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +49 -148
- package/dist/index.mjs.map +1 -1
- package/docs/core/urn.md +66 -64
- package/package.json +1 -1
package/docs/core/urn.md
CHANGED
|
@@ -5,21 +5,22 @@
|
|
|
5
5
|
`Urn` 是 `urn:buka` 统一资源名称的解析与操作工具类,无需注册模块即可使用。
|
|
6
6
|
|
|
7
7
|
```typescript
|
|
8
|
-
import { Urn
|
|
8
|
+
import { Urn } from "@buka/nestjs-kit";
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
## 格式
|
|
12
12
|
|
|
13
13
|
```
|
|
14
|
-
urn:buka:<domain>:<resource
|
|
14
|
+
urn:buka:<domain>:<resource...>
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
- **domain**:概念域,按资源类型定义权归属划分,通常为系统名
|
|
18
|
+
- **resource**:domain 之后的完整资源路径(`:` 分隔,零到多段)。parser 不区分"类型"和"实例",应用层自行约定各段语义
|
|
18
19
|
|
|
19
20
|
```typescript
|
|
20
|
-
Urn.of("galaxy");
|
|
21
|
-
Urn.of("galaxy", "
|
|
22
|
-
Urn.of("galaxy", "
|
|
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
|
-
|
|
32
|
+
根据参数创建 URN。第二参数可为 `: ` 分隔的字符串或 `string[]`。
|
|
32
33
|
|
|
33
34
|
```typescript
|
|
34
|
-
static of(domain: string):
|
|
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");
|
|
41
|
-
Urn.of("galaxy", "principal");
|
|
42
|
-
Urn.of("galaxy", "principal", "
|
|
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;
|
|
56
|
-
urn.
|
|
57
|
-
urn.
|
|
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,
|
|
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");
|
|
83
|
-
urn.is("galaxy", "principal");
|
|
84
|
-
urn.is("galaxy", "principal"
|
|
85
|
-
|
|
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
|
-
###
|
|
128
|
+
### withResource
|
|
113
129
|
|
|
114
|
-
从概念域级 URN
|
|
130
|
+
从概念域级 URN 衍生出资源级 URN。
|
|
115
131
|
|
|
116
132
|
```typescript
|
|
117
|
-
|
|
133
|
+
withResource(...segments: string[]): Urn
|
|
118
134
|
```
|
|
119
135
|
|
|
120
136
|
```typescript
|
|
121
|
-
Urn.of("galaxy").
|
|
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
|
-
|
|
129
|
-
|
|
140
|
+
Urn.of("galaxy").withResource("auth-client", "galaxy", "console");
|
|
141
|
+
// → urn:buka:galaxy:auth-client:galaxy:console
|
|
130
142
|
```
|
|
131
143
|
|
|
132
|
-
|
|
133
|
-
Urn.of("galaxy", "principal").withResourceId("550e8400-..."); // → ResourceIdUrn
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
### resourceTypeSegments
|
|
144
|
+
### resourceSegments
|
|
137
145
|
|
|
138
|
-
返回 `
|
|
146
|
+
返回 `resource` 按 `:` 拆分后的段数组。
|
|
139
147
|
|
|
140
148
|
```typescript
|
|
141
|
-
get
|
|
149
|
+
get resourceSegments(): string[]
|
|
142
150
|
```
|
|
143
151
|
|
|
144
152
|
```typescript
|
|
145
|
-
Urn.parse("urn:buka:galaxy:
|
|
146
|
-
// → ['
|
|
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
|
-
|
|
159
|
+
类型守卫:判断是否为概念域级 URN(无 resource)。
|
|
152
160
|
|
|
153
161
|
```typescript
|
|
154
|
-
isDomainUrn():
|
|
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(
|
|
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.
|
|
167
|
-
urn.
|
|
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.
|
|
185
|
-
const principalId = subjectUrn.
|
|
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.
|
|
196
|
+
if (urn.match("urn:buka:galaxy:principal:*")) {
|
|
195
197
|
// 处理 Principal 类型
|
|
196
|
-
} else if (urn.
|
|
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
|
|
206
|
+
const domainUrn = Urn.of("galaxy");
|
|
205
207
|
|
|
206
|
-
const urn1 =
|
|
207
|
-
const urn2 =
|
|
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
|
+
"version": "4.3.4",
|
|
4
4
|
"description": "Buka Nestjs Kit",
|
|
5
5
|
"packageManager": "pnpm@10.33.4+sha512.1c67b3b359b2d408119ba1ed289f34b8fc3c6873412bec6fd264fbdc82489e510fcbecb9ce9d22dae7f3b76269d8441046014bdca53b9979cd7a561ad631b800",
|
|
6
6
|
"types": "dist/index.d.ts",
|