@madmed677/url-builder
v0.1.0
Published
The module which build urls for NodeJS
Downloads
5
Readme
UrlBuilder
Install
$ npm i --save @madmed677/url-builder
How to use
1 Import into your project
const {ApplicationUrlBuilder} = require('@madmed677/url-builder');
2: Configure all your routes in one single place. For an example: routes.js
const urlBuilder = new ApplicationUrlBuilder({
applications: {
installments: {
protocol: 'https',
host: 'kassa.yandex.ru'
},
checkout: {
protocol: 'https',
host: 'kassa.yandex.ru'
},
portal: {
protocol: 'https',
host: 'money.yandex.ru'
}
}
});
const routes = urlBuilder.routes({
portal: {
'main-page': {
pathname: ''
},
'transfer-page': {
pathname: 'transfer'
},
'transfer-search-page': {
pathname: 'transfer/search',
query: {
param3: 'val3'
}
}
}
});
module.exports = routes;
3: After that import your routes and build route by name. For an example: app.js
const routes = require('/path/to/routes');
const portalTransferPage = routes
.application('portal')
.action('transfer-page')
.build()
;
// 'https://money.yandex.ru/transfer/'
const portalTransferSearchPage = routes
.application('portal')
.action('transfer-search-page')
.build()
;
// 'https://money.yandex.ru/transfer/search?foo=baz'
const portalTransferSearchQueryPage = routes
.application('portal')
.action('transfer-search-page')
.build({
query: {
param1: 'val1',
param2: 'val2'
},
hash: 'some-hash'
})
;
// 'https://money.yandex.ru/transfer/search?param1=val1¶m2=val2¶m3=val3#some-hash'