mirror of
https://github.com/movie-web/providers.git
synced 2025-09-13 14:53:24 +00:00
35 lines
868 B
TypeScript
35 lines
868 B
TypeScript
import FormData from 'form-data';
|
|
|
|
import { FetcherOptions } from '@/fetchers/types';
|
|
import { isReactNative } from '@/utils/native';
|
|
|
|
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) {
|
|
if (body instanceof URLSearchParams && isReactNative()) {
|
|
return {
|
|
headers: {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
},
|
|
body: body.toString(),
|
|
};
|
|
}
|
|
return {
|
|
headers: {},
|
|
body,
|
|
};
|
|
}
|
|
|
|
// serialize as JSON
|
|
return {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(body),
|
|
};
|
|
}
|