diff --git a/package.json b/package.json index 95b1b79..b9711fc 100644 --- a/package.json +++ b/package.json @@ -61,12 +61,12 @@ "vite": "^4.0.0", "vite-plugin-dts": "^3.5.3", "vite-plugin-eslint": "^1.8.1", - "vitest": "^0.32.2" + "vitest": "^0.32.2", + "node-fetch": "^3.3.2" }, "dependencies": { "cheerio": "^1.0.0-rc.12", "crypto-js": "^4.1.1", - "form-data": "^4.0.0", - "node-fetch": "^3.3.2" + "form-data": "^4.0.0" } } diff --git a/src/fetchers/fetch.ts b/src/fetchers/fetch.ts new file mode 100644 index 0000000..d797c4f --- /dev/null +++ b/src/fetchers/fetch.ts @@ -0,0 +1,22 @@ +/** + * This file is a very relaxed definition of the fetch api + * Only containing what we need for it to function. + */ + +export type FetchOps = { + headers: Record; + method: string; + body: any; +}; + +export type FetchHeaders = { + get(key: string): string | undefined; +}; + +export type FetchReply = { + text(): Promise; + json(): Promise; + headers: FetchHeaders; +}; + +export type FetchType = (url: string, ops?: FetchOps) => Promise; diff --git a/src/fetchers/simpleProxy.ts b/src/fetchers/simpleProxy.ts index 94e6b28..70b8602 100644 --- a/src/fetchers/simpleProxy.ts +++ b/src/fetchers/simpleProxy.ts @@ -1,6 +1,5 @@ -import fetch from 'node-fetch'; - import { makeFullUrl } from '@/fetchers/common'; +import { FetchType } from '@/fetchers/fetch'; import { makeStandardFetcher } from '@/fetchers/standardFetch'; import { Fetcher } from '@/fetchers/types'; @@ -10,7 +9,7 @@ const headerMap: Record = { origin: 'X-Origin', }; -export function makeSimpleProxyFetcher(proxyUrl: string, f: typeof fetch): Fetcher { +export function makeSimpleProxyFetcher(proxyUrl: string, f: FetchType): Fetcher { const fetcher = makeStandardFetcher(f); const proxiedFetch: Fetcher = async (url, ops) => { const fullUrl = makeFullUrl(url, ops); diff --git a/src/fetchers/standardFetch.ts b/src/fetchers/standardFetch.ts index 5110d0e..1f0d105 100644 --- a/src/fetchers/standardFetch.ts +++ b/src/fetchers/standardFetch.ts @@ -1,10 +1,9 @@ -import fetch from 'node-fetch'; - import { serializeBody } from '@/fetchers/body'; import { makeFullUrl } from '@/fetchers/common'; +import { FetchType } from '@/fetchers/fetch'; import { Fetcher } from '@/fetchers/types'; -export function makeStandardFetcher(f: typeof fetch): Fetcher { +export function makeStandardFetcher(f: FetchType): Fetcher { const normalFetch: Fetcher = async (url, ops) => { const fullUrl = makeFullUrl(url, ops); const seralizedBody = serializeBody(ops.body);