From 9a8ad62924706d0c8edfab473a2b74f9cd4a2c8d Mon Sep 17 00:00:00 2001 From: m-shaka Date: Thu, 27 Jun 2024 15:46:13 +0900 Subject: [PATCH 1/2] fix: use global fetch instead of @supabase/node-fetch --- package-lock.json | 1 - package.json | 1 - src/SupabaseClient.ts | 2 +- src/lib/fetch.ts | 31 ++----------------------------- 4 files changed, 3 insertions(+), 32 deletions(-) diff --git a/package-lock.json b/package-lock.json index ac8489c2..8964be18 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,6 @@ "dependencies": { "@supabase/auth-js": "2.64.2", "@supabase/functions-js": "2.4.1", - "@supabase/node-fetch": "2.6.15", "@supabase/postgrest-js": "1.15.6", "@supabase/realtime-js": "2.10.1", "@supabase/storage-js": "2.6.0" diff --git a/package.json b/package.json index e19a4ade..69fea9e0 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,6 @@ "dependencies": { "@supabase/auth-js": "2.64.2", "@supabase/functions-js": "2.4.1", - "@supabase/node-fetch": "2.6.15", "@supabase/postgrest-js": "1.15.6", "@supabase/realtime-js": "2.10.1", "@supabase/storage-js": "2.6.0" diff --git a/src/SupabaseClient.ts b/src/SupabaseClient.ts index 46e61b76..454c84ff 100644 --- a/src/SupabaseClient.ts +++ b/src/SupabaseClient.ts @@ -100,7 +100,7 @@ export default class SupabaseClient< this.headers, settings.global.fetch ) - this.fetch = fetchWithAuth(supabaseKey, this._getAccessToken.bind(this), settings.global.fetch) + this.fetch = fetchWithAuth(supabaseKey, this._getAccessToken.bind(this)) this.realtime = this._initRealtimeClient({ headers: this.headers, ...settings.realtime }) this.rest = new PostgrestClient(`${_supabaseUrl}/rest/v1`, { diff --git a/src/lib/fetch.ts b/src/lib/fetch.ts index c3ec5f42..b26267df 100644 --- a/src/lib/fetch.ts +++ b/src/lib/fetch.ts @@ -1,39 +1,12 @@ -// @ts-ignore -import nodeFetch, { Headers as NodeFetchHeaders } from '@supabase/node-fetch' - type Fetch = typeof fetch -export const resolveFetch = (customFetch?: Fetch): Fetch => { - let _fetch: Fetch - if (customFetch) { - _fetch = customFetch - } else if (typeof fetch === 'undefined') { - _fetch = nodeFetch as unknown as Fetch - } else { - _fetch = fetch - } - return (...args: Parameters) => _fetch(...args) -} - -export const resolveHeadersConstructor = () => { - if (typeof Headers === 'undefined') { - return NodeFetchHeaders - } - - return Headers -} - export const fetchWithAuth = ( supabaseKey: string, - getAccessToken: () => Promise, - customFetch?: Fetch + getAccessToken: () => Promise ): Fetch => { - const fetch = resolveFetch(customFetch) - const HeadersConstructor = resolveHeadersConstructor() - return async (input, init) => { const accessToken = (await getAccessToken()) ?? supabaseKey - let headers = new HeadersConstructor(init?.headers) + let headers = new Headers(init?.headers) if (!headers.has('apikey')) { headers.set('apikey', supabaseKey) From a4fd29d5149da77b0d9ee4d8a97af53b69083fc6 Mon Sep 17 00:00:00 2001 From: m-shaka Date: Fri, 28 Jun 2024 07:47:51 +0900 Subject: [PATCH 2/2] revert customFetch --- src/SupabaseClient.ts | 2 +- src/lib/fetch.ts | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/SupabaseClient.ts b/src/SupabaseClient.ts index 454c84ff..46e61b76 100644 --- a/src/SupabaseClient.ts +++ b/src/SupabaseClient.ts @@ -100,7 +100,7 @@ export default class SupabaseClient< this.headers, settings.global.fetch ) - this.fetch = fetchWithAuth(supabaseKey, this._getAccessToken.bind(this)) + this.fetch = fetchWithAuth(supabaseKey, this._getAccessToken.bind(this), settings.global.fetch) this.realtime = this._initRealtimeClient({ headers: this.headers, ...settings.realtime }) this.rest = new PostgrestClient(`${_supabaseUrl}/rest/v1`, { diff --git a/src/lib/fetch.ts b/src/lib/fetch.ts index b26267df..b00564ae 100644 --- a/src/lib/fetch.ts +++ b/src/lib/fetch.ts @@ -2,9 +2,11 @@ type Fetch = typeof fetch export const fetchWithAuth = ( supabaseKey: string, - getAccessToken: () => Promise + getAccessToken: () => Promise, + customFetch?: Fetch ): Fetch => { return async (input, init) => { + const fetch_ = customFetch ?? fetch const accessToken = (await getAccessToken()) ?? supabaseKey let headers = new Headers(init?.headers) @@ -16,6 +18,6 @@ export const fetchWithAuth = ( headers.set('Authorization', `Bearer ${accessToken}`) } - return fetch(input, { ...init, headers }) + return fetch_(input, { ...init, headers }) } }