@littlebox/strapi-suite 1.0.42 → 1.0.43

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/README.md CHANGED
@@ -203,6 +203,7 @@ GET /api/littlebox-strapi-suite/modules/pages?slug=<SLUG>
203
203
  Using the slug module settings (as shown in the screenshot above), you can choose which content will be set as the homepage. Then, just call the API to get the data:
204
204
  ```
205
205
  GET /api/littlebox-strapi-suite/modules/pages/home?properties=attributes
206
+ GET /api/littlebox-strapi-suite/modules/pages/home?properties=attributes,content
206
207
  ```
207
208
  The return will be the content in the default language and the respective localizations:
208
209
  ```
@@ -1598,6 +1598,7 @@ async function getSlugByDocumentId(params) {
1598
1598
  state: "published"
1599
1599
  }
1600
1600
  });
1601
+ if (!document) return null;
1601
1602
  if (document.parentContentId) {
1602
1603
  const parentSlug = await getSlugByDocumentId({
1603
1604
  contentId: document.parentContentId,
@@ -1731,26 +1732,20 @@ async function buildBreadcrumbs(params) {
1731
1732
  }, []);
1732
1733
  return mappedResult;
1733
1734
  }
1734
- function handleAttributes(attributes2, level) {
1735
+ function handleAttributes(attributes2) {
1735
1736
  const query = {};
1736
1737
  Object.keys(attributes2).forEach((key) => {
1737
1738
  switch (attributes2[key]["type"]) {
1738
1739
  case "component":
1739
1740
  const componentData = strapi.components[attributes2[key]["component"]];
1740
- query[key] = { populate: handleAttributes(componentData.attributes, level) };
1741
+ query[key] = { populate: handleAttributes(componentData.attributes) };
1741
1742
  break;
1742
1743
  case "media":
1743
1744
  query[key] = { populate: "*" };
1744
1745
  break;
1745
1746
  case "relation":
1746
1747
  if (key === "roles" || key === "users" || key === "createdBy" || key === "updatedBy") break;
1747
- query[key] = key === "localizations" ? { populate: "*" } : level === 3 ? { populate: "*" } : {
1748
- populate: populateQueryFromContentType(
1749
- strapi,
1750
- attributes2[key]["target"],
1751
- level + 1
1752
- )
1753
- };
1748
+ query[key] = { populate: "*" };
1754
1749
  break;
1755
1750
  case "dynamiczone":
1756
1751
  const components = attributes2[key]["components"];
@@ -1758,7 +1753,7 @@ function handleAttributes(attributes2, level) {
1758
1753
  components.forEach((component) => {
1759
1754
  const componentData2 = strapi.components[component];
1760
1755
  query[key]["on"][component] = {
1761
- populate: handleAttributes(componentData2.attributes, level)
1756
+ populate: handleAttributes(componentData2.attributes)
1762
1757
  };
1763
1758
  });
1764
1759
  break;
@@ -1766,11 +1761,46 @@ function handleAttributes(attributes2, level) {
1766
1761
  });
1767
1762
  return Object.keys(query).length === 0 ? "*" : query;
1768
1763
  }
1769
- function populateQueryFromContentType(strapi2, contentType, level = 1) {
1764
+ function populateQueryFromContentType(strapi2, contentType) {
1770
1765
  const attributes2 = strapi2.contentTypes[contentType]["attributes"];
1771
- const query = handleAttributes(attributes2, level);
1766
+ const query = handleAttributes(attributes2);
1772
1767
  return query;
1773
1768
  }
1769
+ async function findDocumentIdsAndFetchSlugs(params) {
1770
+ if (!params.document || typeof params.document !== "object") return;
1771
+ const promises = [];
1772
+ async function traverse(current) {
1773
+ if (!current || typeof current !== "object") return;
1774
+ if (Array.isArray(current)) {
1775
+ for (let i = 0; i < current.length; i++) {
1776
+ await traverse(current[i]);
1777
+ }
1778
+ return;
1779
+ }
1780
+ if ("documentId" in current) {
1781
+ promises.push(
1782
+ (async () => {
1783
+ const slug = await getSlugByDocumentId({
1784
+ contentId: current.documentId,
1785
+ locale: current.locale,
1786
+ defaultLocale: params.defaultLocale,
1787
+ showDefaultLanguage: params.showDefaultLanguage,
1788
+ strapi
1789
+ });
1790
+ current.slug = slug;
1791
+ })()
1792
+ );
1793
+ }
1794
+ for (const key in current) {
1795
+ if (Object.prototype.hasOwnProperty.call(current, key) && current[key] !== null) {
1796
+ await traverse(current[key]);
1797
+ }
1798
+ }
1799
+ }
1800
+ await traverse(params.document);
1801
+ await Promise.all(promises);
1802
+ return params.document;
1803
+ }
1774
1804
  const SlugModuleService = ({ strapi: strapi2 }) => ({
1775
1805
  async adminGetAll() {
1776
1806
  const ctx = strapi2.requestContext.get();
@@ -1898,6 +1928,11 @@ const SlugModuleService = ({ strapi: strapi2 }) => ({
1898
1928
  status: "published",
1899
1929
  documentId: page.contentId
1900
1930
  });
1931
+ await findDocumentIdsAndFetchSlugs({
1932
+ document,
1933
+ defaultLocale,
1934
+ showDefaultLanguage
1935
+ });
1901
1936
  for (const localization of document.localizations) {
1902
1937
  if (homepageSlugStrategy === SLUG_LANGUAGE_STRATEGY && localization.documentId === homepageContentId) {
1903
1938
  localization.slug = localization.locale === defaultLocale ? "" : localization.locale.toLowerCase();
@@ -2083,13 +2118,18 @@ const SlugModuleService = ({ strapi: strapi2 }) => ({
2083
2118
  ...attributes2?.parentContentId ? { parent: { id: attributes2.parentContentId, model: attributes2.parentContentModel } } : {}
2084
2119
  };
2085
2120
  }
2086
- const query = populateQueryFromContentType(strapi2, page.contentModel);
2121
+ const query = properties && properties.includes("content") ? populateQueryFromContentType(strapi2, page.contentModel) : { localizations: { populate: "*" } };
2087
2122
  const document = await strapi2.documents(page.contentModel).findOne({
2088
2123
  locale: page.locale,
2089
2124
  populate: query,
2090
2125
  status: "published",
2091
2126
  documentId: page.contentId
2092
2127
  });
2128
+ await findDocumentIdsAndFetchSlugs({
2129
+ document,
2130
+ defaultLocale,
2131
+ showDefaultLanguage
2132
+ });
2093
2133
  for (const localization of document.localizations) {
2094
2134
  if (homepageSlugStrategy === SLUG_LANGUAGE_STRATEGY) {
2095
2135
  localization.slug = localization.locale === defaultLocale ? "" : localization.locale.toLowerCase();
@@ -1597,6 +1597,7 @@ async function getSlugByDocumentId(params) {
1597
1597
  state: "published"
1598
1598
  }
1599
1599
  });
1600
+ if (!document) return null;
1600
1601
  if (document.parentContentId) {
1601
1602
  const parentSlug = await getSlugByDocumentId({
1602
1603
  contentId: document.parentContentId,
@@ -1730,26 +1731,20 @@ async function buildBreadcrumbs(params) {
1730
1731
  }, []);
1731
1732
  return mappedResult;
1732
1733
  }
1733
- function handleAttributes(attributes2, level) {
1734
+ function handleAttributes(attributes2) {
1734
1735
  const query = {};
1735
1736
  Object.keys(attributes2).forEach((key) => {
1736
1737
  switch (attributes2[key]["type"]) {
1737
1738
  case "component":
1738
1739
  const componentData = strapi.components[attributes2[key]["component"]];
1739
- query[key] = { populate: handleAttributes(componentData.attributes, level) };
1740
+ query[key] = { populate: handleAttributes(componentData.attributes) };
1740
1741
  break;
1741
1742
  case "media":
1742
1743
  query[key] = { populate: "*" };
1743
1744
  break;
1744
1745
  case "relation":
1745
1746
  if (key === "roles" || key === "users" || key === "createdBy" || key === "updatedBy") break;
1746
- query[key] = key === "localizations" ? { populate: "*" } : level === 3 ? { populate: "*" } : {
1747
- populate: populateQueryFromContentType(
1748
- strapi,
1749
- attributes2[key]["target"],
1750
- level + 1
1751
- )
1752
- };
1747
+ query[key] = { populate: "*" };
1753
1748
  break;
1754
1749
  case "dynamiczone":
1755
1750
  const components = attributes2[key]["components"];
@@ -1757,7 +1752,7 @@ function handleAttributes(attributes2, level) {
1757
1752
  components.forEach((component) => {
1758
1753
  const componentData2 = strapi.components[component];
1759
1754
  query[key]["on"][component] = {
1760
- populate: handleAttributes(componentData2.attributes, level)
1755
+ populate: handleAttributes(componentData2.attributes)
1761
1756
  };
1762
1757
  });
1763
1758
  break;
@@ -1765,11 +1760,46 @@ function handleAttributes(attributes2, level) {
1765
1760
  });
1766
1761
  return Object.keys(query).length === 0 ? "*" : query;
1767
1762
  }
1768
- function populateQueryFromContentType(strapi2, contentType, level = 1) {
1763
+ function populateQueryFromContentType(strapi2, contentType) {
1769
1764
  const attributes2 = strapi2.contentTypes[contentType]["attributes"];
1770
- const query = handleAttributes(attributes2, level);
1765
+ const query = handleAttributes(attributes2);
1771
1766
  return query;
1772
1767
  }
1768
+ async function findDocumentIdsAndFetchSlugs(params) {
1769
+ if (!params.document || typeof params.document !== "object") return;
1770
+ const promises = [];
1771
+ async function traverse(current) {
1772
+ if (!current || typeof current !== "object") return;
1773
+ if (Array.isArray(current)) {
1774
+ for (let i = 0; i < current.length; i++) {
1775
+ await traverse(current[i]);
1776
+ }
1777
+ return;
1778
+ }
1779
+ if ("documentId" in current) {
1780
+ promises.push(
1781
+ (async () => {
1782
+ const slug = await getSlugByDocumentId({
1783
+ contentId: current.documentId,
1784
+ locale: current.locale,
1785
+ defaultLocale: params.defaultLocale,
1786
+ showDefaultLanguage: params.showDefaultLanguage,
1787
+ strapi
1788
+ });
1789
+ current.slug = slug;
1790
+ })()
1791
+ );
1792
+ }
1793
+ for (const key in current) {
1794
+ if (Object.prototype.hasOwnProperty.call(current, key) && current[key] !== null) {
1795
+ await traverse(current[key]);
1796
+ }
1797
+ }
1798
+ }
1799
+ await traverse(params.document);
1800
+ await Promise.all(promises);
1801
+ return params.document;
1802
+ }
1773
1803
  const SlugModuleService = ({ strapi: strapi2 }) => ({
1774
1804
  async adminGetAll() {
1775
1805
  const ctx = strapi2.requestContext.get();
@@ -1897,6 +1927,11 @@ const SlugModuleService = ({ strapi: strapi2 }) => ({
1897
1927
  status: "published",
1898
1928
  documentId: page.contentId
1899
1929
  });
1930
+ await findDocumentIdsAndFetchSlugs({
1931
+ document,
1932
+ defaultLocale,
1933
+ showDefaultLanguage
1934
+ });
1900
1935
  for (const localization of document.localizations) {
1901
1936
  if (homepageSlugStrategy === SLUG_LANGUAGE_STRATEGY && localization.documentId === homepageContentId) {
1902
1937
  localization.slug = localization.locale === defaultLocale ? "" : localization.locale.toLowerCase();
@@ -2082,13 +2117,18 @@ const SlugModuleService = ({ strapi: strapi2 }) => ({
2082
2117
  ...attributes2?.parentContentId ? { parent: { id: attributes2.parentContentId, model: attributes2.parentContentModel } } : {}
2083
2118
  };
2084
2119
  }
2085
- const query = populateQueryFromContentType(strapi2, page.contentModel);
2120
+ const query = properties && properties.includes("content") ? populateQueryFromContentType(strapi2, page.contentModel) : { localizations: { populate: "*" } };
2086
2121
  const document = await strapi2.documents(page.contentModel).findOne({
2087
2122
  locale: page.locale,
2088
2123
  populate: query,
2089
2124
  status: "published",
2090
2125
  documentId: page.contentId
2091
2126
  });
2127
+ await findDocumentIdsAndFetchSlugs({
2128
+ document,
2129
+ defaultLocale,
2130
+ showDefaultLanguage
2131
+ });
2092
2132
  for (const localization of document.localizations) {
2093
2133
  if (homepageSlugStrategy === SLUG_LANGUAGE_STRATEGY) {
2094
2134
  localization.slug = localization.locale === defaultLocale ? "" : localization.locale.toLowerCase();
@@ -0,0 +1,7 @@
1
+ import type { Core } from '@strapi/strapi';
2
+ export declare function findDocumentIdsAndFetchSlugs(params: {
3
+ document: any;
4
+ defaultLocale: string;
5
+ showDefaultLanguage: boolean;
6
+ strapi: Core.Strapi;
7
+ }): Promise<any>;
@@ -1,2 +1,2 @@
1
1
  import type { Core } from '@strapi/strapi';
2
- export declare function populateQueryFromContentType(strapi: Core.Strapi, contentType: string, level?: number): any;
2
+ export declare function populateQueryFromContentType(strapi: Core.Strapi, contentType: string): any;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.42",
2
+ "version": "1.0.43",
3
3
  "keywords": [
4
4
  "strapi",
5
5
  "strapi plugin",