Skip to content
/ raptor Public

Fast router handler for Deno and Deno Deploy

License

Notifications You must be signed in to change notification settings

nhttp/raptor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Raptor

License deno.land PRs Welcome deps badge cache badge

Fast router handler for Deno server and Deploy.

Raptor implemented to nhttp

Features

  • Middleware support.
  • Sub Router support.

Usage

import { serve } from "https://deno.land/std@0.118.0/http/server.ts";
import { JsonResponse, raptor } from "https://deno.land/x/raptor@0.0.8/mod.ts";

serve(
  raptor()
    .make("GET", () => new Response("Hello World"))
    .make("GET/hello/:name", (req) => new Response(`Hello ${req.params.name}`))
    .make("GET/hello-json", () => new JsonResponse({ name: "raptor" }))
    .resolve,
);

console.log("Raptor was here !!");

Run

deno run --allow-net file.ts

and visit http://localhost:8000 with path / or /hello-json or /hello/yourname

Make

Make everything with raptor().make(verb, ...fns).

the verb is => GET | POST | DELETE | PUT | PATCH | OPTIONS | HEAD | ANY | ROUTER | WARE | ERROR | 404 and path.

Make Method Handlers

serve(
  raptor()
    .make("GET/hello/:name", (req) => new Response(`Hello ${req.params.name}`))
    .resolve,
);

Make Global Middleware

serve(
  raptor()
    .make("WARE", (req, next) => {
      req.foo = "foo";
      return next();
    })
    .make("GET/hello", (req) => new Response(`Hello ${req.foo}`))
    .resolve,
);

If Inline Middleware

serve(
  raptor()
    .make("GET/hello", (req, next) => {
      req.foo = "foo";
      return next();
    }, (req) => {
      return new Response(`Hello ${req.foo}`);
    })
    .resolve,
);

Make Sub Router

const router = raptor.createRouter();
router.make("GET/user", () => new Response("Hello from router"));

serve(
  raptor()
    .make("ROUTER/api/v1", [router])
    .resolve,
);

// visit http://localhost:8000/api/v1/user

Make onError

Global error response.

serve(
  raptor()
    // example
    .make(
      "ERROR",
      (err, req) => new Response(err.message, { status: err.status || 500 }),
    )
    .resolve,
);

Make on404

Global 404 response.

serve(
  raptor()
    // example
    .make("404", (req) => new Response("404 not found url", { status: 404 }))
    .resolve,
);

See examples

License

MIT