Files
providers/src/fetchers/standardFetch.ts
2023-09-27 19:50:55 +02:00

27 lines
754 B
TypeScript

import { serializeBody } from '@/fetchers/body';
import { makeFullUrl } from '@/fetchers/common';
import { FetchLike } from '@/fetchers/fetch';
import { Fetcher } from '@/fetchers/types';
export function makeStandardFetcher(f: FetchLike): Fetcher {
const normalFetch: Fetcher = async (url, ops) => {
const fullUrl = makeFullUrl(url, ops);
const seralizedBody = serializeBody(ops.body);
const res = await f(fullUrl, {
method: ops.method,
headers: {
...seralizedBody.headers,
...ops.headers,
},
body: seralizedBody.body,
});
const isJson = res.headers.get('content-type')?.includes('application/json');
if (isJson) return res.json();
return res.text();
};
return normalFetch;
}