Skip to content

Commit

Permalink
feat: add optional timeout to auction requests
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloReszczynski committed Jul 17, 2024
1 parent 7321da8 commit 1be6295
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
Binary file modified bun.lockb
Binary file not shown.
10 changes: 10 additions & 0 deletions src/lib/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ class APIClient {
}
}

private setupTimeoutSignal(config: Config): AbortSignal | undefined {
if (config.timeout != null) {
const controller = new AbortController();
setTimeout(() => controller.abort(), config.timeout);
return controller.signal;
}
}

public async post(endpoint: string, body: unknown, config: Config): Promise<unknown> {
const signal = this.setupTimeoutSignal(config);
return this.request(endpoint, {
method: "POST",
headers: {
Expand All @@ -46,6 +55,7 @@ class APIClient {
Authorization: `Bearer ${config.apiKey}`,
},
body: JSON.stringify(body),
signal,
});
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/types/shared.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export interface Config {
/// The API key to use for requests.
apiKey: string;
/// An optional alternative host to use for requests.
host?: string;
/// An optional timeout for requests in milliseconds.
timeout?: number;
}

0 comments on commit 1be6295

Please sign in to comment.