url-naong
v1.3.0
Published
url builder library for javascript, typescript
Downloads
62
Maintainers
Readme
url-naong
url-naong
is a simple url builder. The URL class is great, but it doesn't allow you to apply variables to pathnames and arrays to querystrings. This is where url-naong
comes in handy. url-naong is url builder that is inspired by urlcat
naong is korean name of Meowth(pokemon monster)
install
npm install url-naong --save
Usage
urlna
function take 4 parameters.
- (required) host
- (required) pathname
- (optional) path parameters and querystring
- (optional) options of the qs library
Variable in pathname
const urlna = require('url-naong').urlna;
const url = urlna(undefined, '/hero/:hid/comic/:cid/name/:name',
{
hid: 1, // evaluate in pathname
cid: 13, // evaluate in pathname
name: 'ironman', // evaluate in pathname
tid: '72368bb0-a6cc-4fa1-9288-c903fd45c333' // evaluate in querystring
}
);
console.log(url);
// created> '/hero/1/comic/13/name/ironman?tid=72368bb0-a6cc-4fa1-9288-c903fd45c333'
Querystring
const urlna = require('url-naong').urlna;
const url = urlna(undefined, '/hero/:hid/comic/:cid/name/:name',
{
hid: 1, // evaluate in pathname
cid: 13, // evaluate in pathname
name: 'ironman', // evaluate in pathname
tid: '72368bb0-a6cc-4fa1-9288-c903fd45c333' // evaluate in querystring
}
);
console.log(url);
// created> '/hero/1/comic/13/name/ironman?tid=72368bb0-a6cc-4fa1-9288-c903fd45c333'
TypeScript
import { urlna } from 'url-naong';
const url = urlna(undefined, '/hero/:hid/comic/:cid/name/:name',
{ name: 'ironman', hid: 1, cid: 13, tid: '72368bb0-a6cc-4fa1-9288-c903fd45c333', }
);
console.log(url);
// created> '/hero/1/comic/13/name/ironman?tid=72368bb0-a6cc-4fa1-9288-c903fd45c333'
Array of querystring
url-naong using qs for stringify querystring. Section Stringifying how to stringify array in querystring.
const urlna = require('url-naong').default;
const url = urlna(
undefined, '/hero/:hid/comic/:cid/name/:name',
{
name: 'ironman',
hid: 1,
cid: 13,
tid: '72368bb0-a6cc-4fa1-9288-c903fd45c333',
category: ['marvel', 'advengers', 'S.H.I.E.L.D'],
},
{
arrayFormat: 'repeat',
},
);
// created> '/hero/1/comic/13/name/ironman?tid=72368bb0-a6cc-4fa1-9288-c903fd45c333&category=marvel&category=advengers&category=S.H.I.E.L.D',