0.2.1 • Published 4 years ago

json-api-query-param-parser v0.2.1

Weekly downloads
1
License
ISC
Repository
github
Last release
4 years ago

JSON API Query Param Parser

Provides a function to parse the include JSON API query parameter into an object.

Installation

$ npm i json-api-query-parser

Description

The JSON API specification provides ways to request a resources relations (include) via query parameters. These relations are provided as a comma separated string, which this module transforms into an object.

Usage

const { includeParser } = require('json-api-query-param-parser');

console.dir(includeParser('author'), { depth: null });
// { author: [] }

console.dir(includeParser('author.comments'), { depth: null });
// { author: ['comments'] }

console.dir(includeParser('author.comments.author'), { depth: null });
// { author: [ { comments: ['author'] } ] }

console.dir(includeParser('author.comments,author.books'), { depth: null });
// { author: ['comments', 'books'] }

console.dir(includeParser('author.comment-logs,author.books', { convertCase: 'camelCase' }), { depth: null });
// { author: ['commentLogs', 'books'] }

Functions

includeParser (String param, Object options)

Takes the given param and creates an object representing the relationships requested.

The top level keys of the object represent the direct relations requested and will always have an array value, which will either be empty (meaning only the top level relation was requested) or contain the nested relations that were requested.

You can also provide an object with the following options:

  • convertCase

Accepted values: ['camelCase']

Will return the relations object camelCase'd.

TODO

  • Add parser for sparse fieldsets parameters

Contributing

Bugfixes / feature requests welcome.

Fork the repo and then:

  • npm install
  • npm test
  • npm run lint -- --fix
  • Create a Pull Request