@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/CHANGELOG.md +14 -0
- package/dist/index.d.mts +35 -117
- package/dist/index.d.ts +35 -117
- package/dist/index.js +67 -155
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +68 -152
- 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
|
-
*
|
|
426
|
-
*
|
|
427
|
-
* @throws Error 当前 URN 已有 resourceType 时抛出
|
|
428
|
-
*/
|
|
429
|
-
withResourceType(resourceType) {
|
|
430
|
-
if (this.resourceType !== void 0) throw new Error(`Cannot derive ResourceTypeUrn from a URN that already has a resourceType: ${this.toString()}`);
|
|
431
|
-
return new Urn(this.domain, resourceType);
|
|
432
|
-
}
|
|
433
|
-
/**
|
|
434
|
-
* 衍生出实例级 URN
|
|
416
|
+
* 从概念域级 URN 衍生出资源级 URN
|
|
435
417
|
*
|
|
436
|
-
* @throws Error 当前 URN
|
|
418
|
+
* @throws Error 当前 URN 已有 resource 时抛出
|
|
437
419
|
*/
|
|
438
|
-
|
|
439
|
-
if (this.
|
|
440
|
-
|
|
441
|
-
return new Urn(this.domain, this.resourceType, resourceId);
|
|
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(":"));
|
|
442
423
|
}
|
|
443
424
|
/**
|
|
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
|
* - `*` 匹配恰好一个段
|
|
@@ -2881,6 +2782,21 @@ function resolveEntityType(entityRef, eager) {
|
|
|
2881
2782
|
if (!cls) return Object;
|
|
2882
2783
|
return eager ? cls : PrimaryKeyType(cls);
|
|
2883
2784
|
}
|
|
2785
|
+
/**
|
|
2786
|
+
* 为 Swagger schema 解析实体类引用。始终返回完整实体类(非 PrimaryKeyType),
|
|
2787
|
+
* 保证 `$ref` 指向正确注册的 schema。
|
|
2788
|
+
*
|
|
2789
|
+
* 返回函数形式以支持延迟求值,避免装饰器执行时的循环引用问题。
|
|
2790
|
+
*/
|
|
2791
|
+
function resolveEntitySchemaType(entityRef) {
|
|
2792
|
+
return () => {
|
|
2793
|
+
if (!entityRef) return Object;
|
|
2794
|
+
const result = entityRef();
|
|
2795
|
+
const entityName = Array.isArray(result) ? result[0] : result;
|
|
2796
|
+
if (!entityName) return Object;
|
|
2797
|
+
return resolveEntityClass(entityName) || Object;
|
|
2798
|
+
};
|
|
2799
|
+
}
|
|
2884
2800
|
//#endregion
|
|
2885
2801
|
//#region src/modules/mikro-orm/decorators/cardinality/many-to-one.cardinality.ts
|
|
2886
2802
|
/**
|
|
@@ -2993,8 +2909,8 @@ function OneToMany(options) {
|
|
|
2993
2909
|
},
|
|
2994
2910
|
schema: {
|
|
2995
2911
|
description: options.comment,
|
|
2996
|
-
type:
|
|
2997
|
-
|
|
2912
|
+
type: resolveEntitySchemaType(entityRef),
|
|
2913
|
+
isArray: true
|
|
2998
2914
|
}
|
|
2999
2915
|
})(target, propertyKey);
|
|
3000
2916
|
};
|
|
@@ -3034,8 +2950,8 @@ function ManyToMany(entity, mappedBy, options) {
|
|
|
3034
2950
|
},
|
|
3035
2951
|
schema: {
|
|
3036
2952
|
description: resolvedOptions?.comment,
|
|
3037
|
-
type:
|
|
3038
|
-
|
|
2953
|
+
type: resolveEntitySchemaType(entityRef),
|
|
2954
|
+
isArray: true
|
|
3039
2955
|
}
|
|
3040
2956
|
})(target, propertyKey);
|
|
3041
2957
|
};
|
|
@@ -10249,16 +10165,12 @@ exports.HasAnyKey = HasAnyKey;
|
|
|
10249
10165
|
exports.HttpException = HttpException;
|
|
10250
10166
|
exports.IS_CROCKFORD_BASE32 = IS_CROCKFORD_BASE32;
|
|
10251
10167
|
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
10168
|
exports.IS_SCALAR_DICTIONARY = IS_SCALAR_DICTIONARY;
|
|
10255
10169
|
exports.IS_URN = IS_URN;
|
|
10256
10170
|
exports.InjectLogger = InjectLogger;
|
|
10257
10171
|
exports.IsBrowserRequest = IsBrowserRequest;
|
|
10258
10172
|
exports.IsCrockfordBase32 = IsCrockfordBase32;
|
|
10259
10173
|
exports.IsDomainUrn = IsDomainUrn;
|
|
10260
|
-
exports.IsResourceIdUrn = IsResourceIdUrn;
|
|
10261
|
-
exports.IsResourceTypeUrn = IsResourceTypeUrn;
|
|
10262
10174
|
exports.IsScalar = IsScalar;
|
|
10263
10175
|
exports.IsScalarDictionary = IsScalarDictionary;
|
|
10264
10176
|
exports.IsUrn = IsUrn;
|