-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(PostgrestBuilder): dynamic import
@supabase/node-fetch
- Loading branch information
1 parent
efa3e5c
commit 8464176
Showing
3 changed files
with
62 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Fetch } from '../types' | ||
|
||
export const resolveFetch = (customFetch?: Fetch): Fetch => { | ||
let _fetch: Fetch | ||
if (customFetch) { | ||
_fetch = customFetch | ||
} else if (typeof fetch === 'undefined') { | ||
_fetch = (...args) => | ||
import('@supabase/node-fetch' as any).then(({ default: fetch }) => fetch(...args)) | ||
} else { | ||
_fetch = fetch | ||
} | ||
return (...args) => _fetch(...args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { resolveFetch } from '../src/lib/helpers' | ||
|
||
describe('resolveFetch', () => { | ||
const TEST_URL = 'https://example.com' | ||
const TEST_OPTIONS = { method: 'GET' } | ||
|
||
beforeEach(() => { | ||
// Reset any mocks between tests | ||
jest.resetModules() | ||
jest.clearAllMocks() | ||
}) | ||
|
||
it('should use custom fetch if provided', async () => { | ||
const customFetch = jest.fn() | ||
const resolvedFetch = resolveFetch(customFetch) | ||
|
||
await resolvedFetch(TEST_URL, TEST_OPTIONS) | ||
|
||
expect(customFetch).toHaveBeenCalledTimes(1) | ||
expect(customFetch).toHaveBeenCalledWith(TEST_URL, TEST_OPTIONS) | ||
}) | ||
|
||
it('should use global fetch if no custom fetch is provided', async () => { | ||
const globalFetch = jest.fn() | ||
global.fetch = globalFetch | ||
const resolvedFetch = resolveFetch() | ||
|
||
await resolvedFetch(TEST_URL, TEST_OPTIONS) | ||
|
||
expect(globalFetch).toHaveBeenCalledTimes(1) | ||
expect(globalFetch).toHaveBeenCalledWith(TEST_URL, TEST_OPTIONS) | ||
}) | ||
|
||
it('should use node-fetch if global fetch is not available', async () => { | ||
const nodeFetch = jest.fn() | ||
jest.mock('@supabase/node-fetch', () => nodeFetch) | ||
|
||
global.fetch = undefined as any | ||
const resolvedFetch = resolveFetch() | ||
|
||
await resolvedFetch(TEST_URL, TEST_OPTIONS) | ||
|
||
expect(nodeFetch).toHaveBeenCalledTimes(1) | ||
expect(nodeFetch).toHaveBeenCalledWith(TEST_URL, TEST_OPTIONS) | ||
}) | ||
}) |