Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Commit

Permalink
feat: add a way to get the last-known rate limit status for a client
Browse files Browse the repository at this point in the history
  • Loading branch information
thislooksfun committed Apr 10, 2022
1 parent 0d1b3b7 commit 20f285f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Credentials } from "./gateway/creds";
import type { Gateway } from "./gateway/gateway";
import type { ClientAuth } from "./gateway/oauth";
import type { RateLimit } from "./gateway/types";
import type { Maybe } from "./helper/types";

import { AnonGateway } from "./gateway/anon";
Expand Down Expand Up @@ -142,6 +143,17 @@ export class Client {
*/
public readonly gateway: Gateway;

/**
* Get the last-known rate limit status for this client.
*
* @note The rate limit status is only updated when a request is made from
* this client. It will also get out of sync if there are multiple clients
* using the same authorization (and thus the same rate limit pool).
*/
get rateLimit(): Maybe<RateLimit> {
return this.gateway.getRateLimit();
}

/**
* Make a new snoots Client.
*
Expand Down
5 changes: 5 additions & 0 deletions src/gateway/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ export abstract class Gateway {
return await this.doPost(path, jsonOptions, query);
}

/** @internal */
public getRateLimit(): Maybe<RateLimit> {
return this.rateLimit ? { ...this.rateLimit } : undefined;
}

protected abstract auth(): Promise<Maybe<Auth>>;

protected async doPost<T>(
Expand Down
2 changes: 1 addition & 1 deletion src/gateway/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
export type { Credentials } from "./creds";
export type { Gateway } from "./gateway";
export type { ClientAuth, TokenAuth, UsernameAuth } from "./oauth";
// export type { Query, QueryValue } from "./types";
export type { RateLimit } from "./types";

0 comments on commit 20f285f

Please sign in to comment.