🚀 Launch Week Day 3:Introducing Supply Chain Attack Campaigns Tracking.Learn More →
Socket
Book a DemoInstallSign in
Socket

@gradient/query-array-parser

Package Overview
Dependencies
Maintainers
2
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@gradient/query-array-parser

Middleware to parse comma separated query params into string arrays.

latest
Source
npmnpm
Version
1.0.2
Version published
Maintainers
2
Created
Source

Query Array Parser

Middleware to parse comma separated query params into string arrays.

This provides an alternative method for passing arrays as query parameters within express applications.

The default express system requires you to pass up arrays like this:

https://myapp.io?arr=valueOne&arr=valueTwo&arr=valueThree

With this middleware, you can use a comma separated single parameter instead such as this:

https://myapp.io?arr=valueOne,valueTwo,valueThree

Installation

npm install @gradient/query-array-parser --save

Usage

As global middleware

const express = require('express');
const queryArrayParser = require('@gradient/query-array-parser');

const app = express();

// queryArrayParser accepts either a single string, or an array of
// strings. These strings denote the query parameter name, which will
// be transformed into a string.
app.use(queryArrayParser('categories'));

app.get('/', (req, res, next) => {
  // req.params.categories is an array
  console.log(req.params.categories);
  res.json(req.params.categories).end();
});

app.listen(3000, () => {
  console.log('listening');
});

As route specific middleware

const express = require('express');
const queryArrayParser = require('@gradient/query-array-parser');

const app = express();

// queryArrayParser accepts either a single string, or an array of
// strings. These strings denote the query parameter name, which will
// be transformed into a string.
const options = {
  paramNames: ['categories', 'owners'],
  delim: ','
};

app.get('/', queryArrayParser(options), (req, res, next) => {
  console.log(req.params.categories);
  console.log(req.params.owners);
  res.json({categories: req.query.categories, tags: req.query.owners});
});

app.listen(3000, () => {
  console.log('listening');
});

API

queryArrayParser takes either a string or an options object as below:

  • queryArrayParser(string) - The name of the single query parameter that should be parsed with the default delimiter.
  • queryArrayParser(object) - An options object

Options

  • paramNames - A single string or array of strings denoting the query parameter(s) to be parsed.
  • delim - The delimiter that will be used in the query parameters to be parsed. Defaults to ,.

Copyright (c) 2017 Gradient Limited - Released under the MIT license.

Keywords

express

FAQs

Package last updated on 16 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts