add simpleProxyFetcher and add fetcher unit tests

This commit is contained in:
mrjvs
2023-09-10 19:09:23 +02:00
parent a31e05adfb
commit aa24946d6f
5 changed files with 286 additions and 3 deletions

View File

@@ -0,0 +1,35 @@
import fetch from 'node-fetch';
import { makeFullUrl } from '@/fetchers/common';
import { makeStandardFetcher } from '@/fetchers/standardFetch';
import { Fetcher } from '@/fetchers/types';
const headerMap: Record<string, string> = {
cookie: 'X-Cookie',
referer: 'X-Referer',
origin: 'X-Origin',
};
export function makeSimpleProxyFetcher(proxyUrl: string, f: typeof fetch): Fetcher {
const fetcher = makeStandardFetcher(f);
const proxiedFetch: Fetcher = async (url, ops) => {
const fullUrl = makeFullUrl(url, ops);
const headerEntries = Object.entries(ops.headers).map((entry) => {
const key = entry[0].toLowerCase();
if (headerMap[key]) return [headerMap[key], entry[1]];
return entry;
});
return fetcher(proxyUrl, {
...ops,
query: {
destination: fullUrl,
},
headers: Object.fromEntries(headerEntries),
baseUrl: undefined,
});
};
return proxiedFetch;
}