Simple named url patterns in Javascript
npm install named-urlsSimple named url patterns in JavaScript.
[![CircleCI][Badge-CI]][CI]
[![Code Coverage][Badge-Coverage]][Coverage]
[![PRs Welcome][Badge-PRWelcome]][PRWelcome]
[![MIT License][Badge-License]][LICENSE]
[![Watch on GitHub][Badge-Watch]][Watch]
[![Star on GitHub][Badge-Stars]][Star]
[![Tweet][Badge-Twitter]][Twitter]
> Implementing a static route config and named routes on top of (react-router) at this point is like a 20 line ordeal.
– Ryan Florence
Named routes are essential to keep route config DRY and prevent silly errors
due to typos. This feature was removed from react-router in 1.0 and I missed
it since then as many
others.
There're other libs dealing with named routes, some of them provide customLink, Route routes, some of them have more features to integrate withexpress. Here's incomplete list of libs I considered before writing these
20 lines of code (and 200+ lines of other files to publish this package):
- https://github.com/adamziel/react-router-named-routes
- https://github.com/taion/use-named-routes
- https://github.com/alubbe/named-routes
> NOTE: v2 introduces breaking changes. Please check out the migration guide before upgrading.
```
$ npm install named-urls
or
``
$ yarn add named-urls
Create file with all routes in your application (e.g. routes.js). Usenamed-urls/include to create namespaced group of routes with common prefix:
`jsx
// routes.js
import { include } from 'named-urls'
export default {
// simple route
profile: '/profile',
// route with params
article: '/article/:articleId',
// route with optional params
messages: '/messages/:messageId?',
// Routes with common path prefix
auth: include('/auth', {
// Absolute url (ignore /auth prefix)
login: '/login/',
// Relative urls (prefixed with /auth)
passwordReset: 'password/reset/',
passwordVerify: 'password/verify/',
}),
// Routes with params
messages: include('/messages', {
all: '',
unread: 'unread/',
// nesting of includes is allowed
detail: include(':messageId/', {
show: '',
edit: 'edit/',
comments: 'comments/',
})
})
}
`
Use routes in Route component from react-router-dom:
`jsx
// App.js
import React from 'react'
import { Switch, Route } from 'react-router-dom'
import routes from './routes'
import * as scenes from './scenes' // just a convention, could be "pages"
function App() {
return (
// ...
)
}
`
Routes with parameters can be formatted using reverse function:
`jsx
// Navigation.js
import React from 'react'
import { Link } from 'react-router'
import { reverse } from 'named-urls'
function Navigation({ messages }) {
return (
}>Profile, { messageId: message.id })}>
Profile
Ending slash
Patterns ending with slash are always reversed to URL with ending slash and vice
versa: Paterns without ending slash are always reserved to URL without endlish
slash:
`js
// pattern with ending slash
reverse('pattern/:optional?', { optional: 42 }) // pattern/42
reverse('pattern/:optional?') // pattern// pattern without ending slash
reverse('pattern/:optional?/', { optional: 42 }) // pattern/42/
reverse('pattern/:optional?/') // pattern/
`Migrating from v1.x.x to v2.x.x
For better compatibility with React Router, v2 uses path-to-regexp to resolve URLs. This means some of your routes may break when you upgrade.
$3
`diff
-reverse('pattern/page:param?', {})
+reverse('pattern/(page:param)?', {})
`$3
`diff
-reverseForce('pattern/page:param?', {})
+reverseForce('pattern/(page:param)?', {})
`To get a full overview of all accepted patterns, consult the path-to-regexp documentation.
Some tricks
$3
If you define a route as an include, calling it directly will return you a function. To by-pass that, you have a solution to create an empty route inside, let's call it
self:`jsx
// routes.js
import { include } from 'named-urls'export default {
messages: include('/messages', {
self: '',
detail: include(':messageId/', {
show: '',
edit: 'edit/',
comments: 'comments/',
})
})
}
`so you'll be able to do:
`jsx
`A way to not define a useless route is to use the string way of a route like that:
`jsx
// routes.js
import { include } from 'named-urls'export default {
messages: include('/messages', {
detail: include(':messageId/', {
edit: 'edit/',
comments: 'comments/',
})
})
}
``jsx
${routes.messages}} component={Messages} />// OR
``License
[MIT][License]
[Badge-CI]: https://circleci.com/gh/kennedykori/named-urls/tree/master.svg?style=shield
[Badge-Coverage]: https://codecov.io/gh/kennedykori/named-urls/branch/master/graph/badge.svg?token=Y8Q2C7XAGP
[Badge-License]: https://img.shields.io/github/license/kennedykori/named-urls.svg
[Badge-Watch]: https://img.shields.io/github/watchers/kennedykori/named-urls.svg?style=social&label=Watch
[Badge-Stars]: https://img.shields.io/github/stars/tricoder42/named-urls.svg?style=social&label=Stars
[Badge-Twitter]: https://img.shields.io/twitter/url/https/github.com/kennedykori/named-urls.svg?style=social
[Badge-PRWelcome]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
[CI]: https://circleci.com/gh/kennedykori/named-urls/tree/master
[Coverage]: https://codecov.io/gh/kennedykori/named-urls
[License]: https://github.com/kennedykori/named-urls/blob/master/LICENSE
[Watch]: https://github.com/kennedykori/named-urls/watchers
[Star]: https://github.com/kennedykori/named-urls/stargazers
[Twitter]: https://twitter.com/intent/tweet?text=Check%20out%20named-urls!%20https://github.com/kennedykori/named-urls%20%F0%9F%91%8D
[PRWelcome]: http://makeapullrequest.com