Expedite
Build webservers faster and more type-safely
Get Started
Expedite can be installed with npm
or yarn
:
npm install --save @zensors/expedite
yarn add @zensors/expedite
Example
// blog.ts
import { Router, marshalParams, marshalBody } from "@zensors/expedite";
import { M } from "@zensors/sheriff";
import { authenticateAdmin, getPost, createPost, generateSlug } from "...";
const BlogRouter = new Router();
BlogRouter.get("/:slug")
.then(marshalParams(M.obj({ slug: M.str })))
.return(async (req) => {
return await getPost(req.slug); // because we marshalled the params, TS knows that req.slug is of type string
});
const BlogRouterWithAuth = BlogRouter.then(authenticateAdmin);
// If authenticateAdmin throws an error if the requester isn't an admin, then all endpoints chained off of
// BlogRouterWithAuth are only accessible by admins
BlogRouterWithAuth.post("/")
.then(marshalBody(M.obj({ title: M.str, author: M.str, date: M.num, content: M.str })))
.finish(async (req, res) => {
// Because we marshalled the body, we can use req.body without having everything typed as any
const slug = generateSlug(req.body.title);
await createPost(slug, req.body);
res.redirect(`/${slug}`);
});
export default BlogRouterWithAuth;
// index.ts
import { Router } from "@zensors/expedite";
import { MarshalError } from "@zensors/sheriff";
import express from "express";
import bp from "body-parser";
import BlogRouter from "./blog";
// Contstruct the main router
const mainRouter =
new Router()
.use("/blog", BlogRouter);
// Initialize the app
const app = express();
app.use(bp.json()); // Expedite doesn't parse the body for you; use body-parser for that
app.use(mainRouter.toExpress());
app.use((error: any, req, res, next) => {
// You are expected to handle your own errors
if (error instanceof MarshalError) { // Add a "bad request" handler for marshal errors
res.status(400).json({ message: error.message });
} else {
res.status(500).json({ message: "Internal server error" });
}
});
app.listen(8080);