1.8 KiB
1.8 KiB
Expo Integration
Run Elysia on Expo (React Native)
What It Is
Create API routes in Expo app (SDK 50+, App Router v3).
Setup
- Create
app/[...slugs]+api.ts - Define Elysia server
- Export
Elysia.fetchas HTTP methods
// app/[...slugs]+api.ts
import { Elysia, t } from 'elysia'
const app = new Elysia()
.get('/', 'hello Expo')
.post('/', ({ body }) => body, {
body: t.Object({ name: t.String() })
})
export const GET = app.fetch
export const POST = app.fetch
Prefix for Non-Root
If placed in app/api/[...slugs]+api.ts, set prefix:
const app = new Elysia({ prefix: '/api' })
.get('/', 'Hello Expo')
export const GET = app.fetch
export const POST = app.fetch
Ensures routing works in any location.
Eden (End-to-End Type Safety)
- Export type:
// app/[...slugs]+api.ts
const app = new Elysia()
.get('/', 'Hello Nextjs')
.post('/user', ({ body }) => body, {
body: treaty.schema('User', { name: 'string' })
})
export type app = typeof app
export const GET = app.fetch
export const POST = app.fetch
- Create client:
// lib/eden.ts
import { treaty } from '@elysiajs/eden'
import type { app } from '../app/[...slugs]+api'
export const api = treaty<app>('localhost:3000/api')
- Use in components:
// app/page.tsx
import { api } from '../lib/eden'
export default async function Page() {
const message = await api.get()
return <h1>Hello, {message}</h1>
}
Deployment
- Deploy as normal Elysia app OR
- Use experimental Expo server runtime
With Expo runtime:
expo export
# Creates dist/server/_expo/functions/[...slugs]+api.js
Edge function, not normal server (no port allocation).
Adapters
- Express
- Netlify
- Vercel
pnpm
Manual install:
pnpm add @sinclair/typebox openapi-types