@buka/nestjs-kit 4.1.9 → 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/dist/index.js CHANGED
@@ -364,114 +364,181 @@ function IsScalarDictionary(ctor, validationOptions) {
364
364
  //#endregion
365
365
  //#region src/modules/core/models/urn.ts
366
366
  const URN_PREFIX = "urn:buka:";
367
- const URN_PATTERN = /^urn:buka:([a-z][a-z0-9-]*)(?::([a-z][a-z0-9-]*)(?::(.+))?)?$/;
368
367
  /**
369
368
  * 统一资源名称(URN)
370
369
  *
371
- * 格式: urn:buka:<namespace>[:<resource>[:<identifier>]]
370
+ * 格式: urn:buka:<domain>:<resource-type>[:<resource-id>]
372
371
  *
373
372
  * 支持三种粒度:
374
- * - namespace 级: urn:buka:galaxy
373
+ * - 概念域级: urn:buka:galaxy
375
374
  * - 资源类型级: urn:buka:galaxy:principal
376
375
  * - 实例级: urn:buka:galaxy:principal:550e8400-e29b-41d4-a716-446655440000
377
376
  *
378
- * namespace 划分原则:按资源类型的定义权归属划分,非按部署单元。
377
+ * domain 划分原则:按资源类型的定义权归属划分,非按部署单元。
378
+ * resource-type 第一级推荐用于 env(环境),这是一个语义约定,不强制。
379
379
  *
380
380
  * 推荐通过静态工厂方法或派生方法构建层级 URN:
381
381
  * ```ts
382
- * Urn.of('galaxy') // → NamespaceUrn
383
- * Urn.of('galaxy', 'principal') // → ResourceUrn
384
- * Urn.of('galaxy', 'principal', '550e8400-...') // → IdentifierUrn
382
+ * Urn.of('galaxy') // → DomainUrn
383
+ * Urn.of('galaxy', 'principal') // → ResourceTypeUrn
384
+ * Urn.of('galaxy', 'principal', '550e8400-...') // → ResourceIdUrn
385
385
  *
386
386
  * // 或通过派生方法逐层构建
387
387
  * Urn.of('galaxy')
388
- * .withResource('principal') // → ResourceUrn
389
- * .withIdentifier('550e8400-...') // → IdentifierUrn
388
+ * .withResourceType('principal') // → ResourceTypeUrn
389
+ * .withResourceId('550e8400-...') // → ResourceIdUrn
390
390
  * ```
391
391
  */
392
392
  var Urn = class Urn {
393
- constructor(namespace, resource, identifier) {
394
- this.namespace = namespace;
395
- this.resource = resource;
396
- this.identifier = identifier;
393
+ constructor(domain, resourceType, resourceId) {
394
+ this.domain = domain;
395
+ this.resourceType = resourceType;
396
+ this.resourceId = resourceId;
397
+ if (!/^(?:\*|[a-z][a-z0-9-]*)$/.test(domain)) throw new Error(`Invalid domain: ${domain}`);
398
+ if (resourceType !== void 0) {
399
+ for (const seg of resourceType.split(":")) if (!/^(?:\*|[a-z][a-z0-9-]*)$/.test(seg)) throw new Error(`Invalid resourceType segment: '${seg}' in '${resourceType}'`);
400
+ }
401
+ if (resourceId !== void 0 && !/^(?:\*{1,2}|[a-z0-9][a-z0-9-]*)$/.test(resourceId)) throw new Error(`Invalid resourceId: ${resourceId}`);
397
402
  }
398
403
  /**
399
404
  * 序列化为 URN 字符串
400
405
  */
401
406
  toString() {
402
- let urn = `${URN_PREFIX}${this.namespace}`;
403
- if (this.resource) urn += `:${this.resource}`;
404
- if (this.identifier) urn += `:${this.identifier}`;
407
+ let urn = `${URN_PREFIX}${this.domain}`;
408
+ if (this.resourceType) urn += `:${this.resourceType}`;
409
+ if (this.resourceId) urn += `:${this.resourceId}`;
405
410
  return urn;
406
411
  }
407
412
  /**
413
+ * 返回 resourceType 按 `:` 拆分后的层级数组
414
+ */
415
+ get resourceTypeSegments() {
416
+ return this.resourceType ? this.resourceType.split(":") : [];
417
+ }
418
+ toSegments() {
419
+ const segments = [this.domain];
420
+ if (this.resourceType) segments.push(...this.resourceType.split(":"));
421
+ if (this.resourceId) segments.push(this.resourceId);
422
+ return segments;
423
+ }
424
+ /**
408
425
  * 衍生出资源类型级 URN
409
426
  *
410
- * @throws Error 当前 URN 已有 resource 时抛出
427
+ * @throws Error 当前 URN 已有 resourceType 时抛出
411
428
  */
412
- withResource(resource) {
413
- if (this.resource !== void 0) throw new Error(`Cannot derive ResourceUrn from a URN that already has a resource: ${this.toString()}`);
414
- return new Urn(this.namespace, resource);
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);
415
432
  }
416
433
  /**
417
434
  * 衍生出实例级 URN
418
435
  *
419
- * @throws Error 当前 URN 无 resource 或已有 identifier 时抛出
436
+ * @throws Error 当前 URN 无 resourceType 或已有 resourceId 时抛出
420
437
  */
421
- withIdentifier(identifier) {
422
- if (this.resource === void 0) throw new Error(`Cannot derive IdentifierUrn from a URN without a resource: ${this.toString()}`);
423
- if (this.identifier !== void 0) throw new Error(`Cannot derive IdentifierUrn from a URN that already has an identifier: ${this.toString()}`);
424
- return new Urn(this.namespace, this.resource, identifier);
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);
425
442
  }
426
443
  /**
427
- * 类型守卫:判断是否为 namespace 级 URN
444
+ * 类型守卫:判断是否为概念域级 URN
428
445
  */
429
- isNamespaceUrn() {
430
- return this.resource === void 0;
446
+ isDomainUrn() {
447
+ return this.resourceType === void 0;
431
448
  }
432
449
  /**
433
450
  * 类型守卫:判断是否为资源类型级 URN
434
451
  */
435
- isResourceUrn() {
436
- return this.resource !== void 0 && this.identifier === void 0;
452
+ isResourceTypeUrn() {
453
+ return this.resourceType !== void 0 && this.resourceId === void 0;
437
454
  }
438
455
  /**
439
456
  * 类型守卫:判断是否为实例级 URN
440
457
  */
441
- isIdentifierUrn() {
442
- return this.resource !== void 0 && this.identifier !== void 0;
458
+ isResourceIdUrn() {
459
+ return this.resourceType !== void 0 && this.resourceId !== void 0;
443
460
  }
444
- static of(namespace, resource, identifier) {
445
- return new Urn(namespace, resource, identifier);
461
+ static of(domain, resourceType, resourceId) {
462
+ return new Urn(domain, resourceType, resourceId);
446
463
  }
447
464
  /**
448
- * 从 URN 字符串解析
465
+ * 从 URN 字符串解析,支持通配符 `*` 和 `**`
466
+ *
467
+ * - `*` 匹配恰好一个段
468
+ * - `**` 匹配零个或多个尾部段(仅允许出现在末尾)
449
469
  *
450
470
  * @throws Error URN 格式不合法时抛出
451
471
  */
452
472
  static parse(urn) {
453
- const match = urn.match(URN_PATTERN);
454
- if (!match) throw new Error(`Invalid URN: ${urn}`);
455
- return new Urn(match[1], match[2], match[3]);
473
+ if (!urn.startsWith(URN_PREFIX)) throw new Error(`Invalid URN: ${urn}`);
474
+ const parts = urn.slice(9).split(":");
475
+ if (parts.length === 0 || parts[0] === "") throw new Error(`Invalid URN: ${urn}`);
476
+ const segments = [];
477
+ for (let i = 0; i < parts.length; i++) {
478
+ const part = parts[i];
479
+ if (part === "**") {
480
+ if (i !== parts.length - 1) throw new Error(`Invalid URN: '**' is only allowed at the end: ${urn}`);
481
+ segments.push(part);
482
+ break;
483
+ }
484
+ if (part === "*" || /^[a-z0-9][a-z0-9-]*$/.test(part)) segments.push(part);
485
+ else throw new Error(`Invalid URN segment: '${part}' in ${urn}`);
486
+ }
487
+ if (segments.length === 0) throw new Error(`Invalid URN: ${urn}`);
488
+ const domain = segments[0];
489
+ let resourceType;
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);
456
501
  }
457
502
  /**
458
- * 判断是否匹配指定的 namespace、resource 和 identifier
503
+ * 判断是否匹配指定的 domain、resourceType 和 resourceId
459
504
  *
460
505
  * 仅比较传入的参数,未传入的参数不参与比较。
461
506
  */
462
- is(namespace, resource, identifier) {
463
- if (this.namespace !== namespace) return false;
464
- if (resource !== void 0 && this.resource !== resource) return false;
465
- if (identifier !== void 0 && this.identifier !== identifier) return false;
507
+ is(domain, resourceType, resourceId) {
508
+ if (this.domain !== domain) return false;
509
+ if (resourceType !== void 0 && this.resourceType !== resourceType) return false;
510
+ if (resourceId !== void 0 && this.resourceId !== resourceId) return false;
511
+ return true;
512
+ }
513
+ /**
514
+ * 判断当前 URN 是否包含另一个 URN
515
+ *
516
+ * 当前 URN 含通配符(`*` 或 `**`)时,作为集合模式判断:
517
+ * - `*` 匹配恰好一个段
518
+ * - `**` 匹配零个或多个尾部段(仅末尾有效)
519
+ *
520
+ * 当前 URN 为具体 URN(无通配符)时,仅包含自身(严格相等)。
521
+ */
522
+ contains(other) {
523
+ const thisSegs = this.toSegments();
524
+ const otherSegs = other.toSegments();
525
+ if (thisSegs.length > 0 && thisSegs[thisSegs.length - 1] === "**") {
526
+ const fixedSegs = thisSegs.slice(0, -1);
527
+ if (otherSegs.length < fixedSegs.length) return false;
528
+ for (let i = 0; i < fixedSegs.length; i++) if (fixedSegs[i] !== "*" && fixedSegs[i] !== otherSegs[i]) return false;
529
+ return true;
530
+ }
531
+ if (thisSegs.length !== otherSegs.length) return false;
532
+ for (let i = 0; i < thisSegs.length; i++) if (thisSegs[i] !== "*" && thisSegs[i] !== otherSegs[i]) return false;
466
533
  return true;
467
534
  }
468
535
  };
469
536
  //#endregion
470
537
  //#region src/modules/core/decorators/class-validator/is-urn.ts
471
538
  const IS_URN = "isUrn";
472
- const IS_NAMESPACE_URN = "isNamespaceUrn";
473
- const IS_RESOURCE_URN = "isResourceUrn";
474
- const IS_IDENTIFIER_URN = "isIdentifierUrn";
539
+ const IS_DOMAIN_URN = "isDomainUrn";
540
+ const IS_RESOURCE_TYPE_URN = "isResourceTypeUrn";
541
+ const IS_RESOURCE_ID_URN = "isResourceIdUrn";
475
542
  /**
476
543
  * 验证字符串是否为合法的 URN(任意层级)。
477
544
  *
@@ -501,27 +568,27 @@ function IsUrn(validationOptions) {
501
568
  }
502
569
  },
503
570
  defaultMessage() {
504
- return "$property must be a valid URN (urn:buka:<namespace>[:<resource>[:<identifier>]])";
571
+ return "$property must be a valid URN (urn:buka:<domain>[:<resource-type>[:<resource-id>]])";
505
572
  }
506
573
  }
507
574
  });
508
575
  };
509
576
  }
510
577
  /**
511
- * 验证字符串是否为 namespace 级 URN(仅含 namespace,无 resource 和 identifier)。
578
+ * 验证字符串是否为概念域级 URN(仅含 domain,无 resourceType 和 resourceId)。
512
579
  *
513
580
  * @example
514
581
  * ```typescript
515
582
  * class TokenPayloadDTO {
516
- * @IsNamespaceUrn()
583
+ * @IsDomainUrn()
517
584
  * aud: string
518
585
  * }
519
586
  * ```
520
587
  */
521
- function IsNamespaceUrn(validationOptions) {
588
+ function IsDomainUrn(validationOptions) {
522
589
  return function(object, propertyName) {
523
590
  (0, class_validator.registerDecorator)({
524
- name: IS_NAMESPACE_URN,
591
+ name: IS_DOMAIN_URN,
525
592
  target: object.constructor,
526
593
  propertyName,
527
594
  options: validationOptions,
@@ -529,33 +596,33 @@ function IsNamespaceUrn(validationOptions) {
529
596
  validate(value) {
530
597
  if (typeof value !== "string") return false;
531
598
  try {
532
- return Urn.parse(value).isNamespaceUrn();
599
+ return Urn.parse(value).isDomainUrn();
533
600
  } catch {
534
601
  return false;
535
602
  }
536
603
  },
537
604
  defaultMessage() {
538
- return "$property must be a namespace-level URN (urn:buka:<namespace>)";
605
+ return "$property must be a domain-level URN (urn:buka:<domain>)";
539
606
  }
540
607
  }
541
608
  });
542
609
  };
543
610
  }
544
611
  /**
545
- * 验证字符串是否为资源类型级 URN(含 namespace 和 resource,无 identifier)。
612
+ * 验证字符串是否为资源类型级 URN(含 domain 和 resourceType,无 resourceId)。
546
613
  *
547
614
  * @example
548
615
  * ```typescript
549
616
  * class ResourceDTO {
550
- * @IsResourceUrn()
617
+ * @IsResourceTypeUrn()
551
618
  * resourceType: string
552
619
  * }
553
620
  * ```
554
621
  */
555
- function IsResourceUrn(validationOptions) {
622
+ function IsResourceTypeUrn(validationOptions) {
556
623
  return function(object, propertyName) {
557
624
  (0, class_validator.registerDecorator)({
558
- name: IS_RESOURCE_URN,
625
+ name: IS_RESOURCE_TYPE_URN,
559
626
  target: object.constructor,
560
627
  propertyName,
561
628
  options: validationOptions,
@@ -563,33 +630,33 @@ function IsResourceUrn(validationOptions) {
563
630
  validate(value) {
564
631
  if (typeof value !== "string") return false;
565
632
  try {
566
- return Urn.parse(value).isResourceUrn();
633
+ return Urn.parse(value).isResourceTypeUrn();
567
634
  } catch {
568
635
  return false;
569
636
  }
570
637
  },
571
638
  defaultMessage() {
572
- return "$property must be a resource-level URN (urn:buka:<namespace>:<resource>)";
639
+ return "$property must be a resource-type-level URN (urn:buka:<domain>:<resource-type>)";
573
640
  }
574
641
  }
575
642
  });
576
643
  };
577
644
  }
578
645
  /**
579
- * 验证字符串是否为实例级 URN(含完整三段:namespace、resource 和 identifier)。
646
+ * 验证字符串是否为实例级 URN(含完整三段:domain、resourceType 和 resourceId)。
580
647
  *
581
648
  * @example
582
649
  * ```typescript
583
650
  * class TokenPayloadDTO {
584
- * @IsIdentifierUrn()
651
+ * @IsResourceIdUrn()
585
652
  * sub: string
586
653
  * }
587
654
  * ```
588
655
  */
589
- function IsIdentifierUrn(validationOptions) {
656
+ function IsResourceIdUrn(validationOptions) {
590
657
  return function(object, propertyName) {
591
658
  (0, class_validator.registerDecorator)({
592
- name: IS_IDENTIFIER_URN,
659
+ name: IS_RESOURCE_ID_URN,
593
660
  target: object.constructor,
594
661
  propertyName,
595
662
  options: validationOptions,
@@ -597,13 +664,13 @@ function IsIdentifierUrn(validationOptions) {
597
664
  validate(value) {
598
665
  if (typeof value !== "string") return false;
599
666
  try {
600
- return Urn.parse(value).isIdentifierUrn();
667
+ return Urn.parse(value).isResourceIdUrn();
601
668
  } catch {
602
669
  return false;
603
670
  }
604
671
  },
605
672
  defaultMessage() {
606
- return "$property must be an identifier-level URN (urn:buka:<namespace>:<resource>:<identifier>)";
673
+ return "$property must be a resource-id-level URN (urn:buka:<domain>:<resource-type>:<resource-id>)";
607
674
  }
608
675
  }
609
676
  });
@@ -10091,17 +10158,17 @@ exports.HAS_ANY_KEY = HAS_ANY_KEY;
10091
10158
  exports.HasAnyKey = HasAnyKey;
10092
10159
  exports.HttpException = HttpException;
10093
10160
  exports.IS_CROCKFORD_BASE32 = IS_CROCKFORD_BASE32;
10094
- exports.IS_IDENTIFIER_URN = IS_IDENTIFIER_URN;
10095
- exports.IS_NAMESPACE_URN = IS_NAMESPACE_URN;
10096
- exports.IS_RESOURCE_URN = IS_RESOURCE_URN;
10161
+ exports.IS_DOMAIN_URN = IS_DOMAIN_URN;
10162
+ exports.IS_RESOURCE_ID_URN = IS_RESOURCE_ID_URN;
10163
+ exports.IS_RESOURCE_TYPE_URN = IS_RESOURCE_TYPE_URN;
10097
10164
  exports.IS_SCALAR_DICTIONARY = IS_SCALAR_DICTIONARY;
10098
10165
  exports.IS_URN = IS_URN;
10099
10166
  exports.InjectLogger = InjectLogger;
10100
10167
  exports.IsBrowserRequest = IsBrowserRequest;
10101
10168
  exports.IsCrockfordBase32 = IsCrockfordBase32;
10102
- exports.IsIdentifierUrn = IsIdentifierUrn;
10103
- exports.IsNamespaceUrn = IsNamespaceUrn;
10104
- exports.IsResourceUrn = IsResourceUrn;
10169
+ exports.IsDomainUrn = IsDomainUrn;
10170
+ exports.IsResourceIdUrn = IsResourceIdUrn;
10171
+ exports.IsResourceTypeUrn = IsResourceTypeUrn;
10105
10172
  exports.IsScalar = IsScalar;
10106
10173
  exports.IsScalarDictionary = IsScalarDictionary;
10107
10174
  exports.IsUrn = IsUrn;