Skip to content

Commit

Permalink
Custom fetch functions (#24)
Browse files Browse the repository at this point in the history
* Custom fetch functions

* Prep our changelog

* Add proper tests

* use node-fetch instead of native v18
  • Loading branch information
thewilloftheshadow authored Apr 3, 2023
1 parent 30bfee0 commit a426d64
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-spies-pump.md
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,8 @@
},
"engines": {
"node": ">=16.17.1"
},
"volta": {
"node": "16.17.1"
}
}
3 changes: 3 additions & 0 deletions src/AmariBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
APIRewards,
APIUser,
APIWeeklyLeaderboard,
CustomFetch,
PaginationOptions,
RawPaginationOptions,
} from "."
Expand All @@ -17,6 +18,7 @@ export class AmariBot {
debug: boolean
baseURL: string
version: string
customFetch?: CustomFetch

/**
* @private
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/RequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions src/errors/APIError.ts → src/errors/AmariError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/errors/RatelimitError.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Response } from "node-fetch"
import ms from "ms"
import { APIError } from ".."
import { APIError, CustomResponse } from ".."

/**
* @extends Error
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CustomResponse>
export type CustomResponse = NodeFetchResponse | Response

export type PaginationOptions = {
limit?: number
page?: number
Expand Down
16 changes: 16 additions & 0 deletions tests/amari.test.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
})

0 comments on commit a426d64

Please sign in to comment.