@strapi/utils 5.0.0-beta.8 → 5.0.0-beta.9

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
@@ -1,7 +1,7 @@
1
1
  import * as _ from "lodash";
2
2
  import ___default, { kebabCase } from "lodash";
3
3
  import * as dates$1 from "date-fns";
4
- import { has, union, getOr, assoc, assign, cloneDeep, remove, eq, curry, isObject, isNil, clone, isArray, isEmpty, toPath, defaults, isString, mergeAll, get, toNumber, isInteger, pick, omit, trim, pipe as pipe$1, split, map as map$1, flatten, first, identity, constant, join, merge, trimChars, trimCharsEnd, trimCharsStart, isNumber } from "lodash/fp";
4
+ import { has, union, getOr, assoc, assign, cloneDeep, remove, eq, curry, isObject, isNil, clone, isArray, isEmpty, toPath, defaults, isString, get, toNumber, isInteger, pick, omit, trim, pipe as pipe$1, split, map as map$1, flatten, first, identity, constant, join, merge, trimChars, trimCharsEnd, trimCharsStart, isNumber } from "lodash/fp";
5
5
  import { randomUUID } from "crypto";
6
6
  import { machineIdSync } from "node-machine-id";
7
7
  import * as yup$1 from "yup";
@@ -1024,27 +1024,45 @@ const createTransformer = ({ getModel }) => {
1024
1024
  }
1025
1025
  throw new InvalidPopulateError();
1026
1026
  };
