Git Product home page Git Product logo

strict-qs's Introduction

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

strict-qs's People

Contributors

nfroidure avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

strict-qs's Issues

return as value instead of throwing

Would it be possible to add an option to return the error as a value instead of throwing?
I would rather have a "success" check instead of having a try catch.
eg:

//do this
const qsOptions = {..., safe : true}
let match = qs.qsStrict(qsOptions, qsDefintion, "...");
if (!match.success) { ... }
// instead of this:
try {
	let match = qs.qsStrict(qsOptions, qsDefintion, "...");
} catch (error) {
	//...
}

try catch makes everything more complex and simply handling a error value is much easier.

it would just be a new parameter in the options type and instead of always throwing, you just check what the safe option is set to.

Doing this would also follow the same conventions of zod.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.