@treely/strapi-slices 5.11.3 → 5.13.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 (48) 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/PageProps.d.ts +1 -0
  12. package/dist/models/strapi/StrapiGlobal.d.ts +2 -0
  13. package/dist/strapi-slices.cjs.development.js +205 -31
  14. package/dist/strapi-slices.cjs.development.js.map +1 -1
  15. package/dist/strapi-slices.cjs.production.min.js +1 -1
  16. package/dist/strapi-slices.cjs.production.min.js.map +1 -1
  17. package/dist/strapi-slices.esm.js +205 -34
  18. package/dist/strapi-slices.esm.js.map +1 -1
  19. package/dist/utils/getMessages.d.ts +142 -0
  20. package/package.json +1 -1
  21. package/src/components/ContextProvider/ContextProvider.tsx +3 -5
  22. package/src/components/MinimalProviders/MinimalProviders.tsx +2 -8
  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/PageProps.ts +1 -0
  38. package/src/models/strapi/StrapiGlobal.ts +2 -0
  39. package/src/test/strapiMocks/minimalGlobalData.ts +1 -0
  40. package/src/utils/getMessages.ts +18 -0
  41. package/src/utils/mergeGlobalAndStrapiBlogPostData.test.ts +25 -4
  42. package/src/utils/mergeGlobalAndStrapiBlogPostData.ts +1 -0
  43. package/src/utils/mergeGlobalAndStrapiCustomerStoryData.test.ts +24 -4
  44. package/src/utils/mergeGlobalAndStrapiCustomerStoryData.ts +1 -0
  45. package/src/utils/mergeGlobalAndStrapiPageData.test.ts +22 -0
  46. package/src/utils/mergeGlobalAndStrapiPageData.ts +1 -0
  47. package/src/utils/mergeGlobalAndStrapiProjectData.test.ts +25 -4
  48. package/src/utils/mergeGlobalAndStrapiProjectData.ts +1 -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;
@@ -28,6 +28,7 @@ interface PageProps {
28
28
  topBanner?: StrapiTopBanner;
29
29
  customerStories: IStrapiData<StrapiCustomerStory>[];
30
30
  preview: boolean;
31
+ isFallbackLocale: boolean;
31
32
  }
32
33
 
33
34
  export default PageProps;
@@ -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;
@@ -7,7 +7,7 @@ import portfolioProjectMock from '../test/integrationMocks/portfolioProjectMock'
7
7
 
