1.1.0 • Published 3 years ago
http-url-builder v1.1.0
UrlBuilder
branch | build | coverage |
---|---|---|
main | ||
develop |
Utility to help building http urls for your API.
Installing
$ npm install http-url-builder
Examples
Basic setup:
import { UrlBuilder } from 'http-url-builder';
const url = UrlBuilder.create('localhost', 8080).build();
console.log(url);
// https://localhost:8080
Build an url:
// 'false' uses 'http' instead of 'https'
const url = UrlBuilder.create('localhost', 8080/*, false*/)
.addPath('foo')
.addPath('bar')
.addQueryParam('baz', 'qux')
.addQueryParam('test', 123)
.build();
console.log(url);
// https://localhost:8080/foo/bar?baz=qux&test=123
addQueryParam()
accepts strings, numbers, booleans and objects. Objects will be converted to their JSON representation to create the url.
const obj = {
foo: 'bar',
baz: 0,
qux: true,
};
const url = UrlBuilder.create('localhost', 8080)
.addPath('example')
.addQueryParam('myObj', obj)
.build();
console.log(url);
// https://localhost:8080/example?myObj={"foo":"bar","baz":0,"qux":true}
Each addPath()
and addQueryParam()
operation creates a new UrlBuilder instance.
Thanks to immutability, paths can not be accidentally modified:
const base = UrlBuilder.create('localhost', 8080).addPath('base');
const fooUrl = base.addPath('foo');
const barUrl = base.addPath('bar');
console.log(base.build()); // https://localhost:8080/base
console.log(fooUrl.build()); // https://localhost:8080/base/foo
console.log(barUrl.build()); // https://localhost:8080/base/bar
If you already have an url, you can convert it to an UrlBuilder instance by using its constructor:
const url = new UrlBuilder("https://localhost:8080/my/path");
License
1.1.0-dev-2021.11.9-10.22.33
3 years ago
1.1.0-dev-2021.11.11-10.22.5
3 years ago
1.1.0-dev-2021.10.28-10.22.5
3 years ago
1.1.0-dev-2021.10.4-10.21.28
3 years ago
1.1.0-dev-2021.9.9-10.21.45
3 years ago
1.1.0-dev-2021.9.8-10.22.8
3 years ago
1.1.0-dev-2021.8.15-10.20.34
3 years ago
1.1.0
3 years ago
1.1.0-dev-2021.8.14-10.20.43
3 years ago
1.0.0-dev-2021.8.10-10.21.21
3 years ago
1.0.0-dev-2021.8.5-10.21.19
3 years ago
1.0.0-dev-2021.7.28-10.21.57
3 years ago
1.0.0-dev-2021.7.26-10.36.26
3 years ago
1.0.0-dev-2021.7.25-10.21.6
3 years ago
1.0.0-dev-2021.7.22-10.21.33
3 years ago
1.0.0-dev-2021.7.18-14.50.31
3 years ago
1.0.0
3 years ago