mirror of
https://github.com/movie-web/providers.git
synced 2025-09-13 17:43:25 +00:00
add simpleProxyFetcher and add fetcher unit tests
This commit is contained in:
35
src/fetchers/simpleProxy.ts
Normal file
35
src/fetchers/simpleProxy.ts
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user