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

chore(PostgrestBuilder): dynamic import @supabase/node-fetch #576

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 2 additions & 10 deletions src/PostgrestBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// @ts-ignore
import nodeFetch from '@supabase/node-fetch'

import type { Fetch, PostgrestSingleResponse } from './types'
import PostgrestError from './PostgrestError'
import { resolveFetch } from './lib/helpers'

export default abstract class PostgrestBuilder<Result>
implements PromiseLike<PostgrestSingleResponse<Result>>
Expand All @@ -27,13 +25,7 @@ export default abstract class PostgrestBuilder<Result>
this.signal = builder.signal
this.isMaybeSingle = builder.isMaybeSingle

if (builder.fetch) {
this.fetch = builder.fetch
} else if (typeof fetch === 'undefined') {
this.fetch = nodeFetch
} else {
this.fetch = fetch
}
this.fetch = resolveFetch(builder.fetch)
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/lib/helpers.ts
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)
}
46 changes: 46 additions & 0 deletions test/helpers.test.ts
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)
})
})