@buka/nestjs-kit 4.4.12 → 4.4.13

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
@@ -1580,6 +1580,27 @@ function swaggerMetadataToJsonSchema(metadata) {
1580
1580
  }
1581
1581
  //#endregion
1582
1582
  //#region src/modules/core/converters/filter-query-type/get-filter-query-schema.ts
1583
+ /**
1584
+ * 为单个标量属性生成 filter 操作符的 Swagger schema。
1585
+ *
1586
+ * 生成 `{ type: 'object', properties: { eq: ..., ne: ..., in: [...], ... } }` 结构,
1587
+ * 值类型取自原始 Swagger metadata(如 `{ type: 'string' }`),无 metadata 时兜底为 `string`。
1588
+ */
1589
+ function getScalarOperatorSchema(classRef, propertyKey) {
1590
+ const propertySchema = {
1591
+ type: "object",
1592
+ properties: {},
1593
+ additionalProperties: false
1594
+ };
1595
+ const originalSchema = getMetadata$1(classRef, propertyKey);
1596
+ const operators = getPropertyOperators(classRef, propertyKey);
1597
+ for (const operator of operators) if (["in", "nin"].includes(operator)) propertySchema.properties[operator] = {
1598
+ type: "array",
1599
+ items: originalSchema ? swaggerMetadataToJsonSchema(originalSchema) : { type: "string" }
1600
+ };
1601
+ else propertySchema.properties[operator] = originalSchema ? swaggerMetadataToJsonSchema(originalSchema) : { type: "string" };
1602
+ return propertySchema;
1603
+ }
1583
1604
  function getObjectSchema(classRef, visited = /* @__PURE__ */ new WeakSet()) {
1584
1605
  const properties = {};
1585
1606
  const requiredKeys = [];
@@ -1592,38 +1613,28 @@ function getObjectSchema(classRef, visited = /* @__PURE__ */ new WeakSet()) {
1592
1613
  for (const propertyKey of ModelRegister.getModelPropertyKeys(classRef)) {
1593
1614
  if (typeof propertyKey !== "string") continue;
1594
1615
  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
1616
  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 = {
1617
+ if (propertyMetadata?.kind === "scalar" || propertyMetadata?.kind === "enum") properties[propertyKey] = getScalarOperatorSchema(classRef, propertyKey);
1618
+ else if (propertyMetadata?.kind === "composite" && propertyMetadata.type) {
1619
+ const compositeType = propertyMetadata.type();
1620
+ if (ModelRegister.isModel(compositeType) && !visited.has(compositeType)) properties[propertyKey] = getObjectSchema(compositeType, visited);
1621
+ else properties[propertyKey] = getScalarOperatorSchema(classRef, propertyKey);
1622
+ } else if (propertyMetadata?.kind === "list" || propertyMetadata?.kind === "dictionary") {
1623
+ let elementSchema;
1624
+ if (propertyMetadata.type && !isScalarClass(propertyMetadata.type)) {
1625
+ const elementType = propertyMetadata.type();
1626
+ if (ModelRegister.isModel(elementType) && !visited.has(elementType)) elementSchema = getObjectSchema(elementType, visited);
1627
+ else elementSchema = getScalarOperatorSchema(classRef, propertyKey);
1628
+ } else elementSchema = getScalarOperatorSchema(classRef, propertyKey);
1629
+ const operators = getCollectionOperators(classRef, propertyKey);
1630
+ const wrapper = {
1614
1631
  type: "object",
1615
1632
  properties: {},
1616
1633
  additionalProperties: false
1617
1634
  };
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
- }
1635
+ for (const operator of operators) wrapper.properties[operator] = elementSchema;
1636
+ properties[propertyKey] = wrapper;
1637
+ } else properties[propertyKey] = getScalarOperatorSchema(classRef, propertyKey);
1627
1638
  }
1628
1639
  if (requiredKeys.length > 0) schema.required = requiredKeys;
1629
1640
  return schema;
@@ -1728,6 +1739,23 @@ function createFilterQueryObjectClassRef(classRef, visited = /* @__PURE__ */ new
1728
1739
  optional: isOptional
1729
1740
  })(FilterQueryObjectClass.prototype, propertyKey);
1730
1741
  }
1742
+ } else if (propertyMetadata?.kind === "composite" && propertyMetadata?.type) {
1743
+ const compositeType = propertyMetadata.type();
1744
+ if (ModelRegister.isModel(compositeType) && !visited.has(compositeType)) {
1745
+ const sub = createFilterQueryObjectClassRef(compositeType, visited);
1746
+ Composite({
1747
+ type: () => sub,
1748
+ optional: isOptional
1749
+ })(FilterQueryObjectClass.prototype, propertyKey);
1750
+ if (!isOptional) HasAnyKey(getFilterQueryOperators(classRef, propertyKey) || getPropertyOperators(classRef, propertyKey))(FilterQueryObjectClass.prototype, propertyKey);
1751
+ } else {
1752
+ const propertyClass = createFilterQueryPropertyClassRef(classRef, propertyKey);
1753
+ Composite({
1754
+ type: () => propertyClass,
1755
+ optional: isOptional
1756
+ })(FilterQueryObjectClass.prototype, propertyKey);
1757
+ if (!isOptional) HasAnyKey(getPropertyOperators(classRef, propertyKey))(FilterQueryObjectClass.prototype, propertyKey);
1758
+ }
1731
1759
  } else {
1732
1760
  const propertyClass = createFilterQueryPropertyClassRef(classRef, propertyKey);
1733
1761
  Composite({