fix: several bugs
This commit is contained in:
parent
82b6b5acee
commit
7fa5ea6762
4 changed files with 23 additions and 9 deletions
|
@ -38,6 +38,7 @@
|
||||||
"abort-controller": "^3.0.0",
|
"abort-controller": "^3.0.0",
|
||||||
"async": "^3.2.0",
|
"async": "^3.2.0",
|
||||||
"form-data": "^4.0.0",
|
"form-data": "^4.0.0",
|
||||||
|
"lodash": "^4.17.21",
|
||||||
"node-fetch": "^2.6.1",
|
"node-fetch": "^2.6.1",
|
||||||
"ws": "^7.5.2"
|
"ws": "^7.5.2"
|
||||||
},
|
},
|
||||||
|
@ -55,6 +56,7 @@
|
||||||
"@types/async": "^3",
|
"@types/async": "^3",
|
||||||
"@types/chai": "^4.2.19",
|
"@types/chai": "^4.2.19",
|
||||||
"@types/eslint": "^7.2.13",
|
"@types/eslint": "^7.2.13",
|
||||||
|
"@types/lodash": "^4",
|
||||||
"@types/mocha": "^8.2.2",
|
"@types/mocha": "^8.2.2",
|
||||||
"@types/node": "^15.14.0",
|
"@types/node": "^15.14.0",
|
||||||
"@types/node-fetch": "^2.5.10",
|
"@types/node-fetch": "^2.5.10",
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { AbortController } from 'abort-controller';
|
import { AbortController } from 'abort-controller';
|
||||||
import { queue, QueueObject } from 'async';
|
import { queue, QueueObject } from 'async';
|
||||||
import FormData from 'form-data';
|
import FormData from 'form-data';
|
||||||
import fetch from 'node-fetch';
|
import fetch, { Headers } from 'node-fetch';
|
||||||
|
|
||||||
import type { DeepRequired, IApiClientOptions, IMakeRequestOptions } from '../utils/types';
|
import type { DeepRequired, IApiClientOptions, IMakeRequestOptions } from '../utils/types';
|
||||||
|
|
||||||
|
@ -21,24 +21,25 @@ export class ApiHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
public get baseApiUrl(): string {
|
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> {
|
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.headers) for (const prop in options.headers) headers.set(prop, options.headers[prop]!);
|
||||||
if (options.reason) headers['X-Audit-Log-Reason'] = encodeURIComponent(options.reason);
|
|
||||||
|
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;
|
let body: FormData | string;
|
||||||
if (options.files && options.files.length) {
|
if (options.files && options.files.length) {
|
||||||
body = new FormData();
|
body = new FormData();
|
||||||
for (const file of options.files) if (file && file.file) body.append(file.name, file.file, file.name);
|
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));
|
if (options.body) body.append('payload_json', JSON.stringify(options.body));
|
||||||
headers = Object.assign(headers, body.getHeaders());
|
|
||||||
} else if (options.body) {
|
} else if (options.body) {
|
||||||
body = JSON.stringify(options.body);
|
body = JSON.stringify(options.body);
|
||||||
headers['Content-Type'] = 'application/json';
|
headers.set('Content-Type', 'application/json');
|
||||||
}
|
}
|
||||||
|
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
|
@ -47,7 +48,7 @@ export class ApiHandler {
|
||||||
try {
|
try {
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
// @ts-ignore
|
// @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
|
// TODO: handle Ratelimits
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import _ from 'lodash';
|
||||||
|
|
||||||
import { ApiClient } from '../api/apiClient';
|
import { ApiClient } from '../api/apiClient';
|
||||||
import { defaults } from '../utils/defaults';
|
import { defaults } from '../utils/defaults';
|
||||||
|
|
||||||
|
@ -10,7 +12,7 @@ export class Client {
|
||||||
private _token: string;
|
private _token: string;
|
||||||
|
|
||||||
public constructor(token: string, options: IClientOptions = {}) {
|
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._token = token;
|
||||||
|
|
||||||
this.api = new ApiClient(this._token, this.options.api);
|
this.api = new ApiClient(this._token, this.options.api);
|
||||||
|
|
|
@ -549,6 +549,7 @@ __metadata:
|
||||||
"@types/async": ^3
|
"@types/async": ^3
|
||||||
"@types/chai": ^4.2.19
|
"@types/chai": ^4.2.19
|
||||||
"@types/eslint": ^7.2.13
|
"@types/eslint": ^7.2.13
|
||||||
|
"@types/lodash": ^4
|
||||||
"@types/mocha": ^8.2.2
|
"@types/mocha": ^8.2.2
|
||||||
"@types/node": ^15.14.0
|
"@types/node": ^15.14.0
|
||||||
"@types/node-fetch": ^2.5.10
|
"@types/node-fetch": ^2.5.10
|
||||||
|
@ -573,6 +574,7 @@ __metadata:
|
||||||
husky: ^7.0.0
|
husky: ^7.0.0
|
||||||
inquirer: ^8.1.1
|
inquirer: ^8.1.1
|
||||||
lint-staged: ^11.0.0
|
lint-staged: ^11.0.0
|
||||||
|
lodash: ^4.17.21
|
||||||
mocha: ^9.0.1
|
mocha: ^9.0.1
|
||||||
node-fetch: ^2.6.1
|
node-fetch: ^2.6.1
|
||||||
nyc: ^15.1.0
|
nyc: ^15.1.0
|
||||||
|
@ -1243,6 +1245,13 @@ __metadata:
|
||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
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:*":
|
"@types/minimatch@npm:*":
|
||||||
version: 3.0.4
|
version: 3.0.4
|
||||||
resolution: "@types/minimatch@npm:3.0.4"
|
resolution: "@types/minimatch@npm:3.0.4"
|
||||||
|
|
Reference in a new issue