Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

cv3-route

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cv3-route

Dependency injection code.

latest
npmnpm
Version
0.0.3
Version published
Weekly downloads
6
-40%
Maintainers
1
Weekly downloads
 
Created
Source

avatar

cv3-route

Simple routing for ES6.

cv3-route Travis (.org) Downloads Badge Size badge

Install

$ npm install cv3-route

Usage

Creating routers

Inject your routes into the Router class to produce a routable subclass.

Scalar values will be returned directly, methods will be evaluated & returned.

[false] will be called if no other routes match.

import { Inject } from 'cv3-inject/Inject';
import { Router } from 'cv3-route/Router';

const routes = {
	index:     'Hello, world!'
	, other:   'Other route!'
	, [false]: 'Not found!'
};

export class ExampleRouter extends Inject(Router, {routes})
{
	// Non-routable methods here
};

Routing

Pass a cv3-route/Path object to the route method on the Router object

import { ExampleRouter } from './ExampleRouter';
import { Path } from 'cv3-route/Path';

const exampleRouter = new ExampleRouter;

const emptyPath = new Path('');
const indexPath = new Path('index');
const otherPath = new Path('other');
const errorPath = new Path('does-not-exist');

console.log(exampleRouter.route(emptyPath));
console.log(exampleRouter.route(indexPath));
console.log(exampleRouter.route(otherPath));
console.log(exampleRouter.route(errorPath));

Routing to methods

You can route to methods instead of directly returning strings. The router object will be passed as the first parameter, and the current path will be passed as the second parameter.

Internal methods may be called on the router.

import { Inject } from 'cv3-inject/Inject';
import { Router } from 'cv3-route/Router';

const routes = {
	'do-something': (router, path) => {
		return 'did something.';
	}

	, 'do-something-else': (router, path) => {
		return router.internalMethod();
	}
};

export class ExampleRouter extends Inject(Router, {routes})
{
	internalMethod()
	{
		return 'did something.';
	}
};
import { ExampleRouter } from './ExampleRouter';
import { Path } from 'cv3-route/Path';

const exampleRouter = new ExampleRouter;

const somethingPath = new Path('do-something');
const somethingElsePath = new Path('do-something-else');

console.log(exampleRouter.route(somethingPath));
console.log(exampleRouter.route(somethingElsePath));

Regex Routing

Regex based routing can be achieved with computed property names.

The [square bracket] syntax allows us to maintain regex syntax highlighting in the IDE. Quotes may also be used. Any property starting with a / will be processed as a regex path.

If a method is supplied, it will be called with the match groups from the regex operation as the 3rd parameter.

import { Inject } from 'cv3-inject/Inject';
import { Router } from 'cv3-route/Router';

const routes = {
	[/^regex-([-\w]+)$/]: (router, path, matchGroups) => {

		return matchGroups[1].split('-').join(',');
	}
};

export class ExampleRouter extends Inject(Router, {routes})
{
	// Non-routable methods here
};
import { ExampleRouter } from './ExampleRouter';
import { Path } from 'cv3-route/Path';

const exampleRouter = new ExampleRouter;

const onePath   = new Path('regex-one');
const twoPath   = new Path('regex-one-two');
const threePath = new Path('regex-one-two-three');

console.log(exampleRouter.route(onePath));
console.log(exampleRouter.route(twoPath));
console.log(exampleRouter.route(threePath));

Nested Routing

Path objects split their source string on /. This allows us to do nested routing.

import { Inject } from 'cv3-inject/Inject';
import { Router } from 'cv3-route/Router';

const nestedRoutes = {
	index: 'Nested index.'
	, sub: 'Nested route.'
}

const routes = {
	nested: Inject(Router, {routes: nestedRoutes})
};

export class ExampleRouter extends Inject(Router, {routes})
{
	// Non-routable methods here
};
import { ExampleRouter } from './ExampleRouter';
import { Path } from 'cv3-route/Path';

const exampleRouter = new ExampleRouter;

const nestedIndexPath = new Path('nested');
const nestedRoutePath = new Path('nested/sub');

console.log(exampleRouter.route(nestedIndexPath));
console.log(exampleRouter.route(nestedRoutePath));

Routing with promises

Promises will be passed through, so promise-based routing is simple:

import { Inject } from 'cv3-inject/Inject';
import { Router } from 'cv3-route/Router';

const routes = {
	promise: new Promise((accept, reject) => {
		accept('fulfilled.');
	})
};

export class ExampleRouter extends Inject(Router, {routes})
{
	// Non-routable methods here
};
import { ExampleRouter } from './ExampleRouter';
import { Path } from 'cv3-route/Path';

const exampleRouter = new ExampleRouter;
const promisePath   = new Path('promise');

exampleRouter.route(promisePath).then((result) => {
	console.log(result);
}).catch((error) => {
	console.error(error);
});

License

cv3-route © Sean Morris 2019

All code in this package is relased under the Apache 2.0 licence.

FAQs

Package last updated on 24 Oct 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts