@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/dist/index.mjs CHANGED
@@ -339,99 +339,71 @@ const URN_PREFIX = "urn:buka:";
339
339
  /**
340
340
  * 统一资源名称(URN)
341
341
  *
342
- * 格式: urn:buka:<domain>:<resource-type>[:<resource-id>]
342
+ * 格式: urn:buka:<domain>:<resource...>
343
343
  *
344
- * 支持三种粒度:
345
344
  * - 概念域级: urn:buka:galaxy
346
- * - 资源类型级: urn:buka:galaxy:principal
347
- * - 实例级: urn:buka:galaxy:principal:550e8400-e29b-41d4-a716-446655440000
345
+ * - 资源级: urn:buka:galaxy:auth-client:galaxy:console
348
346
  *
349
- * domain 划分原则:按资源类型的定义权归属划分,非按部署单元。
350
- * resource-type 第一级推荐用于 env(环境),这是一个语义约定,不强制。
347
+ * domain 之后的全部段为 resource 路径(: 分隔),parser 不区分"类型"和"实例"。
348
+ * 应用层自行约定 resource 各段的语义。
351
349
  *
352
- * 推荐通过静态工厂方法或派生方法构建层级 URN:
353
350
  * ```ts
354
- * Urn.of('galaxy') // → DomainUrn
355
- * Urn.of('galaxy', 'principal') // → ResourceTypeUrn
356
- * Urn.of('galaxy', 'principal', '550e8400-...') // → ResourceIdUrn
357
- *
358
- * // 或通过派生方法逐层构建
359
- * Urn.of('galaxy')
360
- * .withResourceType('principal') // → ResourceTypeUrn
361
- * .withResourceId('550e8400-...') // → ResourceIdUrn
351
+ * Urn.of('galaxy') // → 概念域级 URN
352
+ * Urn.of('galaxy', 'auth-client:galaxy:console') // → 资源级 URN
353
+ * Urn.of('galaxy', ['auth-client', 'galaxy', 'console'])
354
+ *
355
+ * // 或通过派生方法构建
356
+ * Urn.of('galaxy').withResource('auth-client', 'galaxy', 'console')
362
357
  * ```
363
358
  */
