diff --git a/src/fetch.ts b/src/fetch.ts index e48351e0..6c85c4ec 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -16,6 +16,7 @@ export interface CreateFetchOptions { defaults?: FetchOptions; fetch?: Fetch; Headers?: typeof Headers; + AbortController?: typeof AbortController; } export type FetchRequest = RequestInfo; @@ -88,8 +89,11 @@ const retryStatusCodes = new Set([ ]); export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch { - const { fetch = globalThis.fetch, Headers = globalThis.Headers } = - globalOptions; + const { + fetch = globalThis.fetch, + Headers = globalThis.Headers, + AbortController = globalThis.AbortController, + } = globalOptions; async function onError(context: FetchContext): Promise> { // Is Abort diff --git a/src/index.ts b/src/index.ts index 8d68d839..4893fbf1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,6 +24,7 @@ export const fetch = (() => Promise.reject(new Error("[ofetch] global.fetch is not supported!"))); export const Headers = _globalThis.Headers; +export const AbortController = _globalThis.AbortController; -export const ofetch = createFetch({ fetch, Headers }); +export const ofetch = createFetch({ fetch, Headers, AbortController }); export const $fetch = ofetch; diff --git a/src/node.ts b/src/node.ts index 0b2d45d2..9aa4a9dd 100644 --- a/src/node.ts +++ b/src/node.ts @@ -1,6 +1,9 @@ import http from "node:http"; import https, { AgentOptions } from "node:https"; -import nodeFetch, { Headers as _Headers } from "node-fetch-native"; +import nodeFetch, { + Headers as _Headers, + AbortController as _AbortController, +} from "node-fetch-native"; import { createFetch } from "./base"; @@ -33,6 +36,7 @@ export function createNodeFetch() { export const fetch = globalThis.fetch || createNodeFetch(); export const Headers = globalThis.Headers || _Headers; +export const AbortController = globalThis.AbortController || _AbortController; -export const ofetch = createFetch({ fetch, Headers }); +export const ofetch = createFetch({ fetch, Headers, AbortController }); export const $fetch = ofetch;