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

declarative-routes

v0.1.4

Published

Declarative and type-safe routes for any project

Downloads

1

Readme

About declarative-routes

The goal of this project is to provide a system to finally use routes in your application in a type-safe manner. That means no more magic strings in the navigation! Library is framework agnostic so it should work with any frontend framework. This library was designed with TypeScript in mind but should work with pure JavaScript as well, however I recommend TypeScript for everyone if you are thinking about type safety.

Getting started

To install with npm

npm install declarative-routes

To install with yarn

yarn add declarative-routes

Simple use-case

Imagine you have following routes in you application:

order/:id
order/:id/share
order/:id/edit
order/:id/edit/preview
cart

Where :id is a route-parameter.

Create file routes.ts in a place from where it would be comfortable to import.

// routes.ts
import { buildRoutes, parameter, route } from './route-builder';

// Builds route table, accepts map-like object with routes
export const routes = buildRoutes({
  cart: route('cart'), // Simple route without any child routes
  order: route('order').withChildren({  // Simple route with children
    _id: parameter('id').withChildren({ // Nested parameter with children
        share: route('share')
        edit: route('edit').withChildren({
          preview: route('preview')
        }),
      }),
  })
});

Then whenever you want to generate navigation url:

// some-place-with-nav.ts
import { routes } from './routes'; // Actual path here

// ...

/**
 * Given orderId = 'o-123' this function will return:
 * 'order/o-123/edit'
 */
function getEditUrl(orderId: string) {
  return routes.order._id(orderId).edit.render();
}

// ...

// Assuming you have relative navigation and you know
// you're inside 'order' route - you can create temp root route
// pointing at it

/**
 * Given orderId = 'o-123' this function will return:
 * 'o-123/edit'
 */
function getRelativeEditUrl(orderId: string) {  
  // Upon rendering this will remove anything leading to `_id`
  const orderRoute = routes.order._id(orderId).asRoot(); 
  return orderRoute.edit.render();
}

In case you're working with angular and want to use navigation:

// component-with-nav.ts

// other angular imports...
import { Router } from '@angular/router';
import { routes } from './routes'; // Actual path here

@Component({
  template: '<a [routerLink]="getOrderUrl(testOrderId)"></a>'
})
class SomeComponent {
  private readonly router: Router; // Assuming it came from angular DI
  testOrderId = '0-123';

  // ... constructor etc.

  /**
   * Given orderId = 'o-123' this function will navigate to:
   * ['order', 'o-123', 'edit']
   * This could be used both for 'router.navigate' and [routerLink] directory
   */
  getOrderUrl(orderId: string) {
    return routes.order._id(orderId).edit.renderArray();
  }

  /**
   * Given orderId = 'o-123' this function will navigate to:
   * 'order/o-123/edit'
   */
  editOrder(orderId: string) {
    const navigationCommands = this.getOrderUrl(orderId);
    // navigationCommands = ['order', 'o-123', 'edit']
    this.router.navigate(this.getOrderUrl(orderId));
  }
}

Contributing

Please let me know if you know how to improve library or you know about any uncovered use-cases. Or any feedback really.

If you want to say thanks

Give a star to this project on github, that would be enough!

But you want to support me then you can

License

MIT


GitHub @feafarot