Skip to main content
Deno 2 is finally here 🎉️
Learn more

Functor

It was a 10-hour project…

Functor was designed to be:

  • Independent (doesn’t required other dependencies!)
  • The Fastest Deno router

Types

  • Request

Where you have to return a Response

//fun Router is just a router so a server that give a request and expect  Response is needed
import { serve } from "https://deno.land/std@0.159.0/http/server.ts";
// import fun

import fun from "../../fun.ts";

await serve(
  fun()([
    {
      type: "response",
      path: "/",
      r: (_) => new Response("hello world"),
    },
  ]),
  { port: 8080 },
);
  • Response

Return a BodyInt and you can add Headers and status code, you have access to the Request

await serve(
  fun()([
    {
      path: "/",
      r: (_) => "hello world",
    },
  ]),
  { port: 8080 },
);
  • Params

Return a BodyInt and you can add Headers and status code, you have access to the Request and params

It’s required to add the elements

await serve(
  fun()([
    {
      path: "/test/:id",
      r: (f) => f.param.id,
    },
  ]),
  { port: 8080, hostname: "127.0.0.1" },
);
  • Params and Query

Return a BodyInt and you can add Headers and status code, you have access to the Request, params and query

await serve(
  fun()([
    {
      path: "/test/:id",
      r: (f) => f.param.id + " " + (f.query?.hello || ""),
    },
  ]),
  { port: 8080, hostname: "127.0.0.1" },
);

Route options

export type funRouterOptions = {
  hasName?: string;
  globalNotFound?: true;
  paramsStartsWith?: string;
  notFound?: { (x: Request): Response };
  badMethod?: { (x: Request): Response };
};
  • “hasName”: is the name if the server and it always has to finish with “/” , example: “http://127.0.0.1:8080/”, the router will be 5% faster if a name is given.
  • “globalNotFound”: change the default behaviour of the router, in the first “/” it will always return to the index if the route it’s not founded, but this will make the router 0,5% slower for routes with many “/”.
  • “paramsStartsWith”: by default, a parameter is defined by “:” next to a “/”, changing this value, will take the first character and check if it’s follow by “/” to star a new parameter.
  • “notFound”: changes the default NOT_FOUND.
  • “badMethod”: changes the default BAD_METHOD.

Methods

There are 4 methods

type ParamsMethod = "GET" | "HEAD" | "POST" | "DELETE";

Bear in mind that a method “PUT” , “PULL” or anything that starts with “P” will always end in “POST”

Benchmarks

We are using “serve” from Deno with the next commands

/

oha -z 10s -c 50 'http://127.0.0.1:8080/'

One Parameter

oha -z 10s -c 50 'http://127.0.0.1:8080/test/hi'

One Query

oha -z 10s -c 50 'http://127.0.0.1:8080/test?hello=world'

One Parameter and One Query

oha -z 10s -c 50 'http://127.0.0.1:8080/test/both/hello?hello=world'

Three Parameter

oha -z 10s -c 50 'http://127.0.0.1:8080/test/mul/1/2/3'

Three Query

oha -z 10s -c 50 'http://127.0.0.1:8080/q?d=1&e=2&f=3'

Three Parameter and Three Query

oha -z 10s -c 50 'http://127.0.0.1:8080/test/mul2/1/2/3?d=1&e=2&f=3'

License

CC BY-ND 4.0