Skip to content

Commit

Permalink
feat: internally handle rate-limit
Browse files Browse the repository at this point in the history
fix #15
  • Loading branch information
Xiphe committed Jan 15, 2024
1 parent 115989e commit 9882473
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ class Mailjs {
private token: string;
id: string;
address: string;
rateLimitRetries: number;

constructor() {
constructor({ rateLimitRetries }: { rateLimitRetries?: number } = {}) {
this.baseUrl = "https://api.mail.tm";
this.baseMercure = "https://mercure.mail.tm/.well-known/mercure";
this.listener = null;
this.rateLimitRetries = rateLimitRetries || 3;
this.events = {};
this.token = "";
this.id = "";
Expand Down Expand Up @@ -235,7 +237,8 @@ class Mailjs {
async _send(
path: string,
method: type.Methods = "GET",
body?: object
body?: object,
retry: number = 0
): type.PromiseResult<any> {
const options: type.IRequestObject = {
method,
Expand All @@ -252,6 +255,12 @@ class Mailjs {
}

const res: Response = await fetch(this.baseUrl + path, options);

if (res.status === 429 && retry < this.rateLimitRetries) {
await new Promise((resolve) => setTimeout(resolve, 1000));
return this._send(path, method, body, retry + 1);
}

let data: any;

const contentType = res.headers.get("content-type");
Expand Down

0 comments on commit 9882473

Please sign in to comment.