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

View File

@@ -1,13 +1,7 @@
import { getFeaturedModels, getFeaturedVideos } from "$lib/services"; import { getFeaturedModels, getFeaturedVideos } from "$lib/services";
export async function load({ fetch }) { 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 { return {
models: JSON.parse(JSON.stringify(models)), models: await getFeaturedModels(3, fetch),
videos: JSON.parse(JSON.stringify(videos)), videos: await getFeaturedVideos(3, fetch),
}; };
} }