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

Feat: Allow users to input their own fetch functions (for proxies) #64

Merged
merged 4 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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/fifty-baboons-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"kiai.js": minor
---

Add an option to include a fetchFunction for the client which changes the function RequestHandler uses to contact the API
6 changes: 4 additions & 2 deletions packages/kiai.js/src/KiaiClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Message, RateLimitError, VirtualMessage } from "@buape/kiai-api-types"
import { RequestHandler } from "./RequestHandler"
import * as handlers from "./handlers"
import fetch from "node-fetch"
import { RequestInfo, RequestInit, Response } from "node-fetch"

export class KiaiClient {
apiKey: string
Expand All @@ -23,12 +25,12 @@ export class KiaiClient {
* @param options.debug Whether to enable debug mode
* @constructor
*/
constructor(apiKey: string, options?: { baseURL?: string; version: `v${number}`; debug?: boolean }) {
constructor(apiKey: string, options?: { baseURL?: string; version?: `v${number}`; debug?: boolean; fetchFunction: ((url: URL | RequestInfo, init?: RequestInit | undefined) => Promise<Response>) }) {
this.apiKey = apiKey
this.version = options?.version || "v1"
this.baseURL = options?.baseURL || `https://api.kiaibot.com/${this.version}`
this.debug = options?.debug || false
this._requestHandler = new RequestHandler(this.baseURL, this.apiKey, this.debug)
this._requestHandler = new RequestHandler(this.baseURL, this.apiKey, this.debug, options?.fetchFunction ?? fetch)

this.blacklist = new handlers.Blacklist(this._requestHandler)
this.leveling = new handlers.Leveling(this._requestHandler)
Expand Down
7 changes: 5 additions & 2 deletions packages/kiai.js/src/RequestHandler.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { RateLimitError as APIRateLimitError } from "@buape/kiai-api-types"
import fetch from "node-fetch"
import { RequestInfo, RequestInit, Response } from "node-fetch"
import { RatelimitError, APIError } from "."
export class RequestHandler {
baseURL: string
apiKey: string
debug: boolean
constructor(baseURL: string, apiKey: string, debug: boolean = false) {
fetchFunction: ((url: URL | RequestInfo, init?: RequestInit | undefined) => Promise<Response>)
constructor(baseURL: string, apiKey: string, debug: boolean = false, fetchFunction: ((url: URL | RequestInfo, init?: RequestInit | undefined) => Promise<Response>) = fetch) {
this.baseURL = baseURL
this.apiKey = apiKey
this.debug = debug
this.fetchFunction = fetchFunction
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -31,7 +34,7 @@ export class RequestHandler {
}
if (this.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 = await this.fetchFunction(url, options)
if (res.status >= 200 && res.status < 300) {
const json = (await res.json())
if (this.debug) console.debug("Success: \n", json)
Expand Down