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

Request to dynamic import @supabase/node-fetch #574

Open
liyachun01 opened this issue Nov 6, 2024 · 0 comments · May be fixed by #576
Open

Request to dynamic import @supabase/node-fetch #574

liyachun01 opened this issue Nov 6, 2024 · 0 comments · May be fixed by #576

Comments

@liyachun01
Copy link

liyachun01 commented Nov 6, 2024

Chore

Describe the chore

Request update the static import of @supabase/node-fetch in src/PostgrestBuilder:

import nodeFetch from '@supabase/node-fetch'

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

Use dynamic import('@supabase/node-fetch') to avoid possible Cannot read property 'bind' of undefined error.

Additional context

When using @supabase/supabase-js which requires @supabase/postgrest-js, a user is able to provide custom fetch by providing a global.fetch option. However, with @supabase/node-fetch statically imported in @supabase/postgrest-js, a Cannot read property 'bind' of undefined error may throw at runtime.

TypeError: Cannot read property 'bind' of undefined
    at path/to/node_modules/@supabase/node-fetch/browser.js

The error is thrown when the runtime environment does not have native fetch on globalObject at all.

https://github.com/supabase/node-fetch/blob/bd8f50a4ae647e56cda296b8301d7dff01d4a87b/browser.js#L18.

/* @supabase/node-fetch/browser.js#L18 */
export default globalObject.fetch.bind(globalObject);

In some other @supabase/ packages like @supabase/auth-js, the fetch is resolved using a resolveFetch function:

https://github.com/supabase/auth-js/blob/8af88b6f4e41872b73e84c40f71793dab6c62126/src/lib/helpers.ts#L93-L104.

/* @supabase/auth-js/src/lib/helpers.ts#L93-L104. */
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 way, the node-fetch import is safe.

@liyachun01 liyachun01 changed the title dynamic import @supabase/node-fetch Request to dynamic import @supabase/node-fetch Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant