Files
discord.js/src/client/rest/APIRouter.js
Crawl 4adecf4aef 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
2017-07-27 03:04:03 +02:00

35 lines
1.0 KiB
JavaScript

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,
];
function buildRoute(manager) {
const route = [''];
const handler = {
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));
}
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);
}
module.exports = buildRoute;