@acai/router

An agnostic router that is part of the Açaí Framework ecosytem that can be easily extended to be used both on the backend and frontend.

Usage no npm install needed!

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

README

Açai Router Module

https://gitlab.com/acaijs/router.git https://www.npmjs.com/package/@acai/router https://www.npmjs.com/package/@acai/router https://www.npmjs.com/package/@acai/router

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";

route.group("/users", () => {
    route.get("/", 	"controllers/user@index"); // this route will be /users -> controllers/user@index
    
    route.group("/auth", () => {
        route.get("/", 		"controllers/user@show"); // this route will be /users/auth -> 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("/",		"any/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

// if you wish to use multiple http methods for the same route, you can use many
route.many(["PUT", "PATCH"], "/", "put/and/patch/routes");

// you can define a route variable using
route("/{variableName}", "any/route/with/variable");
// and an optional variable with
route("/{variableName?}", "any/route/with/optional/variable");

// you can also pass an callback to the file parameter
route("/", () => {});

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"]
    });
});

Macro/Use

Macro is a bundle of reusable code logic you can use to create routes with a common pattern. By default it provides a resource macro. You can also overwrite an already defined macro by just defining a new one with the same name.

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

// define macro that can be used
route.macro("related", (name: string, controller: string) => {
    route.group(name, () => {
        route.get("/",		`${controller}@index`);
        route.put("/",		`${controller}@set`);
        route.post("/",		`${controller}@add`);

        route.group("/{id}", () => {
        route.get("/",		`${controller}@show`);
        route.patch("/",	`${controller}@edit`);
        route.delete("/",	`${controller}@delete`);
        });
    });
});

// Use macro
route.use("related", "posts",		"controllers/post.controller");
route.use("related", "comments",	"controllers/comment.controller");

Resource macro

url method controller
"/<name>" GET "@index"
"/<name>" POST "@store"
"/<name>/{id}" GET "@show"
"/<name>/{id}" PATCH | PUT "@update"
"/<name>/{id}" DELETE "@destroy"

Support

Do you have a question? Please open an issue on our main repo.

License

BSD Clause 3

Copyright (c) 2021 The Nuinalp Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.