From 54fb5f84d9f800b0c90cf8c4a3ad392334c67868 Mon Sep 17 00:00:00 2001 From: Shadow Date: Mon, 3 Apr 2023 02:29:48 -0500 Subject: [PATCH 1/4] Custom fetch functions --- package.json | 2 +- src/AmariBot.ts | 3 +++ src/RequestHandler.ts | 2 +- src/index.ts | 5 +++++ 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 40e22c0..ef88c63 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "amaribot.js", - "version": "2.0.4", + "version": "2.1.0-beta", "description": "A node.js wrapper for the AmariBot API", "type": "module", "exports": { 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/index.ts b/src/index.ts index 6611de2..27b9dee 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,6 +5,8 @@ or just shoot me that too */ +import { Response } from "node-fetch" + export { RequestHandler } from "./RequestHandler" export { AmariBot } from "./AmariBot" export { AmariError } from "./errors/APIError" @@ -14,8 +16,11 @@ export type AmariBotOptions = { baseURL?: string version?: string debug?: boolean + customFetch?: CustomFetch } +export type CustomFetch = (url: string, options: RequestInit) => Promise + export type PaginationOptions = { limit?: number page?: number From 0e1ea4615dedd88258a5762db99950b67ea176cf Mon Sep 17 00:00:00 2001 From: Shadow Date: Mon, 3 Apr 2023 02:34:07 -0500 Subject: [PATCH 2/4] Prep our changelog --- .changeset/ten-spies-pump.md | 5 +++++ package.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 .changeset/ten-spies-pump.md 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 ef88c63..40e22c0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "amaribot.js", - "version": "2.1.0-beta", + "version": "2.0.4", "description": "A node.js wrapper for the AmariBot API", "type": "module", "exports": { From f511ccdf1c0b4b41f968161061bbf2a1de5aacc3 Mon Sep 17 00:00:00 2001 From: Shadow Date: Mon, 3 Apr 2023 02:42:44 -0500 Subject: [PATCH 3/4] Add proper tests --- src/errors/{APIError.ts => AmariError.ts} | 5 ++--- src/errors/RatelimitError.ts | 5 ++--- src/index.ts | 7 ++++--- tests/amari.test.ts | 14 ++++++++++++++ 4 files changed, 22 insertions(+), 9 deletions(-) rename src/errors/{APIError.ts => AmariError.ts} (77%) 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 27b9dee..a4831dc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,11 +5,11 @@ or just shoot me that too */ -import { Response } from "node-fetch" +import { Response as NodeFetchResponse } 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 = { @@ -19,7 +19,8 @@ export type AmariBotOptions = { customFetch?: CustomFetch } -export type CustomFetch = (url: string, options: RequestInit) => Promise +export type CustomFetch = (url: string, options: RequestInit) => Promise +export type CustomResponse = NodeFetchResponse | Response export type PaginationOptions = { limit?: number diff --git a/tests/amari.test.ts b/tests/amari.test.ts index 6bbde64..2019347 100644 --- a/tests/amari.test.ts +++ b/tests/amari.test.ts @@ -147,3 +147,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 From 70c537f5a3c15b50fe2f5d2ccde91a0d6e5dc1c0 Mon Sep 17 00:00:00 2001 From: Shadow Date: Mon, 3 Apr 2023 03:07:12 -0500 Subject: [PATCH 4/4] use node-fetch instead of native v18 --- package.json | 3 +++ src/index.ts | 2 +- tests/amari.test.ts | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) 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/index.ts b/src/index.ts index a4831dc..ef793f6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ or just shoot me that too */ -import { Response as NodeFetchResponse } from "node-fetch" +import { Response as NodeFetchResponse, RequestInit } from "node-fetch" export { RequestHandler } from "./RequestHandler" export { AmariBot } from "./AmariBot" diff --git a/tests/amari.test.ts b/tests/amari.test.ts index 2019347..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"