@buka/nestjs-kit 4.3.1 → 4.3.3

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
@@ -994,7 +994,7 @@ function Composite(options) {
994
994
  if (optional) (0, class_validator.IsOptional)()(target, propertyKey);
995
995
  else (0, class_validator.IsNotEmpty)({ message: "$property is required" })(target, propertyKey);
996
996
  (0, class_transformer.Type)(options.type)(target, propertyKey);
997
- if (optional) (0, _nestjs_swagger.ApiPropertyOptional)(schema)(target, propertyKey);
997
+ if (!options.lazy) if (optional) (0, _nestjs_swagger.ApiPropertyOptional)(schema)(target, propertyKey);
998
998
  else (0, _nestjs_swagger.ApiProperty)(schema)(target, propertyKey);
999
999
  ModelRegister.addProperty(target.constructor, propertyKey, {
1000
1000
  kind: "composite",
@@ -1019,7 +1019,7 @@ function applyUntypedList(target, propertyKey, options) {
1019
1019
  each: true,
1020
1020
  message: "$property is required"
1021
1021
  })(target, propertyKey);
1022
- if (optional) (0, _nestjs_swagger.ApiPropertyOptional)(schema)(target, propertyKey);
1022
+ if (!options.lazy) if (optional) (0, _nestjs_swagger.ApiPropertyOptional)(schema)(target, propertyKey);
1023
1023
  else (0, _nestjs_swagger.ApiProperty)(schema)(target, propertyKey);
1024
1024
  ModelRegister.addProperty(target.constructor, propertyKey, {
1025
1025
  kind: "list",
@@ -1043,7 +1043,7 @@ function applyScalarList(target, propertyKey, options) {
1043
1043
  each: true,
1044
1044
  message: "$property is required"
1045
1045
  })(target, propertyKey);
1046
- if (optional) (0, _nestjs_swagger.ApiPropertyOptional)(schema)(target, propertyKey);
1046
+ if (!options.lazy) if (optional) (0, _nestjs_swagger.ApiPropertyOptional)(schema)(target, propertyKey);
1047
1047
  else (0, _nestjs_swagger.ApiProperty)(schema)(target, propertyKey);
1048
1048
  ModelRegister.addProperty(target.constructor, propertyKey, {
1049
1049
  kind: "list",
@@ -1068,7 +1068,7 @@ function applyCompositeList(target, propertyKey, options) {
1068
1068
  each: true,
1069
1069
  message: "$property is required"
1070
1070
  })(target, propertyKey);
1071
- if (optional) (0, _nestjs_swagger.ApiPropertyOptional)(schema)(target, propertyKey);
1071
+ if (!options.lazy) if (optional) (0, _nestjs_swagger.ApiPropertyOptional)(schema)(target, propertyKey);
1072
1072
  else (0, _nestjs_swagger.ApiProperty)(schema)(target, propertyKey);
1073
1073
  ModelRegister.addProperty(target.constructor, propertyKey, {
1074
1074
  kind: "list",
@@ -2784,6 +2784,48 @@ __decorate([
2784
2784
  __decorateMetadata("design:type", String)
2785
2785
  ], DiscreteEntity.prototype, "id", void 0);
2786
2786
  //#endregion
2787
+ //#region src/modules/mikro-orm/converters/entity-dto-type/entity-dto-type.ts
2788
+ /**
2789
+ * 基于实体生成一个无继承关系的纯 DTO 类,用于安全派生。
2790
+ *
2791
+ * 返回的类:
2792
+ * - 不继承实体类,避免 `Collection` 类型与 DTO 字段冲突
2793
+ * - 仅包含非 `lazy: true` 的 Model 属性
2794
+ * - 保留 `Ref<Entity>` 类型(序列化层自动处理:populated 时完整序列化,否则仅主键)
2795
+ * - 保留所有 Swagger、class-validator、class-transformer 元数据
2796
+ * - Swagger schema 与 `findOne()` 不加 populate 的结果一致
2797
+ *
2798
+ * @param classRef - 使用 `@Model()` 标注的实体类引用
2799
+ * @returns 可用于 `extends` 派生的纯 DTO 类
2800
+ *
2801
+ * @example
2802
+ * ```typescript
2803
+ * // 基本用法:派生不含 Collection 的 DTO
2804
+ * class UserProfileBriefDto extends EntityDto(UserProfile) {}
2805
+ *
2806
+ * // 扩展用法:手动添加 lazy 属性
2807
+ * class UserProfileDetailDto extends EntityDto(UserProfile) {
2808
+ * @List({ type: () => PrimaryKeyType(Avatar) })
2809
+ * avatars!: PrimaryKeyTypeClass[]
2810
+ * }
2811
+ * ```
2812
+ */
2813
+ function EntityDto(classRef) {
2814
+ if (!ModelRegister.isModel(classRef)) throw new TypeError("EntityDto only accepts a class annotated with @Model().");
2815
+ class EntityDtoClass {}
2816
+ for (const propertyKey of ModelRegister.getModelPropertyKeys(classRef)) {
2817
+ if (ModelRegister.getProperty(classRef, propertyKey)?.lazy) continue;
2818
+ ModelRegister.copyProperty(classRef, EntityDtoClass, propertyKey);
2819
+ const metadataKeys = Reflect.getMetadataKeys(classRef.prototype, propertyKey);
2820
+ for (const metadataKey of metadataKeys) {
2821
+ const value = Reflect.getMetadata(metadataKey, classRef.prototype, propertyKey);
2822
+ if (Reflect.hasMetadata(metadataKey, EntityDtoClass.prototype, propertyKey)) continue;
2823
+ Reflect.defineMetadata(metadataKey, value, EntityDtoClass.prototype, propertyKey);
2824
+ }
2825
+ }
2826
+ return EntityDtoClass;
2827
+ }
2828
+ //#endregion
2787
2829
  //#region src/modules/mikro-orm/utils/mikro-orm-utils.ts
2788
2830
  function getMetadata(classRef) {
2789
2831
  const metadatas = [];
@@ -2944,6 +2986,7 @@ function OneToMany(options) {
2944
2986
  List({
2945
2987
  type,
2946
2988
  optional: options.nullable,
2989
+ lazy: options.eager !== true,
2947
2990
  association: {
2948
2991
  kind: "1:m",
2949
2992
  type
@@ -2984,6 +3027,7 @@ function ManyToMany(entity, mappedBy, options) {
2984
3027
  List({
2985
3028
  type,
2986
3029
  optional: resolvedOptions?.nullable,
3030
+ lazy: resolvedOptions?.eager !== true,
2987
3031
  association: {
2988
3032
  kind: "m:n",
2989
3033
  type
@@ -3293,6 +3337,36 @@ var Slice = class Slice {
3293
3337
  }
3294
3338
  };
3295
3339
  //#endregion
3340
+ //#region src/modules/core/converters/pick-type/pick-type.ts
3341
+ /**
3342
+ * 从模型中选取指定属性,生成新的子集类型。
3343
+ *
3344
+ * 类似 TypeScript 的 `Pick<T, K>`,同时保留 class-validator、class-transformer 和 Swagger 的元数据。
3345
+ *
3346
+ * @param classRef - 使用 `@Model()` 标注的类引用
3347
+ * @param keys - 需要选取的属性名数组
3348
+ * @returns 仅包含指定属性的新类
3349
+ *
3350
+ * @example
3351
+ * ```typescript
3352
+ * const CreateUserDTO = PickType(UserDTO, ['name', 'email'])
3353
+ * ```
3354
+ */
3355
+ function PickType(classRef, keys) {
3356
+ if (!ModelRegister.isModel(classRef)) throw new TypeError("PickType only accepts a class annotated with @Model().");
3357
+ const PickTypeClass = (0, _nestjs_swagger.PickType)(classRef, keys);
3358
+ for (const key of keys) {
3359
+ ModelRegister.copyProperty(classRef, PickTypeClass, key);
3360
+ const metadataKeys = Reflect.getMetadataKeys(classRef.prototype, key);
3361
+ for (const metadataKey of metadataKeys) {
3362
+ const metadata = Reflect.getMetadata(metadataKey, classRef.prototype, key);
3363
+ if (Reflect.hasMetadata(metadataKey, PickTypeClass.prototype, key)) continue;
3364
+ Reflect.defineMetadata(metadataKey, metadata, PickTypeClass.prototype, key);
3365
+ }
3366
+ }
3367
+ return PickTypeClass;
3368
+ }
3369
+ //#endregion
3296
3370
  //#region src/modules/core/converters/omit-type/omit-type.ts
3297
3371
  /**
3298
3372
  * 从模型中排除指定属性,生成新的类型。
@@ -3324,68 +3398,6 @@ function OmitType(classRef, keys) {
3324
3398
  return OmitTypeClass;
3325
3399
  }
3326
3400
  //#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
3401
  //#region src/modules/core/converters/partial-type/partial-type.ts
3390
3402
  /**
3391
3403
  * 将模型的所有属性变为可选,生成新的 Partial 类型。
@@ -3737,12 +3749,12 @@ let OpenBaoTokenManager = _OpenBaoTokenManager = class OpenBaoTokenManager {
3737
3749
  this.logger.log("Token renewed successfully");
3738
3750
  this.scheduleRenewal();
3739
3751
  } catch (renewError) {
3740
- this.logger.error("Failed to renew token, attempting re-authentication", renewError);
3752
+ this.logger.error("Failed to renew token, attempting re-authentication", renewError instanceof Error ? renewError.stack : void 0);
3741
3753
  try {
3742
3754
  await this.authenticate();
3743
3755
  this.scheduleRenewal();
3744
3756
  } catch (authError) {
3745
- this.logger.error("Re-authentication also failed, will retry in 5s", authError);
3757
+ this.logger.error("Re-authentication also failed, will retry in 5s", authError instanceof Error ? authError.stack : void 0);
3746
3758
  this.renewalTimer = setTimeout(() => {
3747
3759
  this.renewToken();
3748
3760
  }, 5e3);
@@ -9956,13 +9968,19 @@ let ObjectStorageService = _ObjectStorageService = class ObjectStorageService {
9956
9968
  * @param options - 可选配置,如是否自动添加前缀
9957
9969
  */
9958
9970
  async get(path, options) {
9971
+ const key = this.buildPath(path, options);
9959
9972
  const command = new _aws_sdk_client_s3.GetObjectCommand({
9960
9973
  Bucket: this.config.bucket,
9961
- Key: this.buildPath(path, options)
9974
+ Key: key
9962
9975
  });
9963
- const res = await this.client.send(command);
9964
- if (!res.Body) throw new Error("No body");
9965
- return node_stream.Readable.fromWeb(res.Body.transformToWebStream());
9976
+ try {
9977
+ const res = await this.client.send(command);
9978
+ if (!res.Body) throw new Error("No body");
9979
+ return node_stream.Readable.fromWeb(res.Body.transformToWebStream());
9980
+ } catch (err) {
9981
+ this.logger.error(`对象存储下载失败, bucket: ${this.config.bucket}, key: ${key}`, err instanceof Error ? err.stack : void 0);
9982
+ throw err;
9983
+ }
9966
9984
  }
9967
9985
  /**
9968
9986
  * 上传文件内容到指定路径。
@@ -9974,41 +9992,55 @@ let ObjectStorageService = _ObjectStorageService = class ObjectStorageService {
9974
9992
  */
9975
9993
  async upload(path, contents, options) {
9976
9994
  const key = this.buildPath(path, options);
9977
- await new _aws_sdk_lib_storage.Upload({
9995
+ const upload = new _aws_sdk_lib_storage.Upload({
9978
9996
  client: this.client,
9979
9997
  params: {
9980
9998
  Bucket: this.config.bucket,
9981
9999
  Key: key,
9982
10000
  Body: contents
9983
10001
  }
9984
- }).done();
9985
- return key;
10002
+ });
10003
+ try {
10004
+ await upload.done();
10005
+ return key;
10006
+ } catch (err) {
10007
+ this.logger.error(`对象存储上传失败, bucket: ${this.config.bucket}, key: ${key}`, err instanceof Error ? err.stack : void 0);
10008
+ throw err;
10009
+ }
9986
10010
  }
9987
10011
  /**
9988
10012
  * 获取文件的元数据(metadata)。
9989
10013
  */
9990
10014
  async getMetadata(path, options) {
10015
+ const key = this.buildPath(path, options);
9991
10016
  const command = new _aws_sdk_client_s3.HeadObjectCommand({
9992
10017
  Bucket: this.config.bucket,
9993
- Key: this.buildPath(path, options)
10018
+ Key: key
9994
10019
  });
9995
- const res = await this.client.send(command);
9996
- if (!res.Metadata) throw new Error("No metadata");
9997
- return res.Metadata;
10020
+ try {
10021
+ const res = await this.client.send(command);
10022
+ if (!res.Metadata) throw new Error("No metadata");
10023
+ return res.Metadata;
10024
+ } catch (err) {
10025
+ this.logger.error(`对象存储获取元数据失败, bucket: ${this.config.bucket}, key: ${key}`, err instanceof Error ? err.stack : void 0);
10026
+ throw err;
10027
+ }
9998
10028
  }
9999
10029
  /**
10000
10030
  * 检查文件是否存在。
10001
10031
  */
10002
10032
  async exists(path, options) {
10033
+ const key = this.buildPath(path, options);
10003
10034
  const command = new _aws_sdk_client_s3.HeadObjectCommand({
10004
10035
  Bucket: this.config.bucket,
10005
- Key: this.buildPath(path, options)
10036
+ Key: key
10006
10037
  });
10007
10038
  try {
10008
10039
  await this.client.send(command);
10009
10040
  return true;
10010
10041
  } catch (err) {
10011
10042
  if (err instanceof Error && err.name === "NotFound") return false;
10043
+ this.logger.error(`对象存储检查文件存在性失败, bucket: ${this.config.bucket}, key: ${key}`, err instanceof Error ? err.stack : void 0);
10012
10044
  throw err;
10013
10045
  }
10014
10046
  }
@@ -10016,11 +10048,17 @@ let ObjectStorageService = _ObjectStorageService = class ObjectStorageService {
10016
10048
  * 删除指定路径的文件。
10017
10049
  */
10018
10050
  async remove(path, options) {
10051
+ const key = this.buildPath(path, options);
10019
10052
  const command = new _aws_sdk_client_s3.DeleteObjectCommand({
10020
10053
  Bucket: this.config.bucket,
10021
- Key: this.buildPath(path, options)
10054
+ Key: key
10022
10055
  });
10023
- await this.client.send(command);
10056
+ try {
10057
+ await this.client.send(command);
10058
+ } catch (err) {
10059
+ this.logger.error(`对象存储删除文件失败, bucket: ${this.config.bucket}, key: ${key}`, err instanceof Error ? err.stack : void 0);
10060
+ throw err;
10061
+ }
10024
10062
  }
10025
10063
  /**
10026
10064
  * 获取文件的预签名 URL,用于临时授权访问。
@@ -10030,11 +10068,17 @@ let ObjectStorageService = _ObjectStorageService = class ObjectStorageService {
10030
10068
  * @param options - 可选配置
10031
10069
  */
10032
10070
  async getSignedUrl(path, expiresInSecond, options) {
10071
+ const key = this.buildPath(path, options);
10033
10072
  const command = new _aws_sdk_client_s3.GetObjectCommand({
10034
10073
  Bucket: this.config.bucket,
10035
- Key: this.buildPath(path, options)
10074
+ Key: key
10036
10075
  });
10037
- return await (0, _aws_sdk_s3_request_presigner.getSignedUrl)(this.client, command, { expiresIn: expiresInSecond });
10076
+ try {
10077
+ return await (0, _aws_sdk_s3_request_presigner.getSignedUrl)(this.client, command, { expiresIn: expiresInSecond });
10078
+ } catch (err) {
10079
+ this.logger.error(`对象存储获取签名URL失败, bucket: ${this.config.bucket}, key: ${key}`, err instanceof Error ? err.stack : void 0);
10080
+ throw err;
10081
+ }
10038
10082
  }
10039
10083
  };
10040
10084
  ObjectStorageService = _ObjectStorageService = __decorate([
@@ -10123,13 +10167,13 @@ exports.DatabaseConfig = DatabaseConfig;
10123
10167
  exports.DegradeException = DegradeException;
10124
10168
  exports.Dictionary = Dictionary;
10125
10169
  exports.DiscreteEntity = DiscreteEntity;
10126
- exports.EagerType = EagerType;
10127
10170
  Object.defineProperty(exports, "EncryptedPayload", {
10128
10171
  enumerable: true,
10129
10172
  get: function() {
10130
10173
  return EncryptedPayload;
10131
10174
  }
10132
10175
  });
10176
+ exports.EntityDto = EntityDto;
10133
10177
  exports.EntityRef = EntityRef;
10134
10178
  exports.Enum = Enum;
10135
10179
  Object.defineProperty(exports, "EnvelopeEncryptionModule", {