Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/alecell/smart-routes

A framework agnostic simple and lightweight way to create and reuse routes on web apps.
https://github.com/alecell/smart-routes

angular angularjs prs react-router reuse-routes vue-router

Last synced: about 2 months ago
JSON representation

A framework agnostic simple and lightweight way to create and reuse routes on web apps.

Awesome Lists containing this project

README

        

# Smart Routes ![PRs Welcome](https://img.shields.io/badge/PRs-welcome-green.svg "PRs Welcome")

A framework agnostic simple and lightweight way to create and reuse routes on web apps.

**IMPORTANT:** This **isn't** a router like `vue-router` or `react-router`. Smart Routes just brings a simplified and safer way to declare and reuse routes.

## Motivation
As our web application grow, its amount of routes grows too. Apps with 20, 30, 40 and more routes variations can be really messy to deal with, even more when we add dynamic routing.

That kind of thing came with some problems like:
* Links leading to unexpected route;
* Send parameter to a not parameterized route;
* Non intelligible routes names;
* Work around to apply parameters;
* Forget to replace a link when an entirely route needs to be changed.

A routes single source of truth allows to better organize your routes, better use them and prevent routing mistakes.

## Install
### npm
```
npm install smart-routes
```
### yarn
```
yarn add smart-routes
```

## Basic usage
Import `Route` from the package
```
import { Route } from 'smart-routes';
```

Crete your route

```js
const routes = {
user: new Route('/user')
}
```

Defining route

```jsx

```

Link like this

```jsx
// Leads to /user

// ...

```

## Route parameters
```js
const routes = {
user: new Route('/user', ':userId')
}
```

```jsx
// `routes.user().path` leads to /user/:userId

```

```jsx
const someUserId = 123;

// Leads to /user/123

// ...

```

## Subroutes
```js
const routes = {
user: new Route('/user', {
info: new Route('/info')
})
}
```

```jsx

```

```jsx
// Leads to /user/info

// ...

```

## Complete example
```js
const routes = {
user: new Route('user', ':userId', {
info: new Route('info'),
cart: new Route('cart', {
item: new Route('item', ':itemId')
})
})
}
```

```jsx
// /user/:userId

// /user/:userId/info

// /user/:userId/cart

// /user/:userId/cart/item/:itemId

```

```jsx
// Leads to /user/123

...

// Leads to /user/123/info

...

// Leads to /user/123/cart

...

// Leads to /user/123/cart/item/456

...

```