@strapi/utils 4.14.5 → 4.15.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
@@ -552,7 +552,7 @@ yup$1.addMethod(
552
552
  function uniqueProperty(propertyName, message) {
553
553
  return this.test("unique", message, function unique(list) {
554
554
  const errors2 = [];
555
- list == null ? void 0 : list.forEach((element, index2) => {
555
+ list?.forEach((element, index2) => {
556
556
  const sameElements = list.filter(
557
557
  (e) => get(propertyName, e) === get(propertyName, element)
558
558
  );
@@ -744,13 +744,10 @@ const isDraft = (data, model) => hasDraftAndPublish(model) && _$1.get(data, PUBL
744
744
  const isSingleType = ({ kind = COLLECTION_TYPE }) => kind === SINGLE_TYPE;
745
745
  const isCollectionType = ({ kind = COLLECTION_TYPE }) => kind === COLLECTION_TYPE;
746
746
  const isKind = (kind) => (model) => model.kind === kind;
747
- const getStoredPrivateAttributes = (model) => {
748
- var _a;
749
- return union(
750
- ((_a = strapi == null ? void 0 : strapi.config) == null ? void 0 : _a.get("api.responses.privateAttributes", [])) ?? [],
751
- getOr([], "options.privateAttributes", model)
752
- );
753
- };
747
+ const getStoredPrivateAttributes = (model) => union(
748
+ strapi?.config?.get("api.responses.privateAttributes", []) ?? [],
749
+ getOr([], "options.privateAttributes", model)
750
+ );
754
751
  const getPrivateAttributes = (model) => {
755
752
  return _$1.union(
756
753
  getStoredPrivateAttributes(model),
@@ -758,22 +755,20 @@ const getPrivateAttributes = (model) => {
758
755
  );
759
756
  };
760
757
  const isPrivateAttribute = (model, attributeName) => {
761
- var _a, _b;
762
- if (((_b = (_a = model == null ? void 0 : model.attributes) == null ? void 0 : _a[attributeName]) == null ? void 0 : _b.private) === true) {
758
+ if (model?.attributes?.[attributeName]?.private === true) {
763
759
  return true;
764
760
  }
765
761
  return getStoredPrivateAttributes(model).includes(attributeName);
766
762
  };
767
763
  const isScalarAttribute = (attribute) => {
768
- return !["media", "component", "relation", "dynamiczone"].includes(attribute == null ? void 0 : attribute.type);
764
+ return !["media", "component", "relation", "dynamiczone"].includes(attribute?.type);
769
765
  };
770
- const isMediaAttribute = (attribute) => (attribute == null ? void 0 : attribute.type) === "media";
771
- const isRelationalAttribute = (attribute) => (attribute == null ? void 0 : attribute.type) === "relation";
772
- const isComponentAttribute = (attribute) => ["component", "dynamiczone"].includes(attribute == null ? void 0 : attribute.type);
773
- const isDynamicZoneAttribute = (attribute) => (attribute == null ? void 0 : attribute.type) === "dynamiczone";
766
+ const isMediaAttribute = (attribute) => attribute?.type === "media";
767
+ const isRelationalAttribute = (attribute) => attribute?.type === "relation";
768
+ const isComponentAttribute = (attribute) => ["component", "dynamiczone"].includes(attribute?.type);
769
+ const isDynamicZoneAttribute = (attribute) => attribute?.type === "dynamiczone";
774
770
  const isMorphToRelationalAttribute = (attribute) => {
775
- var _a, _b;
776
- return isRelationalAttribute(attribute) && ((_b = (_a = attribute == null ? void 0 : attribute.relation) == null ? void 0 : _a.startsWith) == null ? void 0 : _b.call(_a, "morphTo"));
771
+ return isRelationalAttribute(attribute) && attribute?.relation?.startsWith?.("morphTo");
777
772
  };
778
773
  const getComponentAttributes = (schema) => {
779
774
  return _$1.reduce(
@@ -1170,7 +1165,7 @@ const forEachAsync = async (array, func, options) => {
1170
1165
  await pMap(array, func, options);
1171
1166
  };
1172
1167
  const visitor$7 = ({ key: key2, attribute }, { remove: remove2 }) => {
1173
- if ((attribute == null ? void 0 : attribute.type) === "password") {
1168
+ if (attribute?.type === "password") {
1174
1169
  remove2(key2);
1175
1170
  }
1176
1171
  };
@@ -1186,7 +1181,6 @@ const visitor$6 = ({ schema, key: key2, attribute }, { remove: remove2 }) => {
1186
1181
  const ACTIONS_TO_VERIFY$1 = ["find"];
1187
1182
  const { CREATED_BY_ATTRIBUTE: CREATED_BY_ATTRIBUTE$1, UPDATED_BY_ATTRIBUTE: UPDATED_BY_ATTRIBUTE$1 } = constants$1;
1188
1183
  const removeRestrictedRelations = (auth) => async ({ data, key: key2, attribute, schema }, { remove: remove2, set }) => {
1189
- var _a;
1190
1184
  if (!attribute) {
1191
1185
  return;
1192
1186
  }
@@ -1221,7 +1215,7 @@ const removeRestrictedRelations = (auth) => async ({ data, key: key2, attribute,
1221
1215
  await handleMorphRelation();
1222
1216
  return;
1223
1217
  }
1224
- if (isCreatorRelation && ((_a = schema.options) == null ? void 0 : _a.populateCreatorFields)) {
1218
+ if (isCreatorRelation && schema.options?.populateCreatorFields) {
1225
1219
  return;
1226
1220
  }
1227
1221
  await handleRegularRelation();
@@ -1289,7 +1283,7 @@ const removeRestrictedFields = (restrictedFields = null) => ({ key: key2, path:
1289
1283
  return;
1290
1284
  }
1291
1285
  const isRestrictedNested = restrictedFields.some(
1292
- (allowedPath) => path == null ? void 0 : path.toString().startsWith(`${allowedPath}.`)
1286
+ (allowedPath) => path?.toString().startsWith(`${allowedPath}.`)
1293
1287
  );
1294
1288
  if (isRestrictedNested) {
1295
1289
  remove2(key2);
@@ -1432,24 +1426,23 @@ const traverseFactory = () => {
1432
1426
  }
1433
1427
  };
1434
1428
  const traverse = async (visitor2, options, data) => {
1435
- var _a, _b, _c;
1436
1429
  const { path = DEFAULT_PATH, schema } = options ?? {};
1437
1430
  for (const { predicate, handler } of state.interceptors) {
1438
1431
  if (predicate(data)) {
1439
1432
  return handler(visitor2, options, data, { recurse: traverse });
1440
1433
  }
1441
1434
  }
1442
- const parser = (_a = state.parsers.find((parser2) => parser2.predicate(data))) == null ? void 0 : _a.parser;
1443
- const utils2 = parser == null ? void 0 : parser(data);
1435
+ const parser = state.parsers.find((parser2) => parser2.predicate(data))?.parser;
1436
+ const utils2 = parser?.(data);
1444
1437
  if (!utils2) {
1445
1438
  return data;
1446
1439
  }
1447
1440
  let out = utils2.transform(data);
1448
1441
  const keys = utils2.keys(out);
1449
1442
  for (const key2 of keys) {
1450
- const attribute = ((_b = schema == null ? void 0 : schema.attributes) == null ? void 0 : _b[key2]) ?? // FIX: Needed to not break existing behavior on the API.
1443
+ const attribute = schema?.attributes?.[key2] ?? // FIX: Needed to not break existing behavior on the API.
1451
1444
  // It looks for the attribute in the DB metadata when the key is in snake_case
1452
- ((_c = schema == null ? void 0 : schema.attributes) == null ? void 0 : _c[strapi.db.metadata.get(schema == null ? void 0 : schema.uid).columnToAttribute[key2]]);
1445
+ schema?.attributes?.[strapi.db.metadata.get(schema?.uid).columnToAttribute[key2]];
1453
1446
  const newPath = { ...path };
1454
1447
  newPath.raw = isNil(path.raw) ? key2 : `${path.raw}.${key2}`;
1455
1448
  if (!isNil(attribute)) {
@@ -1522,16 +1515,16 @@ const traverseFactory = () => {
1522
1515
  return this;
1523
1516
  },
1524
1517
  onRelation(handler) {
1525
- return this.onAttribute(({ attribute }) => (attribute == null ? void 0 : attribute.type) === "relation", handler);
1518
+ return this.onAttribute(({ attribute }) => attribute?.type === "relation", handler);
1526
1519
  },
1527
1520
  onMedia(handler) {
1528
- return this.onAttribute(({ attribute }) => (attribute == null ? void 0 : attribute.type) === "media", handler);
1521
+ return this.onAttribute(({ attribute }) => attribute?.type === "media", handler);
1529
1522
  },
1530
1523
  onComponent(handler) {
1531
- return this.onAttribute(({ attribute }) => (attribute == null ? void 0 : attribute.type) === "component", handler);
1524
+ return this.onAttribute(({ attribute }) => attribute?.type === "component", handler);
1532
1525
  },
1533
1526
  onDynamicZone(handler) {
1534
- return this.onAttribute(({ attribute }) => (attribute == null ? void 0 : attribute.type) === "dynamiczone", handler);
1527
+ return this.onAttribute(({ attribute }) => attribute?.type === "dynamiczone", handler);
1535
1528
  }
1536
1529
  };
1537
1530
  };
@@ -1711,8 +1704,7 @@ const populate = traverseFactory().intercept(isStringArray$2, async (visitor2, o
1711
1704
  );
1712
1705
  return visitedPopulate.filter((item) => !isNil(item));
1713
1706
  }).intercept(isWildCardConstant, (visitor2, options, _data, { recurse }) => {
1714
- var _a;
1715
- const attributes = (_a = options.schema) == null ? void 0 : _a.attributes;
1707
+ const attributes = options.schema?.attributes;
1716
1708
  if (!attributes) {
1717
1709
  return "*";
1718
1710
  }
@@ -1783,10 +1775,10 @@ const populate = traverseFactory().intercept(isStringArray$2, async (visitor2, o
1783
1775
  return;
1784
1776
  }
1785
1777
  if (isMorphToRelationalAttribute(attribute)) {
1786
- if (!isObject(value2) || !("on" in value2 && isObject(value2 == null ? void 0 : value2.on))) {
1778
+ if (!isObject(value2) || !("on" in value2 && isObject(value2?.on))) {
1787
1779
  return;
1788
1780
  }
1789
- const newValue2 = await recurse(visitor2, { schema, path }, { on: value2 == null ? void 0 : value2.on });
1781
+ const newValue2 = await recurse(visitor2, { schema, path }, { on: value2?.on });
1790
1782
  set(key2, { on: newValue2 });
1791
1783
  }
1792
1784
  const targetSchemaUID = attribute.target;
@@ -1915,12 +1907,11 @@ const isObjKey = (key2, obj2) => {
1915
1907
  return key2 in obj2;
1916
1908
  };
1917
1909
  const isOperatorOfType = (type, key2, ignoreCase = false) => {
1918
- var _a, _b;
1919
1910
  if (ignoreCase) {
1920
- return ((_a = OPERATORS_LOWERCASE[type]) == null ? void 0 : _a.includes(key2.toLowerCase())) ?? false;
1911
+ return OPERATORS_LOWERCASE[type]?.includes(key2.toLowerCase()) ?? false;
1921
1912
  }
1922
1913
  if (isObjKey(type, OPERATORS)) {
1923
- return ((_b = OPERATORS[type]) == null ? void 0 : _b.includes(key2)) ?? false;
1914
+ return OPERATORS[type]?.includes(key2) ?? false;
1924
1915
  }
1925
1916
  return false;
1926
1917
  };
@@ -2199,7 +2190,7 @@ const throwInvalidParam = ({ key: key2 }) => {
2199
2190
  throw new ValidationError(`Invalid parameter ${key2}`);
2200
2191
  };
2201
2192
  const visitor$3 = ({ key: key2, attribute }) => {
2202
- if ((attribute == null ? void 0 : attribute.type) === "password") {
2193
+ if (attribute?.type === "password") {
2203
2194
  throwInvalidParam({ key: key2 });
2204
2195
  }
2205
2196
  };
@@ -2215,7 +2206,6 @@ const visitor$2 = ({ schema, key: key2, attribute }) => {
2215
2206
  const ACTIONS_TO_VERIFY = ["find"];
2216
2207
  const { CREATED_BY_ATTRIBUTE, UPDATED_BY_ATTRIBUTE } = constants$1;
2217
2208
  const throwRestrictedRelations = (auth) => async ({ data, key: key2, attribute, schema }) => {
2218
- var _a;
2219
2209
  if (!attribute) {
2220
2210
  return;
2221
2211
  }
@@ -2244,7 +2234,7 @@ const throwRestrictedRelations = (auth) => async ({ data, key: key2, attribute,
2244
2234
  await handleMorphRelation();
2245
2235
  return;
2246
2236
  }
2247
- if (isCreatorRelation && ((_a = schema.options) == null ? void 0 : _a.populateCreatorFields)) {
2237
+ if (isCreatorRelation && schema.options?.populateCreatorFields) {
2248
2238
  return;
2249
2239
  }
2250
2240
  await handleRegularRelation();
@@ -2310,7 +2300,7 @@ const throwRestrictedFields = (restrictedFields = null) => ({ key: key2, path: {
2310
2300
  throwInvalidParam({ key: key2 });
2311
2301
  }
2312
2302
  const isRestrictedNested = restrictedFields.some(
2313
- (allowedPath) => path == null ? void 0 : path.toString().startsWith(`${allowedPath}.`)
2303
+ (allowedPath) => path?.toString().startsWith(`${allowedPath}.`)
2314
2304
  );
2315
2305
  if (isRestrictedNested) {
2316
2306
  throwInvalidParam({ key: key2 });
@@ -2803,7 +2793,7 @@ const convertAndSanitizeFilters = (filters2, schema) => {
2803
2793
  }
2804
2794
  const removeOperator = (operator) => delete filters2[operator];
2805
2795
  for (const [key2, value2] of Object.entries(filters2)) {
2806
- const attribute = get(key2, schema == null ? void 0 : schema.attributes);
2796
+ const attribute = get(key2, schema?.attributes);
2807
2797
  const validKey = isOperator(key2) || isValidSchemaAttribute(key2, schema);
2808
2798
  if (!validKey) {
2809
2799
  removeOperator(key2);