0.7.4 • Published 7 months ago

urlset v0.7.4

Weekly downloads
45
License
ISC
Repository
github
Last release
7 months ago

urlset

Application URLs model. Replacing static string URLs with hierarchical model.

Problem

URLs in our apps are hard-coded.

Solution

Building a hierarchical/readable model of URLs. Instead of '\home' we can write url.home(), or '\profile\'+name+'\photos' -> url.profile.photos(name)

Usage

Using urlset is very simple:

	var path	= require('path');
	var urlset	= require('urlset');

	// sitemap.json contains configuration file for urlset
	var links = urlset(require('./sitemap.json'));

	// now, where we need to use link '/' we ca use:
	links.home();

Configuration file

Configuration file(s) are in json format.

Special names

  1. $url - url format for current node;
  2. $path - append path for current node and childs;
  3. $root - ignore parents $path;

Example

{
	"home": "/",
	"item": "/item/${0}-${1}",
	"account": "/account-${name}",
	"accounts": {
		"$path": "/accounts",
		"profile": "/profile",
		"photos": "/${0}/photos",
		"points":"/${0}/points",
		"friends": {
			"$url": "/${0}/friends"
		}
	}
}

Every url in config file can be a string(like "home": "/") or an object: "friends": { "url": "/${0}/friends" }.

This example creates:

	links.home(); // with no params, output: '/'
	links.item(p0,p1); // with 2 params, output: '/item/p0-p1'
	links.accounts.profile(); // with no params, output: '/accounts/profile'
	links.accounts.photos(p0); // with one param, output: '/accounts/p0/photos'
	links.accounts.points(p0); // with one param, output: '/p0/points' !! link is absolute, so it don't adds '/accounts' prefix
	links.accounts.friends(p0); // with one param, output: '/accounts/p0/friends'
	links.account({name: 'uniq', lang: 'ru'}); // with one param, output: '/account-uniq?lang=ru'

All urls accept an extra param(object) for query: links.home({lang:'ru',_ref:'home'}) -> '/?lang=ru&_ref=home'

API

(files, options)

Creates a new links object.

0.7.2

7 months ago

0.7.1

7 months ago

0.7.4

7 months ago

0.7.3

7 months ago

0.7.0

8 months ago

0.6.1

6 years ago

0.6.0

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.2

9 years ago

0.4.1

9 years ago

0.4.0

9 years ago

0.3.4

9 years ago

0.3.2

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago