Socket
Socket
Sign inDemoInstall

json-api-query-string

Package Overview
Dependencies
5
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    json-api-query-string

Work with JSON API query strings using a declarative syntax


Version published
Weekly downloads
3
Maintainers
1
Install size
51.3 kB
Created
Weekly downloads
 

Readme

Source

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.

Keywords

FAQs

Last updated on 19 Jun 2021

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc