feat(geschichten): fetch own drafts in page loader for blog writers
Blog writers now get a separate resilient DRAFT fetch alongside the PUBLISHED list. A network failure degrades to drafts: [] rather than a 500, so the overview stays usable even if the draft fetch times out. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,20 +1,23 @@
|
||||
import { error } from '@sveltejs/kit';
|
||||
import { createApiClient, extractErrorCode } from '$lib/shared/api.server';
|
||||
import { getErrorMessage } from '$lib/shared/errors';
|
||||
import { settled } from '$lib/shared/server/settled';
|
||||
import type { components } from '$lib/generated/api';
|
||||
import type { PageServerLoad } from './$types';
|
||||
|
||||
type Person = components['schemas']['Person'];
|
||||
type GeschichteSummary = components['schemas']['GeschichteSummary'];
|
||||
|
||||
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
||||
|
||||
export const load: PageServerLoad = async ({ url, fetch }) => {
|
||||
export const load: PageServerLoad = async ({ url, fetch, parent }) => {
|
||||
const { canBlogWrite } = await parent();
|
||||
const api = createApiClient(fetch);
|
||||
const personIds = url.searchParams.getAll('personId');
|
||||
const rawDocumentId = url.searchParams.get('documentId');
|
||||
const documentId = rawDocumentId && UUID_PATTERN.test(rawDocumentId) ? rawDocumentId : null;
|
||||
|
||||
const [listResult, docResult, ...personResults] = await Promise.all([
|
||||
const [listSettled, draftsSettled, docSettled, ...personResults] = await Promise.allSettled([
|
||||
api.GET('/api/geschichten', {
|
||||
params: {
|
||||
query: {
|
||||
@@ -24,20 +27,32 @@ export const load: PageServerLoad = async ({ url, fetch }) => {
|
||||
}
|
||||
}
|
||||
}),
|
||||
canBlogWrite
|
||||
? api.GET('/api/geschichten', { params: { query: { status: 'DRAFT' } } })
|
||||
: Promise.resolve(null),
|
||||
documentId
|
||||
? api.GET('/api/documents/{id}', { params: { path: { id: documentId } } })
|
||||
: Promise.resolve(null),
|
||||
...personIds.map((id) => api.GET('/api/persons/{id}', { params: { path: { id } } }))
|
||||
]);
|
||||
|
||||
if (!listResult.response.ok) {
|
||||
throw error(listResult.response.status, getErrorMessage(extractErrorCode(listResult.error)));
|
||||
const listResult = listSettled.status === 'fulfilled' ? listSettled.value : null;
|
||||
if (!listResult?.response.ok) {
|
||||
throw error(
|
||||
listResult?.response.status ?? 500,
|
||||
getErrorMessage(extractErrorCode(listResult?.error))
|
||||
);
|
||||
}
|
||||
|
||||
const personFilters = personResults
|
||||
.filter((r) => r && r.response.ok && r.data)
|
||||
.map((r) => r!.data!) as Person[];
|
||||
const drafts: GeschichteSummary[] = canBlogWrite
|
||||
? (settled<GeschichteSummary[]>(draftsSettled) ?? [])
|
||||
: [];
|
||||
|
||||
const personFilters = personResults
|
||||
.filter((r) => r.status === 'fulfilled' && r.value?.response.ok && r.value?.data)
|
||||
.map((r) => (r as PromiseFulfilledResult<{ response: Response; data: Person }>).value.data);
|
||||
|
||||
const docResult = docSettled.status === 'fulfilled' ? docSettled.value : null;
|
||||
let documentFilter: { id: string; title: string | null } | null = null;
|
||||
if (documentId) {
|
||||
if (docResult && docResult.response.ok && docResult.data) {
|
||||
@@ -53,6 +68,7 @@ export const load: PageServerLoad = async ({ url, fetch }) => {
|
||||
|
||||
return {
|
||||
geschichten: listResult.data ?? [],
|
||||
drafts,
|
||||
personFilters,
|
||||
documentFilter
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user