Archived
0
0
Fork 0

fix: several bugs

This commit is contained in:
Daryl Ronningen 2021-07-06 14:20:34 -05:00
parent 82b6b5acee
commit 7fa5ea6762
Signed by: Daryl Ronningen
GPG key ID: FD23F0C934A5EC6B
4 changed files with 23 additions and 9 deletions

View file

@ -38,6 +38,7 @@
"abort-controller": "^3.0.0",
"async": "^3.2.0",
"form-data": "^4.0.0",
"lodash": "^4.17.21",
"node-fetch": "^2.6.1",
"ws": "^7.5.2"
},
@ -55,6 +56,7 @@
"@types/async": "^3",
"@types/chai": "^4.2.19",
"@types/eslint": "^7.2.13",
"@types/lodash": "^4",
"@types/mocha": "^8.2.2",
"@types/node": "^15.14.0",
"@types/node-fetch": "^2.5.10",

View file

@ -1,7 +1,7 @@
import { AbortController } from 'abort-controller';
import { queue, QueueObject } from 'async';
import FormData from 'form-data';
import fetch from 'node-fetch';
import fetch, { Headers } from 'node-fetch';
import type { DeepRequired, IApiClientOptions, IMakeRequestOptions } from '../utils/types';
@ -21,24 +21,25 @@ export class ApiHandler {
}
public get baseApiUrl(): string {
return `${this.options.apiUrl}/${this.options.apiVersion}`;
return `${this.options.apiUrl}/v${this.options.apiVersion}`;
}
public async makeRequest<T>(options: IMakeRequestOptions): Promise<T> {
let headers: Record<string, unknown> = options.headers ?? {};
const headers: Headers = new Headers();
if (options.requireAuth) headers['Authorization'] = `Bot ${this._token}`;
if (options.reason) headers['X-Audit-Log-Reason'] = encodeURIComponent(options.reason);
if (options.headers) for (const prop in options.headers) headers.set(prop, options.headers[prop]!);
if (options.requireAuth) headers.set('Authorization', `Bot ${this._token}`);
if (options.reason) headers.set('X-Audit-Log-Reason', encodeURIComponent(options.reason));
let body: FormData | string;
if (options.files && options.files.length) {
body = new FormData();
for (const file of options.files) if (file && file.file) body.append(file.name, file.file, file.name);
if (options.body) body.append('payload_json', JSON.stringify(options.body));
headers = Object.assign(headers, body.getHeaders());
} else if (options.body) {
body = JSON.stringify(options.body);
headers['Content-Type'] = 'application/json';
headers.set('Content-Type', 'application/json');
}
const controller = new AbortController();
@ -47,7 +48,7 @@ export class ApiHandler {
try {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const res = await fetch(`${this.baseApiUrl}/${options.path}`, { method: options.method, headers, signal: controller.signal, body });
const res = await fetch(`${this.baseApiUrl}${options.path}`, { method: options.method, headers, signal: controller.signal, body });
// TODO: handle Ratelimits

View file

@ -1,3 +1,5 @@
import _ from 'lodash';
import { ApiClient } from '../api/apiClient';
import { defaults } from '../utils/defaults';
@ -10,7 +12,7 @@ export class Client {
private _token: string;
public constructor(token: string, options: IClientOptions = {}) {
this.options = Object.assign(defaults.clientOptions, options as DeepRequired<IClientOptions>);
this.options = _.merge(defaults.clientOptions, options as DeepRequired<IClientOptions>);
this._token = token;
this.api = new ApiClient(this._token, this.options.api);

View file

@ -549,6 +549,7 @@ __metadata:
"@types/async": ^3
"@types/chai": ^4.2.19
"@types/eslint": ^7.2.13
"@types/lodash": ^4
"@types/mocha": ^8.2.2
"@types/node": ^15.14.0
"@types/node-fetch": ^2.5.10
@ -573,6 +574,7 @@ __metadata:
husky: ^7.0.0
inquirer: ^8.1.1
lint-staged: ^11.0.0
lodash: ^4.17.21
mocha: ^9.0.1
node-fetch: ^2.6.1
nyc: ^15.1.0
@ -1243,6 +1245,13 @@ __metadata:
languageName: node
linkType: hard
"@types/lodash@npm:^4":
version: 4.14.170
resolution: "@types/lodash@npm:4.14.170"
checksum: 238a440804e787b85461cc280a11926c80779f7502fec21a70b4424d5feba4cc34cdcbbbc1dca2ec5e75b06d5dc42e42add798be3b6651e8dce9f4b5318d6022
languageName: node
linkType: hard
"@types/minimatch@npm:*":
version: 3.0.4
resolution: "@types/minimatch@npm:3.0.4"