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

internally handle rate-limit #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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