npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsonapi-query-parser

v1.3.1

Published

Class to parse endpoint and its query parameters to a usable request object

Downloads

8,077

Readme

jsonapi-query-parser

JsonApiQueryParser class to parse endpoint and its query string parameters to a usable request object.

To be used for node projects that make use of JSON API

Installation

$ npm install jsonapi-query-parser

Usage

Require the module 'JsonApiQueryParser' into your application and use the 'parseRequest' function to convert the request.url to an easy usable requestData object.

let JsonApiQueryParserClass = require('jsonapi-query-parser');
let JsonApiQueryParser = new JsonApiQueryParserClass();

http.createServer(function (request, response) {
  let requestData = JsonApiQueryParser.parseRequest(request.url);

  // .. Do stuff with your requestData object

}).listen(1337, '127.0.0.1');

Return data information (requestData)

The object returned by the JsonApiQueryParser.parseRequest will always be the same structure. Please note that the query parameters are decoded when parsed! Below you can see 2 parsed examples:

//EXAMPLE 1
let url = '/article/5/relationships/comment'
let requestData = {
  resourceType: 'article',
  identifier: '5',
  relationships: true,
  relationshipType: 'comment',
  queryData: {
    include: [],
    fields: {},
    sort: [],
    page: {},
    filter: {
      like: {},
      not: {},
      lt: {},
      lte: {},
      gt: {},
      gte: {}
    }
  }
};

//EXAMPLE 2
let url = '/article/5/?include=user,comment.user&fields[article]=title%2Cbody&page[limit]=20&sort=-createdon'
let requestData = {
  resourceType: 'article',
  identifier: '5',
  relationships: false,
  relationshipType: null,
  queryData: {
    include: ['user', 'comment.user'],
    fields: {
      article: ['title', 'body']
    },
    sort: ['-createdon'],
    page: {
      limit: 20
    },
    filter: {
      like: {},
      not: {},
      lt: {},
      lte: {},
      gt: {},
      gte: {}
    }
  }
};

Important

If your endpoints contain versioning or other application specific pointers please remove them before parsing! Here are some examples of a request url:

  let CORRECT1 = '/article/';
  let CORRECT2 = '/article/5?include=comments';
  let CORRECT3 = '/article/5/relationships/comments';
  let CORRECT4 = '/article/?include=user,comment.rating&fields[article]=title,body&fields[user]=name';

  // Contains '/v1/api' which cannot be parsed properly
  let INVALID = '/v1/api/article?include=user';

Custom 'filter' implementation!

Filters might not be properly parsed since there are no specifications for this query yet! I hope to update this package as soon as filtering specs are available. For now the filters are handled like the fields parameter.

FilterType is also not supported by JSON API spec. This feature can be used to filter on partial matches, less than, greater than, and more. The feature is currently implemented for bookshelf-jsonapi-params.

//EXAMPLE 1
let url = '/article/5?filter[name]=john%20doe&&filter[age][lt]=15'
let requestData = {
  resourceType: 'article',
  identifier: '5',
  relationships: false,
  relationshipType: null,
  queryData: {
    include: [],
    fields: {},
    sort: [],
    page: {},
    filter: {
      name: 'john doe',
      age: '15',
      like: {},
      not: {},
      lt: {
        age: '15'
      },
      lte: {},
      gt: {},
      gte: {}
    }
  }
};

// alechirsh filter type implementation:
let url = '/article/5?filter[not][name]=jack'
let requestData = {
  resourceType: 'article',
  identifier: '5',
  relationships: false,
  relationshipType: null,
  queryData: {
    include: [],
    fields: {},
    sort: [],
    page: {},
    filter: {
      like: {},
      not: {
        name: 'jack'
      },
      lt: {},
      lte: {},
      gt: {},
      gte: {}
    }
  }
};

Tests!

Tests running using mocha & chai in /test