Skip to content

Commit

Permalink
chore(PostgrestBuilder): dynamic import @supabase/node-fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
lyc authored and liyachun01 committed Nov 21, 2024
1 parent 0af65f5 commit 0281b80
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
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)
})
})

0 comments on commit 0281b80

Please sign in to comment.