Skip to content

Commit

Permalink
fix(types): added explicit types annotations for `serverSupabaseClien…
Browse files Browse the repository at this point in the history
…t` and `serverSupabaseServiceRole` (#411)
  • Loading branch information
th1m0 authored Sep 3, 2024
1 parent de06f15 commit 3f98640
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export default defineNuxtModule<ModuleOptions>({
inline: [resolve('./runtime')],
})
nitroConfig.alias['#supabase/server'] = resolveRuntimeModule('./server/services')
nitroConfig.alias['#supabase/database'] = resolve(nitroConfig.buildDir!, 'types/supabase-database')
})

addTemplate({
Expand Down
7 changes: 4 additions & 3 deletions src/runtime/server/services/serverSupabaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { createServerClient, parseCookieHeader, type CookieOptions } from '@supa
import { getHeader, setCookie, type H3Event } from 'h3'
import { fetchWithRetry } from '../../utils/fetch-retry'
import { useRuntimeConfig } from '#imports'
import type { Database } from '#build/types/supabase-database'
// @ts-expect-error - `#supabase/database` is a runtime alias
import type { Database } from '#supabase/database'

export const serverSupabaseClient = async <T = Database>(event: H3Event): Promise<SupabaseClient<T>> => {
export const serverSupabaseClient: <T = Database>(event: H3Event) => Promise<SupabaseClient<T>> = async <T = Database>(event: H3Event) => {
// No need to recreate client if exists in request context
if (!event.context._supabaseClient) {
// get settings from runtime config
Expand Down Expand Up @@ -38,5 +39,5 @@ export const serverSupabaseClient = async <T = Database>(event: H3Event): Promis
})
}

return event.context._supabaseClient
return event.context._supabaseClient as SupabaseClient<T>
}
5 changes: 3 additions & 2 deletions src/runtime/server/services/serverSupabaseServiceRole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import type { SupabaseClient } from '@supabase/supabase-js'
import { createClient } from '@supabase/supabase-js'
import type { H3Event } from 'h3'
import { useRuntimeConfig } from '#imports'
import type { Database } from '#build/types/supabase-database'
// @ts-expect-error - `#supabase/database` is a runtime alias
import type { Database } from '#supabase/database'

export const serverSupabaseServiceRole = <T = Database>(event: H3Event): SupabaseClient<T> => {
export const serverSupabaseServiceRole: <T = Database>(event: H3Event) => SupabaseClient<T> = <T = Database>(event: H3Event) => {
const {
supabase: { serviceKey },
public: {
Expand Down

0 comments on commit 3f98640

Please sign in to comment.