@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.
- package/dist/components/ContextProvider/ContextProvider.d.ts +1 -2
- package/dist/constants/strapi.d.ts +1 -0
- package/dist/index.d.ts +6 -3
- package/dist/integrations/strapi/getAllSlugsFromStrapi.d.ts +10 -0
- package/dist/integrations/strapi/getStaticPathsFromStrapi.d.ts +1 -0
- package/dist/integrations/strapi/getStaticPropsFromStrapi.d.ts +1 -0
- package/dist/integrations/strapi/getStrapiCollectionType.d.ts +10 -0
- package/dist/integrations/strapi/getStrapiSingleType.d.ts +8 -0
- package/dist/models/Locale.d.ts +3 -1
- package/dist/models/LocalizedEntity.d.ts +7 -0
- package/dist/models/PageProps.d.ts +1 -0
- package/dist/models/strapi/StrapiGlobal.d.ts +2 -0
- package/dist/strapi-slices.cjs.development.js +205 -31
- package/dist/strapi-slices.cjs.development.js.map +1 -1
- package/dist/strapi-slices.cjs.production.min.js +1 -1
- package/dist/strapi-slices.cjs.production.min.js.map +1 -1
- package/dist/strapi-slices.esm.js +205 -34
- package/dist/strapi-slices.esm.js.map +1 -1
- package/dist/utils/getMessages.d.ts +142 -0
- package/package.json +1 -1
- package/src/components/ContextProvider/ContextProvider.tsx +3 -5
- package/src/components/MinimalProviders/MinimalProviders.tsx +2 -8
- package/src/constants/strapi.ts +2 -0
- package/src/index.tsx +8 -2
- package/src/integrations/strapi/getAllSlugsFromStrapi.test.ts +33 -0
- package/src/integrations/strapi/getAllSlugsFromStrapi.ts +50 -0
- package/src/integrations/strapi/getPortfolioProjects.test.ts +24 -0
- package/src/integrations/strapi/getPortfolioProjects.ts +25 -5
- package/src/integrations/strapi/getStaticPathsFromStrapi.ts +1 -0
- package/src/integrations/strapi/getStaticPropsFromStrapi.ts +1 -0
- package/src/integrations/strapi/getStrapiCollectionType.test.ts +65 -0
- package/src/integrations/strapi/getStrapiCollectionType.ts +61 -0
- package/src/integrations/strapi/getStrapiSingleType.test.ts +53 -0
- package/src/integrations/strapi/getStrapiSingleType.ts +50 -0
- package/src/models/Locale.ts +3 -1
- package/src/models/LocalizedEntity.ts +9 -0
- package/src/models/PageProps.ts +1 -0
- package/src/models/strapi/StrapiGlobal.ts +2 -0
- package/src/test/strapiMocks/minimalGlobalData.ts +1 -0
- package/src/utils/getMessages.ts +18 -0
- package/src/utils/mergeGlobalAndStrapiBlogPostData.test.ts +25 -4
- package/src/utils/mergeGlobalAndStrapiBlogPostData.ts +1 -0
- package/src/utils/mergeGlobalAndStrapiCustomerStoryData.test.ts +24 -4
- package/src/utils/mergeGlobalAndStrapiCustomerStoryData.ts +1 -0
- package/src/utils/mergeGlobalAndStrapiPageData.test.ts +22 -0
- package/src/utils/mergeGlobalAndStrapiPageData.ts +1 -0
- package/src/utils/mergeGlobalAndStrapiProjectData.test.ts +25 -4
- 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;
|
package/src/models/Locale.ts
CHANGED
package/src/models/PageProps.ts
CHANGED
|
@@ -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;
|
|
@@ -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
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
});
|
|
@@ -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
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
});
|
|
@@ -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
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
});
|