query-to-json-api

This module takes a parsed query object and creates a copy following the JSON-API specs.

Usage no npm install needed!

<script type="module">
  import queryToJsonApi from 'https://cdn.skypack.dev/query-to-json-api';
</script>

README

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.