From d837eb3e99877769a72d498b22b267146e4f1300 Mon Sep 17 00:00:00 2001 From: spiral Date: Thu, 7 Apr 2022 06:30:51 -0400 Subject: [PATCH] feat: add API ratelimting --- src/api/index.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/api/index.ts b/src/api/index.ts index 7f95846e..d63bd690 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -5,6 +5,15 @@ const baseUrl = () => localStorage.isBeta ? "https://api.beta.pluralkit.me" : "h const methods = ['get', 'post', 'delete', 'patch', 'put']; const noop = () => {}; +const scheduled = []; +const runAPI = () => { + if (scheduled.length == 0) return; + const {axiosData, res, rej} = scheduled.shift(); + axios(axiosData).then((resp) => res(parseData(resp.status, resp.data))).catch(rej); +} + +setInterval(runAPI, 500); + export default function() { const route = []; const handler = { @@ -12,7 +21,7 @@ export default function() { if (route.length == 0 && name != "private") route.push("v2"); if (methods.includes(name)) { - return ({ data = undefined, auth = true, token = null, query = null } = {}) => new Promise((res, rej) => axios({ + return ({ data = undefined, auth = true, token = null, query = null } = {}) => new Promise((res, rej) => scheduled.push({ res, rej, axiosData: { url: baseUrl() + "/" + route.join("/") + (query ? `?${Object.keys(query).map(x => `${x}=${query[x]}`).join("&")}` : ""), method: name, headers: { @@ -21,7 +30,7 @@ export default function() { }, data: !!data ? JSON.stringify(data) : undefined, validateStatus: () => true, - }).then((resp) => res(parseData(resp.status, resp.data))).catch(rej)); + }})); } route.push(name); return new Proxy(noop, handler);