@treely/strapi-slices 5.11.2 → 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 (39) 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 +207 -35
  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 +207 -38
  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/components/portfolio/SmallCheckout/SmallCheckout.tsx +33 -24
  23. package/src/constants/strapi.ts +2 -0
  24. package/src/index.tsx +8 -2
  25. package/src/integrations/strapi/getAllSlugsFromStrapi.test.ts +33 -0
  26. package/src/integrations/strapi/getAllSlugsFromStrapi.ts +50 -0
  27. package/src/integrations/strapi/getPortfolioProjects.test.ts +24 -0
  28. package/src/integrations/strapi/getPortfolioProjects.ts +25 -5
  29. package/src/integrations/strapi/getStaticPathsFromStrapi.ts +1 -0
  30. package/src/integrations/strapi/getStaticPropsFromStrapi.ts +1 -0
  31. package/src/integrations/strapi/getStrapiCollectionType.test.ts +65 -0
  32. package/src/integrations/strapi/getStrapiCollectionType.ts +61 -0
  33. package/src/integrations/strapi/getStrapiSingleType.test.ts +53 -0
  34. package/src/integrations/strapi/getStrapiSingleType.ts +50 -0
  35. package/src/models/Locale.ts +3 -1
  36. package/src/models/LocalizedEntity.ts +9 -0
  37. package/src/models/strapi/StrapiGlobal.ts +2 -0
  38. package/src/test/strapiMocks/minimalGlobalData.ts +1 -0
  39. package/src/utils/getMessages.ts +18 -0
@@ -0,0 +1,61 @@
1
+ import strapiClient from './strapiClient';
2
+ import {
3
+ STRAPI_DEFAULT_PAGE_SIZE,
4
+ STRAPI_FALLBACK_LOCALE,
5
+ } from '../../constants/strapi';
6
+ import IStrapiData from '../../models/strapi/IStrapiData';
7
+ import IStrapiResponse from '../../models/strapi/IStrapiResponse';
8
+ import LocalizedEntity from '../../models/LocalizedEntity';
9
+
10
+ interface Options {
11
+ locale?: string;
12
+ slug?: string;
13
+ preview?: boolean;
14
+ filters?: Record<string, any>;
15
+ }
16
+
17
+ const getStrapiCollectionType = async <
18
+ T extends LocalizedEntity<K>,
19
+ K extends string
20
+ >(
21
+ path: string,
22
+ key: K,
23
+ { locale = 'en', preview = false, filters = {} }: Options
24
+ ): Promise<IStrapiData<T>[]> => {
25
+ const params: Record<string, any> = {
26
+ populate: 'deep,6',
27
+ locale: 'all',
28
+ 'pagination[pageSize]': STRAPI_DEFAULT_PAGE_SIZE,
29
+ filters,
30
+ };
31
+
32
+ if (preview) {
33
+ params.publicationState = 'preview';
34
+ }
35
+
36
+ const { data } = await strapiClient.get<IStrapiResponse<IStrapiData<T>[]>>(
37
+ path,
38
+ { params }
39
+ );
40
+
41
+ const localizedResponses = data.data.filter(
42
+ (d) => d.attributes.locale === locale
43
+ );
44
+
45
+ const fallbackResponses = data.data.filter(
46
+ (d) => d.attributes.locale === STRAPI_FALLBACK_LOCALE
47
+ );
48
+
49
+ const responses = fallbackResponses.map((fallbackResponse) => {
50
+ const localizedResponse = localizedResponses.find(
51
+ (localized) =>
52
+ localized.attributes[key] === fallbackResponse.attributes[key]
53
+ );
54
+
55
+ return localizedResponse || fallbackResponse;
56
+ });
57
+
58
+ return responses;
59
+ };
60
+
61
+ export default getStrapiCollectionType;
@@ -0,0 +1,53 @@
1
+ import MockAxios from 'jest-mock-axios';
2
+ import getStrapiSingleType from './getStrapiSingleType';
3
+ import StrapiPage from '../../models/strapi/StrapiPage';
4
+ import minimalGlobalData from '../../test/strapiMocks/minimalGlobalData';
5
+ import { waitFor } from '@testing-library/react';
6
+
7
+ describe('The getStrapiSingleType function', () => {
8
+ afterEach(() => {
9
+ MockAxios.reset();
10
+ });
11
+
12
+ it('returns the localized versions if available', async () => {
13
+ const globalPromise = getStrapiSingleType<StrapiPage>('/api/global', {});
14
+
15
+ MockAxios.mockResponseFor(
16
+ { url: '/api/global' },
17
+ { data: { data: minimalGlobalData } }
18
+ );
19
+
20
+ const global = await globalPromise;
21
+
22
+ expect(global).toStrictEqual(minimalGlobalData);
23
+ });
24
+
25
+ it('returns the localized versions if available', async () => {
26
+ const globalPromise = getStrapiSingleType<StrapiPage>('/api/global', {
27
+ locale: 'de',
28
+ });
29
+
30
+ let firstRequestInfo = MockAxios.lastReqGet();
31
+
32
+ MockAxios.mockError(
33
+ { isAxiosError: true, response: { status: 404 } },
34
+ firstRequestInfo
35
+ );
36
+
37
+ let secondRequestInfo;
38
+
39
+ await waitFor(() => {
40
+ secondRequestInfo = MockAxios.lastReqGet();
41
+ expect(secondRequestInfo).toBeDefined();
42
+ });
43
+
44
+ MockAxios.mockResponse(
45
+ { data: { data: minimalGlobalData } },
46
+ secondRequestInfo
47
+ );
48
+
49
+ const global = await globalPromise;
50
+
51
+ await expect(global).toStrictEqual(minimalGlobalData);
52
+ });
53
+ });
@@ -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;