Code Monkey home page Code Monkey logo

url-routes-generator's Introduction

Introduction

A simple URL generator in Javascript.

Installation

To install the stable version:

npm install --save url-routes-generator

This assumes you are using npm as your package manager.

Usage

First, register your routes:

import { uri } from "url-routes-generator";

export const Routes = {
  home: params                     => uri("/")(params),
  invoices: params                 => uri("/invoices")(params),
  invoice: (id, params)            => uri("/invoices/:id", {id})(params),
  sendInvoice: (id, email, params) => uri("/invoices/:id/sendto/:email", {id, email})(params),
  about: params                    => uri("/about")(params)
};

Then, you can call the Routes object to generate a valid URL.

Generate a route

Printing routes:

console.log(Routes.home()); // /
console.log(Routes.about()); // /about
console.log(Routes.invoice(23)); // /invoices/23
console.log(Routes.sendInvoice(23, '[email protected]')); // /invoices/23/sendto/[email protected]
console.log(Routes.invoices({orderBy:"date")); // /about?orderBy=date
console.log(Routes.invoice(23, {print:true, page: 2})); // /invoices/23?print=true&page=2

Example usage with react-router:

<Link to={Routes.about()}>About us</Link>
<Link to={Routes.invoice(23)}>Invoice</Link>
<Redirect to={Routes.about({source: "dashboard"})}/>

Get the route pattern

If you are using react-router, you need to place the route pattern (/invoices/:id) in the route definition. To get the route pattern, call the route with no params.

Example:

<Switch>
    <Route exact path={Routes.invoices()} component={Dashboard}/>
    <Route path={Routes.invoice()} component={Invoice}/>
</Switch>

Typescript

Type declarations are included in the package.

License

Open-sourced software licensed under the MIT license

url-routes-generator's People

Contributors

federicomoretti avatar javidalpe avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

goldbely

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.