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

@billow/easy-route

v2.0.8

Published

A middleware routing system for Vue Router inspired by Laravel routing

Downloads

25

Readme

Easy Route for Vue Router

Vue

A middleware system that ties into Vue Router. It allows you to define guards and protect routes.

Installation

Install Easy Route with npm.

  npm i @billow/easy-route

Usage

Import:

import EasyRoute from '@billow/easy-route' 

Install:

Easy Route requires a Vue Router instance to be passed in the options.

Additionally you may pass an array of guards to the plugin, but this will be discussed futher down.

import router from '@/router'

Vue.use(EasyRoute, {
  router
})

Installing the plugin makes Easy Route aware of navigation through the beforeEach method on the Vue Router API. It will however not have any effect until guards are added to the mix.

Routes

The Route method has been designed to simplify route definitions. This method will return a route object for Vue Router.

import { Route } from '@billow/easy-route'

Route('/path', 'route-name', VueComponent, ['stacked', 'middlewares'])

Route Paramaters

| Paramater | type | default | | ------------- |---------------| ---------| | path | string | required | | name | string | '' | | component | Vue Component | required | | middleware | Array | [] |

RouteGroup

The RouteGroup() method gives you the ability to group routes with common middlewares or path prefixes together.

import { Route, RouteGroup } from '@billow/easy-route'

// Example components
import UserIndex from '@/components/user/Index'
import CreateUser from '@/components/user/Create'

RouteGroup({
    
    // Prefix all routes in group
    prefix: '/admin',
    
    // Apply guards to all routes in group
    middleware: [
        'auth',
        'admin'
    ],
    
    routes: [
        Route('/users', 'user-index', UserIndex),
        Route('/user', 'create-user', CreateUser, ['can-create-user']) // Additional guard on single route
    ]
    
})

RouteGroup Options

| Paramater | type | default | | ------------- |---------------| ---------| | prefix | String | '' | | middleware | Array | [] | | routes | Array | required | | parent | Route | null |

RouteGroup with children

It is quite common to require routes with nested children. In these scenarios you may use the parent option of a RouteGroup. parent should be a Route() and should not include a name. Once you have added the parent option, the routes array will become the children of the parent.

Note

Easy Route will strip names & prefixes on parent routes. Child routes will inherit their prefix from the parent route.

import { Route, RouteGroup } from '@billow/easy-route'

// Example components
import UserManager from '@/components/user/Manager'
import UserOverview from '@/components/user/Overview'
import EditUser from '@/components/user/EditUser'

RouteGroup({
    
    parent: Route('/users/:userId', '', UserManager),
    
    middleware: ['auth', 'admin'],
    
    routes: [
        Route('/', 'user-overview', UserOverview),
        Route('edit', 'edit-user', EditUser)
    ]
    
})

Guards

At this point, Easy Route does not ship with any built in guards, each app is unique and needs its own set of guards. Having said that creating custom guards for your application is very easy.

Let's look at an example of what an auth guard might look like:

// guards/auth.js

let guard = (context) => new Promise((resolve, reject) => {
    if (authenticated === true) {
        resolve()
    } else {
        reject({
          name: 'login',
          path: '/login' // optional, using a named route is better
        })
    }
})

export {
    name: 'auth',
    run: guard
}

In a nutshell guards allow you to protected your routes.

Your Guards must:

  1. Return an object with a name key and run key. // { name: 'guard-name', run: Promise() }
  2. run must return a promise
  3. if run fails you must provide a fallback route. // reject({ name: '404' })

Your Guards will receive the current route context as the only parameter.

Installing Guards

// main.js
import auth from '@/guards/auth'
import EasyRoute from '@billow/easy-route' 

// Guard Example. 
// Please make sure you use your applications method of determining auth.
// The below is strictly an example of a guard.

Vue.use(EasyRoute, {
  router,
  guards: [auth]
})

Putting it all together

import Vue from 'vue'
import VueRouter from 'vue-router'
import { concat } from 'lodash'
import auth from '@/guards/auth'
import guest from '@/guards/guest'
import EasyRoute from '@billow/easy-route' 
import { Route, RouteGroup } from '@billow/easy-route'

Vue.use(VueRouter)

let guestRoutes = RouteGroup({
    
    middleware: ['guest'],
    
    routes: [
        Route('/', 'welcome', WelcomePage),
        Route('/login', 'login', LoginPage)
    ]
    
})

let appRoutes = RouteGroup({
    
    middleware: ['auth'],
    
    prefix: '/app',
    
    routes: [
        Route('/', 'dashboard', AppDashboard),
        Route('/users', 'user-index', UserIndex)
    ]
    
})

let router = new Router({
    routes: concact(
        guestRoutes,
        appRoutes
    )
})

Vue.use(EasyRoute, {
    router,
    guards: [
        auth,
        guest
    ]
})

new Vue({
    ...
    router
    ...
})