PathMaker
A factory for making URL/FileSystem paths with templates and URL query strings.
Usage
var basePath = PathMaker('/my/path/or/url/');
var subPath = basePath.sub('sub/path');
basePath();
subPath();
See Examples below for extended usage including templates and
URL query strings.
Options
Name | Default Value | Description |
---|
delimiter | '/' | The path delimiter. |
tokenPrefix | ':' | The path token prefix. |
path | '' | The path represented by the PathMaker . |
Notes
- Trailing delimiters are not automatically added or removed at any time.
- Only query object values are encoded with
encodeURIComponent
. No other
part of the path is automatically encoded.
Examples
import PathMaker from 'pathmaker';
const site = PathMaker('http://www.site.test/');
const api = PathMaker('http://api.site.test/');
const base = PathMaker('http://www.site.test/app-name/');
const AppURL = {
base,
site,
login: base.sub('/login/'),
product: base.sub('/products/:id/'),
products: base.sub('/products/'),
api: {
base: api,
account: api.sub('account'),
oauth: api.sub('oauth'),
user: api.sub('users/:id'),
users: api.sub('users'),
}
};
export default AppURL;
import AppURL from './AppURL';
console.log(
AppURL.site()
);
console.log(
AppURL.base()
);
console.log(
AppURL.product({ id: 999 });
);
console.log(
AppURL.login({ query: {redirect: '/dashboard'} })
);
console.log(
AppURL.base('login/', { query: {redirect: '/dashboard'} })
);
console.log(
AppURL.api.user({ id: 10 })
);
console.log(
AppURL.api.users([10, 'organizations/search'], { query: {q: 'the query'} })
);
Roadmap
Needs Tests!
This code is trivial, it works and it's been in use for a while before it was
published as an NPM. However, we need tests to protect against regressions!
I think jest is already setup here for us.
History