8
8
  describe('The mergeGlobalAndStrapiBlogPostData util', () => {
9
9
  it('returns the global metadata if there is no page metadata', () => {
10
- const pageDataWithoutMetadata = {
10
+ const blogPostDataWithoutMetadata = {
11
11
  ...strapiBlogPostMock,
12
12
  attributes: {
13
13
  ...strapiBlogPostMock.attributes,
@@ -18,7 +18,7 @@ describe('The mergeGlobalAndStrapiBlogPostData util', () => {
18
18
  const result = mergeGlobalAndStrapiBlogPostData(
19
19
  getStaticPropsContextMock,
20
20
  minimalGlobalData,
21
- pageDataWithoutMetadata,
21
+ blogPostDataWithoutMetadata,
22
22
  [],
23
23
  []
24
24
  );
@@ -32,10 +32,11 @@ describe('The mergeGlobalAndStrapiBlogPostData util', () => {
32
32
  expect(result.metadata.description).toBe(
33
33
  minimalGlobalData.attributes.metadata.description
34
34
  );
35
+ expect(result.isFallbackLocale).toBeFalsy();
35
36
  });
36
37
 
37
38
  it('returns the pages metadata if the page data includes metadata', () => {
38
- const pageDataWithMetadata = {
39
+ const blogPostDataWithMetadata = {
39
40
  ...strapiBlogPostMock,
40
41
  data: {
41
42
  ...strapiBlogPostMock,
@@ -49,7 +50,7 @@ describe('The mergeGlobalAndStrapiBlogPostData util', () => {
49
50
  const result = mergeGlobalAndStrapiBlogPostData(
50
51
  getStaticPropsContextMock,
51
52
  minimalGlobalData,
52
- pageDataWithMetadata,
53
+ blogPostDataWithMetadata,
53
54
  [],
54
55
  []
55
56
  );
@@ -198,4 +199,24 @@ describe('The mergeGlobalAndStrapiBlogPostData util', () => {
198
199
  expect(result.blogPosts).toStrictEqual([]);
199
200
  expect(result.projects).toStrictEqual([portfolioProjectMock]);
200
201
  });
202
+
203
+ it('returns isFallbackLocale=true if the blog post is in a different language', () => {
204
+ const blogPostDataInDe = {
205
+ ...strapiBlogPostMock,
206
+ attributes: {
207
+ ...strapiBlogPostMock.attributes,
208
+ locale: 'de',
209
+ },
210
+ };
211
+
212
+ const result = mergeGlobalAndStrapiBlogPostData(
213
+ getStaticPropsContextMock,
214
+ minimalGlobalData,
215
+ blogPostDataInDe,
216
+ [],
217
+ []
218
+ );
219
+
220
+ expect(result.isFallbackLocale).toBeTruthy();
221
+ });
201
222
  });
@@ -74,6 +74,7 @@ const mergeGlobalAndStrapiBlogPostData = (
74
74
  topBanner: post?.attributes.topBanner || global.attributes.topBanner,
75
75
  customerStories: [],
76
76
  preview: !!context.preview,
77
+ isFallbackLocale: context.locale !== post.attributes.locale,
77
78
  };
78
79
  };
79
80
 
@@ -6,7 +6,7 @@ import { strapiMetadataMock } from '../test/strapiMocks/strapiMetadata';
6
6
 
7
7
  describe('The mergeGlobalAndStrapiCustomerStoryData util', () => {
8
8
  it('returns the global metadata if there is no page metadata', () => {
9
- const pageDataWithoutMetadata = {
9
+ const customerStoryDataWithoutMetadata = {
10
10
  ...strapiCustomerStoryMock,
11
11
  attributes: {
12
12
  ...strapiCustomerStoryMock.attributes,
@@ -17,7 +17,7 @@ describe('The mergeGlobalAndStrapiCustomerStoryData util', () => {
17
17
  const result = mergeGlobalAndStrapiCustomerStoryData(
18
18
  getStaticPropsContextMock,
19
19
  minimalGlobalData,
20
- pageDataWithoutMetadata,
20
+ customerStoryDataWithoutMetadata,
21
21
  []
22
22
  );
23
23
 
@@ -30,10 +30,11 @@ describe('The mergeGlobalAndStrapiCustomerStoryData util', () => {
30
30
  expect(result.metadata.description).toBe(
31
31
  minimalGlobalData.attributes.metadata.description
32
32
  );
33
+ expect(result.isFallbackLocale).toBeFalsy();
33
34
  });
34
35
 
35
36
  it('returns the pages metadata if the page data includes metadata', () => {
36
- const pageDataWithMetadata = {
37
+ const customerStoryDataWithMetadata = {
37
38
  ...strapiCustomerStoryMock,
38
39
  data: {
39
40
  ...strapiCustomerStoryMock,
@@ -47,7 +48,7 @@ describe('The mergeGlobalAndStrapiCustomerStoryData util', () => {
47
48
  const result = mergeGlobalAndStrapiCustomerStoryData(
48
49
  getStaticPropsContextMock,
49
50
  minimalGlobalData,
50
- pageDataWithMetadata,
51
+ customerStoryDataWithMetadata,
51
52
  []
52
53
  );
53
54
 
@@ -167,4 +168,23 @@ describe('The mergeGlobalAndStrapiCustomerStoryData util', () => {
167
168
 
168
169
  expect(result.customerStories).toStrictEqual([strapiCustomerStoryMock]);
169
170
  });
171
+
172
+ it('returns isFallbackLocale=true if the customer story is in a different language', () => {
173
+ const customerStoryDataInDe = {
174
+ ...strapiCustomerStoryMock,
175
+ attributes: {
176
+ ...strapiCustomerStoryMock.attributes,
177
+ locale: 'de',
178
+ },
179
+ };
180
+
181
+ const result = mergeGlobalAndStrapiCustomerStoryData(
182
+ getStaticPropsContextMock,
183
+ minimalGlobalData,
184
+ customerStoryDataInDe,
185
+ []
186
+ );
187
+
188
+ expect(result.isFallbackLocale).toBeTruthy();
189
+ });
170
190
  });
@@ -68,6 +68,7 @@ const mergeGlobalAndStrapiCustomerStoryData = (
68
68
  customerStory?.attributes.topBanner || global.attributes.topBanner,
69
69
  blogPosts: [],
70
70
  preview: !!context.preview,
71
+ isFallbackLocale: context.locale !== customerStory.attributes.locale,
71
72
  };
72
73
  };
73
74
 
@@ -35,6 +35,7 @@ describe('The mergeGlobalAndStrapiPageData util', () => {
35
35
  expect(result.metadata.description).toBe(
36
36
  minimalGlobalData.attributes.metadata.description
37
37
  );
38
+ expect(result.isFallbackLocale).toBeFalsy();
38
39
  });
39
40
 
40
41
  it('returns the pages metadata if the page data includes metadata', () => {
@@ -317,4 +318,25 @@ describe('The mergeGlobalAndStrapiPageData util', () => {
317
318
  expect(result.blogPosts).toStrictEqual([]);
318
319
  expect(result.customerStories).toStrictEqual([]);
319
320
  });
321
+
322
+ it('returns isFallbackLocale=true if the page is in a different language', () => {
323
+ const pageDataInDe = {
324
+ ...strapiPageMock,
325
+ attributes: {
326
+ ...strapiPageMock.attributes,
327
+ locale: 'de',
328
+ },
329
+ };
330
+
331
+ const result = mergeGlobalAndStrapiPageData(
332
+ getStaticPropsContextMock,
333
+ minimalGlobalData,
334
+ pageDataInDe,
335
+ [],
336
+ [],
337
+ []
338
+ );
339
+
340
+ expect(result.isFallbackLocale).toBeTruthy();
341
+ });
320
342
  });
@@ -89,6 +89,7 @@ const mergeGlobalAndStrapiPageData = (
89
89
  topBanner: page?.attributes.topBanner || global.attributes.topBanner,
90
90
  customerStories: returnCustomerStories ? customerStories : [],
91
91
  preview: !!context.preview,
92
+ isFallbackLocale: context.locale !== page.attributes.locale,
92
93
  };
93
94
  };
94
95
 
@@ -8,7 +8,7 @@ import mergeGlobalAndStrapiProjectData from './mergeGlobalAndStrapiProjectData';
8
8
 
9
9
  describe('The mergeGlobalAndStrapiProjectData util', () => {
10
10
  it('returns the global metadata if there is no page metadata', () => {
11
- const pageDataWithoutMetadata = {
11
+ const projectDataWithoutMetadata = {
12
12
  ...strapiProjectMock,
13
13
  attributes: {
14
14
  ...strapiProjectMock.attributes,
@@ -19,7 +19,7 @@ describe('The mergeGlobalAndStrapiProjectData util', () => {
19
19
  const result = mergeGlobalAndStrapiProjectData(
20
20
  getStaticPropsContextMock,
21
21
  minimalGlobalData,
22
- pageDataWithoutMetadata,
22
+ projectDataWithoutMetadata,
23
23
  [],
24
24
  []
25
25
  );
@@ -33,10 +33,11 @@ describe('The mergeGlobalAndStrapiProjectData util', () => {
33
33
  expect(result.metadata.description).toBe(
34
34
  minimalGlobalData.attributes.metadata.description
35
35
  );
36
+ expect(result.isFallbackLocale).toBeFalsy();
36
37
  });
37
38
 
38
39
  it('returns the pages metadata if the page data includes metadata', () => {
39
- const pageDataWithMetadata = {
40
+ const projectDataWithMetadata = {
40
41
  ...strapiProjectMock,
41
42
  attributes: {
42
43
  ...strapiProjectMock.attributes,
@@ -47,7 +48,7 @@ describe('The mergeGlobalAndStrapiProjectData util', () => {
47
48
  const result = mergeGlobalAndStrapiProjectData(
48
49
  getStaticPropsContextMock,
49
50
  minimalGlobalData,
50
- pageDataWithMetadata,
51
+ projectDataWithMetadata,
51
52
  [],
52
53
  []
53
54
  );
@@ -280,4 +281,24 @@ describe('The mergeGlobalAndStrapiProjectData util', () => {
280
281
  expect(result.blogPosts).toStrictEqual([]);
281
282
  expect(result.projects).toStrictEqual([portfolioProjectMock]);
282
283
  });
284
+
285
+ it('returns isFallbackLocale=true if the project is in a different language', () => {
286
+ const projectDataInDe = {
287
+ ...strapiProjectMock,
288
+ attributes: {
289
+ ...strapiProjectMock.attributes,
290
+ locale: 'de',
291
+ },
292
+ };
293
+
294
+ const result = mergeGlobalAndStrapiProjectData(
295
+ getStaticPropsContextMock,
296
+ minimalGlobalData,
297
+ projectDataInDe,
298
+ [],
299
+ []
300
+ );
301
+
302
+ expect(result.isFallbackLocale).toBeTruthy();
303
+ });
283
304
  });
@@ -83,6 +83,7 @@ const mergeGlobalAndStrapiProject = (
83
83
  topBanner: project?.attributes.topBanner || global.attributes.topBanner,
84
84
  customerStories: [],
85
85
  preview: !!context.preview,
86
+ isFallbackLocale: context.locale !== project.attributes.locale,
86
87
  };
87
88
  };
88
89