@asterflow/router
v2.2.0
Published
Typed route and middleware definitions (Method, Router, Middleware) for AsterFlow applications.
Maintainers
Readme
@asterflow/router
Typed route and middleware definitions (
Method,Router,Middleware) for AsterFlow applications.
📦 Installation
bun install @asterflow/router✨ Features
Method- defines a single route bound to one HTTP verb, passed as the first argument (new Method('post', {...})orMethod.POST).Method.create(method)defers the handler so plugins can chain in request extensions (e.g..multipart({...})) before the terminal.handler(fn)call.Router- groups handlers for several HTTP verbs under one path.Router.builder()gives an extensible, per-method builder (.method('post', b => ...)) so each verb can carry its own request extensions independently.Middleware- typed middleware chain.onRuneither callsnext(params)to continue (mergingparamsinto the typedmiddlewarecontext) or returns anAsterResponseto short-circuit the chain.- Schema validation - route schemas accept a Zod schema or a
@caeljs/tshshape; the parsed result is inferred straight into the handler'sschemaargument. - Extension registry - a
WeakMap-based store (extensionRegistry.ts) that plugins use to attach per-router, per-method data, read back at request time.
❓ How to Use
For a normal route, use new Method(method, options) - the HTTP method comes first, either as a plain string or as one of Method's own constants (Method.GET, Method.POST, ...) - and validate the body with Zod:
import { Method } from '@asterflow/router'
import { z } from 'zod'
export default new Method(Method.POST, {
path: '/users',
schema: z.object({ name: z.string(), email: z.string().email() }),
handler({ schema, response }) {
return response.created({ user: schema })
}
})Only reach for Method.create(method, options?) when you need a plugin's fully-typed extension - like multipart's .multipart(schema) - chained in before the handler. create() defers the handler so the extension can widen the request type first, and options is optional:
import { Method } from '@asterflow/router'
export default Method.create(Method.POST)
.multipart({
avatar: { mimeTypes: ['image/png', 'image/jpeg'], maxSize: 5 * 1024 * 1024, required: true }
})
.handler(({ request, response }) => {
const avatar = request.getFile('avatar')
return response.success({ filename: avatar.filename })
})Both examples use export default: it's what app.controller(route) expects when you register a route by hand, and it's the export @asterflow/fs's file-based routing looks for in every route file.
🔗 Related Packages
- asterflow - core framework, depends on this package to register and run routes
- @asterflow/request - request abstraction; this package imports its
Request/AsterRequesttypes for route and middleware handlers - @asterflow/fs - filesystem-based routing plugin, builds routes with
Method/Router - @asterflow/multipart - multipart upload plugin, extends
Methodroutes
📄 License
This project is licensed under the MIT License.