364
359
  var Urn = class Urn {
365
- constructor(domain, resourceType, resourceId) {
360
+ constructor(domain, resource) {
366
361
  this.domain = domain;
367
- this.resourceType = resourceType;
368
- this.resourceId = resourceId;
362
+ this.resource = resource;
369
363
  if (!/^(?:\*|[a-z][a-z0-9-]*)$/.test(domain)) throw new Error(`Invalid domain: ${domain}`);
370
- if (resourceType !== void 0) {
371
- for (const seg of resourceType.split(":")) if (!/^(?:\*|[a-z][a-z0-9-]*)$/.test(seg)) throw new Error(`Invalid resourceType segment: '${seg}' in '${resourceType}'`);
364
+ if (resource !== void 0) {
365
+ 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}'`);
372
366
  }
373
- if (resourceId !== void 0 && !/^(?:\*{1,2}|[a-z0-9][a-z0-9-]*)$/.test(resourceId)) throw new Error(`Invalid resourceId: ${resourceId}`);
367
+ }
368
+ /**
369
+ * resource 按 `:` 拆分后的段数组
370
+ */
371
+ get resourceSegments() {
372
+ return this.resource ? this.resource.split(":") : [];
374
373
  }
375
374
  /**
376
375
  * 序列化为 URN 字符串
377
376
  */
378
377
  toString() {
379
378
  let urn = `${URN_PREFIX}${this.domain}`;
380
- if (this.resourceType) urn += `:${this.resourceType}`;
381
- if (this.resourceId) urn += `:${this.resourceId}`;
379
+ if (this.resource) urn += `:${this.resource}`;
382
380
  return urn;
383
381
  }
384
- /**
385
- * 返回 resourceType 按 `:` 拆分后的层级数组
386
- */
387
- get resourceTypeSegments() {
388
- return this.resourceType ? this.resourceType.split(":") : [];
389
- }
390
382
  toSegments() {
391
383
  const segments = [this.domain];
392
- if (this.resourceType) segments.push(...this.resourceType.split(":"));
393
- if (this.resourceId) segments.push(this.resourceId);
384
+ if (this.resource) segments.push(...this.resource.split(":"));
394
385
  return segments;
395
386
  }
396
387
  /**
397
- * 衍生出资源类型级 URN
388
+ * 从概念域级 URN 衍生出资源级 URN
398
389
  *
399
- * @throws Error 当前 URN 已有 resourceType 时抛出
390
+ * @throws Error 当前 URN 已有 resource 时抛出
400
391
  */
401
- withResourceType(resourceType) {
402
- if (this.resourceType !== void 0) throw new Error(`Cannot derive ResourceTypeUrn from a URN that already has a resourceType: ${this.toString()}`);
403
- return new Urn(this.domain, resourceType);
392
+ withResource(...segments) {
393
+ if (this.resource !== void 0) throw new Error(`Cannot derive resource URN from a URN that already has a resource: ${this.toString()}`);
394
+ return new Urn(this.domain, segments.join(":"));
404
395
  }
405
396
  /**
406
- * 衍生出实例级 URN
407
- *
408
- * @throws Error 当前 URN 无 resourceType 或已有 resourceId 时抛出
409
- */
410
- withResourceId(resourceId) {
411
- if (this.resourceType === void 0) throw new Error(`Cannot derive ResourceIdUrn from a URN without a resourceType: ${this.toString()}`);
412
- if (this.resourceId !== void 0) throw new Error(`Cannot derive ResourceIdUrn from a URN that already has a resourceId: ${this.toString()}`);
413
- return new Urn(this.domain, this.resourceType, resourceId);
414
- }
415
- /**
416
- * 类型守卫:判断是否为概念域级 URN
397
+ * 类型守卫:判断是否为概念域级 URN(无 resource)
417
398
  */
418
399
  isDomainUrn() {
419
- return this.resourceType === void 0;
400
+ return this.resource === void 0;
420
401
  }
421
402
  /**
422
- * 类型守卫:判断是否为资源类型级 URN
403
+ * 根据参数创建对应层级的 URN
423
404
  */
424
- isResourceTypeUrn() {
425
- return this.resourceType !== void 0 && this.resourceId === void 0;
426
- }
427
- /**
428
- * 类型守卫:判断是否为实例级 URN
429
- */
430
- isResourceIdUrn() {
431
- return this.resourceType !== void 0 && this.resourceId !== void 0;
432
- }
433
- static of(domain, resourceType, resourceId) {
434
- return new Urn(domain, resourceType, resourceId);
405
+ static of(domain, resource) {
406
+ return new Urn(domain, resource === void 0 ? void 0 : Array.isArray(resource) ? resource.join(":") : resource);
435
407
  }
436
408
  /**
437
409
  * 从 URN 字符串解析,支持通配符 `*` 和 `**`
@@ -458,31 +430,30 @@ var Urn = class Urn {
458
430
  }
459
431
  if (segments.length === 0) throw new Error(`Invalid URN: ${urn}`);
460
432
  const domain = segments[0];
461
- let resourceType;
462
- let resourceId;
463
- if (segments[segments.length - 1] === "**") {
464
- if (segments.length === 1) {} else if (segments.length === 2) resourceType = segments[1] !== "**" ? segments[1] : void 0;
465
- else resourceType = segments.slice(1, -1).join(":");
466
- resourceId = "**";
467
- } else if (segments.length === 2) resourceType = segments[1];
468
- else if (segments.length >= 3) {
469
- resourceType = segments.slice(1, -1).join(":");
470
- resourceId = segments[segments.length - 1];
471
- }
472
- return new Urn(domain, resourceType, resourceId);
433
+ return new Urn(domain, segments.length > 1 ? segments.slice(1).join(":") : void 0);
473
434
  }
474
435
  /**
475
- * 判断是否匹配指定的 domain、resourceType 和 resourceId
436
+ * 判断是否匹配指定的 domain 和 resource
476
437
  *
477
- * 仅比较传入的参数,未传入的参数不参与比较。
438
+ * 未传入的参数不参与比较。
478
439
  */
479
- is(domain, resourceType, resourceId) {
440
+ is(domain, resource) {
480
441
  if (this.domain !== domain) return false;
481
- if (resourceType !== void 0 && this.resourceType !== resourceType) return false;
482
- if (resourceId !== void 0 && this.resourceId !== resourceId) return false;
442
+ if (resource !== void 0 && this.resource !== resource) return false;
483
443
  return true;
484
444
  }
485
445
  /**
446
+ * 判断当前 URN 是否匹配指定的 URN 模式(支持通配符)
447
+ *
448
+ * ```ts
449
+ * urn.match('urn:buka:galaxy:principal:*') // resource 以 principal: 开头
450
+ * urn.match('urn:buka:galaxy:auth-client:**') // 任意深度的 auth-client
451
+ * ```
452
+ */
453
+ match(pattern) {
454
+ return Urn.parse(pattern).contains(this);
455
+ }
456
+ /**
486
457
  * 判断当前 URN 是否包含另一个 URN
487
458
  *
488
459
  * 当前 URN 含通配符(`*` 或 `**`)时,作为集合模式判断:
@@ -509,8 +480,6 @@ var Urn = class Urn {
509
480
  //#region src/modules/core/decorators/class-validator/is-urn.ts
510
481
  const IS_URN = "isUrn";
511
482
  const IS_DOMAIN_URN = "isDomainUrn";
512
- const IS_RESOURCE_TYPE_URN = "isResourceTypeUrn";
513
- const IS_RESOURCE_ID_URN = "isResourceIdUrn";
514
483
  /**
515
484
  * 验证字符串是否为合法的 URN(任意层级)。
516
485
  *
@@ -581,74 +550,6 @@ function IsDomainUrn(validationOptions) {
581
550
  };
582
551
  }
583
552
  /**
584
- * 验证字符串是否为资源类型级 URN(含 domain 和 resourceType,无 resourceId)。
585
- *
586
- * @example
587
- * ```typescript
588
- * class ResourceDTO {
589
- * @IsResourceTypeUrn()
590
- * resourceType: string
591
- * }
592
- * ```
593
- */
594
- function IsResourceTypeUrn(validationOptions) {
595
- return function(object, propertyName) {
596
- registerDecorator({
597
- name: IS_RESOURCE_TYPE_URN,
598
- target: object.constructor,
599
- propertyName,
600
- options: validationOptions,
601
- validator: {
602
- validate(value) {
603
- if (typeof value !== "string") return false;
604
- try {
605
- return Urn.parse(value).isResourceTypeUrn();
606
- } catch {
607
- return false;
608
- }
609
- },
610
- defaultMessage() {
611
- return "$property must be a resource-type-level URN (urn:buka:<domain>:<resource-type>)";
612
- }
613
- }
614
- });
615
- };
616
- }
617
- /**
618
- * 验证字符串是否为实例级 URN(含完整三段:domain、resourceType 和 resourceId)。
619
- *
620
- * @example
621
- * ```typescript
622
- * class TokenPayloadDTO {
623
- * @IsResourceIdUrn()
624
- * sub: string
625
- * }
626
- * ```
627
- */
628
- function IsResourceIdUrn(validationOptions) {
629
- return function(object, propertyName) {
630
- registerDecorator({
631
- name: IS_RESOURCE_ID_URN,
632
- target: object.constructor,
633
- propertyName,
634
- options: validationOptions,
635
- validator: {
636
- validate(value) {
637
- if (typeof value !== "string") return false;
638
- try {
639
- return Urn.parse(value).isResourceIdUrn();
640
- } catch {
641
- return false;
642
- }
643
- },
644
- defaultMessage() {
645
- return "$property must be a resource-id-level URN (urn:buka:<domain>:<resource-type>:<resource-id>)";
646
- }
647
- }
648
- });
649
- };
650
- }
651
- /**
652
553
  * 验证字符串是否匹配指定的 URN 模式(支持通配符 `*` 和 `**`)。
653
554
  *
654
555
  * - `*` 匹配恰好一个段
@@ -10066,6 +9967,6 @@ ObjectStorageModule = __decorate([Global(), Module({
10066
9967
  exports: [ObjectStorageService]
10067
9968
  })], ObjectStorageModule);
10068
9969
  //#endregion
10069
- export { AuthException, BUKA_MODULE_OPTIONS_TOKEN, Base32, BlindIndex, BlindIndexModule, BlindIndexService, BlindIndexV1Hasher, BukaConfigurableModuleClass, BukaExceptions, BukaModule, BukaPageQueryValidationPipe, BukaValidationPipe, BusinessException, Cardinality, Column, Composite, ConfigurableModuleClass, ConflictException, CursorPagination, DatabaseConfig, DegradeException, Dictionary, DiscreteEntity, EncryptedPayload, EntityDto, EntityRef, Enum, EnvelopeEncryptionModule, EnvelopeEncryptionService, ErrorCategory, ErrorCode, ErrorCodeController, ErrorCodeExceptionFilter, ErrorCodeDefinitionDto as ErrorCodeItemDto, ErrorCodeMetadataDto, ErrorCodePartsDto, ErrorCodeRegistry, ErrorResponseSchema, ExceptionDetailSchema, ExceptionModule, FeatureException, FilterQuery, FilterQueryOperators, FilterQueryOperatorsMetadataKey, FilterQueryTransformPipe, HAS_ANY_KEY, HasAnyKey, HttpException, IS_CROCKFORD_BASE32, IS_DOMAIN_URN, IS_RESOURCE_ID_URN, IS_RESOURCE_TYPE_URN, IS_SCALAR_DICTIONARY, IS_URN, InjectLogger, IsBrowserRequest, IsCrockfordBase32, IsDomainUrn, IsResourceIdUrn, IsResourceTypeUrn, IsScalar, IsScalarDictionary, IsUrn, KeyEnvelope, LinearEntity, List, ListErrorCodesResponseDto, ListResponseBodyType, Logger, LoggerConfig, LoggerErrorInterceptor, LoggerModule, MATCH_JSON_SCHEMA, MODULE_OPTIONS_TOKEN, MatchJsonSchema, MatchesUrn, Model, ModelRegister, ModuleExceptions, NextCursorPageSchema, ObjectStorageModule, ObjectStorageModuleConfig, ObjectStorageService, OffsetPageSchema, OffsetPagination, OmitType, OpenBaoAppRoleAuthConfig, OpenBaoHttpClient, OpenBaoKubernetesAuthConfig, OpenBaoModule, OpenBaoModuleConfig, OpenBaoTokenAuthConfig, OpenBaoTokenManager, OpenBaoUserpassAuthConfig, OptionalPageQuery, OrderQueryType, PageQuery, ParseUUIDv7Pipe, PartialType, PendingException, PickType, PreviousCursorPageSchema, PrimaryKeyType, PrimaryKeyTypeClassMetadataPropertyKey, Property, RateLimitException, ResponseBodyType, SaltedHash, SaltedHashModule, SaltedHashService, SaltedHashV1Hasher, Slice, SwaggerPatcher, SystemException, ThirdPartyException, TimestampedEntity, Urn, VALIDATE_NESTED_DICTIONARY, ValidateNestedDictionary, ValidationException, getExceptionModuleId, getFilterQueryOperators, isScalarClass, serializeModel, setOpenBaoToken, stableStringify };
9970
+ export { AuthException, BUKA_MODULE_OPTIONS_TOKEN, Base32, BlindIndex, BlindIndexModule, BlindIndexService, BlindIndexV1Hasher, BukaConfigurableModuleClass, BukaExceptions, BukaModule, BukaPageQueryValidationPipe, BukaValidationPipe, BusinessException, Cardinality, Column, Composite, ConfigurableModuleClass, ConflictException, CursorPagination, DatabaseConfig, DegradeException, Dictionary, DiscreteEntity, EncryptedPayload, EntityDto, EntityRef, Enum, EnvelopeEncryptionModule, EnvelopeEncryptionService, ErrorCategory, ErrorCode, ErrorCodeController, ErrorCodeExceptionFilter, ErrorCodeDefinitionDto as ErrorCodeItemDto, ErrorCodeMetadataDto, ErrorCodePartsDto, ErrorCodeRegistry, ErrorResponseSchema, ExceptionDetailSchema, ExceptionModule, FeatureException, FilterQuery, FilterQueryOperators, FilterQueryOperatorsMetadataKey, FilterQueryTransformPipe, HAS_ANY_KEY, HasAnyKey, HttpException, IS_CROCKFORD_BASE32, IS_DOMAIN_URN, IS_SCALAR_DICTIONARY, IS_URN, InjectLogger, IsBrowserRequest, IsCrockfordBase32, IsDomainUrn, IsScalar, IsScalarDictionary, IsUrn, KeyEnvelope, LinearEntity, List, ListErrorCodesResponseDto, ListResponseBodyType, Logger, LoggerConfig, LoggerErrorInterceptor, LoggerModule, MATCH_JSON_SCHEMA, MODULE_OPTIONS_TOKEN, MatchJsonSchema, MatchesUrn, Model, ModelRegister, ModuleExceptions, NextCursorPageSchema, ObjectStorageModule, ObjectStorageModuleConfig, ObjectStorageService, OffsetPageSchema, OffsetPagination, OmitType, OpenBaoAppRoleAuthConfig, OpenBaoHttpClient, OpenBaoKubernetesAuthConfig, OpenBaoModule, OpenBaoModuleConfig, OpenBaoTokenAuthConfig, OpenBaoTokenManager, OpenBaoUserpassAuthConfig, OptionalPageQuery, OrderQueryType, PageQuery, ParseUUIDv7Pipe, PartialType, PendingException, PickType, PreviousCursorPageSchema, PrimaryKeyType, PrimaryKeyTypeClassMetadataPropertyKey, Property, RateLimitException, ResponseBodyType, SaltedHash, SaltedHashModule, SaltedHashService, SaltedHashV1Hasher, Slice, SwaggerPatcher, SystemException, ThirdPartyException, TimestampedEntity, Urn, VALIDATE_NESTED_DICTIONARY, ValidateNestedDictionary, ValidationException, getExceptionModuleId, getFilterQueryOperators, isScalarClass, serializeModel, setOpenBaoToken, stableStringify };
10070
9971
 
10071
9972
  //# sourceMappingURL=index.mjs.map