feat: add strapi cms data
- add types - add layout data loading - add mapping functions - add CmsComponent - remove image
This commit is contained in:
55
src/routes/+layout.server.ts
Normal file
55
src/routes/+layout.server.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { STRAPI_API_KEY } from '$env/static/private';
|
||||
import { PUBLIC_STRAPI_URL } from '$env/static/public';
|
||||
import type { LayoutServerLoad } from './$types';
|
||||
import { error } from '@sveltejs/kit';
|
||||
import { mapFooterData, mapHeaderData } from '$lib/mapping/strapiMapping.svelte.js';
|
||||
import type { StrapiComponentUnion } from '$lib/types/strapi';
|
||||
|
||||
export const load: LayoutServerLoad = async () => {
|
||||
const query = new URLSearchParams({
|
||||
// Profile: category 'shared', component 'profile', relation 'profile', sub-field 'image'
|
||||
'populate[components][on][shared.profile][populate][profile][populate]': 'image',
|
||||
|
||||
// Project: category 'shared', component 'project', relation 'project', sub-relation 'entries'
|
||||
'populate[components][on][shared.project][populate][project][populate]': 'entries',
|
||||
|
||||
// Skill: category 'shared', component 'skill', relation 'skill', sub-relation 'entries'
|
||||
'populate[components][on][shared.skill][populate][skill][populate]': 'entries',
|
||||
|
||||
// Contact: category 'shared', component 'contact', relation 'contact'
|
||||
'populate[components][on][shared.contact][populate]': 'contact'
|
||||
}).toString();
|
||||
|
||||
const requestInit: RequestInit = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${STRAPI_API_KEY}`
|
||||
}
|
||||
};
|
||||
|
||||
const [homepageResponse, footerResponse] = await Promise.allSettled([
|
||||
fetch(`${PUBLIC_STRAPI_URL}/api/homepage?${query}`, requestInit),
|
||||
fetch(`${PUBLIC_STRAPI_URL}/api/footer`, requestInit)
|
||||
]);
|
||||
|
||||
if (
|
||||
homepageResponse.status === 'rejected' ||
|
||||
footerResponse.status === 'rejected' ||
|
||||
!homepageResponse.value.ok ||
|
||||
!footerResponse.value.ok
|
||||
) {
|
||||
throw error(500, 'Failed to connect to the CMS');
|
||||
}
|
||||
|
||||
const [homepageData, footerData] = await Promise.all([
|
||||
homepageResponse.value.json(),
|
||||
footerResponse.value.json()
|
||||
]);
|
||||
|
||||
return {
|
||||
header: mapHeaderData(homepageData),
|
||||
components: homepageData.data.components as StrapiComponentUnion[],
|
||||
footer: mapFooterData(footerData)
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user