Lambda Voie
Project archived
Voie was initially created to migrate some old projects to Typescript and a more modern router engine, aiming to be as effortlessly as possible, as well as a personal experience development (also because there was no known good existing package that meets the needs).
For newer projects, I recommend moving to Hono
or Unjs's Nitro
/H3
.
While a fan of Unjs, currently I would prefer Hono
because their documentation covers a lot more things, and their middleware + generics binding + RPC is really good, also things like lambda response stream + compression just work!
Voie (French word for "way/path/lane/route", English for... "Very Opinionated Itinerary Editor"?)
Nah, just a random word I came up for this package, haha.
Voie is a simple router + middleware wrapper/engine for AWS Lambda with the main purpose of making things easier.
Features
- find-my-way: A crazy fast (used by Fastify) HTTP router, internally uses an highly performant Radix Tree (aka compact Prefix Tree), supports route params, wildcards.
- Clean syntax:
app.route(method, path, handler)
, chainable to add middlewares easily: Example - Packed to relieve headache:
- event.route: easy access object that contains:
- { method, path, params: postBody+searchParams+parametricRoute, cookies }
- postBody: event.body is automatically parsed and added to params if is object type
- cookies: event.cookies will be parsed to Record type if exists, can be undefined.
- response(statusCode, body, options): with support for
compress
- plugins:
- cors: app.use(cors, { routes: ['/corsEnabledPath/*', 'someAPI'] })
Usage
Install package:
npm install lambda-voie
yarn add lambda-voie
pnpm install lambda-voie
Import:
import { Voie } from 'lambda-voie'
Deployed index.mjs
example:
import {
Voie,
cors,
logger,
} from 'lambda-voie'
const app = new Voie({
})
app.use(cors, {
})
app.setDefaultRoute((event, context) => app.respone(400, {
message: 'Route not found',
routeInfo: event.route
}))
app.logger.info('hi')
app.route('GET', '/test', (event, context) => ({
statusCode: 200,
body: 'Success',
before: event.addedByBefore,
willBeAddedChangedByAfter: 4,
alsoAddedByAnotherBefore: event.addedByBefore2,
}))
.before((event, context) => { event.addedByBefore = 'Hi' })
.after((event, context, res) => { res.willBeChangedByAfter = res.willBeChangedByAfter * 4 })
app.route('GET', '/test')
.before((event, context) => { event.addedByBefore2 = 'Hi' })
.after((event, context, res) => { res.addedByAfter = 'This wasnt defined' })
app.eventRoute('aws:s3', 'log S3 PutObject', (Record, context) => {
if (Record.eventName === 'ObjectCreated:Put')
logger.info(`S3 Put: ${Record.s3.bucket.name}/${Record.s3.object.key}`)
})
export const handler = app.handle()
For advanced use cases, you can extend the class and modify Voie's behavior:
class MyVoie extends Voie {
makeOnHandler(route: Route) {
return this._lookupTransform(({ method, url, event, context, params, store, searchParams }) => {
event._trackingId = '🦄'
return this.routeHandler(route, event, context)
})
}
}
Roadmap
License
MIT License © 2023 NamesMT