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

vue-route-handler

v1.0.3

Published

Elegant, fluent route definitions for Vue Router, inspired by Laravel.

Downloads

36

Readme

Vue Route Handler with TypeScript Support

Vue Route Handler is a powerful library that provides elegant and fluent route definitions for Vue Router, drawing inspiration from the structure of Laravel routes. Elegant, fluent route definitions for Vue Router, inspired by Laravel.

To install the latest version, run the following command:

npm i vue-route-handler
yarn add vue-route-handler

Overview

Vue Route Handler simplifies the process of declaring route definitions for Vue Router. Drawing inspiration from Laravel, it utilizes chained calls to construct your routes, enabling you to group and nest them as deeply as needed.

Here's an example usage:

import { createRouter, createWebHistory } from "vue-router";
import { Factory, Guard, Route } from "vue-route-handler";
import Home from "./views/Home.vue";
import About from "./views/About.vue";
import ManageAccount from "./views/ManageAccount.vue";
import ViewSubscription from "./views/ViewSubscription.vue";
import CancelSubscription from "./views/CancelSubscription.vue";
import UpgradeSubscription from "./views/UpgradeSubscription.vue";
import StartUpgrade from "./views/StartUpgrade.vue";
import SelectNewPlan from "./views/SelectNewPlan.vue";
import ReviewPaymentMethod from "./views/ReviewPaymentMethod.vue";
import ManageCards from "./views/ManageCards.vue";
export const routeHomePage = {
  name: "home",
};

class AuthGuard extends Guard {
  handle(resolve: () => void, reject: (reason: { name: string }) => void) {
    let isAuthenticated = false;

    if (isAuthenticated) {
      resolve();
    } else {
      reject(routeHomePage);
    }
  }
}

class GuestAuthenticationGuard extends Guard {
  handle(resolve: () => void): void {
    resolve();
  }
}

Factory.withGuards({
  auth: AuthGuard,
  guest: GuestAuthenticationGuard,
});

Route.view({ path: "/", view: Home }).name("home");
Route.view({ path: "about", view: About }).name("about").guard("auth");

Route.group({ prefix: "account", name: "account" }, () => {
  Route.view({ path: "/", view: ManageAccount }).name("manage");

  Route.group({ prefix: "subscription", name: "subscription" }, () => {
    Route.view({ path: "/", view: ViewSubscription }).name("view");
    Route.view({ path: "cancel", view: CancelSubscription }).name("cancel");

    Route.view({ path: "upgrade", view: UpgradeSubscription })
      .name("upgrade")
      .children(() => {
        Route.view({ path: "/", view: StartUpgrade }).name("start");
        Route.group({ prefix: "steps" }, () => {
          Route.view({ path: "select-new-plan", view: SelectNewPlan }).name(
            "select-new-plan"
          );
          Route.view({
            path: "review-payment-method",
            view: ReviewPaymentMethod,
          }).name("review-payment-method");
        });
      });
  });

  Route.view({ path: "cards", view: ManageCards }).name("cards");
});

const router = createRouter({
  routes: Factory.routes(),
  history: createWebHistory(),
});

export default router;

using usingResolver method and import.meta.globEager

import { createRouter, createWebHistory } from "vue-router";
import { Factory, Guard, Route } from "vue-route-handler";

interface Views {
  [key: string]: { default: any };
}

class AuthGuard extends Guard {
  handle(resolve: () => void): void {

    console.log('MyGuard.handle()');
    resolve();

  }
}

class GuestAuthenticationGuard extends Guard {
  handle(resolve: () => void): void {

    console.log('GuestAuthenticationGuard.handle()');
    resolve();

  }
}


Factory.withGuards({
  auth: AuthGuard,
  guest: GuestAuthenticationGuard
})


const views = import.meta.glob("./views/**/*.vue") as Views;
const view = (path: string) => views[`./views/${path}.vue`];

Factory.usingResolver(view).withGuards({ AuthGuard });

Route.view({ path: "/", view: "Home" }).name("home");
Route.view({ path: "/about", view: "About" }).guard("AuthGuard").name("about");

Route.group({ prefix: "account", name: "account" }, () => {
  Route.view({ path: "/", view: "ManageAccount" }).name("manage");

  Route.group({ prefix: "subscription", name: "subscription" }, () => {
    Route.view({ path: "/", view: "ViewSubscription" }).name("view");
    Route.view({ path: "cancel", view: "CancelSubscription" }).name("cancel");

    Route.view({ path: "upgrade", view: "UpgradeSubscription" })
      .name("upgrade")
      .children(() => {
        Route.view({ path: "/", view: "StartUpgrade" }).name("start");
        Route.group({ prefix: "steps" }, () => {
          Route.view({ path: "select-new-plan", view: "SelectNewPlan" }).name("select-new-plan");
          Route.view({ path: "review-payment-method", view: "ReviewPaymentMethod" }).name("review-payment-method");
        });
      });
  });

  Route.view({ path: "cards", view: "ManageCards" }).name("cards");
});

const router = createRouter({
  routes: Factory.routes(),
  history: createWebHistory(),
});

export default router;

This results in an array of routes in the format expected by Vue Router. The library's behavior is somewhat akin to Laravel's router, including:

  • Utilizing callbacks to iteratively collect routes
  • Properly joining nested routes, regardless of prefixed slashes
  • Correctly joining the names of nested routes using a separator of your choice

License

Vue Route Handler is licensed under the ISC license, a more permissive variant of the MIT license. For details, refer to the licence file.

Contributing

Contributions to Vue Route Handler are welcome! To contribute, open a Pull Request with your changes. Provide a clear description of the changes, indicating whether they fix a bug, enhance an existing feature, or introduce a new one.

If you discover a bug but are unable to fix it, or if you lack the time to do so, open an issue.

Please ensure the issue is Ensure the issue is descriptive and includes a link to a reproduction of the problem. Prefix the title with the applicable version of route-handler, such as [1.0].

For feature requests, submit an issue with the title prefixed by "Feature Request."