buddy

node MVC discord bot
Log | Files | Refs | README

APIRequest.js (2406B)


      1 'use strict';
      2 
      3 const https = require('https');
      4 const FormData = require('@discordjs/form-data');
      5 const AbortController = require('abort-controller');
      6 const fetch = require('node-fetch');
      7 const { browser, UserAgent } = require('../util/Constants');
      8 
      9 if (https.Agent) var agent = new https.Agent({ keepAlive: true });
     10 
     11 class APIRequest {
     12   constructor(rest, method, path, options) {
     13     this.rest = rest;
     14     this.client = rest.client;
     15     this.method = method;
     16     this.route = options.route;
     17     this.options = options;
     18 
     19     let queryString = '';
     20     if (options.query) {
     21       // Filter out undefined query options
     22       const query = Object.entries(options.query).filter(([, value]) => value !== null && typeof value !== 'undefined');
     23       queryString = new URLSearchParams(query).toString();
     24     }
     25     this.path = `${path}${queryString && `?${queryString}`}`;
     26   }
     27 
     28   make() {
     29     const API =
     30       this.options.versioned === false
     31         ? this.client.options.http.api
     32         : `${this.client.options.http.api}/v${this.client.options.http.version}`;
     33     const url = API + this.path;
     34     let headers = {};
     35 
     36     if (this.options.auth !== false) headers.Authorization = this.rest.getAuth();
     37     if (this.options.reason) headers['X-Audit-Log-Reason'] = encodeURIComponent(this.options.reason);
     38     if (!browser) headers['User-Agent'] = UserAgent;
     39     if (this.options.headers) headers = Object.assign(headers, this.options.headers);
     40 
     41     let body;
     42     if (this.options.files && this.options.files.length) {
     43       body = new FormData();
     44       for (const file of this.options.files) if (file && file.file) body.append(file.name, file.file, file.name);
     45       if (typeof this.options.data !== 'undefined') body.append('payload_json', JSON.stringify(this.options.data));
     46       if (!browser) headers = Object.assign(headers, body.getHeaders());
     47       // eslint-disable-next-line eqeqeq
     48     } else if (this.options.data != null) {
     49       body = JSON.stringify(this.options.data);
     50       headers['Content-Type'] = 'application/json';
     51     }
     52 
     53     const controller = new AbortController();
     54     const timeout = this.client.setTimeout(() => controller.abort(), this.client.options.restRequestTimeout);
     55     return fetch(url, {
     56       method: this.method,
     57       headers,
     58       agent,
     59       body,
     60       signal: controller.signal,
     61     }).finally(() => this.client.clearTimeout(timeout));
     62   }
     63 }
     64 
     65 module.exports = APIRequest;