Eden Fetch â
A fetch-like alternative to Eden Treaty .
With Eden Fetch can interact with Elysia server in a type-safe manner using Fetch API.
First export your existing Elysia server type:
// server.ts
import { Elysia, t } from 'elysia'
const app = new Elysia()
.get('/', () => 'Hi Elysia')
.get('/id/:id', ({ params: { id } }) => id)
.post('/mirror', ({ body }) => body, {
body: t.Object({
id: t.Number(),
name: t.String()
})
})
.listen(8080)
export type App = typeof app
// server.ts
import { Elysia, t } from 'elysia'
const app = new Elysia()
.get('/', () => 'Hi Elysia')
.get('/id/:id', ({ params: { id } }) => id)
.post('/mirror', ({ body }) => body, {
body: t.Object({
id: t.Number(),
name: t.String()
})
})
.listen(8080)
export type App = typeof app
Then import the server type, and consume the Elysia API on client:
// client.ts
import { edenFetch } from '@elysiajs/eden'
import type { App } from './server'
const fetch = edenFetch<App>('http://localhost:8080')
// response type: 'Hi Elysia'
const pong = await fetch('/', {})
// response type: 1895
const id = await fetch('/id/:id', {
params: {
id: '1895'
}
})
// response type: { id: 1895, name: 'Skadi' }
const nendoroid = await fetch('/mirror', {
method: 'POST',
body: {
id: 1895,
name: 'Skadi'
}
})
// client.ts
import { edenFetch } from '@elysiajs/eden'
import type { App } from './server'
const fetch = edenFetch<App>('http://localhost:8080')
// response type: 'Hi Elysia'
const pong = await fetch('/', {})
// response type: 1895
const id = await fetch('/id/:id', {
params: {
id: '1895'
}
})
// response type: { id: 1895, name: 'Skadi' }
const nendoroid = await fetch('/mirror', {
method: 'POST',
body: {
id: 1895,
name: 'Skadi'
}
})
Error Handling â
You can handle errors the same way as Eden Treaty:
// response type: { id: 1895, name: 'Skadi' }
const { data: nendoroid, error } = app.mirror.post({
id: 1895,
name: 'Skadi'
})
if(error) {
switch(error.status) {
case 400:
case 401:
warnUser(error.value)
break
case 500:
case 502:
emergencyCallDev(error.value)
break
default:
reportError(error.value)
break
}
throw error
}
const { id, name } = nendoroid
// response type: { id: 1895, name: 'Skadi' }
const { data: nendoroid, error } = app.mirror.post({
id: 1895,
name: 'Skadi'
})
if(error) {
switch(error.status) {
case 400:
case 401:
warnUser(error.value)
break
case 500:
case 502:
emergencyCallDev(error.value)
break
default:
reportError(error.value)
break
}
throw error
}
const { id, name } = nendoroid
When should I use Eden Fetch over Eden Treaty â
Unlike Elysia < 1.0, Eden Fetch is not faster than Eden Treaty anymore.
The preference is base on you and your team agreement, however we recommend to use Eden Treaty instead.
For Elysia < 1.0:
Using Eden Treaty requires a lot of down-level iteration to map all possible types in a single go, while in contrast, Eden Fetch can be lazily executed until you pick a route.
With complex types and a lot of server routes, using Eden Treaty on a low-end development device can lead to slow type inference and auto-completion.
But as Elysia has tweaked and optimized a lot of types and inference, Eden Treaty can perform very well in the considerable amount of routes.
If your single process contains more than 500 routes, and you need to consume all of the routes in a single frontend codebase, then you might want to use Eden Fetch as it has a significantly better TypeScript performance than Eden Treaty.