Add RequestInit.signal to node-fetch (#35636)

* Add RequestInit.signal to node-fetch

* Make comments more clear.

Co-Authored-By: Niklas Lindgren <nikc@iki.fi>

* Move AbortSignal into its own file

* rename to externals.d.ts per suggestion

* Make more requested changes

* add back files entry, as required by checks

* Add back import/export
This commit is contained in:
Mike Marcacci
2019-06-05 11:24:35 -07:00
committed by Andrew Casey
co-authored by Niklas Lindgren
parent 1e006bf325
commit 879861b8ee
4 changed files with 40 additions and 1 deletions
+21
View File
@@ -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);
}
+2
View File
@@ -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.
+15
View File
@@ -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(
+2 -1
View File
@@ -19,6 +19,7 @@
},
"files": [
"index.d.ts",
"externals.d.ts",
"node-fetch-tests.ts"
]
}
}