1027
- const hasFragmentPopulateDefined = (populate2) => {
1027
+ const hasPopulateFragmentDefined = (populate2) => {
1028
1028
  return typeof populate2 === "object" && "on" in populate2 && !isNil(populate2.on);
1029
1029
  };
1030
+ const hasCountDefined = (populate2) => {
1031
+ return typeof populate2 === "object" && "count" in populate2 && typeof populate2.count === "boolean";
1032
+ };
1030
1033
  const convertPopulateObject = (populate2, schema) => {
1031
1034
  if (!schema) {
1032
1035
  return {};
1033
1036
  }
1034
1037
  const { attributes } = schema;
1035
1038
  return Object.entries(populate2).reduce((acc, [key, subPopulate]) => {
1039
+ if (___default.isString(subPopulate)) {
1040
+ try {
1041
+ const subPopulateAsBoolean = parseType({ type: "boolean", value: subPopulate });
1042
+ return subPopulateAsBoolean === true ? { ...acc, [key]: true } : acc;
1043
+ } catch {
1044
+ }
1045
+ }
1036
1046
  if (___default.isBoolean(subPopulate)) {
1037
- return { ...acc, [key]: subPopulate };
1047
+ return subPopulate === true ? { ...acc, [key]: true } : acc;
1038
1048
  }
1039
1049
  const attribute = attributes[key];
1040
1050
  if (!attribute) {
1041
1051
  return acc;
1042
1052
  }
1043
- const isAllowedAttributeForFragmentPopulate = isDynamicZoneAttribute(attribute) || isMorphToRelationalAttribute(attribute);
1044
- if (isAllowedAttributeForFragmentPopulate && hasFragmentPopulateDefined(subPopulate)) {
1045
- return {
1046
- ...acc,
1047
- [key]: {
1053
+ const isMorphLikeRelationalAttribute = isDynamicZoneAttribute(attribute) || isMorphToRelationalAttribute(attribute);
1054
+ if (isMorphLikeRelationalAttribute) {
1055
+ const hasInvalidProperties = Object.keys(subPopulate).some(
1056
+ (key2) => !["on", "count"].includes(key2)
1057
+ );
1058
+ if (hasInvalidProperties) {
1059
+ throw new Error(
1060
+ `Invalid nested populate for ${schema.info?.singularName}.${key} (${schema.uid}). Expected a fragment ("on") or "count" but found ${JSON.stringify(subPopulate)}`
1061
+ );
1062
+ }
1063
+ const newSubPopulate = {};
1064
+ if (hasPopulateFragmentDefined(subPopulate)) {
1065
+ Object.assign(newSubPopulate, {
1048
1066
  on: Object.entries(subPopulate.on).reduce(
1049
1067
  (acc2, [type, typeSubPopulate]) => ({
1050
1068
  ...acc2,
@@ -1052,21 +1070,15 @@ const createTransformer = ({ getModel }) => {
1052
1070
  }),
1053
1071
  {}
1054
1072
  )
1055
- }
1056
- };
1057
- }
1058
- if (isDynamicZoneAttribute(attribute)) {
1059
- const populates = attribute.components.map((uid) => getModel(uid)).map((schema2) => convertNestedPopulate(subPopulate, schema2)).map((populate22) => populate22 === true ? {} : populate22).filter((populate22) => populate22 !== false);
1060
- if (isEmpty(populates)) {
1061
- return acc;
1073
+ });
1074
+ }
1075
+ if (hasCountDefined(subPopulate)) {
1076
+ Object.assign(newSubPopulate, { count: subPopulate.count });
1062
1077
  }
1063
- return {
1064
- ...acc,
1065
- [key]: mergeAll(populates)
1066
- };
1078
+ return { ...acc, [key]: newSubPopulate };
1067
1079
  }
1068
- if (isMorphToRelationalAttribute(attribute)) {
1069
- return { ...acc, [key]: convertNestedPopulate(subPopulate, void 0) };
1080
+ if (!isMorphLikeRelationalAttribute && hasPopulateFragmentDefined(subPopulate)) {
1081
+ throw new Error(`Using fragments is not permitted to populate "${key}" in "${schema.uid}"`);
1070
1082
  }
1071
1083
  let targetSchemaUID;
1072
1084
  if (attribute.type === "relation") {
@@ -1822,7 +1834,8 @@ const populate = traverseFactory().intercept(isStringArray$1, async (visitor2, o
1822
1834
  return;
1823
1835
  }
1824
1836
  const newValue2 = await recurse(visitor2, { schema, path, getModel }, { on: value?.on });
1825
- set(key, { on: newValue2 });
1837
+ set(key, newValue2);
1838
+ return;
1826
1839
  }
1827
1840
  const targetSchemaUID = attribute.target;
1828
1841
  const targetSchema = getModel(targetSchemaUID);
@@ -1844,36 +1857,15 @@ const populate = traverseFactory().intercept(isStringArray$1, async (visitor2, o
1844
1857
  const targetSchema = getModel(attribute.component);
1845
1858
  const newValue = await recurse(visitor2, { schema: targetSchema, path, getModel }, value);
1846
1859
  set(key, newValue);
1847
- }).onDynamicZone(
1848
- async ({ key, value, attribute, schema, visitor: visitor2, path, getModel }, { set, recurse }) => {
1849
- if (isNil(value)) {
1850
- return;
1851
- }
1852
- if (isObject(value)) {
1853
- const { components } = attribute;
1854
- const newValue = {};
1855
- let newProperties = omit("on", value);
1856
- for (const componentUID of components) {
1857
- const componentSchema = getModel(componentUID);
1858
- const properties = await recurse(
1859
- visitor2,
1860
- { schema: componentSchema, path, getModel },
1861
- value
1862
- );
1863
- newProperties = merge(newProperties, properties);
1864
- }
1865
- Object.assign(newValue, newProperties);
1866
- if ("on" in value && value.on) {
1867
- const newOn = await recurse(visitor2, { schema, path, getModel }, { on: value.on });
1868
- Object.assign(newValue, newOn);
1869
- }
1870
- set(key, newValue);
1871
- } else {
1872
- const newValue = await recurse(visitor2, { schema, path, getModel }, value);
1873
- set(key, newValue);
1874
- }
1860
+ }).onDynamicZone(async ({ key, value, schema, visitor: visitor2, path, getModel }, { set, recurse }) => {
1861
+ if (isNil(value) || !isObject(value)) {
1862
+ return;
1875
1863
  }
1876
- );
1864
+ if ("on" in value && value.on) {
1865
+ const newOn = await recurse(visitor2, { schema, path, getModel }, { on: value.on });
1866
+ set(key, newOn);
1867
+ }
1868
+ });
1877
1869
  const traverseQueryPopulate = curry(populate.traverse);
1878
1870
  const isStringArray = (value) => isArray(value) && value.every(isString);
1879
1871
  const fields = traverseFactory().intercept(isStringArray, async (visitor2, options, fields2, { recurse }) => {