Skip to content

Eden 获取

¥Eden Fetch

这是一个类似于 Eden Treaty 的替代方案。

¥A fetch-like alternative to Eden Treaty.

借助 Eden Fetch,你可以使用 Fetch API 以类型安全的方式与 Elysia 服务器交互。

¥With Eden Fetch, you can interact with Elysia server in a type-safe manner using Fetch API.


首先导出你现有的 Elysia 服务器类型:

¥First export your existing Elysia server type:

typescript
// server.ts
import { Elysia, t } from 'elysia'

const app = new Elysia()
    .get('/hi', () => 'Hi Elysia')
    .get('/id/:id', ({ params: { id } }) => id)
    .post('/mirror', ({ body }) => body, {
        body: t.Object({
            id: t.Number(),
            name: t.String()
        })
    })
    .listen(3000)

export type App = typeof app

然后导入服务器类型,并在客户端使用 Elysia API:

¥Then import the server type, and consume the Elysia API on client:

typescript
import { edenFetch } from '@elysiajs/eden'
import type { App } from './server'

const fetch = edenFetch<App>('http://localhost:3000')

// response type: 'Hi Elysia'
const pong = await fetch('/hi', {})

// 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

你可以像《Eden 条约》一样处理错误:

¥You can handle errors the same way as Eden Treaty:

typescript
import { edenFetch } from '@elysiajs/eden'
import type { App } from './server'

const fetch = edenFetch<App>('http://localhost:3000')

// response type: { id: 1895, name: 'Skadi' }
const { data: nendoroid, error } = await fetch('/mirror', {
    method: 'POST',
    body: {
        id: 1895,
        name: 'Skadi'
    }
})

if(error) {
    switch(error.status) {
        case 400:
        case 401:
            throw error.value
            break

        case 500:
        case 502:
            throw error.value
            break

        default:
            throw error.value
            break
    }
}

const { id, name } = nendoroid

何时应该使用 Eden Fetch 而不是 Eden Treaty

¥When should I use Eden Fetch over Eden Treaty

与 Elysia < 1.0 版本不同,Eden Fetch 不再比 Eden Treaty 更快。

¥Unlike Elysia < 1.0, Eden Fetch is not faster than Eden Treaty anymore.

首选项基于你和你的团队的协议,但我们建议使用 Eden 条约

¥The preference is base on you and your team agreement, however we recommend to use Eden Treaty instead.

对于 Elysia < 1.0:

¥For Elysia < 1.0:

使用 Eden Treaty 需要大量的底层迭代才能一次性映射所有可能的类型,而 Eden Fetch 则可以延迟执行,直到你选择合适的路径。

¥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.

由于类型复杂且服务器路由众多,在低端开发设备上使用 Eden Treaty 会导致类型推断和自动补齐速度缓慢。

¥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.

但是由于 Elysia 对许多类型和推断进行了调整和优化,Eden Treaty 在相当多的路由上表现非常出色。

¥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.

如果你的单个进程包含超过 500 条路由,并且你需要在单个前端代码库中使用所有路由,那么你可能需要使用 Eden Fetch,因为它的 TypeScript 性能明显优于 Eden Treaty。

¥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.