Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
psr7-js
Advanced tools
Readme
Coming from the PHP World, you probably use PSR-7 UriInterface almost every day to manipulate URIs.
But what if you come to front-end development? You can use native URL
objects, which are mutable, have no fluent setters and different property names. And only absolute URLs are supported.
So, this library is intended to expose the same methods as PSR-7, in an immutable way:
import {URI} from 'psr7-js';
let uri = new URI('/foo'); // Relative URLs are supported - Defaults to window.location.href
uri = uri.withQuery('foo=bar');
console.log(uri.toString()); // /foo?foo=bar
Query Strings aren't part of the PSR-7 specification, but there are still issues when dealing with complex query strings in Javascript.
There are lots of query string manipulation libraries on NPM packages, but I couldn't find one which properly handles PHP's array syntax (sequential and/or associative).
import {QueryString} from 'psr7-js';
let qs = new QueryString('?foo=bar'); // Accepts strings (leading ? is ignored) or objects - Defaults to window.location.search
qs = qs.withParam('bar', 'baz'); // foo=bar&bar=baz
qs = qs.withoutParam('bar'); // foo=bar
qs = qs.withParam('foos', ['foo', 'bar']); // foo=bar&foos[]=foo&foos[]=bar
qs = qs.withParam('sort', {'updated_at': 'desc', 'hits': 'desc'}); // foo=bar&foos[]=foo&foos[]=bar&sort[updated_at]=desc&hits=desc
qs = qs.withoutParam('sort', 'hits'); // foo=bar&foos[]=foo&foos[]=bar&sort[updated_at]=desc
console.log(qs.getParams());
/*
{
"foo": "bar",
"foos": [
"foo",
"bar"
],
"sort": {
"updated_at": "desc"
}
}
*/
console.log(qs.toString()); // foo=bar&foos[]=foo&foos[]=bar&sort[updated_at]=desc
Inspired by bentools/querystring.
yarn test // or npm run-script test
MIT.
FAQs
URI and Query String immutable manipulation, support for bracket syntax.
The npm package psr7-js receives a total of 129 weekly downloads. As such, psr7-js popularity was classified as not popular.
We found that psr7-js demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.