Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fluent-url

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fluent-url

A chainable version of the global URL class

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11
decreased by-63.33%
Maintainers
1
Weekly downloads
 
Created
Source

fluent-url

Build Status Code Coverage ISC License

A chainable version of the global URL class

Installation

npm install fluent-url

Goals

A simple class that uses URL and URLSearchParams built-ins in a fluent (chainable) manner.

Example Usage

import url from 'fluent-url';

// simple construction with query string
const search = url('https://api.example.com/search', { term: 'Foo' });
search.href(); // https://api.example.com/search?term=Foo

// chainable example
const search = url('https://api.example.com/search?term=Foo');
search
	.port('8080')
	.search({
		b: 'two',
		a: 'one',
	})
	.sort()
	.path('/login')
	.hash('#username')
	.href(); // https://api.example.com:8080/login?a=one&b=two#username

// extend search query instead of overwriting it
const search = url('https://api.example.com/search?term=Foo');
search.qsExtend({
	limit: 10,
	sort: 'created_at',
});
search.href(); // https://api.example.com/search?term=Foo&limit=10&sort=created_at

Instantiating

FluentURL can be instantiated using multiple signatures:

url(myUrl); // myUrl may be string or instance of URL
url(myUrl, searchParams); // searchParams is a plain object or instance of URLSearchParams
url(relative, baseUrl); // relative is the path and baseUrl is the domain
url(relative, baseUrl, searchParams); // searchParams is a string, plain object or instance of URLSearchParams

Main Methods

URL built-inFluentURL getFluentURL setDescriptionExample
hash.hash().hash(newHash)The hash string including "#"#foo
host.host().host(newHost)The domain name including port if applicableexample.com
hostname.hostname().hostname(newHost)The domain name excluding portexample.com
href.href().href(newHref)The entire URL - same as .toString()http://example.com/foo?bar#baz
origin.origin()N/AThe protocol, domain and porthttp://example.com:8443
password.password().password(newPassword)The passwordftp-password
pathname.pathname().pathname(newPathname)The path including leading slash/admin
port.port().port(newPort)The port number as a string8080
protocol.protocol().protocol(newProtocol)The schemehttps:
search.search().search(newSearch†)The search string†, including "?"?a=one&b=two
searchParams.searchParams.searchParams = paramsThe URLSearchParams object for this URLnew URLSearchParams('a=1')
username.username().username(newUsername)The username stringftpuser
toString().toString()N/AThe entire URLhttp://example.com/foo?bar#baz

newSearch can be a string, URLSearchParams object, or plain object.

Query String Methods

URLSearchParams built-inFluentURLSearchParamsDescription
append().qsAppend(name, value)Add a single param
N/A.qsClear()Delete all params - same as .qsDeleteAll() and .qsReset()
.delete(name).qsDelete(name)Delete one param
N/A.qsDeleteAll()Delete all params - same as .qsClear() and .qsReset()
.entries().qsEntries()Get an iterable param set
N/A.qsExtend(withParams)Set params by string, URLSearchParams object, or plain object
.forEach(callback, thisArg).qsForEach(callback, thisArg)Iterate through params
.get(name).qsGet(name)Get the value of a single param by name
.getAll(name).qsGetAll(name)Get the value of a all params with name
.has(name).qsHas(name)Check if the given param is present
.keys().qsKeys()Get an array of all param names
N/A.qsReset()Clear out all params - same as .qsClear() and .qsDeleteAll()
.set(name, value).qsSet(name, value)Set one param
N/A.qsSetAll(params)Reset then set params by string, URLSearchParams object, or plain object
.sort().qsSort()Sort params alphabetically (helpful for comparison or caching)
.values().qsValues()Get an array of all param values

Wait, what is FluentURLSearchParams? It is a class used internally for all the qs* methods. Its methods are named without the qs such as append(), clear(), delete(), etc. If you would like to use it directly, it is exported by name so it can be used like this:

import url, { FluentURLSearchParams } from 'fluent-url';
const FluentURLSearchParams = require('fluent-url').FluentUrlSearchParams;

Properties

NameDescription
.URLThe URL object
.fluentParamsThe FluentURLSearchParams object
.fluentParams.searchParamsReference to .URL.searchParams

Unit Tests and Code Coverage

Powered by jest

npm test
npm run coverage

Contributing

Contributions are welcome. Please open a GitHub ticket for bugs or feature requests. Please make a pull request for any fixes or new code you'd like to be incorporated.

License

Open Source under the ISC License.

Keywords

FAQs

Package last updated on 20 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc