DocumentMetadataDrawer links to /geschichten?documentId={id}, but the list
loader silently dropped the param — the user got the unfiltered list. The
loader now validates the UUID and forwards it to GET /api/geschichten,
returning it as documentIdFilter in page data.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
44 lines
1.4 KiB
TypeScript
44 lines
1.4 KiB
TypeScript
import { error } from '@sveltejs/kit';
|
|
import { createApiClient, extractErrorCode } from '$lib/shared/api.server';
|
|
import { getErrorMessage } from '$lib/shared/errors';
|
|
import type { components } from '$lib/generated/api';
|
|
import type { PageServerLoad } from './$types';
|
|
|
|
type Person = components['schemas']['Person'];
|
|
|
|
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 }) => {
|
|
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, ...personResults] = await Promise.all([
|
|
api.GET('/api/geschichten', {
|
|
params: {
|
|
query: {
|
|
status: 'PUBLISHED',
|
|
personId: personIds.length ? personIds : undefined,
|
|
documentId: documentId ?? undefined
|
|
}
|
|
}
|
|
}),
|
|
...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 personFilters = personResults
|
|
.filter((r) => r && r.response.ok && r.data)
|
|
.map((r) => r!.data!) as Person[];
|
|
|
|
return {
|
|
geschichten: listResult.data ?? [],
|
|
personFilters,
|
|
documentIdFilter: documentId
|
|
};
|
|
};
|