Skip to content

Commit 4310142

Browse files
authored
feat: add H3 adapter (#36)
* feat: add H3 adapter * feat: add handler overload to H3 adapter for Nuxt file routes * chore: reinstall and dedupe * refactor: use defineHandler middleware composition for per-route auth * test: sensible secret key mock
1 parent 0251690 commit 4310142

7 files changed

Lines changed: 287 additions & 5 deletions

File tree

README.md

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,56 @@ export default { fetch: app.fetch }
252252
253253
The adapter does not handle CORS — use `hono/cors` for that. Per-route auth works naturally by applying the middleware to specific routes.
254254
255+
### H3 / Nuxt
256+
257+
```ts
258+
import { H3 } from 'h3'
259+
import { withSupabase } from '@supabase/server/adapters/h3'
260+
261+
const app = new H3()
262+
263+
// Protected — withSupabase validates the JWT before the handler runs
264+
app.use(withSupabase({ allow: 'user' }))
265+
266+
app.get('/games', async (event) => {
267+
const { supabase } = event.context.supabaseContext
268+
const { data: myGames } = await supabase.from('favorite_games').select()
269+
return myGames
270+
})
271+
272+
// Public — no middleware means no auth
273+
app.get('/health', () => ({ status: 'ok' }))
274+
275+
export default { fetch: app.fetch }
276+
```
277+
278+
For **Nuxt**, use `defineHandler` for file routes:
279+
280+
```ts
281+
// server/api/games.get.ts
282+
import { defineHandler } from 'h3'
283+
import { withSupabase } from '@supabase/server/adapters/h3'
284+
285+
export default defineHandler({
286+
middleware: [withSupabase({ allow: 'user' })],
287+
handler: async (event) => {
288+
const { supabase } = event.context.supabaseContext
289+
return supabase.from('favorite_games').select()
290+
},
291+
})
292+
```
293+
294+
For app-wide auth, register it as a server middleware:
295+
296+
```ts
297+
// server/middleware/supabase.ts
298+
import { withSupabase } from '@supabase/server/adapters/h3'
299+
300+
export default withSupabase({ allow: 'user' })
301+
```
302+
303+
The adapter does not handle CORS — use H3's CORS utilities for that.
304+
255305
## Primitives
256306
257307
For when you need more control than `withSupabase` provides — multiple routes with different auth, custom response headers, or building your own wrapper.
@@ -377,9 +427,10 @@ For other environments, pass overrides via the `env` config option or `resolveEn
377427
378428
- **Supabase Edge Functions** — environment variables are auto-injected. Zero config.
379429
- **Deno / Bun** — works out of the box with the `export default { fetch }` pattern.
380-
- **Node.js** — use the [Hono adapter](#hono) or [core primitives](#primitives) with your framework of choice.
430+
- **Node.js** — use the [Hono adapter](#hono), [H3 adapter](#h3--nuxt), or [core primitives](#primitives) with your framework of choice.
381431
- **Cloudflare Workers**enable `nodejs_compat` in `wrangler.toml` or pass env overrides via the `env` config option.
382-
- **Next.js / Nuxt / SvelteKit / Remix** — use core primitives to build a cookie-based auth adapter. See [`docs/ssr-frameworks.md`](docs/ssr-frameworks.md).
432+
- **Nuxt** — use the [H3 adapter](#h3--nuxt) directly as a server middleware.
433+
- **Next.js / SvelteKit / Remix** — use core primitives to build a cookie-based auth adapter. See [`docs/ssr-frameworks.md`](docs/ssr-frameworks.md).
383434
384435
## Exports
385436
@@ -388,6 +439,7 @@ For other environments, pass overrides via the `env` config option or `resolveEn
388439
| `@supabase/server` | `withSupabase`, `createSupabaseContext` |
389440
| `@supabase/server/core` | `verifyAuth`, `verifyCredentials`, `extractCredentials`, `createContextClient`, `createAdminClient`, `resolveEnv` |
390441
| `@supabase/server/adapters/hono` | `withSupabase` (Hono middleware) |
442+
| `@supabase/server/adapters/h3` | `withSupabase` (H3 / Nuxt middleware) |
391443
392444
## Documentation
393445

package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
"import": "./dist/adapters/hono/index.mjs",
3535
"require": "./dist/adapters/hono/index.cjs"
3636
},
37+
"./adapters/h3": {
38+
"types": "./dist/adapters/h3/index.d.mts",
39+
"import": "./dist/adapters/h3/index.mjs",
40+
"require": "./dist/adapters/h3/index.cjs"
41+
},
3742
"./package.json": "./package.json"
3843
},
3944
"main": "./dist/index.cjs",
@@ -69,9 +74,13 @@
6974
},
7075
"peerDependencies": {
7176
"@supabase/supabase-js": "^2.0.0",
72-
"hono": "^4.0.0"
77+
"hono": "^4.0.0",
78+
"h3": "^2.0.0"
7379
},
7480
"peerDependenciesMeta": {
81+
"h3": {
82+
"optional": true
83+
},
7584
"hono": {
7685
"optional": true
7786
}
@@ -81,6 +90,7 @@
8190
"@commitlint/config-conventional": "^20.4.2",
8291
"@supabase/supabase-js": "^2.98.0",
8392
"eslint": "^10.0.2",
93+
"h3": "2.0.1-rc.20",
8494
"hono": "^4.12.5",
8595
"prettier": "3.8.1",
8696
"pretty-quick": "^4.2.2",

pnpm-lock.yaml

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/adapters/h3/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* H3 framework adapter for `@supabase/server`.
3+
*
4+
* @packageDocumentation
5+
*/
6+
7+
export { withSupabase } from './middleware.js'

0 commit comments

Comments
 (0)