dynamic-url-constructor

1.0.5 • Public • Published

dynamic-url-constructor

Construct dynamic URL strings to be used all around your application.

Github repo: https://github.com/Julian1729/urlconstructor

Installation

Using npm:

npm i --save dynamic-url-constructor

Usage

Require dynamic-url-constructor and instantiate new constructor

const URLConstructor = require('dynamic-url-constructor');
let AppUrlConstructor = new URLConstructor();

Set url base/domain and create routes

// set constructor base
AppUrlConstructor.setBase('http://example.com');
// create routes
AppUrlConstructor.addRoute('dashboard', '/:username/dashboard');
AppUrlConstructor.addRoute('add-post', '/:username/post/add?date=:date');
AppUrlConstructor.addRoute('view-post', '/:username/post/:title');

Set global parameters to be used by all routes

AppUrlConstructor.setGlobal('username', 'michaelscott');

Render URL

let dashboardRoute = AppUrlConstructor.getRoute('dashboard');
dashboardRoute.url(); // http://example.com/michaelscott/dashboard

Inject route specific params

let addPost = AppUrlConstructor.getRoute('add-post');
addPost.setParam('date', '2019-05-03');
let addPostURL = addPost.url();
// http://example.com/michaelscott/post/add?date=2019-05-03

Override parameters on render

let addPostURL = addPost.url({'username': 'littlekidlover'});
// http://example.com/littlekidlover/post/add?date=2019-05-03

Add query params on render

let addPostURL = addPost.url(null, {'preview': 'true'});
// http://example.com/michaelscott/post/add?date=2019-05-03&preview=true

Coming Soon

  • set parameters with an arbitrary object
  • utilize global configuration object
  • add URL protocol to configuration and add dynamically

Dependencies (0)

    Dev Dependencies (2)

    Package Sidebar

    Install

    npm i dynamic-url-constructor

    Weekly Downloads

    6

    Version

    1.0.5

    License

    ISC

    Unpacked Size

    14.6 kB

    Total Files

    7

    Last publish

    Collaborators

    • julian1729