Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions docs/typescript-generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,26 @@ const { data: users } = await supabaseAdmin.from('profiles').select()

## Using with the Hono adapter

The Hono context variable is typed as `SupabaseContext` (without the generic). To get typed clients, assert the type when destructuring:
Pass an app environment type to `Hono<Env>()` so Hono knows the `supabaseContext` variable includes your generated database types:

```ts
import { Hono } from 'hono'
import { withSupabase } from '@supabase/server/adapters/hono'
import type { SupabaseContext } from '@supabase/server'
import type { Database } from './database.types.ts'

const app = new Hono()
type Env = {
Variables: {
supabaseContext: SupabaseContext<Database>
}
}

const app = new Hono<Env>()

app.use('*', withSupabase({ auth: 'user' }))

app.get('/todos', async (c) => {
const { supabase } = c.var.supabaseContext as SupabaseContext<Database>
const { supabase } = c.var.supabaseContext
const { data } = await supabase.from('todos').select('id, title')
return c.json(data)
})
Expand Down
37 changes: 36 additions & 1 deletion src/adapters/hono/middleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
import { Hono } from 'hono'
import { describe, expect, it } from 'vitest'
import { describe, expect, expectTypeOf, it } from 'vitest'

import type { SupabaseContext } from '../../types.js'
import { withSupabase } from './middleware.js'

type Env = { Variables: { supabaseContext: SupabaseContext } }

type Database = {
public: {
Tables: {
todos: {
Row: { id: number; title: string }
Insert: { id?: number; title: string }
Update: { id?: number; title?: string }
Relationships: []
}
}
Views: {}
Functions: {}
Enums: {}
CompositeTypes: {}
}
}

type TypedEnv = {
Variables: {
supabaseContext: SupabaseContext<Database>
}
}

describe('hono supabase middleware', () => {
const env = {
url: 'https://test.supabase.co',
Expand Down Expand Up @@ -34,6 +57,18 @@ describe('hono supabase middleware', () => {
expect(body.hasAdmin).toBe(true)
})

it('uses the Hono app env to type the Supabase context', async () => {
const app = new Hono<TypedEnv>()
app.use('*', withSupabase({ auth: 'none', env }))
app.get('/', (c) => {
const ctx = c.get('supabaseContext')
expectTypeOf(ctx).toEqualTypeOf<SupabaseContext<Database>>()
return c.json({ authMode: ctx.authMode })
})
const res = await app.request('/')
expect(res.status).toBe(200)
})

it('throws HTTPException on auth failure', async () => {
const app = new Hono()
app.use('*', withSupabase({ auth: 'user', env }))
Expand Down
9 changes: 8 additions & 1 deletion src/adapters/hono/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ import type { SupabaseContext, WithSupabaseConfig } from '../../types.js'
* ```ts
* import { Hono } from 'hono'
* import { withSupabase } from '@supabase/server/adapters/hono'
* import type { SupabaseContext } from '@supabase/server'
*
* const app = new Hono()
* type Env = {
* Variables: {
* supabaseContext: SupabaseContext
* }
* }
*
* const app = new Hono<Env>()
* app.use('*', withSupabase({ auth: 'user' }))
*
* app.get('/profile', async (c) => {
Expand Down
Loading