@asterflow/adapter
v2.2.0
Published
Wires a runtime's native server (Bun.serve, Node's http, Express, Fastify) into AsterFlow, converting native requests to Request and sending back AsterResponse the same way regardless of runtime.
Maintainers
Readme
@asterflow/adapter
Wires a runtime's native server (Bun.serve, Node's
http, Express, Fastify) into AsterFlow, converting native requests toRequestand sending backAsterResponsethe same way regardless of runtime.
📦 Installation
bun install @asterflow/adapter✨ Features
adapters- a ready-madeAdapterinstance for each runtime:adapters.bun,adapters.node,adapters.express,adapters.fastifyRuntime- the enum (Bun,Node,Express,Fastify) that types requests, listen arguments, and adapters to their runtimeAdapter- a small class holding aruntime, alisten(...)function typed to that runtime's nativelisten/servesignature, and anonRequesthook that AsterFlow assigns to route native requests into its handler- Request conversion per runtime - each adapter calls the matching
create*Requestfactory from@asterflow/request(createBunRequest,createNodeRequest,createExpressRequest,createFastifyRequest) before handing the request toonRequest - Fallback response - if
listen()is called beforeonRequestis set, every adapter responds with a 500 instead of crashing - Error-to-response conversion -
toErrorResponse(err)turns a thrown value (anError, an already-builtAsterResponse, or anything else) into a JSONAsterResponse; the Node adapter uses it to catch per-request errors and return a 500 instead of hanging the connection
❓ How to Use
Pick an adapter and pass it as the driver when creating an AsterFlow app — everything else (routes, .listen()) stays the same:
import { AsterFlow } from 'asterflow'
import { adapters } from '@asterflow/adapter'
const app = new AsterFlow({ driver: adapters.bun }) // or adapters.node, adapters.express, adapters.fastify
app.listen({ port: 3000 })Express and Fastify need their own instance passed through listen, since AsterFlow mounts a catch-all route on it rather than starting its own server:
import { AsterFlow } from 'asterflow'
import { adapters } from '@asterflow/adapter'
import express from 'express'
const app = new AsterFlow({ driver: adapters.express })
app.listen(express(), 3000)🔗 Related Packages
- @asterflow/request - supplies the
create*Requestfactories each adapter calls to build a typedRequestfrom the runtime's native one - @asterflow/response -
AsterResponseis whatonRequestmust return; adapters convert it to the runtime's native response and use it to build fallback/error responses - Depended on by asterflow - the core framework picks a
driverfromadapters, sets itsonRequest, and delegatesapp.listen(...)todriver.listen(...)
📄 License
This project is licensed under the MIT License.
