fetcher seralization

This commit is contained in:
mrjvs
2023-09-06 15:09:52 +02:00
parent ec3efbd344
commit 637b3b635a
4 changed files with 33 additions and 7 deletions

24
src/fetchers/body.ts Normal file
View File

@@ -0,0 +1,24 @@
import FormData = require('form-data');
import { FetcherOptions } from '@/fetchers/types';
export interface SeralizedBody {
headers: Record<string, string>;
body: FormData | URLSearchParams | string | undefined;
}
export function serializeBody(body: FetcherOptions['body']): SeralizedBody {
if (body === undefined || typeof body === 'string' || body instanceof URLSearchParams || body instanceof FormData)
return {
headers: {},
body,
};
// serialize as JSON
return {
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
};
}