Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@bicycle-codes/routes

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bicycle-codes/routes

Route matcher devised for shared rendering JavaScript applications

  • 4.0.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
162
decreased by-34.68%
Maintainers
1
Weekly downloads
 
Created
Source

routes

tests dependencies types module semantic versioning license

Route matcher devised for shared rendering JavaScript applications

install

npm install -S @bicycle-codes/routes

ESM vs CJS

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

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

example

Get a router instance

import Router from '@bicycle-codes/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.

Keywords

FAQs

Package last updated on 07 May 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc