Skip to content

参数

¥Parameters

我们最终需要将有效负载发送到服务器。

¥We need to send a payload to server eventually.

为了处理这种情况,Eden Treaty 的方法接受两个参数来将数据发送到服务器。

¥To handle this, Eden Treaty's methods accept 2 parameters to send data to server.

两个参数都是类型安全的,并将由 TypeScript 自动引导:

¥Both parameters are type safe and will be guided by TypeScript automatically:

  1. body

  2. 附加参数

    • query

    • headers

    • fetch

typescript
import { Elysia, t } from 'elysia'
import { treaty } from '@elysiajs/eden'

const app = new Elysia()
    .post('/user', ({ body }) => body, {
        body: t.Object({
            name: t.String()
        })
    })
    .listen(3000)

const api = treaty<typeof app>('localhost:3000')

// ✅ works
api.user.post({
    name: 'Elysia'
})

// ✅ also works
api.user.post({
    name: 'Elysia'
}, {
    // This is optional as not specified in schema
    headers: {
        authorization: 'Bearer 12345'
    },
    query: {
        id: 2
    }
})

除非该方法不接受 body,否则 body 将被省略,只保留一个参数。

¥Unless if the method doesn't accept body, then body will be omitted and left with single parameter only.

如果方法 "GET" 或 "HEAD":

¥If the method "GET" or "HEAD":

  1. 附加参数

    • query

    • headers

    • fetch

typescript
import { Elysia } from 'elysia'
import { treaty } from '@elysiajs/eden'

const app = new Elysia()
    .get('/hello', () => 'hi')
    .listen(3000)

const api = treaty<typeof app>('localhost:3000')

// ✅ works
api.hello.get({
    // This is optional as not specified in schema
    headers: {
        hello: 'world'
    }
})

空体

¥Empty body

如果 body 是可选的或不需要,但查询或标头是必需的,你可以将 body 作为 nullundefined 传递。

¥If body is optional or not need but query or headers is required, you may pass the body as null or undefined instead.

typescript
import { Elysia, t } from 'elysia'
import { treaty } from '@elysiajs/eden'

const app = new Elysia()
    .post('/user', () => 'hi', {
        query: t.Object({
            name: t.String()
        })
    })
    .listen(3000)

const api = treaty<typeof app>('localhost:3000')

api.user.post(null, {
    query: {
        name: 'Ely'
    }
})

获取参数

¥Fetch parameters

Eden Treaty 是一个获取封装器,我们可以通过将任何有效的 获取 参数传递给 $fetch 来向 Eden 添加它:

¥Eden Treaty is a fetch wrapper, we may add any valid Fetch parameters to Eden by passing it to $fetch:

typescript
import { Elysia, t } from 'elysia'
import { treaty } from '@elysiajs/eden'

const app = new Elysia()
    .get('/hello', () => 'hi')
    .listen(3000)

const api = treaty<typeof app>('localhost:3000')

const controller = new AbortController()

const cancelRequest = setTimeout(() => {
    controller.abort()
}, 5000)

await api.hello.get({
    fetch: {
        signal: controller.signal
    }
})

clearTimeout(cancelRequest)

文件上传

¥File Upload

我们可以将以下任一内容传递给附加文件:

¥We may either pass one of the following to attach file(s):

  • 文件

  • File[]

  • FileList

  • Blob

附加文件会导致 content-type 变为 multipart/form-data。

¥Attaching a file will results content-type to be multipart/form-data

假设我们的服务器如下:

¥Suppose we have the server as the following:

typescript
import { Elysia, t } from 'elysia'
import { treaty } from '@elysiajs/eden'

const app = new Elysia()
    .post('/image', ({ body: { image, title } }) => title, {
        body: t.Object({
            title: t.String(),
            image: t.Files()
        })
    })
    .listen(3000)

export const api = treaty<typeof app>('localhost:3000')

const images = document.getElementById('images') as HTMLInputElement

const { data } = await api.image.post({
    title: "Misono Mika",
    image: images.files!,
})