rewrite ratelimiting and api route builder (#1667)

* rewrite ratelimiting and api route builder

* more stuff

* let people pass their own handlers

* Update burst.js

* Update RequestHandler.js

* Update burst.js

* Update sequential.js

* Update RequestHandler.js
This commit is contained in:
Gus Caplan
2017-07-20 19:32:40 -05:00
committed by Crawl
parent 11556c0b3b
commit a2eeafc75d
20 changed files with 187 additions and 316 deletions

View File

@@ -1,32 +1,34 @@
const util = require('util');
const noop = () => {}; // eslint-disable-line no-empty-function
const methods = ['get', 'post', 'delete', 'patch', 'put'];
const reflectors = [
'toString', 'valueOf', 'inspect', 'constructor',
Symbol.toPrimitive, util.inspect.custom,
];
module.exports = restManager => {
function buildRoute(manager) {
const route = [''];
const handler = {
get(list, name) {
if (name === 'opts') {
function toReturn(...args) { // eslint-disable-line no-inner-declarations
list.push(...args.filter(x => x !== null && typeof x !== 'undefined'));
return new Proxy(list, handler);
}
const directJoin = () => `${list.join('/')}/${name}`;
for (const r of reflectors) toReturn[r] = directJoin;
for (const method of methods) {
toReturn[method] = options => restManager.request(method, directJoin(), options);
}
return toReturn;
get(target, name) {
if (reflectors.includes(name)) return () => route.join('/');
if (methods.includes(name)) {
return options => manager.request(name, route.join('/'), Object.assign({
route: route.map((r, i) => {
if (/\d{16,19}/g.test(r)) return /channels|guilds/.test(route[i - 1]) ? r : ':id';
return r;
}).join('/'),
}, options));
}
if (reflectors.includes(name)) return () => list.join('/');
if (methods.includes(name)) return options => restManager.request(name, list.join('/'), options);
list.push(name);
return new Proxy(list, handler);
route.push(name);
return new Proxy(noop, handler);
},
apply(target, _, args) {
route.push(...args.filter(x => x != null)); // eslint-disable-line eqeqeq
return new Proxy(noop, handler);
},
};
return new Proxy(noop, handler);
}
return new Proxy([''], handler);
};
module.exports = buildRoute;