From 85813ae09bdcd25e5a57d39dcaa94df44c16ff5a Mon Sep 17 00:00:00 2001 From: Theo Gravity Date: Tue, 2 Jan 2024 13:31:12 -0800 Subject: [PATCH 1/2] Set request timeout to 60s --- src/DefaultRESTClient.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/DefaultRESTClient.ts b/src/DefaultRESTClient.ts index b64381f..181bfb0 100644 --- a/src/DefaultRESTClient.ts +++ b/src/DefaultRESTClient.ts @@ -219,6 +219,8 @@ export default class DefaultRESTClient implements IRESTClient body: this.body as BodyInit, // @ts-ignore (Credentials are not supported on NodeJS) credentials: this.credentials, + // 60-second timeout expressed in ms + timeout: 60000, }, ); From 9ad751b6fe3b11f4d0b39c6aa8dfa6bba9db886c Mon Sep 17 00:00:00 2001 From: Theo Gravity Date: Tue, 9 Jul 2024 16:48:02 -0700 Subject: [PATCH 2/2] Use abort signal instead --- src/DefaultRESTClient.ts | 15 ++++++++++++--- tsconfig.json | 3 ++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/DefaultRESTClient.ts b/src/DefaultRESTClient.ts index 181bfb0..addc85b 100644 --- a/src/DefaultRESTClient.ts +++ b/src/DefaultRESTClient.ts @@ -16,7 +16,7 @@ import IRESTClient, { ErrorResponseHandler, ResponseHandler } from "./IRESTClient"; import ClientResponse from "./ClientResponse"; -import fetch, { BodyInit, RequestCredentials, Response } from 'node-fetch'; +import fetch, { BodyInit, RequestCredentials, Response } from "node-fetch"; import { URLSearchParams } from "url"; /** @@ -33,6 +33,7 @@ export default class DefaultRESTClient implements IRESTClient public credentials: RequestCredentials; public responseHandler: ResponseHandler = DefaultRESTClient.JSONResponseHandler; public errorResponseHandler: ErrorResponseHandler = DefaultRESTClient.ErrorJSONResponseHandler; + public abortSignal: AbortSignal = undefined; constructor(public host: string) { } @@ -202,6 +203,15 @@ export default class DefaultRESTClient implements IRESTClient return this; } + /** + * Sets the AbortSignal for the request. + * @param signal The AbortSignal to use to abort the request. + */ + withAbortSignal(signal: AbortSignal): DefaultRESTClient { + this.abortSignal = signal; + return this; + } + /** * Run the request and return a promise. This promise will resolve if the request is successful * and reject otherwise. @@ -219,8 +229,7 @@ export default class DefaultRESTClient implements IRESTClient body: this.body as BodyInit, // @ts-ignore (Credentials are not supported on NodeJS) credentials: this.credentials, - // 60-second timeout expressed in ms - timeout: 60000, + signal: this.abortSignal, }, ); diff --git a/tsconfig.json b/tsconfig.json index a171172..5f3c4e7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,8 @@ "declaration": true, "sourceMap": true, "lib": [ - "es2015" + "es2015", + "dom" ], "outDir": "build", "types": [