Files
housesof/src/routes/about/+page.server.ts
Félix Péault 93a50770d0 Move API fetch data to different file
Sharing multiple exportable functions in one file impacts the use of private env variables 🤷
2022-08-16 20:00:52 +02:00

91 lines
2.5 KiB
TypeScript

import { error } from '@sveltejs/kit'
import type { PageServerLoad } from './$types'
import { fetchAPI } from '$utils/api/data'
import { getRandomItems } from '$utils/functions'
export const load: PageServerLoad = async () => {
try {
// Get data and total of published photos
const res = await fetchAPI(`
query {
photos: photo (
filter: {
favorite: { _eq: true },
status: { _eq: "published" },
},
limit: -1,
) {
id
}
about {
description
intro_firstphoto {
id
title
}
intro_portraits {
id
title
}
intro_text
intro_firstlocation {
slug
country {
flag { id }
slug
}
}
purpose_text
process_title
process_subtitle
process_steps {
title
text
image {
id
title
width, height
}
}
process_intention
contact_title
contact_blocks
}
}
`)
const { data: { about, photos: photosIds }} = res
// Get random photos
const randomPhotosIds = [...getRandomItems(photosIds, 42)].map(({ id }) => id)
// Query these random photos from IDs
const photosRes = await fetchAPI(`
query {
photo (filter: { id: { _in: [${randomPhotosIds}] }}) {
id
title
slug
image {
id
title
}
}
}
`)
if (photosRes) {
const { data: { photo: photos }} = photosRes
return {
about,
photos,
}
}
} catch (err) {
throw error(500, err)
}
}