@asterflow/request
v2.2.0
Published
Wraps the native request object of Bun, Node, Express, or Fastify into one typed AsterRequest, so the rest of AsterFlow reads requests the same way regardless of runtime.
Readme
@asterflow/request
Wraps the native request object of Bun, Node, Express, or Fastify into one typed
AsterRequest, so the rest of AsterFlow reads requests the same way regardless of runtime.
📦 Installation
bun install @asterflow/request✨ Features
AsterRequest- a single class withgetBody(),getHeaders(),getMethod(),getPathname(), and a parsedurl(from@asterflow/url-parser), no matter which runtime produced it.- Runtime factories -
createBunRequest,createNodeRequest,createExpressRequest, andcreateFastifyRequesteach adapt their native request object into anAsterRequest. - Body parsing per runtime - Bun and Node read and parse the raw body (JSON or
x-www-form-urlencoded); Express and Fastify reuse the body their own middleware already parsed. .extend(extension)- attaches extra typed properties to a request instance (used by plugins like multipart to add parsed file data) and returns the widened type.- Typed by
Runtime-AsterRequest<Drive>is generic over theRuntimeenum from@asterflow/adapter, sorawis typed as the correct native request for that runtime.
❓ How to Use
Adapt a native request to AsterRequest and read from it the same way regardless of runtime:
import { createBunRequest } from '@asterflow/request'
Bun.serve({
async fetch(req) {
const request = createBunRequest(req)
const body = await request.getBody()
const { pathname } = request.url // parsed by @asterflow/url-parser
}
})Swap the factory and the rest of the code stays the same:
import { createExpressRequest } from '@asterflow/request'
// const request = createExpressRequest(req)🔗 Related Packages
- @asterflow/adapter - supplies the
Runtimeenum this package's types key off of, and calls this package'screate*Requestfactories to build requests for each runtime - asterflow - core framework, uses
Requestas the request type passed into handlers - @asterflow/router - imports
Request/AsterRequestas the request type for route and middleware handlers - @asterflow/multipart - extends
AsterRequestvia.extend()to attach parsed multipart form data
📄 License
This project is licensed under the MIT License.
