@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/dist/index.js
CHANGED
|
@@ -367,99 +367,71 @@ const URN_PREFIX = "urn:buka:";
|
|
|
367
367
|
/**
|
|
368
368
|
* 统一资源名称(URN)
|
|
369
369
|
*
|
|
370
|
-
* 格式: urn:buka:<domain>:<resource
|
|
370
|
+
* 格式: urn:buka:<domain>:<resource...>
|
|
371
371
|
*
|
|
372
|
-
* 支持三种粒度:
|
|
373
372
|
* - 概念域级: urn:buka:galaxy
|
|
374
|
-
* -
|
|
375
|
-
* - 实例级: urn:buka:galaxy:principal:550e8400-e29b-41d4-a716-446655440000
|
|
373
|
+
* - 资源级: urn:buka:galaxy:auth-client:galaxy:console
|
|
376
374
|
*
|
|
377
|
-
* domain
|
|
378
|
-
* resource
|
|
375
|
+
* domain 之后的全部段为 resource 路径(: 分隔),parser 不区分"类型"和"实例"。
|
|
376
|
+
* 应用层自行约定 resource 各段的语义。
|
|
379
377
|
*
|
|
380
|
-
* 推荐通过静态工厂方法或派生方法构建层级 URN:
|
|
381
378
|
* ```ts
|
|
382
|
-
* Urn.of('galaxy')
|
|
383
|
-
* Urn.of('galaxy', '
|
|
384
|
-
* Urn.of('galaxy', '
|
|
385
|
-
*
|
|
386
|
-
* //
|
|
387
|
-
* Urn.of('galaxy')
|
|
388
|
-
* .withResourceType('principal') // → ResourceTypeUrn
|
|
389
|
-
* .withResourceId('550e8400-...') // → ResourceIdUrn
|
|
379
|
+
* Urn.of('galaxy') // → 概念域级 URN
|
|
380
|
+
* Urn.of('galaxy', 'auth-client:galaxy:console') // → 资源级 URN
|
|
381
|
+
* Urn.of('galaxy', ['auth-client', 'galaxy', 'console'])
|
|
382
|
+
*
|
|
383
|
+
* // 或通过派生方法构建
|
|
384
|
+
* Urn.of('galaxy').withResource('auth-client', 'galaxy', 'console')
|
|
390
385
|
* ```
|
|
391
386
|
*/
|
|
392
387
|
var Urn = class Urn {
|
|
393
|
-
constructor(domain,
|
|
388
|
+
constructor(domain, resource) {
|
|
394
389
|
this.domain = domain;
|
|
395
|
-
this.
|
|
396
|
-
this.resourceId = resourceId;
|
|
390
|
+
this.resource = resource;
|
|
397
391
|
if (!/^(?:\*|[a-z][a-z0-9-]*)$/.test(domain)) throw new Error(`Invalid domain: ${domain}`);
|
|
398
|
-
if (
|
|
399
|
-
for (const seg of
|
|
392
|
+
if (resource !== void 0) {
|
|
393
|
+
for (const seg of resource.split(":")) if (!/^(?:\*{1,2}|[a-z0-9][a-z0-9-]*)$/.test(seg)) throw new Error(`Invalid resource segment: '${seg}' in '${resource}'`);
|
|
400
394
|
}
|
|
401
|
-
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* resource 按 `:` 拆分后的段数组
|
|
398
|
+
*/
|
|
399
|
+
get resourceSegments() {
|
|
400
|
+
return this.resource ? this.resource.split(":") : [];
|
|
402
401
|
}
|
|
403
402
|
/**
|
|
404
403
|
* 序列化为 URN 字符串
|
|
405
404
|
*/
|
|
406
405
|
toString() {
|
|
407
406
|
let urn = `${URN_PREFIX}${this.domain}`;
|
|
408
|
-
if (this.
|
|
409
|
-
if (this.resourceId) urn += `:${this.resourceId}`;
|
|
407
|
+
if (this.resource) urn += `:${this.resource}`;
|
|
410
408
|
return urn;
|
|
411
409
|
}
|
|
412
|
-
/**
|
|
413
|
-
* 返回 resourceType 按 `:` 拆分后的层级数组
|
|
414
|
-
*/
|
|
415
|
-
get resourceTypeSegments() {
|
|
416
|
-
return this.resourceType ? this.resourceType.split(":") : [];
|
|
417
|
-
}
|
|
418
410
|
toSegments() {
|
|
419
411
|
const segments = [this.domain];
|
|
420
|
-
if (this.
|
|
421
|
-
if (this.resourceId) segments.push(this.resourceId);
|
|
412
|
+
if (this.resource) segments.push(...this.resource.split(":"));
|
|
422
413
|
return segments;
|
|
423
414
|
}
|
|
424
415
|
/**
|
|
425
|
-
*
|
|
416
|
+
* 从概念域级 URN 衍生出资源级 URN
|
|
426
417
|
*
|
|
427
|
-
* @throws Error 当前 URN 已有
|
|
418
|
+
* @throws Error 当前 URN 已有 resource 时抛出
|
|
428
419
|
*/
|
|
429
|
-
|
|
430
|
-
if (this.
|
|
431
|
-
return new Urn(this.domain,
|
|
420
|
+
withResource(...segments) {
|
|
421
|
+
if (this.resource !== void 0) throw new Error(`Cannot derive resource URN from a URN that already has a resource: ${this.toString()}`);
|
|
422
|
+
return new Urn(this.domain, segments.join(":"));
|
|
432
423
|
}
|
|
433
424
|
/**
|
|
434
|
-
*
|
|
435
|
-
*
|
|
436
|
-
* @throws Error 当前 URN 无 resourceType 或已有 resourceId 时抛出
|
|
437
|
-
*/
|
|
438
|
-
withResourceId(resourceId) {
|
|
439
|
-
if (this.resourceType === void 0) throw new Error(`Cannot derive ResourceIdUrn from a URN without a resourceType: ${this.toString()}`);
|
|
440
|
-
if (this.resourceId !== void 0) throw new Error(`Cannot derive ResourceIdUrn from a URN that already has a resourceId: ${this.toString()}`);
|
|
441
|
-
return new Urn(this.domain, this.resourceType, resourceId);
|
|
442
|
-
}
|
|
443
|
-
/**
|
|
444
|
-
* 类型守卫:判断是否为概念域级 URN
|
|
425
|
+
* 类型守卫:判断是否为概念域级 URN(无 resource)
|
|
445
426
|
*/
|
|
446
427
|
isDomainUrn() {
|
|
447
|
-
return this.
|
|
428
|
+
return this.resource === void 0;
|
|
448
429
|
}
|
|
449
430
|
/**
|
|
450
|
-
*
|
|
431
|
+
* 根据参数创建对应层级的 URN
|
|
451
432
|
*/
|
|
452
|
-
|
|
453
|
-
return
|
|
454
|
-
}
|
|
455
|
-
/**
|
|
456
|
-
* 类型守卫:判断是否为实例级 URN
|
|
457
|
-
*/
|
|
458
|
-
isResourceIdUrn() {
|
|
459
|
-
return this.resourceType !== void 0 && this.resourceId !== void 0;
|
|
460
|
-
}
|
|
461
|
-
static of(domain, resourceType, resourceId) {
|
|
462
|
-
return new Urn(domain, resourceType, resourceId);
|
|
433
|
+
static of(domain, resource) {
|
|
434
|
+
return new Urn(domain, resource === void 0 ? void 0 : Array.isArray(resource) ? resource.join(":") : resource);
|
|
463
435
|
}
|
|
464
436
|
/**
|
|
465
437
|
* 从 URN 字符串解析,支持通配符 `*` 和 `**`
|
|
@@ -486,31 +458,30 @@ var Urn = class Urn {
|
|
|
486
458
|
}
|
|
487
459
|
if (segments.length === 0) throw new Error(`Invalid URN: ${urn}`);
|
|
488
460
|
const domain = segments[0];
|
|
489
|
-
|
|
490
|
-
let resourceId;
|
|
491
|
-
if (segments[segments.length - 1] === "**") {
|
|
492
|
-
if (segments.length === 1) {} else if (segments.length === 2) resourceType = segments[1] !== "**" ? segments[1] : void 0;
|
|
493
|
-
else resourceType = segments.slice(1, -1).join(":");
|
|
494
|
-
resourceId = "**";
|
|
495
|
-
} else if (segments.length === 2) resourceType = segments[1];
|
|
496
|
-
else if (segments.length >= 3) {
|
|
497
|
-
resourceType = segments.slice(1, -1).join(":");
|
|
498
|
-
resourceId = segments[segments.length - 1];
|
|
499
|
-
}
|
|
500
|
-
return new Urn(domain, resourceType, resourceId);
|
|
461
|
+
return new Urn(domain, segments.length > 1 ? segments.slice(1).join(":") : void 0);
|
|
501
462
|
}
|
|
502
463
|
/**
|
|
503
|
-
* 判断是否匹配指定的 domain
|
|
464
|
+
* 判断是否匹配指定的 domain 和 resource
|
|
504
465
|
*
|
|
505
|
-
*
|
|
466
|
+
* 未传入的参数不参与比较。
|
|
506
467
|
*/
|
|
507
|
-
is(domain,
|
|
468
|
+
is(domain, resource) {
|
|
508
469
|
if (this.domain !== domain) return false;
|
|
509
|
-
if (
|
|
510
|
-
if (resourceId !== void 0 && this.resourceId !== resourceId) return false;
|
|
470
|
+
if (resource !== void 0 && this.resource !== resource) return false;
|
|
511
471
|
return true;
|
|
512
472
|
}
|
|
513
473
|
/**
|
|
474
|
+
* 判断当前 URN 是否匹配指定的 URN 模式(支持通配符)
|
|
475
|
+
*
|
|
476
|
+
* ```ts
|
|
477
|
+
* urn.match('urn:buka:galaxy:principal:*') // resource 以 principal: 开头
|
|
478
|
+
* urn.match('urn:buka:galaxy:auth-client:**') // 任意深度的 auth-client
|
|
479
|
+
* ```
|
|
480
|
+
*/
|
|
481
|
+
match(pattern) {
|
|
482
|
+
return Urn.parse(pattern).contains(this);
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
514
485
|
* 判断当前 URN 是否包含另一个 URN
|
|
515
486
|
*
|
|
516
487
|
* 当前 URN 含通配符(`*` 或 `**`)时,作为集合模式判断:
|
|
@@ -537,8 +508,6 @@ var Urn = class Urn {
|
|
|
537
508
|
//#region src/modules/core/decorators/class-validator/is-urn.ts
|
|
538
509
|
const IS_URN = "isUrn";
|
|
539
510
|
const IS_DOMAIN_URN = "isDomainUrn";
|
|
540
|
-
const IS_RESOURCE_TYPE_URN = "isResourceTypeUrn";
|
|
541
|
-
const IS_RESOURCE_ID_URN = "isResourceIdUrn";
|
|
542
511
|
/**
|
|
543
512
|
* 验证字符串是否为合法的 URN(任意层级)。
|
|
544
513
|
*
|
|
@@ -609,74 +578,6 @@ function IsDomainUrn(validationOptions) {
|
|
|
609
578
|
};
|
|
610
579
|
}
|
|
611
580
|
/**
|
|
612
|
-
* 验证字符串是否为资源类型级 URN(含 domain 和 resourceType,无 resourceId)。
|
|
613
|
-
*
|
|
614
|
-
* @example
|
|
615
|
-
* ```typescript
|
|
616
|
-
* class ResourceDTO {
|
|
617
|
-
* @IsResourceTypeUrn()
|
|
618
|
-
* resourceType: string
|
|
619
|
-
* }
|
|
620
|
-
* ```
|
|
621
|
-
*/
|
|
622
|
-
function IsResourceTypeUrn(validationOptions) {
|
|
623
|
-
return function(object, propertyName) {
|
|
624
|
-
(0, class_validator.registerDecorator)({
|
|
625
|
-
name: IS_RESOURCE_TYPE_URN,
|
|
626
|
-
target: object.constructor,
|
|
627
|
-
propertyName,
|
|
628
|
-
options: validationOptions,
|
|
629
|
-
validator: {
|
|
630
|
-
validate(value) {
|
|
631
|
-
if (typeof value !== "string") return false;
|
|
632
|
-
try {
|
|
633
|
-
return Urn.parse(value).isResourceTypeUrn();
|
|
634
|
-
} catch {
|
|
635
|
-
return false;
|
|
636
|
-
}
|
|
637
|
-
},
|
|
638
|
-
defaultMessage() {
|
|
639
|
-
return "$property must be a resource-type-level URN (urn:buka:<domain>:<resource-type>)";
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
});
|
|
643
|
-
};
|
|
644
|
-
}
|
|
645
|
-
/**
|
|
646
|
-
* 验证字符串是否为实例级 URN(含完整三段:domain、resourceType 和 resourceId)。
|
|
647
|
-
*
|
|
648
|
-
* @example
|
|
649
|
-
* ```typescript
|
|
650
|
-
* class TokenPayloadDTO {
|
|
651
|
-
* @IsResourceIdUrn()
|
|
652
|
-
* sub: string
|
|
653
|
-
* }
|
|
654
|
-
* ```
|
|
655
|
-
*/
|
|
656
|
-
function IsResourceIdUrn(validationOptions) {
|
|
657
|
-
return function(object, propertyName) {
|
|
658
|
-
(0, class_validator.registerDecorator)({
|
|
659
|
-
name: IS_RESOURCE_ID_URN,
|
|
660
|
-
target: object.constructor,
|
|
661
|
-
propertyName,
|
|
662
|
-
options: validationOptions,
|
|
663
|
-
validator: {
|
|
664
|
-
validate(value) {
|
|
665
|
-
if (typeof value !== "string") return false;
|
|
666
|
-
try {
|
|
667
|
-
return Urn.parse(value).isResourceIdUrn();
|
|
668
|
-
} catch {
|
|
669
|
-
return false;
|
|
670
|
-
}
|
|
671
|
-
},
|
|
672
|
-
defaultMessage() {
|
|
673
|
-
return "$property must be a resource-id-level URN (urn:buka:<domain>:<resource-type>:<resource-id>)";
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
});
|
|
677
|
-
};
|
|
678
|
-
}
|
|
679
|
-
/**
|
|
680
581
|
* 验证字符串是否匹配指定的 URN 模式(支持通配符 `*` 和 `**`)。
|
|
681
582
|
*
|
|
682
583
|
* - `*` 匹配恰好一个段
|
|
@@ -10249,16 +10150,12 @@ exports.HasAnyKey = HasAnyKey;
|
|
|
10249
10150
|
exports.HttpException = HttpException;
|
|
10250
10151
|
exports.IS_CROCKFORD_BASE32 = IS_CROCKFORD_BASE32;
|
|
10251
10152
|
exports.IS_DOMAIN_URN = IS_DOMAIN_URN;
|
|
10252
|
-
exports.IS_RESOURCE_ID_URN = IS_RESOURCE_ID_URN;
|
|
10253
|
-
exports.IS_RESOURCE_TYPE_URN = IS_RESOURCE_TYPE_URN;
|
|
10254
10153
|
exports.IS_SCALAR_DICTIONARY = IS_SCALAR_DICTIONARY;
|
|
10255
10154
|
exports.IS_URN = IS_URN;
|
|
10256
10155
|
exports.InjectLogger = InjectLogger;
|
|
10257
10156
|
exports.IsBrowserRequest = IsBrowserRequest;
|
|
10258
10157
|
exports.IsCrockfordBase32 = IsCrockfordBase32;
|
|
10259
10158
|
exports.IsDomainUrn = IsDomainUrn;
|
|
10260
|
-
exports.IsResourceIdUrn = IsResourceIdUrn;
|
|
10261
|
-
exports.IsResourceTypeUrn = IsResourceTypeUrn;
|
|
10262
10159
|
exports.IsScalar = IsScalar;
|
|
10263
10160
|
exports.IsScalarDictionary = IsScalarDictionary;
|
|
10264
10161
|
exports.IsUrn = IsUrn;
|