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

Allow to pass fetch options while building the query #527

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

g12i
Copy link

@g12i g12i commented Mar 23, 2024

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.

src/types.ts Outdated Show resolved Hide resolved
@bnjmnt4n
Copy link
Contributor

bnjmnt4n commented Apr 2, 2024

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

@g12i
Copy link
Author

g12i commented Apr 2, 2024

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

@g12i g12i changed the title Allow to pass Next.js options to fetch while building query Allow to pass fetch options while building query Apr 2, 2024
@g12i g12i changed the title Allow to pass fetch options while building query Allow to pass fetch options while building the query Apr 3, 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 this pull request may close these issues.

None yet

2 participants