@acai/router

An agnostic router that can be easily extended

Usage no npm install needed!

<script type="module">
  import acaiRouter from 'https://cdn.skypack.dev/@acai/router';
</script>

README

Açai Router Module

Build Status Support

This repository contains the router module added by the Açai Framework. This is responsible for creating the routes list and doing a match with a url path, passing the information back to you.

Usage

import { route, router } from "@acai/router";

// list routes available

route.post("/register", "controllers/auth@register");
route.post("/login", 	"controllers/auth@login");

route.group("/user", () => {
    route.get("/", 		"controllers/user@show");
    route.patch("/", 	"controllers/user@update");
});

// Use the router to match it
const selectedRouteInfo = router("url/path/here", "GET", route.build());

Grouping

You can group routes, that will make the callbacks inside of them, use their context. Groups can be nested.

import { route } from "@acai/router";

// list routes available

route.group("/users", () => {
    route.get("/", 	"controllers/user@index");
    
    route.group("/auth", () => {
    route.get("/", 		"controllers/user@show");
    route.patch("/", 	"controllers/user@update");
    });
});

HTTP Methods

You can use methods to bind HTTP methods to routes.

import { route } from "@acai/router";

// list routes available

route("/", 			"get/route"); // equivalent of route.any
route.get("/", 		"get/route");
route.post("/", 	"post/route");
route.put("/", 		"put/route");
route.patch("/", 	"patch/route");
route.delete("/", 	"delete/route");
route.any("/", 		"any/route"); // doesn't care about http method

Options

Sometimes you wish to pass additional information to a route, such as a middleware, or anything else. You can bind extra information to the context with options. Objects and arrays will automaticly be joined, if you want to avoid this behaviour, prefix the option key with !. Options will also prevent value duplication in arrays

import { route } from "@acai/router";

// list routes available

route.options({middleware: ["auth", "admin"]}, () => {
    // routes inside of here will inherit the parents options

    // prefixing an option with ! will overwrite it
    route.options({"!middleware": ["auth"]}, () => {
        // middleware value: ["auth"]
    });
});