diff --git a/.changeset/ten-spies-pump.md b/.changeset/ten-spies-pump.md new file mode 100644 index 0000000..81a19f2 --- /dev/null +++ b/.changeset/ten-spies-pump.md @@ -0,0 +1,5 @@ +--- +"amaribot.js": minor +--- + +Add customFetch to the options when creating a new client, allowing you the ability to write custom fetch functions diff --git a/package.json b/package.json index 40e22c0..35b5138 100644 --- a/package.json +++ b/package.json @@ -65,5 +65,8 @@ }, "engines": { "node": ">=16.17.1" + }, + "volta": { + "node": "16.17.1" } } diff --git a/src/AmariBot.ts b/src/AmariBot.ts index 7777db8..c3d654a 100644 --- a/src/AmariBot.ts +++ b/src/AmariBot.ts @@ -7,6 +7,7 @@ import { APIRewards, APIUser, APIWeeklyLeaderboard, + CustomFetch, PaginationOptions, RawPaginationOptions, } from "." @@ -17,6 +18,7 @@ export class AmariBot { debug: boolean baseURL: string version: string + customFetch?: CustomFetch /** * @private @@ -34,6 +36,7 @@ export class AmariBot { this.debug = options.debug || false this.baseURL = options.baseURL || "https://amaribot.com/api" this.version = options.version || "v1" + if(options.customFetch) this.customFetch = options.customFetch this._requestHandler = new RequestHandler(this) if (this.debug) console.debug("amaribot.js initalized\n" + JSON.stringify(options, null, 2)) diff --git a/src/RequestHandler.ts b/src/RequestHandler.ts index 3360568..7f767bf 100644 --- a/src/RequestHandler.ts +++ b/src/RequestHandler.ts @@ -21,7 +21,7 @@ export class RequestHandler { } if (this._client.debug) console.debug(`Sending request to ${url}\nMethod:\n ${options.method}\nParams:\n ${JSON.stringify(query)}`) try { - const res = await fetch(url, options) + const res = this._client.customFetch ? await this._client.customFetch(url, options) : await fetch(url, options) if (res.status >= 200 && res.status < 300) { const json = await res.json() as APIError resolve(json) diff --git a/src/errors/APIError.ts b/src/errors/AmariError.ts similarity index 77% rename from src/errors/APIError.ts rename to src/errors/AmariError.ts index a20f39a..605d8a7 100644 --- a/src/errors/APIError.ts +++ b/src/errors/AmariError.ts @@ -5,15 +5,14 @@ * @property {string} message The message of this error */ -import { Response } from "node-fetch" -import { APIError } from ".." +import { APIError, CustomResponse } from ".." export class AmariError extends Error { name: string status: number message: string - constructor(response: Response, data?: APIError) { + constructor(response: CustomResponse, data?: APIError) { super() this.name = this.constructor.name this.status = response.status diff --git a/src/errors/RatelimitError.ts b/src/errors/RatelimitError.ts index 0d88769..512faf4 100644 --- a/src/errors/RatelimitError.ts +++ b/src/errors/RatelimitError.ts @@ -1,6 +1,5 @@ -import { Response } from "node-fetch" import ms from "ms" -import { APIError } from ".." +import { APIError, CustomResponse } from ".." /** * @extends Error @@ -16,7 +15,7 @@ export class RatelimitError extends Error { remaining: number message: string - constructor(response: Response, data?: APIError) { + constructor(response: CustomResponse, data?: APIError) { super() this.name = this.constructor.name this.status = response.status diff --git a/src/index.ts b/src/index.ts index 6611de2..ef793f6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,17 +5,23 @@ or just shoot me that too */ +import { Response as NodeFetchResponse, RequestInit } from "node-fetch" + export { RequestHandler } from "./RequestHandler" export { AmariBot } from "./AmariBot" -export { AmariError } from "./errors/APIError" +export { AmariError } from "./errors/AmariError" export { RatelimitError } from "./errors/RatelimitError" export type AmariBotOptions = { baseURL?: string version?: string debug?: boolean + customFetch?: CustomFetch } +export type CustomFetch = (url: string, options: RequestInit) => Promise +export type CustomResponse = NodeFetchResponse | Response + export type PaginationOptions = { limit?: number page?: number diff --git a/tests/amari.test.ts b/tests/amari.test.ts index 6bbde64..c74255e 100644 --- a/tests/amari.test.ts +++ b/tests/amari.test.ts @@ -1,5 +1,7 @@ +import fetch from "node-fetch" import { AmariBot } from "../src" import { expect, expectTypeOf, test } from "vitest" +import { RequestInit } from "node-fetch" const guildId = "346474194394939393" const userId = "439223656200273932" @@ -147,3 +149,17 @@ test("The client should calculate the correct level from exp", () => { const xp70 = client.getNextLevelExp(70) expect(xp70).toBe(98035) }) + + +test("Custom fetch functions should work", async () => { + const customClient = new AmariBot(apiKey, { + customFetch: async (url: string, options: RequestInit) => { + const res = await fetch(url, options) + return res + } + }) + + const data = await customClient.getUserLevel(guildId, userId) + expect(data).toBeDefined() + expect(data.id).toBe(userId) +}) \ No newline at end of file