1.3.3 • Published 3 years ago

express-query-parser v1.3.3

Weekly downloads
1,930
License
MIT
Repository
-
Last release
3 years ago

npm.io npm.io npm.io

Express-query-parser

A parser helps you parse request for your express server. You may usually encounter some req.query issues like parsing 'null', 'true' and any numbered string. This parser covert them to the right type.

scenarios

query with object

// GET http://localhost/?a=null&b=true&c[d]=false&c[e]=3.14

// without this parser
req.query = {a: 'null', b: 'true', c: {d: 'false', e: '3.14'}}

// with this parser
req.query = {a: null, b: true, c: {d: false, e: 3.14}}

query with array

// GET http://localhost/?a[]=null&a[]=false

// without this parser
req.query = {a: ['null', 'false']}

// with this parser
req.query = {a: [null, false]}

changelog

2022-05-31 (1.3.3): add empty string convert
2021-11-30 (1.3.2): add undefined convert
2021-10-12 (1.3.0): bump version, add number convert
2021-04-01 (1.2.0): bump version, only use esbuild and add type declarion
2020-10-26 (1.1.0): rewrite to typescript and rollup
2018-05-31 (1.0.2): support array
2018-05-29 (1.0.1): reduce size (177KB -> 17KB)
2018-05-17 (1.0.0): publish init version

feature

  • parse your query for null / boolean / undefined
  • support nested query
  • support array
  • support numbered string convert

install

# use yarn
yarn add express-query-parser

# use npm
npm i express-query-parser

usage

const { queryParser } = require('express-query-parser')
const express = require('express')
const app = express()

app.use(
  queryParser({
    parseNull: true,
    parseUndefined: true,
    parseBoolean: true,
    parseNumber: true
  })
)

options

fielddesctypedefault
parseNullconvert all "null" to nullbooleantrue
parseUndefinedconvert all "undefined" to undefinedbooleantrue
parseBooleanconvert all "true" to true and "false" to falsebooleantrue
parseNumberconvert all numbered string to int and floatbooleantrue

Welcome any issues and PRs submit :D