Skip to content
Open
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
20 changes: 20 additions & 0 deletions docs/typescript-generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,26 @@ app.get('/todos', async (c) => {
})
```

Alternatively, skip the `Env` type and thread `Database` through the `withSupabase<Database>()` generic when using chained declarations. Hono infers the `supabaseContext` variable type from the middleware, so `c.var.supabaseContext.supabase` is still a fully typed `SupabaseClient<Database>`. This plays a little nicer with hono [sub-application routes](https://hono.dev/docs/helpers/route#working-with-sub-applications).

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

const app = new Hono()

const todosApp = new Hono()
.use(withSupabase<Database>({ auth: 'user' }))
.get('/', async (c) => {
const { supabase } = c.var.supabaseContext
const { data } = await supabase.from('todos').select('id, title')
return c.json(data)
})

app.route('/todos', todosApp)
```

## Custom schema

If your tables are in a schema other than `public`, pass it via `supabaseOptions`:
Expand Down
16 changes: 16 additions & 0 deletions src/adapters/hono/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ describe('hono supabase middleware', () => {
expect(res.status).toBe(200)
})

it('uses the Hono adapter to type the Supabase context', async () => {
// match docs example in typescript-generics.md
const app = new Hono()
const rootApp = new Hono()
.use(withSupabase<Database>({ auth: 'none', env }))
.get('/', (c) => {
const ctx = c.var.supabaseContext
expectTypeOf(ctx).toEqualTypeOf<SupabaseContext<Database>>()
return c.json({ authMode: ctx.authMode })
})
app.route('/', rootApp)

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
13 changes: 9 additions & 4 deletions src/adapters/hono/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ import type { SupabaseContext, WithSupabaseConfig } from '../../types.js'
*
* @category Adapters
*/
export function withSupabase(
export function withSupabase<Database = unknown>(
config?: Omit<WithSupabaseConfig, 'cors'>,
): MiddlewareHandler<{ Variables: { supabaseContext: SupabaseContext } }> {
): MiddlewareHandler<{
Variables: { supabaseContext: SupabaseContext<Database> }
}> {
return createMiddleware<{
Variables: { supabaseContext: SupabaseContext }
Variables: { supabaseContext: SupabaseContext<Database> }
}>(async (c, next) => {
// Skip if a previous middleware already set the context.
// This enables route-level overrides: a route can use withSupabase({ auth: 'secret' })
Expand All @@ -55,7 +57,10 @@ export function withSupabase(
return
}

const { data: ctx, error } = await createSupabaseContext(c.req.raw, config)
const { data: ctx, error } = await createSupabaseContext<Database>(
c.req.raw,
config,
)
if (error) {
throw new HTTPException(error.status as 401 | 500, {
message: error.message,
Expand Down
Loading