N
Nuxt4mo ago
Phillip

How to access the event in a server/utils function?

I've created a server utility function in /server/utils/get-facebook-account.ts which looks like this
import { serverSupabaseClient } from '#supabase/server'

export const getFacebookAccount = async () => {
// const event = // how to get event
const supabase = await serverSupabaseClient<Database>(event)

const account = supabase.from()... // do stuff to get account;

return account;
}
import { serverSupabaseClient } from '#supabase/server'

export const getFacebookAccount = async () => {
// const event = // how to get event
const supabase = await serverSupabaseClient<Database>(event)

const account = supabase.from()... // do stuff to get account;

return account;
}
I'm using Supabase so for the serverSupabaseClient I need the event. Now I'm wondering, do I always need to pass the event to my utility function like this? /server/api/campaign.post.ts
export default defineEventHandler(async (event) => {
await getFacebookAccount(event)
})
export default defineEventHandler(async (event) => {
await getFacebookAccount(event)
})
Because I've read there is a useRequestEvent composable but it's only available in the Nuxt context. Is there something similar for /server functions? Thanks!
2 Replies
kingtimm
kingtimm4mo ago
I haven’t tried this but saw it mentioned on another thread. https://nitro.unjs.io/guide/utils#async-context-experimental
Nitro
Server Utils · Nitro
Create web servers with everything you need and deploy them wherever you prefer.
Phillip
Phillip4mo ago
@kingtimm this worked perfectly, thank you!