Git Product home page Git Product logo

expedite's Introduction

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

expedite's People

Contributors

bluepichu avatar zwade avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

expedite's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.