mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 09:03:29 +01:00
http rewrite (actually works this time!!1!) (#1257)
* rewrite http * browser fun * all the mimes dammit * i need a newline * whoops * forgot about this * use promises and HTTPRequest.method * fluent * move httpclient to external module * branding * middleware * revert middleware
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const request = require('superagent');
|
||||
const snekfetch = require('snekfetch');
|
||||
|
||||
const Constants = require('../util/Constants');
|
||||
const convertToBuffer = require('../util/Util').convertToBuffer;
|
||||
@@ -211,11 +211,9 @@ class ClientDataResolver {
|
||||
if (typeof resource === 'string') {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (/^https?:\/\//.test(resource)) {
|
||||
const req = request.get(resource).set('Content-Type', 'blob');
|
||||
if (this.client.browser) req.responseType('arraybuffer');
|
||||
req.end((err, res) => {
|
||||
snekfetch.get(resource)
|
||||
.end((err, res) => {
|
||||
if (err) return reject(err);
|
||||
if (this.client.browser) return resolve(convertToBuffer(res.xhr.response));
|
||||
if (!(res.body instanceof Buffer)) return reject(new TypeError('The response body isn\'t a Buffer.'));
|
||||
return resolve(res.body);
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const request = require('superagent');
|
||||
const snekfetch = require('snekfetch');
|
||||
const Constants = require('../../util/Constants');
|
||||
|
||||
class APIRequest {
|
||||
@@ -34,17 +34,16 @@ class APIRequest {
|
||||
|
||||
gen() {
|
||||
const API = `${this.client.options.http.host}/api/v${this.client.options.http.version}`;
|
||||
const apiRequest = request[this.method](`${API}${this.path}`);
|
||||
if (this.auth) apiRequest.set('authorization', this.getAuth());
|
||||
const request = snekfetch[this.method](`${API}${this.path}`);
|
||||
if (this.auth) request.set('Authorization', this.getAuth());
|
||||
if (!this.rest.client.browser) request.set('User-Agent', this.rest.userAgentManager.userAgent);
|
||||
if (this.files) {
|
||||
for (const file of this.files) if (file && file.file) apiRequest.attach(file.name, file.file, file.name);
|
||||
this.data = this.data || {};
|
||||
apiRequest.field('payload_json', JSON.stringify(this.data));
|
||||
for (const file of this.files) if (file && file.file) request.attach(file.name, file.file, file.name);
|
||||
if (typeof this.data !== 'undefined') request.attach('payload_json', JSON.stringify(this.data));
|
||||
} else if (this.data) {
|
||||
apiRequest.send(this.data);
|
||||
request.send(this.data);
|
||||
}
|
||||
if (!this.client.browser) apiRequest.set('User-Agent', this.rest.userAgentManager.userAgent);
|
||||
return apiRequest;
|
||||
return request;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const superagent = require('superagent');
|
||||
const snekfetch = require('snekfetch');
|
||||
const Constants = require('./Constants');
|
||||
|
||||
/**
|
||||
@@ -54,7 +54,7 @@ class Util {
|
||||
static fetchRecommendedShards(token, guildsPerShard = 1000) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!token) throw new Error('A token must be provided.');
|
||||
superagent.get(Constants.Endpoints.gateway.bot)
|
||||
snekfetch.get(Constants.Endpoints.gateway.bot)
|
||||
.set('Authorization', `Bot ${token.replace(/^Bot\s*/i, '')}`)
|
||||
.end((err, res) => {
|
||||
if (err) reject(err);
|
||||
|
||||
Reference in New Issue
Block a user