Allow to pass fetch options while building the query
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.
Instead of this being Next.js specific, not sure if it might be better to just allow customization of any fetch option?
Yeah, I agree @bnjmnt4n. Let me make a quick update
Hello @soedirgo, any chance this gets reviewed?