Giter Club home page Giter Club logo

route-manager's Introduction

Route Manager

Build Status semantic-release

This router utility generates all the different route configurations required by our stack (React Router, Express routing etc.) and URLs.

API

URL definitions

URL definitions are comprised of key/value pairs:

// app/utils/route-config.js

import routing from '@festicket/route-manager';

export const { getUrl, getPattern, getAllPatterns } = routing({
  home: '/',
  search: '/search',
  foo: '/baz/:bar1/:bar2'
});

getUrl

We can generate URLs by referring to them using the format key:

import { getUrl } from 'app/utils/route-config';

// Generate a simple url with no route params

const homeUrl = getUrl('home');
// returns => '/'

// Generate a complex url with many route params

const complexUrl = getUrl('foo', { bar1: 'test-1', bar2: 'test-2' });
// returns => '/baz/test-1/test-2'

// Generate a url with a query

const searchUrl = getUrl('search', {}, { q: 'something' });
// returns => '/search?q=something'

getPattern

We can generate URL patterns by referring to them by key:

import { getPattern } from 'app/utils/route-config';

// Generate the home pattern

const homePattern = getPattern('home');
// returns => '/'

const complexPattern = getPattern('foo');
// returns /baz/:bar1/bar2

If you need a relative url you can pass a param to getPattern:

const complexPattern = getPattern('foo', true);
// returns baz/:bar1/bar2

getAllPatterns

We can get all patterns (useful for debugging) by calling getAllPatterns:

import { getAllPatterns } from 'app/utils/route-config';

const patterns = getAllPatterns();
// returns => { home: '/', search: '/search', foo: '/baz/:bar1/:bar2' }

If you need a relative url you can pass a param to getAllPatterns:

const patterns = getAllPatterns(true);
// returns => { home: '/', search: 'search', foo: 'baz/:bar1/:bar2' }

getParams

We can get named params as an object by calling getParams:

import { getParams } from 'app/utils/route-config';

const params = getParams('festivals/creamfields/2018/shop');
// returns => { festivalSeries: 'creamfields', festivalEdition: '2018' }

route-manager's People

Contributors

benkingcode avatar richiemccoll avatar cutandpastey avatar joebourne avatar marcoala avatar aghh1504 avatar nunohora avatar

Stargazers

Rokas avatar  avatar

Watchers

James Cloos avatar Richard Quaite avatar Ryan Clarke avatar Brian Walker avatar boda avatar Rokas avatar Justinas Masiulis avatar  avatar

Forkers

benkingcode

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.