supabase / postgrest-js

Isomorphic JavaScript client for PostgREST.
https://supabase.com
MIT License
962 stars 128 forks source link

Allow to pass fetch options while building the query #527

Open g12i opened 3 months ago

g12i commented 3 months ago

What kind of change does this PR introduce?

This adds a possibility to pass any fetch options (e.g. next options to fetch while building the query.

What is the current behavior?

Currently we can pass fetch at PostgrestBuilder creation. Which effectively means once, if using Supabase. One would have to parse URL and alter options depending on the URL. E.g. for passing next.tags options

  const postgrest2 = new PostgrestClient<Database>('http://localhost:3000', {
    fetch: (url, init) => {
      let parsedUrl: URL

      if (url instanceof URL) {
        parsedUrl = url
      } else if (typeof url === 'string') {
        parsedUrl = new URL(url)
      } else {
        parsedUrl = new URL(url.url)
      }

      const tags: string[] = []
      if (parsedUrl.pathname.startsWith('/rest')) {
        const [,, entity] = parsedUrl.pathname.split('/').slice(1)
        tags.push(entity)
      }

      return fetch(url, {
        ...init,
        next: {
          tags,
        },
      })
    },
  })

What is the new behavior?

Allow to pass fetchOptions options when building query. The idea here is

  const res = await postgrest
    .from('users')
    .select()
    .eq('username', 'supabot')
    .fetchOptions({
      next: { tags: ['users/supabot'] },
    })

// ... later in the code

revalidateTag('users/supabot');

Because FetchOptions include signal, it is marked as deprecated and uses .fetchOptions internally.

bnjmnt4n commented 2 months ago

Instead of this being Next.js specific, not sure if it might be better to just allow customization of any fetch option?

g12i commented 2 months ago

Yeah, I agree @bnjmnt4n. Let me make a quick update

g12i commented 3 weeks ago

Hello @soedirgo, any chance this gets reviewed?