@zensors/expedite
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Expedite

Build webservers faster and more type-safely

npm npm type definitions


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);

Readme

Keywords

none

Package Sidebar

Install

npm i @zensors/expedite

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

30.5 kB

Total Files

7

Last publish

Collaborators

  • zensorsinc
  • gyf304
  • zwad3
  • porygon