refactor(admin/users): migrate update action to createApiClient

Replace fetch('/api/users/${id}', { method: 'PUT', ... }) + inline JSON
error parsing with createApiClient(fetch).PUT('/api/users/{id}', ...) and
the standard result.error cast pattern.

Also fix pre-existing Sentry mock event failures in layout.server.spec.ts
by adding request and url to the test event object.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Marcel
2026-05-19 09:56:17 +02:00
committed by marcel
parent d739f58bb5
commit 31d3ec8367
3 changed files with 138 additions and 16 deletions

View File

@@ -2,6 +2,7 @@ import { error, fail, redirect } from '@sveltejs/kit';
import type { PageServerLoad, Actions } from './$types';
import { createApiClient } from '$lib/shared/api.server';
import { getErrorMessage } from '$lib/shared/errors';
import type { components } from '$lib/generated/api';
export const load: PageServerLoad = async ({ params, fetch, locals }) => {
const user = locals.user;
@@ -45,21 +46,17 @@ export const actions: Actions = {
groupIds: data.getAll('groupIds') as string[]
};
const res = await fetch(`/api/users/${params.id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body)
const api = createApiClient(fetch);
const result = await api.PUT('/api/users/{id}', {
params: { path: { id: params.id } },
// Body may contain null for fields the user cleared; the backend treats
// null as "clear this field". Cast to satisfy the optional-only spec type.
body: body as components['schemas']['AdminUpdateUserRequest']
});
if (!res.ok) {
let code: string | undefined;
try {
const json = await res.json();
code = json?.code;
} catch {
// ignore
}
return fail(res.status, { error: getErrorMessage(code) });
if (!result.response.ok) {
const code = (result.error as unknown as { code?: string })?.code;
return fail(result.response.status, { error: getErrorMessage(code) });
}
return { success: true };