Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add withAbortSignal() #93

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/DefaultRESTClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

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

constructor(public host: string) {
}
Expand Down Expand Up @@ -202,6 +203,15 @@ export default class DefaultRESTClient<RT, ERT> implements IRESTClient<RT, ERT>
return this;
}

/**
* Sets the AbortSignal for the request.
* @param signal The AbortSignal to use to abort the request.
*/
withAbortSignal(signal: AbortSignal): DefaultRESTClient<RT, ERT> {
this.abortSignal = signal;
return this;
}

/**
* Run the request and return a promise. This promise will resolve if the request is successful
* and reject otherwise.
Expand All @@ -219,6 +229,7 @@ export default class DefaultRESTClient<RT, ERT> implements IRESTClient<RT, ERT>
body: this.body as BodyInit,
// @ts-ignore (Credentials are not supported on NodeJS)
credentials: this.credentials,
signal: this.abortSignal,
},
);

Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"declaration": true,
"sourceMap": true,
"lib": [
"es2015"
"es2015",
"dom"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

required to add the AbortSignal type

],
"outDir": "build",
"types": [
Expand Down