query-to-json-api

5.0.1 • Public • Published

query-to-json-api

This module takes a query object and creates a copy that follows the JSON:API specs.

If you're turning a string into a query object, I recommend using the query-string module, which is fast and lightweight.

general use

If you're using modern JavaScript:

import queryToJsonApi from 'query-to-json-api'
const url = new URL('https://site.com/?fields[articles]=title,body')

// normal query
console.log(url.searchParams)
// > URLSearchParams { 'fields[articles]' => 'title,body' }

// query for JSON:API
console.log(queryToJsonApi(url.searchParams))
// > { fields: { articles: [ 'title', 'body' ] } }

If you're still on older JavaScript, the query-string library is pretty good:

const queryToJsonApi = require('query-to-json-api')
const parseQueryString = require('query-string')

// normal query parsing
const dirtyQuery = parseQueryString('fields[articles]=title,body')
// {
//     'fields[articles]': 'title,body'
// }

// query for JSON-API
const cleanQuery = queryToJsonApi(dirtyQuery)
// {
//     fields: {
//         articles: [
//             'title',
//             'body'
//         ]
//     }
// }

license

Published and released under the VOL.

Readme

Keywords

Package Sidebar

Install

npm i query-to-json-api

Weekly Downloads

85

Version

5.0.1

License

VOL

Unpacked Size

15.4 kB

Total Files

18

Last publish

Collaborators

  • saibotsivad