mirror of
https://github.com/movie-web/providers.git
synced 2025-09-13 18:13:25 +00:00
More elaborate fetcher API's
This commit is contained in:
@@ -1,8 +1,20 @@
|
||||
import { serializeBody } from '@/fetchers/body';
|
||||
import { makeFullUrl } from '@/fetchers/common';
|
||||
import { FetchLike } from '@/fetchers/fetch';
|
||||
import { FetchLike, FetchReply } from '@/fetchers/fetch';
|
||||
import { Fetcher } from '@/fetchers/types';
|
||||
|
||||
function getHeaders(list: string[], res: FetchReply): Headers {
|
||||
const output = new Headers();
|
||||
list.forEach((header) => {
|
||||
const realHeader = header.toLowerCase();
|
||||
const value = res.headers.get(realHeader);
|
||||
const extraValue = res.extraHeaders?.get(realHeader);
|
||||
if (!value) return;
|
||||
output.set(realHeader, extraValue ?? value);
|
||||
});
|
||||
return output;
|
||||
}
|
||||
|
||||
export function makeStandardFetcher(f: FetchLike): Fetcher {
|
||||
const normalFetch: Fetcher = async (url, ops) => {
|
||||
const fullUrl = makeFullUrl(url, ops);
|
||||
@@ -17,9 +29,17 @@ export function makeStandardFetcher(f: FetchLike): Fetcher {
|
||||
body: seralizedBody.body,
|
||||
});
|
||||
|
||||
let body: any;
|
||||
const isJson = res.headers.get('content-type')?.includes('application/json');
|
||||
if (isJson) return res.json();
|
||||
return res.text();
|
||||
if (isJson) body = await res.json();
|
||||
else body = res.text();
|
||||
|
||||
return {
|
||||
body,
|
||||
finalUrl: res.extraUrl ?? res.url,
|
||||
headers: getHeaders(ops.readHeaders, res),
|
||||
statusCode: res.status,
|
||||
};
|
||||
};
|
||||
|
||||
return normalFetch;
|
||||
|
Reference in New Issue
Block a user