Fix URLSearchParams usage for react-native

This commit is contained in:
Jorrin
2024-02-29 22:44:38 +01:00
parent c735ce33a0
commit 36d4b41baa
4 changed files with 26 additions and 16 deletions

View File

@@ -1,6 +1,7 @@
import FormData from 'form-data';
import { FetcherOptions } from '@/fetchers/types';
import { isReactNative } from '@/utils/native';
export interface SeralizedBody {
headers: Record<string, string>;
@@ -8,11 +9,18 @@ export interface SeralizedBody {
}
export function serializeBody(body: FetcherOptions['body']): SeralizedBody {
if (body === undefined || typeof body === 'string' || body instanceof URLSearchParams || body instanceof FormData)
if (body === undefined || typeof body === 'string' || body instanceof URLSearchParams || body instanceof FormData) {
if (body instanceof URLSearchParams && isReactNative()) {
return {
headers: {},
body: body.toString(),
};
}
return {
headers: {},
body,
};
}
// serialize as JSON
return {