npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

strict-qs

v8.0.2

Published

A stricter Query String parser

Downloads

1,159

Readme

strict-qs

A stricter Query String parser

GitHub license Coverage Status

A stricter query string parser allows to ensure URIs uniqueness and better caching through your own cache but also public HTTP proxies for public endpoints.

To ensure URIs uniqueness, strict-qs checks:

  • the order in which query strings are set,
  • query parameters values aren't set to their default values,
  • values set are reentrant (ie: 1.10 will be refused, its canonical form 1.1 will be required),
  • query parameters used are existing and effective,
  • items collections are sorted (by value for number, alpha-numeric for strings).

As a side effect, it also cast values from strings to their target types.

You may wonder if it is not overkill to be that strict. On every projects I worked on, I never been sad to have built too strict systems. The inverse is not true ;).

Also, it may be less pain to handle such strictness if you generate client APIs that handle that strictness for you, which is recommended. You can see an example of such client here.

Usage

import qs from 'strict-qs';

// The definition formatting is swagger compatible
// but only for the subset I use. PRs are welcome
// for broader support
const qsDefinition = [{
  name: 'lang',
  in: 'query',
  type: 'string',
  required: true,
  description: 'The language for the search'
}, {
  name: 'types',
  in: 'query',
  type: 'array',
  items: {
    type: 'string',
    enum: ['open', 'closed', 'pending', 'idle', 'invalid'],
  },
  description: 'The types of the search'
}, {
  name: 'code',
  in: 'query',
  type: 'integer',
  description: 'The code id'
}];

qs(
  qsDefinition,
  '?lang=fr&types=open&types=closed&types=pending&code=3'
);
// Returns
{
  lang: 'fr',
  types: ['open', 'closed', 'pending'],
  code: 3
}

qs(
  qsDefinition,
  '?code=3&lang=fr&types=open&types=closed&types=pending'
);
// throws an error since the order is bad
new Error('E_BAD_QUERY_PARAM', 'types')

The returned query parameters should still be validated with any JSON Schema validator. You can see how it is done in swagger-http-router for instance.

API

qsStrict(options, definitions, search) ⇒ Object

Parse a queryString according to the provided definitions

Kind: global function
Returns: Object - The parsed properties

| Param | Type | Description | | --- | --- | --- | | options | Object | Parser options | | options.allowEmptySearch | Boolean | Avoid throwing when the search is empty | | options.allowUnknownParams | Boolean | Avoid throwing when some params are unknown | | options.allowDefault | Boolean | Avoid throwing when some params is set to its default value | | options.allowUnorderedParams | Boolean | Avoid throwing when params are not set in the same order than declarations | | definitions | Array | Swagger compatible list of defitions | | search | string | The actual query string to parse |

Example

import qs from 'strict-qs';

const qsOptions = { allowEmptySearch: true };
const qsDefinition = [{
  name: 'pages',
  in: 'query',
  type: 'array',
  items: {
    type: 'number',
  },
  ordered: true,
  description: 'The pages to print',
}];

qs(qsOptions, qsDefinition, '?pages=0&pages=1&pages=2');
// Returns:
// {
//  pages: [0, 1, 2], // eslint-disable-line
// }

Authors

License

MIT