fx214
Hierarchical URL constants in less code.
Installation
npm i fx214
Usage
import buildTree from 'fx214';
const urls = buildTree({
api: {
private: {
newComment: 'new-comment',
newPost: 'new-post',
},
},
});
// `urls` will be like:
/**
{
api: {
private: {
newComment: '/api/private/new-comment',
newPost: '/api/private/new-post',
},
},
}
*/
To customize the value of a URL component, use the special __content__
property:
const urls = buildTree({
api: {
p: {
__content__: 'private',
newComment: 'new-comment',
newPost: 'new-post',
},
},
});
// `urls` will be like:
/**
{
api: {
p: {
newComment: '/api/private/new-comment',
newPost: '/api/private/new-post',
},
},
}
*/
Falsy values are automatically filled with key names:
const urls = buildTree({
api: {
p: {
add: 0,
remove: null,
},
},
});
// `urls` will be like:
/**
{
api: {
p: {
newComment: '/api/p/add',
newPost: '/api/p/remove',
},
},
}
*/
To replace underscores with hyphens, use Option.underscoresToHyphens
:
const urls = buildTree(
{
api: {
user_group: {
add_user: 'add_user',
// Falsy values are automatically filled with key names.
remove_user: 0,
},
},
},
{ underscoresToHyphens: true },
);
// `urls` will be like:
/**
{
api: {
p: {
add_user: '/api/user-group/add-user',
remove_user: '/api/user-group/remove-user',
},
},
}
*/
Use Option.prefix
to set a prefix on all resulting URLs:
const urls = buildTree(
{
api: {
user_group: {
add_user: 'add_user',
// Falsy values are automatically filled with key names.
remove_user: 0,
},
},
},
{ prefix: 'https://www.mgenware.com' },
);
/**
{
api: {
p: {
add_user: 'https://www.mgenware.com/api/user-group/add-user',
remove_user: 'https://www.mgenware.com/api/user-group/remove-user',
},
},
}
*/