@brielov/metal

Metal is a simple, minimal and type-safe router to use with node's `http` module. It is written in `typescript` and it's main feature is incomming data validation.

Usage no npm install needed!

<script type="module">
  import brielovMetal from 'https://cdn.skypack.dev/@brielov/metal';
</script>

README

Usage

import { Metal, y } from "@metal/core";
import { createServer } from "http";

const m = new Metal();
const server = createServer(m.handler);

server.listen(4000, () => console.log(`Listening on http://localhost:4000`));

m.add("POST", "/users", async (ctx) => {
  // This is type safe, since if the validation fails it will respond with 400.
  const { email, password } = await ctx.body({
    email: y.string().email().trim().lowercase().required(),
    password: y.string().min(8).required(),
  });

  // Save to the database
  const user = await db.user.create({ email, password });

  return {
    status: 201,
    body: {
      user,
    },
  };
});