@buka/nestjs-kit 4.4.12 → 4.5.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.mjs CHANGED
@@ -1325,6 +1325,13 @@ let BukaPageQueryValidationPipe = class BukaPageQueryValidationPipe {
1325
1325
  if (metadata.type !== "query") throw new InternalServerErrorException("BukaPageQueryValidationPipe can only be used to validate query parameters.");
1326
1326
  if (!value.page) if (this.options.optional) return value;
1327
1327
  else throw new BadRequestException("Missing required query parameter: page");
1328
+ /**
1329
+ * 走到这里时 `value.page` 已非空,但 query string 可能把 `page` 解析为标量
1330
+ * (如 `?page=1` 解析为字符串 `"1"`),对原始值使用 `in` 运算符会抛 TypeError,
1331
+ * 冒泡到全局异常过滤器后被映射为 500。先保证 `page` 是纯对象,数组一并拦截,
1332
+ * 使非法形态收敛为 400。
1333
+ */
1334
+ if (typeof value.page !== "object" || Array.isArray(value.page)) throw new BadRequestException("Invalid page query: page must be an object.");
1328
1335
  const mode = this.options.mode;
1329
1336
  if ((!mode || mode === "offset") && ("limit" in value.page || "offset" in value.page)) {
1330
1337
  if ("after" in value.page || "first" in value.page || "before" in value.page || "last" in value.page) throw new BadRequestException(mixedOffsetAndCursorError);
@@ -1580,6 +1587,27 @@ function swaggerMetadataToJsonSchema(metadata) {
1580
1587
  }
1581
1588
  //#endregion
1582
1589
  //#region src/modules/core/converters/filter-query-type/get-filter-query-schema.ts
1590
+ /**
1591
+ * 为单个标量属性生成 filter 操作符的 Swagger schema。
1592
+ *
1593
+ * 生成 `{ type: 'object', properties: { eq: ..., ne: ..., in: [...], ... } }` 结构,
1594
+ * 值类型取自原始 Swagger metadata(如 `{ type: 'string' }`),无 metadata 时兜底为 `string`。
1595
+ */
1596
+ function getScalarOperatorSchema(classRef, propertyKey) {
1597
+ const propertySchema = {
1598
+ type: "object",
1599
+ properties: {},
1600
+ additionalProperties: false
1601
+ };
1602
+ const originalSchema = getMetadata$1(classRef, propertyKey);
1603
+ const operators = getPropertyOperators(classRef, propertyKey);
1604
+ for (const operator of operators) if (["in", "nin"].includes(operator)) propertySchema.properties[operator] = {
1605
+ type: "array",
1606
+ items: originalSchema ? swaggerMetadataToJsonSchema(originalSchema) : { type: "string" }
1607
+ };
1608
+ else propertySchema.properties[operator] = originalSchema ? swaggerMetadataToJsonSchema(originalSchema) : { type: "string" };
1609
+ return propertySchema;
1610
+ }
1583
1611
  function getObjectSchema(classRef, visited = /* @__PURE__ */ new WeakSet()) {
1584
1612
  const properties = {};
1585
1613
  const requiredKeys = [];
@@ -1592,38 +1620,28 @@ function getObjectSchema(classRef, visited = /* @__PURE__ */ new WeakSet()) {
1592
1620
  for (const propertyKey of ModelRegister.getModelPropertyKeys(classRef)) {
1593
1621
  if (typeof propertyKey !== "string") continue;
1594
1622
  const propertyMetadata = ModelRegister.getProperty(classRef, propertyKey);
1595
- const isCollection = propertyMetadata?.association?.kind === "1:m" || propertyMetadata?.association?.kind === "m:n";
1596
- const isRelation = !!propertyMetadata?.association;
1597
1623
  if (!!!propertyMetadata?.optional) requiredKeys.push(propertyKey);
1598
- if (isRelation) {
1599
- const relationClassRef = propertyMetadata.association.type();
1600
- if (visited.has(relationClassRef)) continue;
1601
- if (isCollection) {
1602
- const sub = getObjectSchema(relationClassRef, visited);
1603
- const operators = getCollectionOperators(classRef, propertyKey);
1604
- const wrapper = {
1605
- type: "object",
1606
- properties: {},
1607
- additionalProperties: false
1608
- };
1609
- for (const operator of operators) wrapper.properties[operator] = sub;
1610
- properties[propertyKey] = wrapper;
1611
- } else properties[propertyKey] = getObjectSchema(relationClassRef, visited);
1612
- } else {
1613
- const propertySchema = {
1624
+ if (propertyMetadata?.kind === "scalar" || propertyMetadata?.kind === "enum") properties[propertyKey] = getScalarOperatorSchema(classRef, propertyKey);
1625
+ else if (propertyMetadata?.kind === "composite" && propertyMetadata.type) {
1626
+ const compositeType = propertyMetadata.type();
1627
+ if (ModelRegister.isModel(compositeType) && !visited.has(compositeType)) properties[propertyKey] = getObjectSchema(compositeType, visited);
1628
+ else properties[propertyKey] = getScalarOperatorSchema(classRef, propertyKey);
1629
+ } else if (propertyMetadata?.kind === "list" || propertyMetadata?.kind === "dictionary") {
1630
+ let elementSchema;
1631
+ if (propertyMetadata.type && !isScalarClass(propertyMetadata.type)) {
1632
+ const elementType = propertyMetadata.type();
1633
+ if (ModelRegister.isModel(elementType) && !visited.has(elementType)) elementSchema = getObjectSchema(elementType, visited);
1634
+ else elementSchema = getScalarOperatorSchema(classRef, propertyKey);
1635
+ } else elementSchema = getScalarOperatorSchema(classRef, propertyKey);
1636
+ const operators = getCollectionOperators(classRef, propertyKey);
1637
+ const wrapper = {
1614
1638
  type: "object",
1615
1639
  properties: {},
1616
1640
  additionalProperties: false
1617
1641
  };
1618
- const originalSchema = getMetadata$1(classRef, propertyKey);
1619
- const operators = getPropertyOperators(classRef, propertyKey);
1620
- for (const operator of operators) if (["in", "nin"].includes(operator)) propertySchema.properties[operator] = {
1621
- type: "array",
1622
- items: originalSchema ? swaggerMetadataToJsonSchema(originalSchema) : { type: "string" }
1623
- };
1624
- else propertySchema.properties[operator] = originalSchema ? swaggerMetadataToJsonSchema(originalSchema) : { type: "string" };
1625
- properties[propertyKey] = propertySchema;
1626
- }
1642
+ for (const operator of operators) wrapper.properties[operator] = elementSchema;
1643
+ properties[propertyKey] = wrapper;
1644
+ } else properties[propertyKey] = getScalarOperatorSchema(classRef, propertyKey);
1627
1645
  }
1628
1646
  if (requiredKeys.length > 0) schema.required = requiredKeys;
1629
1647
  return schema;
@@ -1728,6 +1746,23 @@ function createFilterQueryObjectClassRef(classRef, visited = /* @__PURE__ */ new
1728
1746
  optional: isOptional
1729
1747
  })(FilterQueryObjectClass.prototype, propertyKey);
1730
1748
  }
1749
+ } else if (propertyMetadata?.kind === "composite" && propertyMetadata?.type) {
1750
+ const compositeType = propertyMetadata.type();
1751
+ if (ModelRegister.isModel(compositeType) && !visited.has(compositeType)) {
1752
+ const sub = createFilterQueryObjectClassRef(compositeType, visited);
1753
+ Composite({
1754
+ type: () => sub,
1755
+ optional: isOptional
1756
+ })(FilterQueryObjectClass.prototype, propertyKey);
1757
+ if (!isOptional) HasAnyKey(getFilterQueryOperators(classRef, propertyKey) || getPropertyOperators(classRef, propertyKey))(FilterQueryObjectClass.prototype, propertyKey);
1758
+ } else {
1759
+ const propertyClass = createFilterQueryPropertyClassRef(classRef, propertyKey);
1760
+ Composite({
1761
+ type: () => propertyClass,
1762
+ optional: isOptional
1763
+ })(FilterQueryObjectClass.prototype, propertyKey);
1764
+ if (!isOptional) HasAnyKey(getPropertyOperators(classRef, propertyKey))(FilterQueryObjectClass.prototype, propertyKey);
1765
+ }
1731
1766
  } else {
1732
1767
  const propertyClass = createFilterQueryPropertyClassRef(classRef, propertyKey);
1733
1768
  Composite({