fix: replace Directus SDK with native fetch for featured content

Replace customEndpoint() with native fetch in getFeaturedModels() and
getFeaturedVideos() to return plain JSON instead of non-serializable
Directus SDK objects. This resolves the SvelteKit serialization error:
"Cannot stringify arbitrary non-POJOs".

Changes:
- Use native fetch with PUBLIC_URL instead of getDirectusInstance()
- Return plain JSON via response.json() instead of SDK request objects
- Remove JSON.parse(JSON.stringify()) serialization hack from +page.server.ts
- Rename fetch parameter to fetchFn for clarity

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Valknar XXX
2025-10-28 23:54:56 +01:00
parent 10ab7a65df
commit 0b07da8e64
2 changed files with 16 additions and 24 deletions

View File

@@ -220,18 +220,17 @@ export async function getVideosForModel(id, fetch?: typeof globalThis.fetch) {
export async function getFeaturedVideos(
limit: number,
fetch?: typeof globalThis.fetch,
fetchFn: typeof globalThis.fetch = globalThis.fetch,
) {
return loggedApiCall(
"getFeaturedVideos",
async () => {
const directus = getDirectusInstance(fetch);
return directus.request<Video[]>(
customEndpoint({
method: "GET",
path: `/sexy/videos?featured=true&limit=${limit}`,
}),
);
const url = `${PUBLIC_URL}/api/sexy/videos?featured=true&limit=${limit}`;
const response = await fetchFn(url);
if (!response.ok) {
throw new Error(`Failed to fetch featured videos: ${response.statusText}`);
}
return (await response.json()) as Video[];
},
{ limit },
);
@@ -290,18 +289,17 @@ export async function getModels(fetch?: typeof globalThis.fetch) {
export async function getFeaturedModels(
limit = 3,
fetch?: typeof globalThis.fetch,
fetchFn: typeof globalThis.fetch = globalThis.fetch,
) {
return loggedApiCall(
"getFeaturedModels",
async () => {
const directus = getDirectusInstance(fetch);
return directus.request<Model[]>(
customEndpoint({
method: "GET",
path: `/sexy/models?featured=true&limit=${limit}`,
}),
);
const url = `${PUBLIC_URL}/api/sexy/models?featured=true&limit=${limit}`;
const response = await fetchFn(url);
if (!response.ok) {
throw new Error(`Failed to fetch featured models: ${response.statusText}`);
}
return (await response.json()) as Model[];
},
{ limit },
);

View File

@@ -1,13 +1,7 @@
import { getFeaturedModels, getFeaturedVideos } from "$lib/services";
export async function load({ fetch }) {
const [models, videos] = await Promise.all([
getFeaturedModels(3, fetch),
getFeaturedVideos(3, fetch),
]);
// Ensure data is serializable by converting to plain JSON
return {
models: JSON.parse(JSON.stringify(models)),
videos: JSON.parse(JSON.stringify(videos)),
models: await getFeaturedModels(3, fetch),
videos: await getFeaturedVideos(3, fetch),
};
}