fix: use schema-based deleteKey and reactive key list refresh

- Add deleteKeySchema for proper FormData validation
- Return { deleted: true } from deleteKey for change tracking
- Use $state for keysPromise to refresh list after create/delete

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Sanju Sivalingam
2026-02-17 14:43:46 +05:30
parent 9422e94a8e
commit 8fc5587876
3 changed files with 16 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
import { form, getRequestEvent, query } from '$app/server'; import { form, getRequestEvent, query } from '$app/server';
import { auth } from '$lib/server/auth'; import { auth } from '$lib/server/auth';
import { createKeySchema } from '$lib/schema/api-keys'; import { createKeySchema, deleteKeySchema } from '$lib/schema/api-keys';
export const listKeys = query(async () => { export const listKeys = query(async () => {
const { request } = getRequestEvent(); const { request } = getRequestEvent();
@@ -16,12 +16,11 @@ export const createKey = form(createKeySchema, async ({ name }) => {
return result; return result;
}); });
export const deleteKey = form(async () => { export const deleteKey = form(deleteKeySchema, async ({ keyId }) => {
const { request } = getRequestEvent(); const { request } = getRequestEvent();
const formData = await request.clone().formData();
const keyId = formData.get('keyId') as string;
await auth.api.deleteApiKey({ await auth.api.deleteApiKey({
body: { keyId }, body: { keyId },
headers: request.headers headers: request.headers
}); });
return { deleted: true };
}); });

View File

@@ -3,3 +3,7 @@ import { object, string, pipe, minLength } from 'valibot';
export const createKeySchema = object({ export const createKeySchema = object({
name: pipe(string(), minLength(1)) name: pipe(string(), minLength(1))
}); });
export const deleteKeySchema = object({
keyId: pipe(string(), minLength(1))
});

View File

@@ -2,10 +2,18 @@
import { listKeys, createKey, deleteKey } from '$lib/api/api-keys.remote'; import { listKeys, createKey, deleteKey } from '$lib/api/api-keys.remote';
let newKeyValue = $state<string | null>(null); let newKeyValue = $state<string | null>(null);
let keysPromise = $state(listKeys());
$effect(() => { $effect(() => {
if (createKey.result?.key) { if (createKey.result?.key) {
newKeyValue = createKey.result.key; newKeyValue = createKey.result.key;
keysPromise = listKeys();
}
});
$effect(() => {
if (deleteKey.result?.deleted) {
keysPromise = listKeys();
} }
}); });
</script> </script>
@@ -71,7 +79,7 @@
<h3 class="font-semibold">Your Keys</h3> <h3 class="font-semibold">Your Keys</h3>
</div> </div>
{#await listKeys()} {#await keysPromise}
<div class="px-6 py-8 text-center text-sm text-neutral-500">Loading keys...</div> <div class="px-6 py-8 text-center text-sm text-neutral-500">Loading keys...</div>
{:then keys} {:then keys}
{#if keys && keys.length > 0} {#if keys && keys.length > 0}