mirror of
https://github.com/movie-web/providers.git
synced 2025-09-13 14:43:25 +00:00
27 lines
754 B
TypeScript
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;
|
|
}
|