mirror of
https://github.com/wireadmin/wireadmin
synced 2025-01-23 04:17:00 +00:00
(chore): manual fetch SvelteKit
actions
This commit is contained in:
parent
af3f37b14f
commit
3fa6e3e63e
25
web/src/lib/fetch-action.ts
Normal file
25
web/src/lib/fetch-action.ts
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user