From 918a01ad96eff5e4642636d4636fa82804b2eb61 Mon Sep 17 00:00:00 2001 From: Jayan Ratna Date: Mon, 22 Jul 2024 09:36:13 +1200 Subject: [PATCH 1/3] feat: Add the ability to use a custom fetch function --- src/blutui.ts | 14 +++++++++----- src/types.ts | 3 ++- src/utils/client.ts | 25 ++++++++++++++++++++++--- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/blutui.ts b/src/blutui.ts index ae6886b..ad17b06 100644 --- a/src/blutui.ts +++ b/src/blutui.ts @@ -53,12 +53,16 @@ export class Blutui { const useAgent: string = `blutui-node/${VERSION}` - this.client = new Client(this.baseURL, { - headers: { - Authorization: `Bearer ${this.accessToken}`, - 'User-Agent': useAgent, + this.client = new Client( + this.baseURL, + { + headers: { + Authorization: `Bearer ${this.accessToken}`, + 'User-Agent': useAgent, + }, }, - }) + options.request?.fetch + ) } /** diff --git a/src/types.ts b/src/types.ts index 76320ab..6e74e06 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3,7 +3,8 @@ export interface BlutuiOptions { apiHostname?: string request?: { - fetch?: typeof fetch + // biome-ignore lint/suspicious/noExplicitAny: We want developers to be able to use node-fetch + fetch?: any } } diff --git a/src/utils/client.ts b/src/utils/client.ts index 0f936f5..fa1019f 100644 --- a/src/utils/client.ts +++ b/src/utils/client.ts @@ -3,10 +3,29 @@ import { FetchException } from '../exceptions' const API_VERSION = 'v1' export class Client { + private readonly _fetchFn + + /** + * Create a new client instance. + */ constructor( readonly baseURL: string, - readonly options?: RequestInit - ) {} + readonly options?: RequestInit, + fetchFn?: typeof fetch + ) { + let newFetchFn = fetchFn + + if (!newFetchFn) { + if (!globalThis.fetch) { + throw new Error( + 'Fetch function not defined in the global scope and no replacement was provided.' + ) + } + newFetchFn = globalThis.fetch + } + + this._fetchFn = newFetchFn + } async get( path: string, @@ -80,7 +99,7 @@ export class Client { } private async fetch(url: string, options?: RequestInit) { - const response = await fetch(url, { + const response = await this._fetchFn(url, { ...this.options, ...options, headers: { From 9fa8c257717cb9597b4741a40dcda7b921b114c3 Mon Sep 17 00:00:00 2001 From: Chengfang Liu Date: Tue, 23 Jul 2024 11:16:31 +1200 Subject: [PATCH 2/3] refactor set custom fetch function --- src/blutui.spec.ts | 16 ++++++++++++++++ src/utils/client.ts | 15 +++++---------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/blutui.spec.ts b/src/blutui.spec.ts index 7f3a903..dd911ea 100644 --- a/src/blutui.spec.ts +++ b/src/blutui.spec.ts @@ -47,6 +47,22 @@ describe('Blutui', () => { }) }) + describe('when access token and fetch functin are provided with constructor', () => { + it('initalizes', async () => { + // Example from JWT.io + const token = + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c' + expect( + () => + new Blutui(token, { + request: { + fetch: fetch, + }, + }) + ).not.toThrow() + }) + }) + describe('version', () => { it('matches the version in `package.json`', async () => { const blutui = new Blutui('eyJhbGciOi') diff --git a/src/utils/client.ts b/src/utils/client.ts index 9258b78..0411308 100644 --- a/src/utils/client.ts +++ b/src/utils/client.ts @@ -13,18 +13,13 @@ export class Client { readonly options?: RequestInit, fetchFn?: typeof fetch ) { - let newFetchFn = fetchFn + this._fetchFn = fetchFn || globalThis.fetch - if (!newFetchFn) { - if (!globalThis.fetch) { - throw new Error( - 'Fetch function not defined in the global scope and no replacement was provided.' - ) - } - newFetchFn = globalThis.fetch + if (!this._fetchFn) { + throw new Error( + 'Fetch function not defined in the global scope and no replacement was provided.' + ) } - - this._fetchFn = newFetchFn } async get( From f846004d395939118b0b2d02c1e3e744ff3cb868 Mon Sep 17 00:00:00 2001 From: Michael Liu <162056370+chengfang-blutui@users.noreply.github.com> Date: Tue, 23 Jul 2024 11:21:56 +1200 Subject: [PATCH 3/3] Update src/blutui.spec.ts Co-authored-by: Jayan Ratna <30396013+jayan-blutui@users.noreply.github.com> --- src/blutui.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/blutui.spec.ts b/src/blutui.spec.ts index dd911ea..98b6df3 100644 --- a/src/blutui.spec.ts +++ b/src/blutui.spec.ts @@ -47,7 +47,7 @@ describe('Blutui', () => { }) }) - describe('when access token and fetch functin are provided with constructor', () => { + describe('when access token and fetch function are provided to the constructor', () => { it('initalizes', async () => { // Example from JWT.io const token =