@treely/strapi-slices 5.11.3 → 5.12.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.
Files changed (38) hide show
  1. package/dist/components/ContextProvider/ContextProvider.d.ts +1 -2
  2. package/dist/constants/strapi.d.ts +1 -0
  3. package/dist/index.d.ts +6 -3
  4. package/dist/integrations/strapi/getAllSlugsFromStrapi.d.ts +10 -0
  5. package/dist/integrations/strapi/getStaticPathsFromStrapi.d.ts +1 -0
  6. package/dist/integrations/strapi/getStaticPropsFromStrapi.d.ts +1 -0
  7. package/dist/integrations/strapi/getStrapiCollectionType.d.ts +10 -0
  8. package/dist/integrations/strapi/getStrapiSingleType.d.ts +8 -0
  9. package/dist/models/Locale.d.ts +3 -1
  10. package/dist/models/LocalizedEntity.d.ts +7 -0
  11. package/dist/models/strapi/StrapiGlobal.d.ts +2 -0
  12. package/dist/strapi-slices.cjs.development.js +197 -27
  13. package/dist/strapi-slices.cjs.development.js.map +1 -1
  14. package/dist/strapi-slices.cjs.production.min.js +1 -1
  15. package/dist/strapi-slices.cjs.production.min.js.map +1 -1
  16. package/dist/strapi-slices.esm.js +197 -30
  17. package/dist/strapi-slices.esm.js.map +1 -1
  18. package/dist/utils/getMessages.d.ts +142 -0
  19. package/package.json +1 -1
  20. package/src/components/ContextProvider/ContextProvider.tsx +3 -5
  21. package/src/components/MinimalProviders/MinimalProviders.tsx +2 -8
  22. package/src/constants/strapi.ts +2 -0
  23. package/src/index.tsx +8 -2
  24. package/src/integrations/strapi/getAllSlugsFromStrapi.test.ts +33 -0
  25. package/src/integrations/strapi/getAllSlugsFromStrapi.ts +50 -0
  26. package/src/integrations/strapi/getPortfolioProjects.test.ts +24 -0
  27. package/src/integrations/strapi/getPortfolioProjects.ts +25 -5
  28. package/src/integrations/strapi/getStaticPathsFromStrapi.ts +1 -0
  29. package/src/integrations/strapi/getStaticPropsFromStrapi.ts +1 -0
  30. package/src/integrations/strapi/getStrapiCollectionType.test.ts +65 -0
  31. package/src/integrations/strapi/getStrapiCollectionType.ts +61 -0
  32. package/src/integrations/strapi/getStrapiSingleType.test.ts +53 -0
  33. package/src/integrations/strapi/getStrapiSingleType.ts +50 -0
  34. package/src/models/Locale.ts +3 -1
  35. package/src/models/LocalizedEntity.ts +9 -0
  36. package/src/models/strapi/StrapiGlobal.ts +2 -0
  37. package/src/test/strapiMocks/minimalGlobalData.ts +1 -0
  38. package/src/utils/getMessages.ts +18 -0
@@ -0,0 +1,50 @@
1
+ import { AxiosResponse } from 'axios';
2
+ import strapiClient from './strapiClient';
3
+ import {
4
+ STRAPI_DEFAULT_PAGE_SIZE,
5
+ STRAPI_FALLBACK_LOCALE,
6
+ } from '../../constants/strapi';
7
+ import IStrapiData from '../../models/strapi/IStrapiData';
8
+ import IStrapiResponse from '../../models/strapi/IStrapiResponse';
9
+
10
+ interface Options {
11
+ locale?: string;
12
+ preview?: boolean;
13
+ filters?: Record<string, any>;
14
+ }
15
+
16
+ const getStrapiSingleType = async <T>(
17
+ path: string,
18
+ { locale = 'en', preview = false, filters = {} }: Options
19
+ ): Promise<IStrapiData<T>> => {
20
+ const params: Record<string, any> = {
21
+ populate: 'deep,6',
22
+ locale,
23
+ 'pagination[pageSize]': STRAPI_DEFAULT_PAGE_SIZE,
24
+ filters,
25
+ };
26
+
27
+ if (preview) {
28
+ params.publicationState = 'preview';
29
+ }
30
+
31
+ let response: AxiosResponse<IStrapiResponse<IStrapiData<T>>>;
32
+
33
+ try {
34
+ response = await strapiClient.get(path, { params });
35
+ return response.data.data;
36
+ } catch (error: any) {
37
+ if (error.isAxiosError && error.response?.status === 404) {
38
+ // Retry request with fallback locale
39
+ response = await strapiClient.get(path, {
40
+ params: { ...params, locale: STRAPI_FALLBACK_LOCALE },
41
+ });
42
+
43
+ return response.data.data;
44
+ }
45
+
46
+ throw error;
47
+ }
48
+ };
49
+
50
+ export default getStrapiSingleType;
@@ -1,3 +1,5 @@
1
- type Locale = 'en' | 'de';
1
+ /** @deprecated Removing this type because this project now falls back to
2
+ * English if a locale is not supported. */
3
+ type Locale = string;
2
4
 
3
5
  export default Locale;
@@ -0,0 +1,9 @@
1
+ import Locale from './Locale';
2
+
3
+ type LocalizedEntity<Key extends string = any> = {
4
+ [key in Key]: any;
5
+ } & {
6
+ locale: Locale;
7
+ };
8
+
9
+ export default LocalizedEntity;
@@ -1,3 +1,4 @@
1
+ import Locale from '../Locale';
1
2
  import IStrapi from './IStrapi';
2
3
  import IStrapiData from './IStrapiData';
3
4
  import StrapiBanner from './StrapiBanner';
@@ -9,6 +10,7 @@ import StrapiNavMenu from './StrapiNavMenu';
9
10
  import StrapiTopBanner from './StrapiTopBanner';
10
11
 
11
12
  interface StrapiGlobal {
13
+ locale: Locale;
12
14
  metadata: StrapiMetadata;
13
15
  favicon: IStrapi<IStrapiData<StrapiMedia>>;
14
16
  metaTitleSuffix: string;
@@ -6,6 +6,7 @@ import { strapiMetadataMock } from './strapiMetadata';
6
6
  const minimalGlobalData: IStrapiData<StrapiGlobal> = {
7
7
  id: 1,
8
8
  attributes: {
9
+ locale: 'en',
9
10
  metadata: strapiMetadataMock,
10
11
  favicon: { data: strapiMediaMock },
11
12
  metaTitleSuffix: 'Meta title suffix',
@@ -0,0 +1,18 @@
1
+ import { STRAPI_FALLBACK_LOCALE } from '../constants/strapi';
2
+ import rootMessagesDe from '../rootMessages.de';
3
+ import rootMessagesEn from '../rootMessages.en';
4
+
5
+ const messages = {
6
+ en: rootMessagesEn,
7
+ de: rootMessagesDe,
8
+ };
9
+
10
+ const getMessages = (locale: string) => {
11
+ const messagesLocale = Object.keys(messages).includes(`${locale}`)
12
+ ? (locale as keyof typeof messages)
13
+ : STRAPI_FALLBACK_LOCALE;
14
+
15
+ return messages[messagesLocale];
16
+ };
17
+
18
+ export default getMessages;