refactor(frontend): route protected API access through server session

This commit is contained in:
Piotr Oleszczyk 2026-03-12 16:27:24 +01:00
parent 1d5630ed8c
commit b11f64d5a1
31 changed files with 727 additions and 249 deletions

View file

@ -1,4 +1,5 @@
import { deleteLabResult, getLabResults, updateLabResult } from '$lib/api';
import { getSessionApiOptions } from '$lib/server/api';
import { fail } from '@sveltejs/kit';
import type { Actions, PageServerLoad } from './$types';
@ -9,7 +10,9 @@ const STATUS_GROUP_FLAGS = {
type StatusGroup = 'all' | 'abnormal' | 'normal' | 'uninterpreted';
export const load: PageServerLoad = async ({ url }) => {
export const load: PageServerLoad = async (event) => {
const { url } = event;
const q = url.searchParams.get('q') ?? undefined;
const test_code = url.searchParams.get('test_code') ?? undefined;
const flag = url.searchParams.get('flag') ?? undefined;
@ -41,7 +44,7 @@ export const load: PageServerLoad = async ({ url }) => {
latest_only: latestOnly,
limit,
offset
});
}, getSessionApiOptions(event));
const totalPages = Math.max(1, Math.ceil(resultPage.total / limit));
return {
@ -64,8 +67,8 @@ function normalizeStatusGroup(value: string | null): StatusGroup {
}
export const actions: Actions = {
update: async ({ request }) => {
const form = await request.formData();
update: async (event) => {
const form = await event.request.formData();
const id = form.get('id') as string;
const collected_at = form.get('collected_at') as string;
const test_code = form.get('test_code') as string;
@ -136,20 +139,20 @@ export const actions: Actions = {
}
try {
await updateLabResult(id, body);
await updateLabResult(id, body, getSessionApiOptions(event));
return { updated: true };
} catch (e) {
return fail(500, { error: (e as Error).message });
}
},
delete: async ({ request }) => {
const form = await request.formData();
delete: async (event) => {
const form = await event.request.formData();
const id = form.get('id') as string;
if (!id) return fail(400, { error: 'Missing id' });
try {
await deleteLabResult(id);
await deleteLabResult(id, getSessionApiOptions(event));
return { deleted: true };
} catch (e) {
return fail(500, { error: (e as Error).message });

View file

@ -1,4 +1,5 @@
import { createLabResult } from '$lib/api';
import { getSessionApiOptions } from '$lib/server/api';
import { fail, redirect } from '@sveltejs/kit';
import type { Actions, PageServerLoad } from './$types';
@ -7,8 +8,8 @@ export const load: PageServerLoad = async () => {
};
export const actions: Actions = {
default: async ({ request }) => {
const form = await request.formData();
default: async (event) => {
const form = await event.request.formData();
const collected_at = form.get('collected_at') as string;
const test_code = form.get('test_code') as string;
const test_name_original = form.get('test_name_original') as string;
@ -32,7 +33,7 @@ export const actions: Actions = {
if (lab) body.lab = lab;
try {
await createLabResult(body);
await createLabResult(body, getSessionApiOptions(event));
} catch (error) {
return fail(500, { error: (error as Error).message });
}

View file

@ -1,8 +1,11 @@
import { getMedications } from '$lib/api';
import { getSessionApiOptions } from '$lib/server/api';
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ url }) => {
export const load: PageServerLoad = async (event) => {
const { url } = event;
const kind = url.searchParams.get('kind') ?? undefined;
const medications = await getMedications({ kind });
const medications = await getMedications({ kind }, getSessionApiOptions(event));
return { medications, kind };
};

View file

@ -1,4 +1,5 @@
import { createMedication } from '$lib/api';
import { getSessionApiOptions } from '$lib/server/api';
import { fail, redirect } from '@sveltejs/kit';
import type { Actions, PageServerLoad } from './$types';
@ -7,8 +8,8 @@ export const load: PageServerLoad = async () => {
};
export const actions: Actions = {
default: async ({ request }) => {
const form = await request.formData();
default: async (event) => {
const form = await event.request.formData();
const kind = form.get('kind') as string;
const product_name = form.get('product_name') as string;
const active_substance = form.get('active_substance') as string;
@ -24,7 +25,7 @@ export const actions: Actions = {
product_name,
active_substance: active_substance || undefined,
notes: notes || undefined
});
}, getSessionApiOptions(event));
} catch (error) {
return fail(500, { error: (error as Error).message });
}