@buka/nestjs-kit 4.4.0 → 4.4.1

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 CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## [4.4.1](https://github.com/buka-ltd/npm.nestjs-kit/compare/v4.4.0...v4.4.1) (2026-06-04)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * 修复 EntityDto 导致swagger属性丢失的问题 ([b6b2f69](https://github.com/buka-ltd/npm.nestjs-kit/commit/b6b2f6902f3eb6ccd8bc4373b0c9b12a772b385d))
9
+
3
10
  ## [4.4.0](https://github.com/buka-ltd/npm.nestjs-kit/compare/v4.3.5...v4.4.0) (2026-06-03)
4
11
 
5
12
 
package/dist/index.js CHANGED
@@ -1442,9 +1442,19 @@ function isString(value) {
1442
1442
  }
1443
1443
  //#endregion
1444
1444
  //#region src/utils/internals/nestjs-swagger.ts
1445
+ /**
1446
+ * 模拟 @nestjs/swagger 的 DECORATORS 常量。
1447
+ *
1448
+ * `@nestjs/swagger` 的类型声明(`index.d.ts`)未导出 `DECORATORS`,
1449
+ * 仅在运行时可用。为避免 `ts(2305)` 错误,直接内联其常量值。
1450
+ */
1451
+ const DECORATORS = {
1452
+ API_MODEL_PROPERTIES: "swagger/apiModelProperties",
1453
+ API_MODEL_PROPERTIES_ARRAY: "swagger/apiModelPropertiesArray"
1454
+ };
1445
1455
  const METADATA_FACTORY_NAME = "_OPENAPI_METADATA_FACTORY";
1446
1456
  function getModelProperties(prototype) {
1447
- return (Reflect.getMetadata(_nestjs_swagger.DECORATORS.API_MODEL_PROPERTIES_ARRAY, prototype) || []).filter(isString).filter((key) => key.charAt(0) === ":" && !isFunction(prototype[key])).map((key) => key.slice(1));
1457
+ return (Reflect.getMetadata(DECORATORS.API_MODEL_PROPERTIES_ARRAY, prototype) || []).filter(isString).filter((key) => key.charAt(0) === ":" && !isFunction(prototype[key])).map((key) => key.slice(1));
1448
1458
  }
1449
1459
  function clonePluginMetadataFactory(target, parent, transformFn = (x) => x) {
1450
1460
  let targetMetadata = {};
@@ -1478,9 +1488,9 @@ function cloneMetadata(target, source, keys) {
1478
1488
  }
1479
1489
  }
1480
1490
  function getMetadataOfDecorator(classRef, propertyKey) {
1481
- if (propertyKey) return Reflect.getMetadata(_nestjs_swagger.DECORATORS.API_MODEL_PROPERTIES, classRef.prototype, propertyKey);
1491
+ if (propertyKey) return Reflect.getMetadata(DECORATORS.API_MODEL_PROPERTIES, classRef.prototype, propertyKey);
1482
1492
  const props = getModelProperties(classRef.prototype);
1483
- return ramda.fromPairs(props.map((prop) => [prop, Reflect.getMetadata(_nestjs_swagger.DECORATORS.API_MODEL_PROPERTIES, classRef.prototype, prop)]));
1493
+ return ramda.fromPairs(props.map((prop) => [prop, Reflect.getMetadata(DECORATORS.API_MODEL_PROPERTIES, classRef.prototype, prop)]));
1484
1494
  }
1485
1495
  function getMetadataOfPlugin(classRef, propertyKey) {
1486
1496
  const propsInPlugin = typeof classRef["_OPENAPI_METADATA_FACTORY"] === "function" ? classRef[METADATA_FACTORY_NAME]() : {};
@@ -2714,6 +2724,11 @@ __decorate([
2714
2724
  function EntityDto(classRef) {
2715
2725
  if (!ModelRegister.isModel(classRef)) throw new TypeError("EntityDto only accepts a class annotated with @Model().");
2716
2726
  class EntityDtoClass {}
2727
+ /**
2728
+ * 收集从源实体复制的 Swagger 属性元数据,
2729
+ * 用于构建 `_OPENAPI_METADATA_FACTORY`。
2730
+ */
2731
+ const swaggerProps = {};
2717
2732
  for (const propertyKey of ModelRegister.getModelPropertyKeys(classRef)) {
2718
2733
  if (ModelRegister.getProperty(classRef, propertyKey)?.lazy) continue;
2719
2734
  ModelRegister.copyProperty(classRef, EntityDtoClass, propertyKey);
@@ -2723,7 +2738,26 @@ function EntityDto(classRef) {
2723
2738
  if (Reflect.hasMetadata(metadataKey, EntityDtoClass.prototype, propertyKey)) continue;
2724
2739
  Reflect.defineMetadata(metadataKey, value, EntityDtoClass.prototype, propertyKey);
2725
2740
  }
2741
+ /**
2742
+ * 收集已复制的 `@ApiProperty` 元数据,后续通过
2743
+ * `_OPENAPI_METADATA_FACTORY` 暴露给 `@nestjs/swagger`,
2744
+ * 使 applyMetadataFactory() 遍历原型链时能发现这些属性。
2745
+ */
2746
+ if (typeof propertyKey === "string") {
2747
+ const swaggerMeta = Reflect.getMetadata(DECORATORS.API_MODEL_PROPERTIES, EntityDtoClass.prototype, propertyKey);
2748
+ if (swaggerMeta) swaggerProps[propertyKey] = swaggerMeta;
2749
+ }
2726
2750
  }
2751
+ /**
2752
+ * 设置 `_OPENAPI_METADATA_FACTORY` 静态方法。
2753
+ *
2754
+ * `@nestjs/swagger` 插件在编译时无法识别 `EntityDto`,
2755
+ * 只对子类(如 `CourseBriefDto`)中直接声明的属性生成 factory。
2756
+ * 当 `applyMetadataFactory()` 遍历原型链时,
2757
+ * 它会在 `EntityDtoClass` 上找到此 factory,
2758
+ * 并将源实体属性应用到子类的 prototype 上。
2759
+ */
2760
+ if (Object.keys(swaggerProps).length > 0) EntityDtoClass[METADATA_FACTORY_NAME] = () => swaggerProps;
2727
2761
  return EntityDtoClass;
2728
2762
  }
2729
2763
  //#endregion