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

json-api-query-string

v0.0.1

Published

Work with JSON API query strings using a declarative syntax

Downloads

18

Readme

json-api-query-string

Feature overview

  • Parse JSON API query strings to objects
  • Stringify JSON API objects to query strings
  • Minimal code, with permissive query-string peer dependency for maximum code re-use
  • Encodes the specified parts of the JSON API specification (i.e. sort, include and fields) and provides a declarative syntax for customising the parts of the specification that are only reserved to contain implementation-specific details (i.e. page and filter)
  • Can be used in the browser or Node.js 6 or later
  • Includes a TypeScript index.d.ts file
  • Reasonable exhaustive test suite

Basic usage

import { configure, stringify, parse } from 'json-api-query-string';

/**
 * Optional global configuration (typically where you'll define how page, filter and other custom parameters
 * are treated.
 */
configure({
  formatOptions: { filter: { arrayFormat: 'comma' }}
});

const queryObject = parse('sort=foo,bar')

queryObject // { sort: ['foo', 'bar'] }

stringify({ sort: [ ...queryObject.sort, 'baz'] }); // 'sort=foo,bar,baz'

Install & Setup

json-api-query-string can be installed as a CommonJS module:

npm install json-api-query-string --save
# OR
yarn add json-api-query-string

Because query-string is often already used in many code bases, it's only a peer dependency of json-api-query-string, so if it's not already installed, you'll to add it:

npm install query-string --save
# OR
yarn add query-string

sort, include and fields

These values are full specified by the JSON API specification, so json-api-query-string comes configured for the correct behaviour:

parse('sort=')                                   // { sort: [] } 
parse('sort=foo')                                // { sort: ['foo'] }
parse('sort=foo,bar')                            // { sort: ['foo', 'bar'] }
parse('sort=foo,-bar')                           // { sort: ['foo', '-bar'] }

parse('include=')                                // { include: [] }
parse('include=foo')                             // { include: ['foo'] }
parse('include=foo,baz')                         // { include: ['foo', 'baz'] }
parse('include=foo.bar')                         // { include: ['foo.bar'] }
parse('include=foo.bar,baz')                     // { include: ['foo.bar', 'baz'] }  
parse('include=foo,bar.baz')                     // { include: ['foo', 'bar.baz'] }  

parse('fields[foo]=')                            // { fields: { foo: [] } }
parse('fields[foo]=bar')                         // { fields: { foo: ['bar'] } }
parse('fields[foo]=bar&fields[baz]=barp')        // { fields: { foo: ['bar'], baz: ['barp'] } }
parse('fields[foo]=bar,zoop&fields[baz]=barp')   // { fields: { foo: ['bar', 'zoop'], baz: ['barp'] } }

// stringify will perform the reverse transformation

filter, page and other custom fields

These parameters are reserved rather than being specified, so will vary depending on the particulars of your implementation.

You can configure json-api-query-string to correctly handle the schema globally by passing the formatOpitons option to the configure function:

import { configure } from 'json-api-query-string';

configure({
  formatOptions: { filter: { arrayFormat: 'comma' }}
});

The keys of this hash must match your parameter name (with any square brackets removed), and the values are option passed to query-string's parse function.

You can also perform "one-off" configuration by passing format options as a second argument to parse or stringify:

parse('filter[foo]=bar,baz')                                         // { filter: { foo: 'bar,baz' } }
parse('filter[foo]=bar,baz', { filter: { arrayFormat: 'comma' } })   // { filter: { foo: ['bar', 'baz'] } }

Similar packages

Thank you to query-to-json-api for the original inspiration and starting point for this package.