Skip to main content

@rtorcato/api-upload-hono v0.1.0

Interfaces

UploadedFile

Defined in: index.ts:6

A file uploaded to S3 — the multipart file plus the object details S3 returns.

Properties

fieldname

fieldname: string

Defined in: index.ts:7

originalname

originalname: string

Defined in: index.ts:8

mimetype

mimetype: string

Defined in: index.ts:9

size

size: number

Defined in: index.ts:10

bucket

bucket: string

Defined in: index.ts:12

Bucket the object was written to.

key

key: string

Defined in: index.ts:14

Object key.

location

location: string

Defined in: index.ts:16

Public URL / S3 location of the object.

etag

etag: string

Defined in: index.ts:17

contentType

contentType: string

Defined in: index.ts:18


UploadOptions

Defined in: index.ts:21

Properties

s3

s3: S3Client

Defined in: index.ts:23

S3 client the object is written with (you own its config/credentials).

bucket

bucket: string

Defined in: index.ts:25

Destination bucket.

field

field: string

Defined in: index.ts:27

Multipart form field holding the file.

key

key: string | ((c, file) => string)

Defined in: index.ts:29

Object key — a string, or a function of the request context + file for deterministic keys.

isPublic?

optional isPublic?: boolean

Defined in: index.ts:31

public-read ACL when true, otherwise private. Default: false.

cacheControl?

optional cacheControl?: string

Defined in: index.ts:33

Cache-Control stored on the object. Default: max-age=31536000 (1 year).

metadata?

optional metadata?: Record<string, string>

Defined in: index.ts:35

Extra object metadata.

maxSizeBytes?

optional maxSizeBytes?: number

Defined in: index.ts:37

Max upload size in bytes; exceeding it rejects with 413 before the object is written to S3.

Functions

uploadFile()

uploadFile(c, options): Promise<UploadedFile>

Defined in: index.ts:63

Upload a single file from a multipart request straight to S3, resolving with the stored object's details.

Hono counterpart to @rtorcato/api-upload (Express): parses the multipart body via c.req.parseBody() and writes the file with the AWS SDK directly — Hono has no multer / multer-s3 equivalent to wrap.

Rejects with an HttpError (from @rtorcato/api-errors): 413 file_too_large when maxSizeBytes is exceeded and 400 no_file when the field is empty or not a file — so it slots into the error-handler middleware.

Example

app.post('/avatar', async (c) => {
const file = await uploadFile(c, {
s3, bucket: 'avatars', field: 'avatar',
key: (ctx) => `users/${ctx.get('userId')}.png`, isPublic: true,
})
return c.json({ url: file.location })
})

Parameters

c

Context

options

UploadOptions

Returns

Promise<UploadedFile>