npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@acai/router

v2.2.0-beta.5

Published

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.

Downloads

15

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.