declarative-routes
TypeScript icon, indicating that this package has built-in type declarations

0.1.4 • Public • Published

declarative-routes

Declarative and type-safe routes for any project

npm version

About declarative-routesGetting startedIf you want to say thanksContributingLicense

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

Buy Me A Coffee

License

MIT


GitHub @feafarot

Package Sidebar

Install

npm i declarative-routes

Weekly Downloads

0

Version

0.1.4

License

MIT

Unpacked Size

58 kB

Total Files

16

Last publish

Collaborators

  • feafarot