2022-03-19 17:37:45 +07:00
|
|
|
'use strict';
|
|
|
|
|
|
2022-08-04 19:27:15 +07:00
|
|
|
const Buffer = require('node:buffer').Buffer;
|
2022-03-19 17:37:45 +07:00
|
|
|
const https = require('node:https');
|
|
|
|
|
const { setTimeout } = require('node:timers');
|
2022-03-24 17:55:32 +07:00
|
|
|
const FormData = require('form-data');
|
2022-08-04 19:27:15 +07:00
|
|
|
const JSONBig = require('json-bigint');
|
2022-03-24 17:55:32 +07:00
|
|
|
const fetch = require('node-fetch');
|
2022-08-29 11:10:47 +07:00
|
|
|
const proxy = require('proxy-agent');
|
2022-03-19 17:37:45 +07:00
|
|
|
|
|
|
|
|
let agent = null;
|
|
|
|
|
|
|
|
|
|
class APIRequest {
|
|
|
|
|
constructor(rest, method, path, options) {
|
|
|
|
|
this.rest = rest;
|
|
|
|
|
this.client = rest.client;
|
|
|
|
|
this.method = method;
|
|
|
|
|
this.route = options.route;
|
|
|
|
|
this.options = options;
|
|
|
|
|
this.retries = 0;
|
|
|
|
|
|
2022-08-04 19:27:15 +07:00
|
|
|
/* Remove
|
2022-03-24 17:55:32 +07:00
|
|
|
const { userAgentSuffix } = this.client.options;
|
2022-06-11 20:13:52 +07:00
|
|
|
this.fullUserAgent = `${randomUA()}${userAgentSuffix.length ? `, ${userAgentSuffix.join(', ')}` : ''}`;
|
2022-08-04 19:27:15 +07:00
|
|
|
*/
|
2022-03-24 17:55:32 +07:00
|
|
|
|
2022-03-19 17:37:45 +07:00
|
|
|
let queryString = '';
|
|
|
|
|
if (options.query) {
|
|
|
|
|
const query = Object.entries(options.query)
|
|
|
|
|
.filter(([, value]) => value !== null && typeof value !== 'undefined')
|
|
|
|
|
.flatMap(([key, value]) => (Array.isArray(value) ? value.map(v => [key, v]) : [[key, value]]));
|
|
|
|
|
queryString = new URLSearchParams(query).toString();
|
|
|
|
|
}
|
|
|
|
|
this.path = `${path}${queryString && `?${queryString}`}`;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-24 17:55:32 +07:00
|
|
|
make() {
|
2022-08-29 11:10:47 +07:00
|
|
|
agent ??=
|
|
|
|
|
typeof this.client.options.proxy === 'string' && this.client.options.proxy.length > 0
|
|
|
|
|
? new proxy(this.client.options.proxy)
|
|
|
|
|
: new https.Agent({ ...this.client.options.http.agent, keepAlive: true });
|
2022-03-19 17:37:45 +07:00
|
|
|
|
|
|
|
|
const API =
|
|
|
|
|
this.options.versioned === false
|
|
|
|
|
? this.client.options.http.api
|
|
|
|
|
: `${this.client.options.http.api}/v${this.client.options.http.version}`;
|
|
|
|
|
const url = API + this.path;
|
|
|
|
|
|
|
|
|
|
let headers = {
|
|
|
|
|
...this.client.options.http.headers,
|
2022-08-04 19:27:15 +07:00
|
|
|
Accept: '*/*',
|
|
|
|
|
'Accept-Language': 'en-US,en;q=0.9',
|
|
|
|
|
'Cache-Control': 'no-cache',
|
|
|
|
|
Pragma: 'no-cache',
|
|
|
|
|
'Sec-Ch-Ua': `"Not A;Brand";v="99", "Chromium";v="${
|
|
|
|
|
this.client.options.ws.properties.browser_version.split('.')[0]
|
|
|
|
|
}", "Google Chrome";v="${this.client.options.ws.properties.browser_version.split('.')[0]}`,
|
|
|
|
|
'Sec-Ch-Ua-Mobile': '?0',
|
|
|
|
|
'Sec-Ch-Ua-Platform': '"Windows"',
|
|
|
|
|
'Sec-Fetch-Dest': 'empty',
|
|
|
|
|
'Sec-Fetch-Mode': 'cors',
|
|
|
|
|
'Sec-Fetch-Site': 'same-origin',
|
|
|
|
|
'X-Debug-Options': 'bugReporterEnabled',
|
|
|
|
|
'X-Super-Properties': `${Buffer.from(JSONBig.stringify(this.client.options.ws.properties), 'ascii').toString(
|
|
|
|
|
'base64',
|
|
|
|
|
)}`,
|
|
|
|
|
'X-Discord-Locale': 'en-US',
|
|
|
|
|
'User-Agent': this.client.options.http.headers['User-Agent'],
|
2022-03-19 17:37:45 +07:00
|
|
|
};
|
|
|
|
|
|
2022-08-04 19:27:15 +07:00
|
|
|
/* Remove
|
2022-05-13 18:17:22 +07:00
|
|
|
this.client.options.http.headers['User-Agent'] = this.fullUserAgent;
|
2022-08-04 19:27:15 +07:00
|
|
|
*/
|
2022-05-13 18:17:22 +07:00
|
|
|
|
2022-03-19 17:37:45 +07:00
|
|
|
if (this.options.auth !== false) headers.Authorization = this.rest.getAuth();
|
|
|
|
|
if (this.options.reason) headers['X-Audit-Log-Reason'] = encodeURIComponent(this.options.reason);
|
|
|
|
|
if (this.options.headers) headers = Object.assign(headers, this.options.headers);
|
|
|
|
|
|
|
|
|
|
let body;
|
|
|
|
|
if (this.options.files?.length) {
|
2022-03-24 17:55:32 +07:00
|
|
|
body = new FormData();
|
|
|
|
|
for (const [index, file] of this.options.files.entries()) {
|
|
|
|
|
if (file?.file) body.append(file.key ?? `files[${index}]`, file.file, file.name);
|
|
|
|
|
}
|
|
|
|
|
if (typeof this.options.data !== 'undefined') {
|
|
|
|
|
if (this.options.dontUsePayloadJSON) {
|
|
|
|
|
for (const [key, value] of Object.entries(this.options.data)) body.append(key, value);
|
|
|
|
|
} else {
|
|
|
|
|
body.append('payload_json', JSON.stringify(this.options.data));
|
|
|
|
|
}
|
2022-06-13 23:53:43 +07:00
|
|
|
} else if (typeof this.options.body !== 'undefined') {
|
|
|
|
|
if (this.options.dontUsePayloadJSON) {
|
|
|
|
|
for (const [key, value] of Object.entries(this.options.body)) body.append(key, value);
|
|
|
|
|
} else {
|
|
|
|
|
body.append('payload_json', JSON.stringify(this.options.body));
|
|
|
|
|
}
|
2022-03-24 17:55:32 +07:00
|
|
|
}
|
|
|
|
|
headers = Object.assign(headers, body.getHeaders());
|
|
|
|
|
// eslint-disable-next-line eqeqeq
|
2022-06-13 19:13:03 +07:00
|
|
|
} else if (this.options.data != null) {
|
2022-04-25 19:28:45 +07:00
|
|
|
body = this.options.data ? JSON.stringify(this.options.data) : undefined;
|
2022-03-24 17:55:32 +07:00
|
|
|
headers['Content-Type'] = 'application/json';
|
2022-03-25 12:37:56 +07:00
|
|
|
} else if (this.options.body != null) {
|
2022-03-25 19:40:13 +07:00
|
|
|
body = new FormData();
|
2022-03-25 12:37:56 +07:00
|
|
|
body.append('payload_json', JSON.stringify(this.options.body));
|
|
|
|
|
headers = Object.assign(headers, body.getHeaders());
|
2022-03-24 17:55:32 +07:00
|
|
|
}
|
2022-03-19 17:37:45 +07:00
|
|
|
|
|
|
|
|
const controller = new AbortController();
|
|
|
|
|
const timeout = setTimeout(() => controller.abort(), this.client.options.restRequestTimeout).unref();
|
|
|
|
|
return fetch(url, {
|
|
|
|
|
method: this.method,
|
|
|
|
|
headers,
|
|
|
|
|
agent,
|
|
|
|
|
body,
|
|
|
|
|
signal: controller.signal,
|
|
|
|
|
}).finally(() => clearTimeout(timeout));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-24 17:55:32 +07:00
|
|
|
module.exports = APIRequest;
|