0.5.0 • Published 5 years ago

micro-map-router v0.5.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

micro-map-router

Routes for lambdas.

Associate functions with the HTTP methods they should be invoked on.

Just define a Map where keys are async functions and values are arrays of allowed HTTP methods.

npm i micro-map-router

./lambda.ts

import { router } from "micro-map-router";
import { send, json } from "micro";
import { IncomingMessage, ServerResponse } from "http";

export default router(
  new Map([
    [
      // only invoked on GET requests
      async (req: IncomingMessage, res: ServerResponse) => {
        send(res, 200, {});
      },
      ["GET"]
    ],
    [
      // only invoked on POST and PUT requests
      async (req: IncomingMessage, res: ServerResponse) => {
        const { user } = await json(req);
        send(res, 200, { user });
      },
      ["POST", "PUT"]
    ]
  ])
);
0.5.0

5 years ago

0.4.0

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago