diff --git a/types/node-fetch/externals.d.ts b/types/node-fetch/externals.d.ts new file mode 100644 index 0000000000..6695fc470f --- /dev/null +++ b/types/node-fetch/externals.d.ts @@ -0,0 +1,21 @@ +// `AbortSignal` is defined here to prevent a dependency on a particular +// implementation like the `abort-controller` package, and to avoid requiring +// the `dom` library in `tsconfig.json`. + +export interface AbortSignal { + aborted: boolean; + + addEventListener: (type: "abort", listener: ((this: AbortSignal, event: any) => any), options?: boolean | { + capture?: boolean, + once?: boolean, + passive?: boolean + }) => void; + + removeEventListener: (type: "abort", listener: ((this: AbortSignal, event: any) => any), options?: boolean | { + capture?: boolean + }) => void; + + dispatchEvent: (event: any) => boolean; + + onabort?: null | ((this: AbortSignal, event: any) => void); +} diff --git a/types/node-fetch/index.d.ts b/types/node-fetch/index.d.ts index c5b5c707a3..f35c13f2a0 100644 --- a/types/node-fetch/index.d.ts +++ b/types/node-fetch/index.d.ts @@ -13,6 +13,7 @@ import { Agent } from "http"; import { URLSearchParams } from "url"; +import { AbortSignal } from "./externals"; export class Request extends Body { constructor(input: string | { href: string } | Request, init?: RequestInit); @@ -42,6 +43,7 @@ export interface RequestInit { headers?: HeadersInit; method?: string; redirect?: RequestRedirect; + signal?: AbortSignal | null; // node-fetch extensions agent?: Agent; // =null http.Agent instance, allows custom proxy, certificate etc. diff --git a/types/node-fetch/node-fetch-tests.ts b/types/node-fetch/node-fetch-tests.ts index 9dfb3179e4..3f42000022 100644 --- a/types/node-fetch/node-fetch-tests.ts +++ b/types/node-fetch/node-fetch-tests.ts @@ -50,6 +50,21 @@ function test_fetchUrlWithRequestObject() { method: "POST", headers: { "Content-Type": "application/json" + }, + signal: { + aborted: false, + + addEventListener: (type: "abort", listener: ((event: any) => any), options?: boolean | { + capture?: boolean, + once?: boolean, + passive?: boolean + }) => undefined, + + removeEventListener: (type: "abort", listener: ((event: any) => any), options?: boolean | { + capture?: boolean + }) => undefined, + + dispatchEvent: (event: any) => false } }; const request: Request = new Request( diff --git a/types/node-fetch/tsconfig.json b/types/node-fetch/tsconfig.json index d349bf9665..50f65d691a 100644 --- a/types/node-fetch/tsconfig.json +++ b/types/node-fetch/tsconfig.json @@ -19,6 +19,7 @@ }, "files": [ "index.d.ts", + "externals.d.ts", "node-fetch-tests.ts" ] -} \ No newline at end of file +}