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

feat: add optional timeout to auction requests #11

Merged
merged 4 commits into from
Aug 2, 2024
Merged
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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ const config = {
// generate your api key in the auction manager - it should look some thing like this
// note: this is an invalid key and won't work, you need to replace it with your own
apiKey: "TSE_4S6o1g1CB5tyRENfhDMAn6viR7A5cy3j1JAR",
userAgent?: "Mozilla/5.0" // optional user agent to be added as part of the request
userAgent: "Mozilla/5.0"
timeout: 50
};

createAuction(config, auctionDetails)
Expand All @@ -71,6 +72,11 @@ createAuction(config, auctionDetails)

`config`: An object containing configuration details including the API key. Please refer to [Auction Manager](https://app.topsort.com/new/en-US/marketplace/account-settings/api-integration)

##### Config Parameters
- `apiKey`: Your Topsort API Key
- `userAgent`: Optional user agent to be added as part of the request. Example: `Mozilla/5.0`
- `timeout`: Optional timeout in milliseconds. Default is 30 seconds. If timeout is reached, the call will be rejected with an [AbortError](https://developer.mozilla.org/en-US/docs/Web/API/DOMException#aborterror).

`auctionDetails`: An object containing the details of the auction to be created, please refer to [Topsort's Auction API doc](https://docs.topsort.com/reference/createauctions) for body specification.

#### Sample response
Expand Down
10 changes: 10 additions & 0 deletions src/lib/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,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 @@ -49,6 +58,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,5 +1,9 @@
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;
userAgent?: string;
}