Skip to content

iamnapo/construct-url

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 

@iamnapo/construct-url

Small utility function to construct URLs, without caring about slashes or encoding

build npm

Install

npm i @iamnapo/construct-url

Usage

import constructUrl from "@iamnapo/construct-url";

constructUrl("https://example.com", "/foo"); // => "https://example.com/foo"
constructUrl("https://example.com/foo", "/bar"); // => "https://example.com/foo/bar"
constructUrl("https://example.com", "/foo", { bar: true }); // => "https://example.com/foo?bar=true"
constructUrl("https://example.com", "", { bar: true }); // => "https://example.com/?bar=true"
constructUrl("https://example.com", "/foo", { "//bar//": true }); // => "https://example.com/foo?%2F%2Fbar%2F%2F=true"

API

constructUrl(base, path?, query?)

Get a full URL, including encoded query parameters.

base

Type: string | URL

Base URL.

path

Type: string
Default: ""

Path relative to base.

query

Type: string | [string, unknown][] | Record<string, unknown> | URLSearchParams
Default: ""

Query parameters to include in the resuliting URL.

About

πŸ—οΈ Small utility function to construct URLs, without caring about slashes or encoding.

Resources

License

Stars

Watchers

Forks