import type { PageServerLoad } from './$types'; import type { Paste } from '$lib/types/paste'; import { getServerToken } from '$lib/utils/auth'; import { apiUrl, viewInternalApiKey } from '$lib/server/api'; import { error } from '@sveltejs/kit'; export const load: PageServerLoad = async ({ params, fetch, url, cookies, parent, request }) => { const { id } = params; const parentData = await parent().catch(() => null); const token = getServerToken(cookies); const userAgent = request.headers.get('user-agent') ?? 'Sharpbin Client'; const pasteRes = await fetch(`${apiUrl}/api/paste/${id}`, { headers: { Authorization: token ? `Bearer ${token}` : '' } }); if (pasteRes.status === 404) { throw error(pasteRes.status, 'Paste not found.'); } const viewed = await fetch(`${apiUrl}/api/paste/${id}/view`, { method: 'POST', headers: { 'X-Internal-API-Key': viewInternalApiKey, Authorization: token ? `Bearer ${token}` : '', 'User-Agent': userAgent } }); const pasteData = await pasteRes.json(); const paste = pasteData.paste as Paste; if (viewed.status === 200) { const viewJson = await viewed.json(); if (viewJson.success === true && viewJson.message === 'Paste view recorded.') { pasteData.views += 1; } } else if (viewInternalApiKey === '') { console.error( 'The API key for internal requests is not set. Please configure it in the environment variables.' ); } const pasteContent = await fetch(`/api/paste/${id}/raw`); const pasteOptions = await fetch('/api/paste/info'); const options = await pasteOptions.json(); const content = await pasteContent.text(); return { paste, content, options, authOptions: parentData && typeof parentData === 'object' && parentData !== null && 'options' in parentData ? (parentData as { options?: unknown }).options : null, url: url.href }; };