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

ave

v0.0.1-0

Published

Flexible JavaScript URL router.

Downloads

48

Readme

ave

Flexible JavaScript URL router.

Travis npm package Coveralls

A Taskworld, our frontend is very complex. Many parts can be affected by the URL independently. The URLs in the app has gone through several iterations and efforts need to be made to ensure backwards compatibility. ave is created to handle all our routing needs. This library is based on url-mapper and uniloc project.

Usage

import { createRouteMapper } from 'ave'

createRouteMapper(routeDefinitions)

Give it an dictionary of Route Definition Objects, and it will create a Route Mapper.

  • routes An Object whose keys are route name and values are Route Definition Objects, each contains these keys:
    • route (required) A String representing the route. They can have placeholders. e.g.: /project/:projectAltId
    • aliases An Array of String that contains the possible aliases for this route.
    • redirect A Function which will redirect this route to another route during route resolution.
    • format A Function which will redirect this route to another route during URL generation. To protect against bugs, the resulting route must redirect back to this route.

Redirect and format is an advanced feature, which will be discussed later. Here’s an example router.

const simpleRouter = createRouteMapper([
  home: { route: '/' },
  about: { route: '/about' },
  post: { route: '/posts/:postId', aliases: [ '/forum/post/:postId' ] },
])

RouteMapper#resolve(path)

Resolves the path String into a Route Object.

  • path A String representing the path to resolve.
  • Returns a Route Object with these keys:
    • name A String representing the route’s name.
    • options An Object containing the options from placeholders or query.
  • Returns null if desired route is not found.
simpleRouter.resolve('/')
// =>
simpleRouter.resolve('/posts/123')
// =>
simpleRouter.resolve('/forum/post/123?page=2')
// =>
simpleRouter.resolve('/oops')
// =>

RouteMapper#generate(routeObject)

Generates a path String based on the given Route Object. This is the reverse of resolve.

  • routeObject A Route Object as described above.
  • Returns a String representing the path that will resolve to this route.
  • Throws an Error if the Route Object is not valid, or if the route options did not contain all the required placeholders defined in the route definition.
simpleRouter.generate({ name: 'home', options: { } })
// =>
simpleRouter.generate({ name: 'home', options: { page: '2' } })
// =>
simpleRouter.generate({ name: 'post', options: { postId: '123' } })
// =>

RouteMapper#getRouteDefinitionByName(name)

Returns the Route Definition Object for a route named by the String name.

simpleRouter.getRouteDefinitionByName('post')
// =>

As mentioned above, you can store extra keys in Route Definition Objects. At Taskworld we add render(options) function to each Route Definition Object, and have the React component call it. e.g.

const appRouter = createRouteMapper([
  projects: {
    route: '/',
    aliases: [ '/projects' ],
    render: ({ }) => <ProjectListPage />
  },
  project: {
    route: '/project/:projectId',
    render ({ projectId }) => <ProjectPage projectId={projectId} />
  }
])

const Application = ({ path }) => {
  const route = appRouter.resolve(path)
  const content = (route
    ? simpleRouter.getRouteDefinitionByName(route.name).render(route.options)
    : <NotFound />
  )
  return <AppLayout route={route}>{content}</AppLayout>
}

Redirects

To be written

Formats

To be written