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 { error } from '@sveltejs/kit';
|
||||||
import { createApiClient, extractErrorCode } from '$lib/shared/api.server';
|
import { createApiClient, extractErrorCode } from '$lib/shared/api.server';
|
||||||
import { getErrorMessage } from '$lib/shared/errors';
|
import { getErrorMessage } from '$lib/shared/errors';
|
||||||
|
import { settled } from '$lib/shared/server/settled';
|
||||||
import type { components } from '$lib/generated/api';
|
import type { components } from '$lib/generated/api';
|
||||||
import type { PageServerLoad } from './$types';
|
import type { PageServerLoad } from './$types';
|
||||||
|
|
||||||
type Person = components['schemas']['Person'];
|
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;
|
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 api = createApiClient(fetch);
|
||||||
const personIds = url.searchParams.getAll('personId');
|
const personIds = url.searchParams.getAll('personId');
|
||||||
const rawDocumentId = url.searchParams.get('documentId');
|
const rawDocumentId = url.searchParams.get('documentId');
|
||||||
const documentId = rawDocumentId && UUID_PATTERN.test(rawDocumentId) ? rawDocumentId : null;
|
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', {
|
api.GET('/api/geschichten', {
|
||||||
params: {
|
params: {
|
||||||
query: {
|
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
|
documentId
|
||||||
? api.GET('/api/documents/{id}', { params: { path: { id: documentId } } })
|
? api.GET('/api/documents/{id}', { params: { path: { id: documentId } } })
|
||||||
: Promise.resolve(null),
|
: Promise.resolve(null),
|
||||||
...personIds.map((id) => api.GET('/api/persons/{id}', { params: { path: { id } } }))
|
...personIds.map((id) => api.GET('/api/persons/{id}', { params: { path: { id } } }))
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (!listResult.response.ok) {
|
const listResult = listSettled.status === 'fulfilled' ? listSettled.value : null;
|
||||||
throw error(listResult.response.status, getErrorMessage(extractErrorCode(listResult.error)));
|
if (!listResult?.response.ok) {
|
||||||
|
throw error(
|
||||||
|
listResult?.response.status ?? 500,
|
||||||
|
getErrorMessage(extractErrorCode(listResult?.error))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const personFilters = personResults
|
const drafts: GeschichteSummary[] = canBlogWrite
|
||||||
.filter((r) => r && r.response.ok && r.data)
|
? (settled<GeschichteSummary[]>(draftsSettled) ?? [])
|
||||||
.map((r) => r!.data!) as Person[];
|
: [];
|
||||||
|
|
||||||
|
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;
|
let documentFilter: { id: string; title: string | null } | null = null;
|
||||||
if (documentId) {
|
if (documentId) {
|
||||||
if (docResult && docResult.response.ok && docResult.data) {
|
if (docResult && docResult.response.ok && docResult.data) {
|
||||||
@@ -53,6 +68,7 @@ export const load: PageServerLoad = async ({ url, fetch }) => {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
geschichten: listResult.data ?? [],
|
geschichten: listResult.data ?? [],
|
||||||
|
drafts,
|
||||||
personFilters,
|
personFilters,
|
||||||
documentFilter
|
documentFilter
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user