Skip to content

Commit

Permalink
Merge pull request #30 from churchtools/timeout-for-calls
Browse files Browse the repository at this point in the history
added timeout for single requests
  • Loading branch information
niklasarnitz authored May 14, 2024
2 parents 03319dd + 7cb5eed commit 20749fe
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/churchtoolsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type PageResponse<Result> = {

type Resolver<Result> = (result: Result | PromiseLike<Result>) => void;

type RequestOptions = { enforceJSON?: boolean; needsAuthentication?: boolean };
type RequestOptions = { enforceJSON?: boolean; needsAuthentication?: boolean; timeout?: number };

type GetOptions = RequestOptions & { rawResponse?: boolean; callDeferred?: boolean };
type PutOptions = RequestOptions;
Expand Down Expand Up @@ -170,13 +170,13 @@ class ChurchToolsClient {
}
}

getAbortSignal(abortController?: AbortController) {
getAbortSignal(abortController?: AbortController, timeout?: number) {
if (!abortController) {
abortController = new AbortController();
}
setTimeout(() => {
abortController!.abort();
}, this.requestTimeout);
}, timeout ?? this.requestTimeout);
return abortController!.signal;
}

Expand Down Expand Up @@ -356,7 +356,7 @@ class ChurchToolsClient {
...data,
[ENFORCE_JSON_PARAM]: options?.enforceJSON,
},
{ signal: this.getAbortSignal(), headers }
{ signal: this.getAbortSignal(undefined, options.timeout), headers }
)
.then((response) => {
resolve(this.responseToData(response));
Expand Down Expand Up @@ -404,7 +404,7 @@ class ChurchToolsClient {
'CSRF-Token': this.csrfToken ?? '',
};
}
config.signal = this.getAbortSignal(options.abortController);
config.signal = this.getAbortSignal(options.abortController, options.timeout);

return this.ax.post(
this.buildUrl(uri),
Expand Down Expand Up @@ -442,9 +442,9 @@ class ChurchToolsClient {
this.buildUrl(uri),
{
...data,
[ENFORCE_JSON_PARAM]: options?.enforceJSON,
[ENFORCE_JSON_PARAM]: options.enforceJSON,
},
{ signal: this.getAbortSignal(), headers }
{ signal: this.getAbortSignal(undefined, options.timeout), headers }
)
.then((response) => {
resolve(this.responseToData(response));
Expand All @@ -469,7 +469,7 @@ class ChurchToolsClient {
this.ax
.delete(this.buildUrl(uri), {
data: { ...data, [ENFORCE_JSON_PARAM]: options?.enforceJSON },
signal: this.getAbortSignal(),
signal: this.getAbortSignal(undefined, options?.timeout),
headers,
})
.then((response) => {
Expand Down

0 comments on commit 20749fe

Please sign in to comment.