Skip to content

Commit 9ad751b

Browse files
author
Theo Gravity
committed
Use abort signal instead
1 parent 85813ae commit 9ad751b

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

Diff for: src/DefaultRESTClient.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import IRESTClient, { ErrorResponseHandler, ResponseHandler } from "./IRESTClient";
1818
import ClientResponse from "./ClientResponse";
19-
import fetch, { BodyInit, RequestCredentials, Response } from 'node-fetch';
19+
import fetch, { BodyInit, RequestCredentials, Response } from "node-fetch";
2020
import { URLSearchParams } from "url";
2121

2222
/**
@@ -33,6 +33,7 @@ export default class DefaultRESTClient<RT, ERT> implements IRESTClient<RT, ERT>
3333
public credentials: RequestCredentials;
3434
public responseHandler: ResponseHandler<RT> = DefaultRESTClient.JSONResponseHandler;
3535
public errorResponseHandler: ErrorResponseHandler<ERT> = DefaultRESTClient.ErrorJSONResponseHandler;
36+
public abortSignal: AbortSignal = undefined;
3637

3738
constructor(public host: string) {
3839
}
@@ -202,6 +203,15 @@ export default class DefaultRESTClient<RT, ERT> implements IRESTClient<RT, ERT>
202203
return this;
203204
}
204205

206+
/**
207+
* Sets the AbortSignal for the request.
208+
* @param signal The AbortSignal to use to abort the request.
209+
*/
210+
withAbortSignal(signal: AbortSignal): DefaultRESTClient<RT, ERT> {
211+
this.abortSignal = signal;
212+
return this;
213+
}
214+
205215
/**
206216
* Run the request and return a promise. This promise will resolve if the request is successful
207217
* and reject otherwise.
@@ -219,8 +229,7 @@ export default class DefaultRESTClient<RT, ERT> implements IRESTClient<RT, ERT>
219229
body: this.body as BodyInit,
220230
// @ts-ignore (Credentials are not supported on NodeJS)
221231
credentials: this.credentials,
222-
// 60-second timeout expressed in ms
223-
timeout: 60000,
232+
signal: this.abortSignal,
224233
},
225234
);
226235

Diff for: tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"declaration": true,
66
"sourceMap": true,
77
"lib": [
8-
"es2015"
8+
"es2015",
9+
"dom"
910
],
1011
"outDir": "build",
1112
"types": [

0 commit comments

Comments
 (0)