59 lines
1 KiB
TypeScript
59 lines
1 KiB
TypeScript
import { API_URL } from '$env/static/private';
|
|
|
|
import type { PageServerLoad } from './$types';
|
|
|
|
import type { Chapter } from '$lib/types';
|
|
|
|
export const load = (async ({ parent, fetch, cookies }) => {
|
|
await parent();
|
|
|
|
const session = cookies.get('session');
|
|
|
|
let res = await fetch(`${API_URL}/chapters`, {
|
|
headers: {
|
|
Authorization: `Bearer ${session}`
|
|
}
|
|
});
|
|
|
|
if (!res.ok) {
|
|
return {
|
|
daily: null
|
|
};
|
|
}
|
|
|
|
const chapters = (await res.json()) as Chapter[];
|
|
|
|
const lastChapter = chapters.filter((chapter) => chapter.show).pop();
|
|
|
|
if (!lastChapter) {
|
|
return {
|
|
daily: null
|
|
};
|
|
}
|
|
|
|
res = await fetch(`${API_URL}/chapter/${lastChapter.id}`, {
|
|
headers: {
|
|
Authorization: `Bearer ${session}`
|
|
}
|
|
});
|
|
|
|
if (!res.ok) {
|
|
return {
|
|
daily: {
|
|
chapter: lastChapter,
|
|
puzzle: null
|
|
}
|
|
};
|
|
}
|
|
|
|
const chapter = (await res.json()) as Chapter;
|
|
|
|
const lastPuzzle = chapter.puzzles.filter((puzzle) => puzzle.show).pop();
|
|
|
|
return {
|
|
daily: {
|
|
chapter: lastChapter,
|
|
puzzle: lastPuzzle
|
|
}
|
|
};
|
|
}) satisfies PageServerLoad;
|