urlbat
v5.0.0
Published
A library to join all parts of a url
Downloads
1,480
Maintainers
Readme
What is this?
This package is meant to be a faster and smaller drop-in alternative for urlcat.
Why
For me, the urlcat package is very useful, but it lacks a few required features, for example, choosing how to format arrays in the querystring.
Also, the original project's last update was 2 years ago
Features
- 750 bytes min + gzip
- Typescript
- 0 Dependencies
- Multiple formatters for arrays in querystrings
- Stable, sorts the querystring
Simple example
const urlbat = require("urlbat").default;
import urlbat from "urlbat";
const url = urlbat("/user/:id/info", {
id: "123",
verbose: true,
escaped: "/mid/",
page: 1,
count: 50,
});
console.log(url);
// /user/123/info?verbose=true&escaped=%2Fmid%2F&page=1&count=50You can pass a base url also
urlbat("https://example.com/", "/user/:id/info", {
id: "123",
nice: "yep",
});
// https://example.com/user/123/info?nice=yepInstall
npm i urlbatOptions
Options is the second non string object passed to the function, after parameters
array: specify which formatting behavior you want to use for arrays, default repeat
More on the array option
Repeat the values:
urlbat(
"https://example.com/",
"/user/:id/info",
{
id: "123",
nice: ["a", "b", "c"],
},
{ array: "repeat" }
);
// https://example.com/user/123/info?nice=a&nice=b&nice=cSeparate them with a comma
urlbat(
"https://example.com/",
"/user/:id/info",
{
id: "123",
nice: ["a", "b", "c"],
},
{ array: "comma" }
);
// https://example.com/user/123/info?nice=a%2Cb%2CcJust JSON.stringify the thing
const url = urlbat(
"https://example.com/",
"/user/:id/info",
{
id: "123",
nice: ["a", "b", "c"],
},
{ array: "stringify" }
);
// https://example.com/user/123/info?nice=%5B%22a%22%2C%22b%22%2C%22c%22%5DPath segments
From v5, the path segments are no longer just split by / but with . as well
v4:
// path segment error, unknown key id.html
assert(urlbat("foo/:id.html", {id: 1}))v5:
assert(urlbat("foo/:id.html",{id: 1}), "foo/1.html")