(chore): manual fetch SvelteKit actions

This commit is contained in:
Shahrad Elahi 2023-11-06 22:14:37 +03:30
parent af3f37b14f
commit 3fa6e3e63e

View File

@ -0,0 +1,25 @@
export interface Request extends Omit<RequestInit, 'url'> {
action?: string;
form?: Record<string, string>;
}
export default function fetchAction(request: Request): Promise<Response> {
return fetch(request.action ?? '/', {
...request,
method: request.method ?? 'GET',
headers: {
...request.headers,
'X-Sveltekit-Action': 'true',
},
body: request.form ? createFormData(request.form) : request.body || undefined,
});
}
function createFormData(data: Record<string, string>): FormData {
const form = new FormData();
for (const key in data) {
if (typeof data[key] !== 'string') continue;
form.set(key, data[key]);
}
return form;
}