diff --git a/package.json b/package.json index 964b9c9..8a77178 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/api/apiHandler.ts b/src/api/apiHandler.ts index a1802ff..d678a24 100644 --- a/src/api/apiHandler.ts +++ b/src/api/apiHandler.ts @@ -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(options: IMakeRequestOptions): Promise { - let headers: Record = 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 diff --git a/src/client/client.ts b/src/client/client.ts index c98926a..73ac269 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -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); + this.options = _.merge(defaults.clientOptions, options as DeepRequired); this._token = token; this.api = new ApiClient(this._token, this.options.api); diff --git a/yarn.lock b/yarn.lock index 94b1499..147e292 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"