Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom fetch functions #24

Merged
merged 4 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})