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.2.0
  • latest
  • 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 endpoint = url('https://api.example.com/search', { term: 'Foo' });
endpoint.href();
// => "https://api.example.com/search?term=Foo"

// chainable example
const endpoint = url('https://api.example.com/search?term=Foo');
endpoint
	.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 endpoint = url('https://api.example.com/search?term=Foo');
endpoint.qsExtend({
	limit: 10,
	sort: 'created_at',
});
endpoint.href();
// => "https://api.example.com/search?term=Foo&limit=10&sort=created_at"

// manipulate relative URLs
url('/search?term=Foo').qsAppend('limit', 10).href();
// => "/search?term=Foo&limit=10"

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, base); // relative is the path and base is a string with protocol, port, domain
url(relative, base, searchParams); // searchParams is a string, plain object or instance of URLSearchParams

URL Built-in 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 applicablesub.example.com:8443
.hostname.hostname().hostname(newHost)The domain name excluding portsub.example.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, plain object, or URLSearchParams object?a=one&b=two
.toString().toString()N/AThe entire URLhttp://example.com/foo?bar#baz
.username.username().username(newUsername)The username stringftpuser

Custom Methods

MethodDescription
.clone()Create a new URL with the same values
.export(props)Export all or the given subset of url attributes
.import(values)Import the given pieces
.isRelative()True if the URL is relative (e.g. starts with a dot or slash)
.makeRelative()Control whether URL is relative
.searchObject([obj])Get or set query string params as 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

Hash routing URLs

fluent-url supports manipulating URLs that have hash routing:

import url from 'fluent-url';

// simple construction with query string
const controller = url('https://example.com/home#/dashboard?msg=Hello');
controller.hashPath(); // "/dashboard"
controller.hashSearch(); // { msg: "Hello" }

controller
	.hashPath('/projects')
	.hashSearch({ query: 'new construction', sort: '-created' })
	.href(); // https://example.com/home#/projects?query=new+construction&sort=-created

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 11 Sep 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