@buka/nestjs-kit 4.3.2 → 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 +14 -0
- package/dist/index.d.mts +89 -149
- package/dist/index.d.ts +89 -149
- package/dist/index.js +127 -218
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +127 -214
- package/dist/index.mjs.map +1 -1
- package/docs/core/converters.md +4 -0
- package/docs/core/models.md +54 -0
- package/docs/core/urn.md +66 -64
- package/docs/mikro-orm.md +61 -0
- 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
|
* - `*` 匹配恰好一个段
|
|
@@ -994,7 +895,7 @@ function Composite(options) {
|
|
|
994
895
|
if (optional) (0, class_validator.IsOptional)()(target, propertyKey);
|
|
995
896
|
else (0, class_validator.IsNotEmpty)({ message: "$property is required" })(target, propertyKey);
|
|
996
897
|
(0, class_transformer.Type)(options.type)(target, propertyKey);
|
|
997
|
-
if (optional) (0, _nestjs_swagger.ApiPropertyOptional)(schema)(target, propertyKey);
|
|
898
|
+
if (!options.lazy) if (optional) (0, _nestjs_swagger.ApiPropertyOptional)(schema)(target, propertyKey);
|
|
998
899
|
else (0, _nestjs_swagger.ApiProperty)(schema)(target, propertyKey);
|
|
999
900
|
ModelRegister.addProperty(target.constructor, propertyKey, {
|
|
1000
901
|
kind: "composite",
|
|
@@ -1019,7 +920,7 @@ function applyUntypedList(target, propertyKey, options) {
|
|
|
1019
920
|
each: true,
|
|
1020
921
|
message: "$property is required"
|
|
1021
922
|
})(target, propertyKey);
|
|
1022
|
-
if (optional) (0, _nestjs_swagger.ApiPropertyOptional)(schema)(target, propertyKey);
|
|
923
|
+
if (!options.lazy) if (optional) (0, _nestjs_swagger.ApiPropertyOptional)(schema)(target, propertyKey);
|
|
1023
924
|
else (0, _nestjs_swagger.ApiProperty)(schema)(target, propertyKey);
|
|
1024
925
|
ModelRegister.addProperty(target.constructor, propertyKey, {
|
|
1025
926
|
kind: "list",
|
|
@@ -1043,7 +944,7 @@ function applyScalarList(target, propertyKey, options) {
|
|
|
1043
944
|
each: true,
|
|
1044
945
|
message: "$property is required"
|
|
1045
946
|
})(target, propertyKey);
|
|
1046
|
-
if (optional) (0, _nestjs_swagger.ApiPropertyOptional)(schema)(target, propertyKey);
|
|
947
|
+
if (!options.lazy) if (optional) (0, _nestjs_swagger.ApiPropertyOptional)(schema)(target, propertyKey);
|
|
1047
948
|
else (0, _nestjs_swagger.ApiProperty)(schema)(target, propertyKey);
|
|
1048
949
|
ModelRegister.addProperty(target.constructor, propertyKey, {
|
|
1049
950
|
kind: "list",
|
|
@@ -1068,7 +969,7 @@ function applyCompositeList(target, propertyKey, options) {
|
|
|
1068
969
|
each: true,
|
|
1069
970
|
message: "$property is required"
|
|
1070
971
|
})(target, propertyKey);
|
|
1071
|
-
if (optional) (0, _nestjs_swagger.ApiPropertyOptional)(schema)(target, propertyKey);
|
|
972
|
+
if (!options.lazy) if (optional) (0, _nestjs_swagger.ApiPropertyOptional)(schema)(target, propertyKey);
|
|
1072
973
|
else (0, _nestjs_swagger.ApiProperty)(schema)(target, propertyKey);
|
|
1073
974
|
ModelRegister.addProperty(target.constructor, propertyKey, {
|
|
1074
975
|
kind: "list",
|
|
@@ -2784,6 +2685,48 @@ __decorate([
|
|
|
2784
2685
|
__decorateMetadata("design:type", String)
|
|
2785
2686
|
], DiscreteEntity.prototype, "id", void 0);
|
|
2786
2687
|
//#endregion
|
|
2688
|
+
//#region src/modules/mikro-orm/converters/entity-dto-type/entity-dto-type.ts
|
|
2689
|
+
/**
|
|
2690
|
+
* 基于实体生成一个无继承关系的纯 DTO 类,用于安全派生。
|
|
2691
|
+
*
|
|
2692
|
+
* 返回的类:
|
|
2693
|
+
* - 不继承实体类,避免 `Collection` 类型与 DTO 字段冲突
|
|
2694
|
+
* - 仅包含非 `lazy: true` 的 Model 属性
|
|
2695
|
+
* - 保留 `Ref<Entity>` 类型(序列化层自动处理:populated 时完整序列化,否则仅主键)
|
|
2696
|
+
* - 保留所有 Swagger、class-validator、class-transformer 元数据
|
|
2697
|
+
* - Swagger schema 与 `findOne()` 不加 populate 的结果一致
|
|
2698
|
+
*
|
|
2699
|
+
* @param classRef - 使用 `@Model()` 标注的实体类引用
|
|
2700
|
+
* @returns 可用于 `extends` 派生的纯 DTO 类
|
|
2701
|
+
*
|
|
2702
|
+
* @example
|
|
2703
|
+
* ```typescript
|
|
2704
|
+
* // 基本用法:派生不含 Collection 的 DTO
|
|
2705
|
+
* class UserProfileBriefDto extends EntityDto(UserProfile) {}
|
|
2706
|
+
*
|
|
2707
|
+
* // 扩展用法:手动添加 lazy 属性
|
|
2708
|
+
* class UserProfileDetailDto extends EntityDto(UserProfile) {
|
|
2709
|
+
* @List({ type: () => PrimaryKeyType(Avatar) })
|
|
2710
|
+
* avatars!: PrimaryKeyTypeClass[]
|
|
2711
|
+
* }
|
|
2712
|
+
* ```
|
|
2713
|
+
*/
|
|
2714
|
+
function EntityDto(classRef) {
|
|
2715
|
+
if (!ModelRegister.isModel(classRef)) throw new TypeError("EntityDto only accepts a class annotated with @Model().");
|
|
2716
|
+
class EntityDtoClass {}
|
|
2717
|
+
for (const propertyKey of ModelRegister.getModelPropertyKeys(classRef)) {
|
|
2718
|
+
if (ModelRegister.getProperty(classRef, propertyKey)?.lazy) continue;
|
|
2719
|
+
ModelRegister.copyProperty(classRef, EntityDtoClass, propertyKey);
|
|
2720
|
+
const metadataKeys = Reflect.getMetadataKeys(classRef.prototype, propertyKey);
|
|
2721
|
+
for (const metadataKey of metadataKeys) {
|
|
2722
|
+
const value = Reflect.getMetadata(metadataKey, classRef.prototype, propertyKey);
|
|
2723
|
+
if (Reflect.hasMetadata(metadataKey, EntityDtoClass.prototype, propertyKey)) continue;
|
|
2724
|
+
Reflect.defineMetadata(metadataKey, value, EntityDtoClass.prototype, propertyKey);
|
|
2725
|
+
}
|
|
2726
|
+
}
|
|
2727
|
+
return EntityDtoClass;
|
|
2728
|
+
}
|
|
2729
|
+
//#endregion
|
|
2787
2730
|
//#region src/modules/mikro-orm/utils/mikro-orm-utils.ts
|
|
2788
2731
|
function getMetadata(classRef) {
|
|
2789
2732
|
const metadatas = [];
|
|
@@ -2944,6 +2887,7 @@ function OneToMany(options) {
|
|
|
2944
2887
|
List({
|
|
2945
2888
|
type,
|
|
2946
2889
|
optional: options.nullable,
|
|
2890
|
+
lazy: options.eager !== true,
|
|
2947
2891
|
association: {
|
|
2948
2892
|
kind: "1:m",
|
|
2949
2893
|
type
|
|
@@ -2984,6 +2928,7 @@ function ManyToMany(entity, mappedBy, options) {
|
|
|
2984
2928
|
List({
|
|
2985
2929
|
type,
|
|
2986
2930
|
optional: resolvedOptions?.nullable,
|
|
2931
|
+
lazy: resolvedOptions?.eager !== true,
|
|
2987
2932
|
association: {
|
|
2988
2933
|
kind: "m:n",
|
|
2989
2934
|
type
|
|
@@ -3293,6 +3238,36 @@ var Slice = class Slice {
|
|
|
3293
3238
|
}
|
|
3294
3239
|
};
|
|
3295
3240
|
//#endregion
|
|
3241
|
+
//#region src/modules/core/converters/pick-type/pick-type.ts
|
|
3242
|
+
/**
|
|
3243
|
+
* 从模型中选取指定属性,生成新的子集类型。
|
|
3244
|
+
*
|
|
3245
|
+
* 类似 TypeScript 的 `Pick<T, K>`,同时保留 class-validator、class-transformer 和 Swagger 的元数据。
|
|
3246
|
+
*
|
|
3247
|
+
* @param classRef - 使用 `@Model()` 标注的类引用
|
|
3248
|
+
* @param keys - 需要选取的属性名数组
|
|
3249
|
+
* @returns 仅包含指定属性的新类
|
|
3250
|
+
*
|
|
3251
|
+
* @example
|
|
3252
|
+
* ```typescript
|
|
3253
|
+
* const CreateUserDTO = PickType(UserDTO, ['name', 'email'])
|
|
3254
|
+
* ```
|
|
3255
|
+
*/
|
|
3256
|
+
function PickType(classRef, keys) {
|
|
3257
|
+
if (!ModelRegister.isModel(classRef)) throw new TypeError("PickType only accepts a class annotated with @Model().");
|
|
3258
|
+
const PickTypeClass = (0, _nestjs_swagger.PickType)(classRef, keys);
|
|
3259
|
+
for (const key of keys) {
|
|
3260
|
+
ModelRegister.copyProperty(classRef, PickTypeClass, key);
|
|
3261
|
+
const metadataKeys = Reflect.getMetadataKeys(classRef.prototype, key);
|
|
3262
|
+
for (const metadataKey of metadataKeys) {
|
|
3263
|
+
const metadata = Reflect.getMetadata(metadataKey, classRef.prototype, key);
|
|
3264
|
+
if (Reflect.hasMetadata(metadataKey, PickTypeClass.prototype, key)) continue;
|
|
3265
|
+
Reflect.defineMetadata(metadataKey, metadata, PickTypeClass.prototype, key);
|
|
3266
|
+
}
|
|
3267
|
+
}
|
|
3268
|
+
return PickTypeClass;
|
|
3269
|
+
}
|
|
3270
|
+
//#endregion
|
|
3296
3271
|
//#region src/modules/core/converters/omit-type/omit-type.ts
|
|
3297
3272
|
/**
|
|
3298
3273
|
* 从模型中排除指定属性,生成新的类型。
|
|
@@ -3324,68 +3299,6 @@ function OmitType(classRef, keys) {
|
|
|
3324
3299
|
return OmitTypeClass;
|
|
3325
3300
|
}
|
|
3326
3301
|
//#endregion
|
|
3327
|
-
//#region src/modules/core/converters/eager-type/eager-type.ts
|
|
3328
|
-
/**
|
|
3329
|
-
* 从模型中排除所有标记为 `lazy: true` 的属性,生成仅包含非懒加载字段的新类型。
|
|
3330
|
-
*
|
|
3331
|
-
* 适用于 API 响应场景,避免返回未加载的字段。
|
|
3332
|
-
*
|
|
3333
|
-
* @param classRef - 使用 `@Model()` 标注的类引用
|
|
3334
|
-
* @returns 排除了 lazy 属性的新类
|
|
3335
|
-
*
|
|
3336
|
-
* @example
|
|
3337
|
-
* ```typescript
|
|
3338
|
-
* @Model()
|
|
3339
|
-
* class User {
|
|
3340
|
-
* @Property()
|
|
3341
|
-
* name: string
|
|
3342
|
-
*
|
|
3343
|
-
* @Property({ lazy: true })
|
|
3344
|
-
* bio: string
|
|
3345
|
-
* }
|
|
3346
|
-
*
|
|
3347
|
-
* const UserResponse = EagerType(User)
|
|
3348
|
-
* // UserResponse 仅包含 name,不包含 bio
|
|
3349
|
-
* ```
|
|
3350
|
-
*/
|
|
3351
|
-
function EagerType(classRef) {
|
|
3352
|
-
if (!ModelRegister.isModel(classRef)) throw new TypeError("EagerType only accepts a class annotated with @Model().");
|
|
3353
|
-
const lazyKeys = [];
|
|
3354
|
-
for (const propertyKey of ModelRegister.getModelPropertyKeys(classRef)) if (ModelRegister.getProperty(classRef, propertyKey)?.lazy) lazyKeys.push(propertyKey);
|
|
3355
|
-
if (lazyKeys.length === 0) return classRef;
|
|
3356
|
-
return OmitType(classRef, lazyKeys);
|
|
3357
|
-
}
|
|
3358
|
-
//#endregion
|
|
3359
|
-
//#region src/modules/core/converters/pick-type/pick-type.ts
|
|
3360
|
-
/**
|
|
3361
|
-
* 从模型中选取指定属性,生成新的子集类型。
|
|
3362
|
-
*
|
|
3363
|
-
* 类似 TypeScript 的 `Pick<T, K>`,同时保留 class-validator、class-transformer 和 Swagger 的元数据。
|
|
3364
|
-
*
|
|
3365
|
-
* @param classRef - 使用 `@Model()` 标注的类引用
|
|
3366
|
-
* @param keys - 需要选取的属性名数组
|
|
3367
|
-
* @returns 仅包含指定属性的新类
|
|
3368
|
-
*
|
|
3369
|
-
* @example
|
|
3370
|
-
* ```typescript
|
|
3371
|
-
* const CreateUserDTO = PickType(UserDTO, ['name', 'email'])
|
|
3372
|
-
* ```
|
|
3373
|
-
*/
|
|
3374
|
-
function PickType(classRef, keys) {
|
|
3375
|
-
if (!ModelRegister.isModel(classRef)) throw new TypeError("PickType only accepts a class annotated with @Model().");
|
|
3376
|
-
const PickTypeClass = (0, _nestjs_swagger.PickType)(classRef, keys);
|
|
3377
|
-
for (const key of keys) {
|
|
3378
|
-
ModelRegister.copyProperty(classRef, PickTypeClass, key);
|
|
3379
|
-
const metadataKeys = Reflect.getMetadataKeys(classRef.prototype, key);
|
|
3380
|
-
for (const metadataKey of metadataKeys) {
|
|
3381
|
-
const metadata = Reflect.getMetadata(metadataKey, classRef.prototype, key);
|
|
3382
|
-
if (Reflect.hasMetadata(metadataKey, PickTypeClass.prototype, key)) continue;
|
|
3383
|
-
Reflect.defineMetadata(metadataKey, metadata, PickTypeClass.prototype, key);
|
|
3384
|
-
}
|
|
3385
|
-
}
|
|
3386
|
-
return PickTypeClass;
|
|
3387
|
-
}
|
|
3388
|
-
//#endregion
|
|
3389
3302
|
//#region src/modules/core/converters/partial-type/partial-type.ts
|
|
3390
3303
|
/**
|
|
3391
3304
|
* 将模型的所有属性变为可选,生成新的 Partial 类型。
|
|
@@ -10155,13 +10068,13 @@ exports.DatabaseConfig = DatabaseConfig;
|
|
|
10155
10068
|
exports.DegradeException = DegradeException;
|
|
10156
10069
|
exports.Dictionary = Dictionary;
|
|
10157
10070
|
exports.DiscreteEntity = DiscreteEntity;
|
|
10158
|
-
exports.EagerType = EagerType;
|
|
10159
10071
|
Object.defineProperty(exports, "EncryptedPayload", {
|
|
10160
10072
|
enumerable: true,
|
|
10161
10073
|
get: function() {
|
|
10162
10074
|
return EncryptedPayload;
|
|
10163
10075
|
}
|
|
10164
10076
|
});
|
|
10077
|
+
exports.EntityDto = EntityDto;
|
|
10165
10078
|
exports.EntityRef = EntityRef;
|
|
10166
10079
|
exports.Enum = Enum;
|
|
10167
10080
|
Object.defineProperty(exports, "EnvelopeEncryptionModule", {
|
|
@@ -10237,16 +10150,12 @@ exports.HasAnyKey = HasAnyKey;
|
|
|
10237
10150
|
exports.HttpException = HttpException;
|
|
10238
10151
|
exports.IS_CROCKFORD_BASE32 = IS_CROCKFORD_BASE32;
|
|
10239
10152
|
exports.IS_DOMAIN_URN = IS_DOMAIN_URN;
|
|
10240
|
-
exports.IS_RESOURCE_ID_URN = IS_RESOURCE_ID_URN;
|
|
10241
|
-
exports.IS_RESOURCE_TYPE_URN = IS_RESOURCE_TYPE_URN;
|
|
10242
10153
|
exports.IS_SCALAR_DICTIONARY = IS_SCALAR_DICTIONARY;
|
|
10243
10154
|
exports.IS_URN = IS_URN;
|
|
10244
10155
|
exports.InjectLogger = InjectLogger;
|
|
10245
10156
|
exports.IsBrowserRequest = IsBrowserRequest;
|
|
10246
10157
|
exports.IsCrockfordBase32 = IsCrockfordBase32;
|
|
10247
10158
|
exports.IsDomainUrn = IsDomainUrn;
|
|
10248
|
-
exports.IsResourceIdUrn = IsResourceIdUrn;
|
|
10249
|
-
exports.IsResourceTypeUrn = IsResourceTypeUrn;
|
|
10250
10159
|
exports.IsScalar = IsScalar;
|
|
10251
10160
|
exports.IsScalarDictionary = IsScalarDictionary;
|
|
10252
10161
|
exports.IsUrn = IsUrn;
|