urlbat
v4.1.0
Published
A library to join all parts of a url
Downloads
139
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=50
You can pass a base url also
urlbat("https://example.com/", "/user/:id/info", {
id: "123",
nice: "yep",
});
// https://example.com/user/123/info?nice=yep
Install
npm i urlbat
Options
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=c
Separate 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%2Cc
Just 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%5D