4.1.0 • Published 9 months ago

urlbat v4.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

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
1.2.0

11 months ago

3.0.1

9 months ago

3.0.0

9 months ago

1.2.3

11 months ago

1.2.2

11 months ago

1.2.1

11 months ago

4.1.0

9 months ago

4.0.0

9 months ago

2.0.1

9 months ago

2.0.0

9 months ago

1.1.1

1 year ago

1.1.0

1 year ago

1.1.2

1 year ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago