mirror of
https://github.com/movie-web/providers.git
synced 2025-09-13 18:13:25 +00:00
fetcher seralization
This commit is contained in:
24
src/fetchers/body.ts
Normal file
24
src/fetchers/body.ts
Normal 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),
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user