Code Monkey home page Code Monkey logo

query-to-json-api's Introduction

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.

query-to-json-api's People

Contributors

saibotsivad avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

query-to-json-api's Issues

Parsing square brackets incorrectly

Given a query like /api/events?filter[state]=published&filter[epoch][$gt]=1 I would expect:

const queryToJsonApi = require('query-to-json-api')
const { parse } = require('url')

const { query } = parse('/api/events?filter%5Bstate%5D=published&filter%5Bepoch%5D%5B%24gt%5D=1&filter%5Bepoch%5D%5B%24lt%5D=2&filter%5Btype%5D=GAME&filter%5BteamId%5D=3')
const q = queryToJsonApi(query)
console.log(q.filter.epoch.$gt) // => 1

However, q.filter is actually:

{
  "epoch][$gt": "1",
  "epoch][$lt": "2",
  "state": "published",
  "teamId": "3",
  "type": "GAME"
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.