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

1.0.4 • Public • Published

Routz /ɹuːts/

CI Status Coverage Status on Codecov Known Vulnerabilities Minified gzipped size Types included License MIT

Define your SPA routes in a single source of truth - use them everywhere.

Installation

Routz is available on NPM:

npm install routz --save

or

yarn add routz

Usage

The routz package exposes a single define function that defines the application routes by the given parameter and returns helper functions to access these routes. A definition of application routes is an object with key/value pairs. Each key is the name of a route. It's meant to be used in your application later on. The value describes the path definition. Each path can contain params. Params are dynamic parts of a single path, that are defined in the format: [param]. Optional params are defined by a question mark at the end like: [optionalParam?].

// urls.js
import { define } from 'routz';

const { receive, resolve } = define({
  'index': '/[locale?]',
  'blog:list': '/[locale?]/blog',
  'blog:article': '/[locale?]/blog/[slug]',
});

export { receive, resolve };

In this example the locale param is optional. The slug param in the route blog:article is required.

The exposed functions can be used inside the application. For example in a react component (using Next.js):

// list.jsx
import Link from 'next/link';
import React from 'react';

import { resolve } from 'app/urls';

export const List = ({ articles }) => (
  <ul>
    {articles.map(({ slug, title }) => (
      <li key={slug}>
        <Link href={resolve('blog:article', { slug })}>
          <a>{title}</a>
        </Link>
      </li>
    ))}
  </ul>
);

API

define(routes)

Defines the routes and returns scoped resolve and receive functions that access the given routes.

resolve(name, params)

This builds a path for a route by name and params. Params are dynamic parts of a single path, that are defined in the format: [param]. Optional params are defined by a question mark at the end like: [optionalParam?]. The params need to be passed as a key/value object. Optional params are not required to be defined in this object, but missing params will throw an error.

receive(name)

Returns the route defintion by a given name.

License

LICENSE (MIT)

Readme

Keywords

none

Package Sidebar

Install

npm i routz

Weekly Downloads

4

Version

1.0.4

License

MIT

Unpacked Size

19.1 kB

Total Files

9

Last publish

Collaborators

  • nrmnrsh