mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 00:23:30 +01:00
44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
const request = require('superagent');
|
|
const Constants = require('../../util/Constants');
|
|
|
|
class APIRequest {
|
|
constructor(rest, method, url, auth, data, file) {
|
|
this.rest = rest;
|
|
this.method = method;
|
|
this.url = url;
|
|
this.auth = auth;
|
|
this.data = data;
|
|
this.file = file;
|
|
}
|
|
|
|
getBucketName() {
|
|
return `${this.method} ${this.url}`;
|
|
}
|
|
|
|
getAuth() {
|
|
if (this.rest.client.store.token && this.rest.client.store.user && this.rest.client.store.user.bot) {
|
|
return `Bot ${this.rest.client.store.token}`;
|
|
} else if (this.rest.client.store.token) {
|
|
return this.rest.client.store.token;
|
|
}
|
|
throw Constants.Errors.NO_TOKEN;
|
|
}
|
|
|
|
gen() {
|
|
const apiRequest = request[this.method](this.url);
|
|
if (this.auth) {
|
|
apiRequest.set('authorization', this.getAuth());
|
|
}
|
|
if (this.data) {
|
|
apiRequest.send(this.data);
|
|
}
|
|
if (this.file) {
|
|
apiRequest.attach('file', this.file.file, this.file.name);
|
|
}
|
|
apiRequest.set('User-Agent', this.rest.userAgentManager.userAgent);
|
|
return apiRequest;
|
|
}
|
|
}
|
|
|
|
module.exports = APIRequest;
|