mirror of
https://github.com/discordjs/discord.js.git
synced 2026-06-01 16:40:07 +00:00
* tinify webpack * meme * fix long version * more changes * even smoler * fix up logic * fix build * undo changes to user agent manager because its not webpack'd anymore * the heck * fix stupid * clean up browser rules * typo
74 lines
2.0 KiB
JavaScript
74 lines
2.0 KiB
JavaScript
const UserAgentManager = require('./UserAgentManager');
|
|
const handlers = require('./handlers');
|
|
const APIRequest = require('./APIRequest');
|
|
const routeBuilder = require('./APIRouter');
|
|
const { Error } = require('../errors');
|
|
const { Endpoints, browser } = require('../util/Constants');
|
|
|
|
class RESTManager {
|
|
constructor(client, tokenPrefix = 'Bot') {
|
|
this.client = client;
|
|
this.handlers = {};
|
|
if (!browser) this.userAgentManager = new UserAgentManager(this);
|
|
this.rateLimitedEndpoints = {};
|
|
this.globallyRateLimited = false;
|
|
this.tokenPrefix = tokenPrefix;
|
|
this.versioned = true;
|
|
}
|
|
|
|
get api() {
|
|
return routeBuilder(this);
|
|
}
|
|
|
|
getAuth() {
|
|
const token = this.client.token || this.client.accessToken;
|
|
const prefixed = !!this.client.application || (this.client.user && this.client.user.bot);
|
|
if (token && prefixed) return `${this.tokenPrefix} ${token}`;
|
|
else if (token) return token;
|
|
throw new Error('TOKEN_MISSING');
|
|
}
|
|
|
|
get cdn() {
|
|
return Endpoints.CDN(this.client.options.http.cdn);
|
|
}
|
|
|
|
destroy() {
|
|
for (const handler of Object.values(this.handlers)) {
|
|
if (handler.destroy) handler.destroy();
|
|
}
|
|
}
|
|
|
|
push(handler, apiRequest) {
|
|
return new Promise((resolve, reject) => {
|
|
handler.push({
|
|
request: apiRequest,
|
|
resolve,
|
|
reject,
|
|
});
|
|
});
|
|
}
|
|
|
|
getRequestHandler() {
|
|
const method = this.client.options.apiRequestMethod;
|
|
if (typeof method === 'function') return method;
|
|
const handler = handlers[method];
|
|
if (!handler) throw new Error('RATELIMIT_INVALID_METHOD');
|
|
return handler;
|
|
}
|
|
|
|
request(method, url, options = {}) {
|
|
const apiRequest = new APIRequest(this, method, url, options);
|
|
if (!this.handlers[apiRequest.route]) {
|
|
this.handlers[apiRequest.route] = new handlers.RequestHandler(this, this.getRequestHandler());
|
|
}
|
|
|
|
return this.push(this.handlers[apiRequest.route], apiRequest);
|
|
}
|
|
|
|
set endpoint(endpoint) {
|
|
this.client.options.http.api = endpoint;
|
|
}
|
|
}
|
|
|
|
module.exports = RESTManager;
|