@asterflow/response
v2.2.0
Published
Type-safe HTTP response builder with status-code helpers, header/cookie management, and binary file responses.
Readme
@asterflow/response
Type-safe HTTP response builder with status-code helpers, header/cookie management, and binary file responses.
📦 Installation
bun install @asterflow/response✨ Features
- Typed status codes —
response.status(code)narrows the accepted body type to what that code expects - Shorthand helpers —
success,created,noContent,badRequest,unauthorized,forbidden,notFound,validationError,internalServerError - JSON responses —
.json(data)sends the body and setsContent-Type: application/json - Binary/file responses —
.file(data, contentType?)sends aUint8Arrayas raw bytes; without acontentType, it's detected from the data's magic bytes (PNG, JPEG, GIF, PDF, BMP, ICO, GZIP, ZIP, WEBP), falling back toapplication/octet-stream - Headers and cookies —
setHeader/setCookie, each chainable and reflected in the response's type - Runtime conversion —
toResponse()for the standard WebResponse,toServerResponse(res)for Node'shttp.ServerResponse
❓ How to Use
import { AsterResponse } from '@asterflow/response'
const response = new AsterResponse()
.status(201)
.setHeader('X-Request-Id', 'abc123')
.json({ id: 1, name: 'User' })Sending a file works the same way — pass the bytes, and the content type is sniffed automatically if you don't already know it:
import { readFile } from 'fs/promises'
import { AsterResponse } from '@asterflow/response'
const data = await readFile('avatar.png')
const response = new AsterResponse().file(data) // Content-Type: image/pngConvert the finished response for whichever runtime you're on: response.toResponse() or response.toServerResponse(res).
🔗 Related Packages
- Depended on by @asterflow/adapter — builds error responses and passes
AsterResponseto its runtime adapters - Depended on by @asterflow/multipart — returns
AsterResponseerrors for malformed multipart requests - Depended on by asterflow — the core framework's request handler works with
AsterResponse
📄 License
This project is licensed under the MIT License.
