4.0.7 • Published 2 months ago

@nichoth/routes v4.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

routes

tests dependencies types module semantic versioning license

Route matcher devised for shared rendering JavaScript applications

install

npm install -S @nichoth/routes

ESM vs CJS

Featuring ESM or CJS versions via package.json exports field.

// esm
import Router from '@nichoth/routes'
// cjs
const Router = require('@nichoth/routes').default

example

Get a router instance

import Router from '@nichoth/routes'
var router = new Router()

Add some routes

router.addRoute('/articles', getArticles);
router.addRoute('/articles/:slug', getArticleBySlug);
router.addRoute('/articles/search/*', searchForArticles);

// can also chain the method calls
router
    .addRoute('/foo', () => {/* ... */})
    .addRoute('/bar', () => {/* ... */})

Find a match

const match = router.match('/articles');
// => RouteMatch

The match object

interface RouteMatch {
    params:Record<string, string>;  // <-- e.g. { slug: 'article-title' }
    splats:string[];
    route:string;
    next?:((...any)=>any)|null;
    action?:(...any)=>any;
    index?:number;
}

You'll get null back if no route matches the provided URL. Otherwise, the route match will provide all the useful information you need inside an object.

KeyDescription
actionThe action passed to addRoute as a second argument. Using a function is recommended
nextFall through to the next route, or null if no other routes match
routeThe route passed to addRoute as the first argument
paramsAn object containing the values for named parameters in the route
splatsAn object filled with the values for wildcard parameters

License

MIT

fork

This is a fork of ruta3, just adding types.

4.0.7

2 months ago

4.0.5

3 months ago

4.0.4

3 months ago

4.0.3

3 months ago

4.0.1

3 months ago

4.0.0

3 months ago

3.0.7

12 months ago

3.0.6

1 year ago

3.0.5

1 year ago

3.0.4

1 year ago

3.0.3

1 year ago

3.0.2

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago