@treely/strapi-slices 7.5.1 → 7.5.3
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/strapi-slices.cjs.development.js +74 -86
- 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 +74 -86
- package/dist/strapi-slices.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/integrations/strapi/getAllSlugsFromStrapi.test.ts +8 -21
- package/src/integrations/strapi/getAllSlugsFromStrapi.ts +24 -22
- package/src/integrations/strapi/getStrapiCollectionType.test.ts +3 -21
- package/src/integrations/strapi/getStrapiCollectionType.ts +41 -36
- package/dist/integrations/strapi/getAvailableLocalesFromStrapi.d.ts +0 -2
- package/src/integrations/strapi/getAvailableLocalesFromStrapi.test.ts +0 -22
- package/src/integrations/strapi/getAvailableLocalesFromStrapi.ts +0 -8
package/package.json
CHANGED
|
@@ -2,32 +2,13 @@ import MockAxios from 'jest-mock-axios';
|
|
|
2
2
|
import getAllSlugsFromStrapi from './getAllSlugsFromStrapi';
|
|
3
3
|
import StrapiPage from '../../models/strapi/StrapiPage';
|
|
4
4
|
import { strapiPageMock } from '../../test/strapiMocks/strapiPage';
|
|
5
|
-
import getAvailableLocalesFromStrapi from './getAvailableLocalesFromStrapi';
|
|
6
|
-
|
|
7
|
-
jest.mock('./getAvailableLocalesFromStrapi', () => ({
|
|
8
|
-
__esModule: true,
|
|
9
|
-
default: jest.fn(),
|
|
10
|
-
}));
|
|
11
5
|
|
|
12
6
|
describe('The getAllSlugsFromStrapi function', () => {
|
|
13
7
|
afterEach(() => {
|
|
14
8
|
MockAxios.reset();
|
|
15
|
-
jest.clearAllMocks();
|
|
16
9
|
});
|
|
17
10
|
|
|
18
11
|
it('returns all slugs and creates a fallback for locales that dont have a translation', async () => {
|
|
19
|
-
(getAvailableLocalesFromStrapi as jest.Mock).mockResolvedValue([
|
|
20
|
-
'en',
|
|
21
|
-
'de',
|
|
22
|
-
'hu',
|
|
23
|
-
]);
|
|
24
|
-
|
|
25
|
-
const slugsPromise = getAllSlugsFromStrapi<StrapiPage>('/api/pages', [
|
|
26
|
-
'en',
|
|
27
|
-
'de',
|
|
28
|
-
'hu',
|
|
29
|
-
]);
|
|
30
|
-
|
|
31
12
|
// This page is available in 'de', 'en', and 'hu'
|
|
32
13
|
MockAxios.get
|
|
33
14
|
.mockResolvedValueOnce({ data: { data: [strapiPageMock] } }) // english
|
|
@@ -44,9 +25,13 @@ describe('The getAllSlugsFromStrapi function', () => {
|
|
|
44
25
|
],
|
|
45
26
|
},
|
|
46
27
|
})
|
|
47
|
-
.
|
|
28
|
+
.mockRejectedValueOnce({ response: { status: 404 } }); // Hungarian version is missing (404)
|
|
48
29
|
|
|
49
|
-
const slugs = await
|
|
30
|
+
const slugs = await getAllSlugsFromStrapi<StrapiPage>('/api/pages', [
|
|
31
|
+
'en',
|
|
32
|
+
'de',
|
|
33
|
+
'hu',
|
|
34
|
+
]);
|
|
50
35
|
|
|
51
36
|
expect(slugs).toStrictEqual([
|
|
52
37
|
{ locale: 'en', slug: strapiPageMock.attributes.slug },
|
|
@@ -54,5 +39,7 @@ describe('The getAllSlugsFromStrapi function', () => {
|
|
|
54
39
|
// Fallback for 'hu' gets created
|
|
55
40
|
{ locale: 'hu', slug: strapiPageMock.attributes.slug },
|
|
56
41
|
]);
|
|
42
|
+
|
|
43
|
+
expect(MockAxios.get).toHaveBeenCalledTimes(3);
|
|
57
44
|
});
|
|
58
45
|
});
|
|
@@ -6,7 +6,6 @@ import {
|
|
|
6
6
|
import IStrapiResponse from '../../models/strapi/IStrapiResponse';
|
|
7
7
|
import IStrapiData from '../../models/strapi/IStrapiData';
|
|
8
8
|
import LocalizedEntity from '../../models/LocalizedEntity';
|
|
9
|
-
import getAvailableLocalesFromStrapi from './getAvailableLocalesFromStrapi';
|
|
10
9
|
|
|
11
10
|
interface Options {
|
|
12
11
|
filters?: Record<string, any>;
|
|
@@ -19,30 +18,33 @@ const getAllSlugsFromStrapi = async <T extends LocalizedEntity<'slug'>>(
|
|
|
19
18
|
locales: string[],
|
|
20
19
|
{ filters = {} }: Options = { filters: {} }
|
|
21
20
|
): Promise<Slug[]> => {
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
21
|
+
const slugPromises = locales.map((locale) =>
|
|
22
|
+
strapiClient
|
|
23
|
+
.get<IStrapiResponse<IStrapiData<T>[]>>(path, {
|
|
24
|
+
params: {
|
|
25
|
+
locale: locale,
|
|
26
|
+
'pagination[pageSize]': STRAPI_DEFAULT_PAGE_SIZE,
|
|
27
|
+
filters,
|
|
28
|
+
},
|
|
29
|
+
})
|
|
30
|
+
.then((response) =>
|
|
31
|
+
response.data.data.map((page) => ({
|
|
32
|
+
slug: page.attributes.slug,
|
|
33
|
+
locale: page.attributes.locale,
|
|
34
|
+
}))
|
|
35
|
+
)
|
|
36
|
+
// when a collection type for a requested locale does not exist, Strapi returns a 404. In this case, we return an empty array instead of throwing an error
|
|
37
|
+
.catch((error) => {
|
|
38
|
+
if (error.response?.status === 404) {
|
|
39
|
+
return [];
|
|
40
|
+
}
|
|
41
|
+
throw error;
|
|
42
|
+
})
|
|
43
|
+
);
|
|
35
44
|
|
|
36
45
|
const slugResults = await Promise.all(slugPromises);
|
|
37
46
|
|
|
38
|
-
let allSlugs = slugResults
|
|
39
|
-
.map((result) =>
|
|
40
|
-
result.data.data.map((page) => ({
|
|
41
|
-
slug: page.attributes.slug,
|
|
42
|
-
locale: page.attributes.locale,
|
|
43
|
-
}))
|
|
44
|
-
)
|
|
45
|
-
.flat();
|
|
47
|
+
let allSlugs = slugResults.flat();
|
|
46
48
|
|
|
47
49
|
// Identify missing locales for each slug
|
|
48
50
|
const missingLocales = locales.flatMap((locale) => {
|
|
@@ -2,12 +2,6 @@ import MockAxios from 'jest-mock-axios';
|
|
|
2
2
|
import getStrapiCollectionType from './getStrapiCollectionType';
|
|
3
3
|
import StrapiPage from '../../models/strapi/StrapiPage';
|
|
4
4
|
import { strapiPageMock } from '../../test/strapiMocks/strapiPage';
|
|
5
|
-
import getAvailableLocalesFromStrapi from './getAvailableLocalesFromStrapi';
|
|
6
|
-
|
|
7
|
-
jest.mock('./getAvailableLocalesFromStrapi', () => ({
|
|
8
|
-
__esModule: true,
|
|
9
|
-
default: jest.fn(),
|
|
10
|
-
}));
|
|
11
5
|
|
|
12
6
|
describe('The getStrapiCollectionType function', () => {
|
|
13
7
|
const germanStrapiPageMock = {
|
|
@@ -25,14 +19,7 @@ describe('The getStrapiCollectionType function', () => {
|
|
|
25
19
|
});
|
|
26
20
|
|
|
27
21
|
it('returns the localized versions if available', async () => {
|
|
28
|
-
(getAvailableLocalesFromStrapi as jest.Mock).mockResolvedValue([
|
|
29
|
-
'en',
|
|
30
|
-
'de',
|
|
31
|
-
'hu',
|
|
32
|
-
]);
|
|
33
|
-
|
|
34
22
|
MockAxios.get
|
|
35
|
-
.mockResolvedValueOnce({ data: { data: [strapiPageMock] } }) // english
|
|
36
23
|
.mockResolvedValueOnce({
|
|
37
24
|
data: {
|
|
38
25
|
data: [
|
|
@@ -46,6 +33,7 @@ describe('The getStrapiCollectionType function', () => {
|
|
|
46
33
|
],
|
|
47
34
|
},
|
|
48
35
|
})
|
|
36
|
+
.mockResolvedValueOnce({ data: { data: [strapiPageMock] } }) // english
|
|
49
37
|
.mockResolvedValueOnce({ data: { data: [] } });
|
|
50
38
|
|
|
51
39
|
const pages = getStrapiCollectionType<StrapiPage, 'slug'>(
|
|
@@ -66,16 +54,10 @@ describe('The getStrapiCollectionType function', () => {
|
|
|
66
54
|
});
|
|
67
55
|
|
|
68
56
|
it('returns the english versions if no localized version is available', async () => {
|
|
69
|
-
(getAvailableLocalesFromStrapi as jest.Mock).mockResolvedValue([
|
|
70
|
-
'en',
|
|
71
|
-
'de',
|
|
72
|
-
'hu',
|
|
73
|
-
]);
|
|
74
|
-
|
|
75
57
|
MockAxios.get
|
|
76
58
|
.mockResolvedValueOnce({ data: { data: [strapiPageMock] } }) // english
|
|
77
|
-
.
|
|
78
|
-
.
|
|
59
|
+
.mockRejectedValueOnce({ response: { status: 404 } }) // Hungarian version is missing (404)
|
|
60
|
+
.mockRejectedValueOnce({ response: { status: 404 } }); // German version is missing (404)
|
|
79
61
|
|
|
80
62
|
const pages = getStrapiCollectionType<StrapiPage, 'slug'>(
|
|
81
63
|
'/api/pages',
|
|
@@ -6,7 +6,6 @@ import {
|
|
|
6
6
|
import IStrapiData from '../../models/strapi/IStrapiData';
|
|
7
7
|
import IStrapiResponse from '../../models/strapi/IStrapiResponse';
|
|
8
8
|
import LocalizedEntity from '../../models/LocalizedEntity';
|
|
9
|
-
import getAvailableLocalesFromStrapi from './getAvailableLocalesFromStrapi';
|
|
10
9
|
|
|
11
10
|
interface Options {
|
|
12
11
|
locale?: string;
|
|
@@ -24,52 +23,58 @@ const getStrapiCollectionType = async <
|
|
|
24
23
|
{ locale = 'en', preview = false, filters = {} }: Options
|
|
25
24
|
): Promise<IStrapiData<T>[]> => {
|
|
26
25
|
const cache = preview ? false : undefined;
|
|
27
|
-
const allLocales = await getAvailableLocalesFromStrapi();
|
|
28
26
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
const sharedParams = {
|
|
28
|
+
pLevel: '6',
|
|
29
|
+
'pagination[pageSize]': STRAPI_DEFAULT_PAGE_SIZE,
|
|
30
|
+
filters,
|
|
31
|
+
...(preview ? { publicationState: 'preview' } : {}),
|
|
32
|
+
};
|
|
32
33
|
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
if (preview) {
|
|
42
|
-
params.publicationState = 'preview';
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return strapiClient.get<IStrapiResponse<IStrapiData<T>[]>>(path, {
|
|
46
|
-
params,
|
|
34
|
+
const requestedLocaleData = await strapiClient
|
|
35
|
+
.get<IStrapiResponse<IStrapiData<T>[]>>(path, {
|
|
36
|
+
params: {
|
|
37
|
+
...sharedParams,
|
|
38
|
+
locale,
|
|
39
|
+
},
|
|
47
40
|
cache,
|
|
41
|
+
})
|
|
42
|
+
.then((response) => response.data.data)
|
|
43
|
+
// when a collection type for a requested locale does not exist, Strapi returns a 404. In this case, we return an empty array instead of throwing an error
|
|
44
|
+
.catch((error) => {
|
|
45
|
+
if (error.response?.status === 404) {
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
throw error;
|
|
48
49
|
});
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
const responses = await Promise.all(promises);
|
|
52
50
|
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
51
|
+
const fallbackLocaleData = await strapiClient
|
|
52
|
+
.get<IStrapiResponse<IStrapiData<T>[]>>(path, {
|
|
53
|
+
params: {
|
|
54
|
+
...sharedParams,
|
|
55
|
+
locale: STRAPI_FALLBACK_LOCALE,
|
|
56
|
+
},
|
|
57
|
+
cache,
|
|
58
|
+
})
|
|
59
|
+
.then((response) => response.data.data)
|
|
60
|
+
// when a collection type for a requested locale does not exist, Strapi returns a 404. In this case, we return an empty array instead of throwing an error
|
|
61
|
+
.catch((error) => {
|
|
62
|
+
if (error.response?.status === 404) {
|
|
63
|
+
return [];
|
|
64
|
+
}
|
|
65
|
+
throw error;
|
|
66
|
+
});
|
|
62
67
|
|
|
63
|
-
const
|
|
64
|
-
const
|
|
68
|
+
const results = fallbackLocaleData.map((fallbackLocaleDataEntry) => {
|
|
69
|
+
const requestedLocale = requestedLocaleData.find(
|
|
65
70
|
(localized) =>
|
|
66
|
-
localized.attributes[key] ===
|
|
71
|
+
localized.attributes[key] === fallbackLocaleDataEntry.attributes[key]
|
|
67
72
|
);
|
|
68
73
|
|
|
69
|
-
return
|
|
74
|
+
return requestedLocale || fallbackLocaleDataEntry;
|
|
70
75
|
});
|
|
71
76
|
|
|
72
|
-
return
|
|
77
|
+
return results;
|
|
73
78
|
};
|
|
74
79
|
|
|
75
80
|
export default getStrapiCollectionType;
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import getAvailableLocalesFromStrapi from './getAvailableLocalesFromStrapi';
|
|
2
|
-
import strapiClient from './strapiClient';
|
|
3
|
-
|
|
4
|
-
jest.mock('./strapiClient', () => ({
|
|
5
|
-
get: jest.fn(),
|
|
6
|
-
}));
|
|
7
|
-
|
|
8
|
-
describe('The getAvailableLocales function', () => {
|
|
9
|
-
it('should fetch available locales and return them correctly', async () => {
|
|
10
|
-
const mockResponse = {
|
|
11
|
-
data: [{ code: 'en' }, { code: 'de' }, { code: 'hu' }],
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
(strapiClient.get as jest.Mock).mockResolvedValue(mockResponse);
|
|
15
|
-
|
|
16
|
-
const locales = await getAvailableLocalesFromStrapi();
|
|
17
|
-
|
|
18
|
-
expect(locales).toEqual(['en', 'de', 'hu']);
|
|
19
|
-
expect(strapiClient.get).toHaveBeenCalledWith('/i18n/locales');
|
|
20
|
-
expect(strapiClient.get).toHaveBeenCalledTimes(1);
|
|
21
|
-
});
|
|
22
|
-
});
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import strapiClient from './strapiClient';
|
|
2
|
-
|
|
3
|
-
const getAvailableLocalesFromStrapi = async (): Promise<string[]> => {
|
|
4
|
-
const { data } = await strapiClient.get('/i18n/locales');
|
|
5
|
-
return data.map((locale: { code: string }) => locale.code);
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
export default getAvailableLocalesFromStrapi;
|