mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 09:33:32 +01:00
REST API speed improvement (#1577)
This commit is contained in:
@@ -50,13 +50,6 @@ class Client extends EventEmitter {
|
||||
*/
|
||||
this.rest = new RESTManager(this);
|
||||
|
||||
/**
|
||||
* API shortcut
|
||||
* @type {Object}
|
||||
* @private
|
||||
*/
|
||||
this.api = this.rest.api;
|
||||
|
||||
/**
|
||||
* The data manager of the client
|
||||
* @type {ClientDataManager}
|
||||
@@ -198,6 +191,15 @@ class Client extends EventEmitter {
|
||||
return this.ws.connection ? this.ws.connection.lastPingTimestamp : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* API shortcut
|
||||
* @type {Object}
|
||||
* @private
|
||||
*/
|
||||
get api() {
|
||||
return this.rest.api;
|
||||
}
|
||||
|
||||
/**
|
||||
* Current status of the client's connection to Discord
|
||||
* @type {?Status}
|
||||
@@ -330,7 +332,7 @@ class Client extends EventEmitter {
|
||||
*/
|
||||
fetchUser(id, cache = true) {
|
||||
if (this.users.has(id)) return Promise.resolve(this.users.get(id));
|
||||
return this.api.users(id).get().then(data =>
|
||||
return this.api.users[id].get().then(data =>
|
||||
cache ? this.dataManager.newUser(data) : new User(this, data)
|
||||
);
|
||||
}
|
||||
@@ -342,7 +344,7 @@ class Client extends EventEmitter {
|
||||
*/
|
||||
fetchInvite(invite) {
|
||||
const code = this.resolver.resolveInviteCode(invite);
|
||||
return this.api.invites(code).get({ query: { with_counts: true } })
|
||||
return this.api.invites[code].get({ query: { with_counts: true } })
|
||||
.then(data => new Invite(this, data));
|
||||
}
|
||||
|
||||
@@ -353,7 +355,7 @@ class Client extends EventEmitter {
|
||||
* @returns {Promise<Webhook>}
|
||||
*/
|
||||
fetchWebhook(id, token) {
|
||||
return this.api.webhooks(id, token).get().then(data => new Webhook(this, data));
|
||||
return this.api.webhooks.opts(id, token).get().then(data => new Webhook(this, data));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -34,13 +34,6 @@ class WebhookClient extends Webhook {
|
||||
*/
|
||||
this.rest = new RESTManager(this);
|
||||
|
||||
/**
|
||||
* API shortcut
|
||||
* @type {Object}
|
||||
* @private
|
||||
*/
|
||||
this.api = this.rest.api;
|
||||
|
||||
/**
|
||||
* The data resolver of the client
|
||||
* @type {ClientDataResolver}
|
||||
@@ -63,6 +56,15 @@ class WebhookClient extends Webhook {
|
||||
this._intervals = new Set();
|
||||
}
|
||||
|
||||
/**
|
||||
* API shortcut
|
||||
* @type {Object}
|
||||
* @private
|
||||
*/
|
||||
get api() {
|
||||
return this.rest.api;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a timeout that will be automatically cancelled if the client is destroyed.
|
||||
* @param {Function} fn Function to execute
|
||||
|
||||
@@ -1,37 +1,30 @@
|
||||
const util = require('util');
|
||||
|
||||
const methods = ['get', 'post', 'delete', 'patch', 'put'];
|
||||
// Paramable exists so we don't return a function unless we actually need one #savingmemory
|
||||
const paramable = [
|
||||
'channels', 'users', 'guilds', 'members',
|
||||
'bans', 'emojis', 'pins', 'permissions',
|
||||
'reactions', 'webhooks', 'messages',
|
||||
'notes', 'roles', 'applications',
|
||||
'invites', 'bot',
|
||||
const reflectors = [
|
||||
'toString', 'valueOf', 'inspect', 'constructor',
|
||||
Symbol.toPrimitive, util.inspect.custom,
|
||||
];
|
||||
const reflectors = ['toString', 'valueOf', 'inspect', Symbol.toPrimitive, util.inspect.custom];
|
||||
|
||||
module.exports = restManager => {
|
||||
const handler = {
|
||||
get(list, name) {
|
||||
if (reflectors.includes(name)) return () => list.join('/');
|
||||
if (paramable.includes(name)) {
|
||||
if (name === 'opts') {
|
||||
function toReturn(...args) { // eslint-disable-line no-inner-declarations
|
||||
list = list.concat(name);
|
||||
for (const arg of args) {
|
||||
if (arg !== null && typeof arg !== 'undefined') list = list.concat(arg);
|
||||
}
|
||||
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, `${list.join('/')}/${name}`, options);
|
||||
toReturn[method] = options => restManager.request(method, directJoin(), options);
|
||||
}
|
||||
return toReturn;
|
||||
}
|
||||
if (reflectors.includes(name)) return () => list.join('/');
|
||||
if (methods.includes(name)) return options => restManager.request(name, list.join('/'), options);
|
||||
return new Proxy(list.concat(name), handler);
|
||||
list.push(name);
|
||||
return new Proxy(list, handler);
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -3,12 +3,18 @@
|
||||
* @extends Error
|
||||
*/
|
||||
class DiscordAPIError extends Error {
|
||||
constructor(error) {
|
||||
constructor(path, error) {
|
||||
super();
|
||||
const flattened = error.errors ? `\n${this.constructor.flattenErrors(error.errors).join('\n')}` : '';
|
||||
this.name = 'DiscordAPIError';
|
||||
this.message = `${error.message}${flattened}`;
|
||||
|
||||
/**
|
||||
* The path of the request relative to the HTTP endpoint
|
||||
* @type {string}
|
||||
*/
|
||||
this.path = path;
|
||||
|
||||
/**
|
||||
* HTTP error code returned by Discord
|
||||
* @type {number}
|
||||
|
||||
@@ -12,8 +12,10 @@ class RESTManager {
|
||||
this.userAgentManager = new UserAgentManager(this);
|
||||
this.rateLimitedEndpoints = {};
|
||||
this.globallyRateLimited = false;
|
||||
}
|
||||
|
||||
this.api = mountApi(this);
|
||||
get api() {
|
||||
return mountApi(this);
|
||||
}
|
||||
|
||||
destroy() {
|
||||
|
||||
@@ -41,7 +41,7 @@ class BurstRequestHandler extends RequestHandler {
|
||||
this.resetTimeout = null;
|
||||
}, Number(res.headers['retry-after']) + this.client.options.restTimeOffset);
|
||||
} else {
|
||||
item.reject(err.status === 400 ? new DiscordAPIError(res.body) : err);
|
||||
item.reject(err.status === 400 ? new DiscordAPIError(res.request.path, res.body) : err);
|
||||
this.handle();
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -65,7 +65,7 @@ class SequentialRequestHandler extends RequestHandler {
|
||||
}, Number(res.headers['retry-after']) + this.restManager.client.options.restTimeOffset);
|
||||
if (res.headers['x-ratelimit-global']) this.globalLimit = true;
|
||||
} else {
|
||||
item.reject(err.status >= 400 && err.status < 500 ? new DiscordAPIError(res.body) : err);
|
||||
item.reject(err.status >= 400 && err.status < 500 ? new DiscordAPIError(res.request.path, res.body) : err);
|
||||
resolve(err);
|
||||
}
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user