Skip to content

basalt / http/src / upload

Function: upload() ​

> upload(options): ZodType<UploadBody>

Defined in: http/src/upload.ts:119

Declares a streaming multipart/form-data body for a route — adapter-neutral: the same route accepts uploads on Fastify, Express and Hono.

ts
route({
  method: 'POST', url: '/documents',
  body: upload({ maxBytes: 20 * 1024 * 1024, maxFiles: 1, allowedTypes: ['application/pdf'] }),
  meta: { auth: true },
  async handler({ body }) {
    for await (const file of body.files) await files().upload({ ..., body: file.stream })
  },
})

The whole pipeline — pre-hooks (rate limit), enrichers (tenant, user) and guards (auth, permissions) — runs BEFORE a single body byte is read. The body is then parsed as the handler consumes it, never buffered, with every limit enforced on the bytes actually received (a declared Content-Length over maxBytes is refused up front). An upload the handler leaves unread is drained (up to maxBytes) and the connection closed, so nothing hangs.

Parameters ​

options ​

UploadOptions

Returns ​

ZodType<UploadBody>

Released under the MIT License.