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 🙏

© 2026 – Pkg Stats / Ryan Hefner

to-query

v1.6.20

Published

Get query object from a request url

Readme

Last version Coverage Status NPM Status

Get query object from a request url as input.

Highlights

  • Get query parameters for any URL. eg. /?foo=bar{ foo: 'bar' }
  • Normalize keys to camel case. eg. /?user_agent=googlebot{ userAgent: 'googlebot' }
  • Auto cast values to native types. eg. /?plan_id=123{ planId: 123 }
  • Friendly boolean values. eg. /?is_enabled{ isEnabled: true }
  • Declare default values. eg. /?{ accept: '*' }

Also it supports required fields, validation, error handling and more.

Installation

$ npm install to-query --save

Get Started

to-query is a convenient way for getting query parameters from any request url as input.

const toQuery = require('to-query')()

const query = toQuery('/?foo=bar') // => { foo: 'bar' }

Default

Sometimes you need to associate a default value to be used in case if one is not provided:

const userAgentString = require('ua-string')
const createQuery = require('to-query')

const toQuery = createQuery({
  userAgent: {
    default: userAgentString
  }
})

toQuery('/?') // => { userAgent: 'Mozilla/5.0 (Macintosh; Intel…' }
toQuery('/?user_agent=googlebot') // =>  { userAgent: 'googlebot' }

Required

Declaring fields as required means it throw an error in case of non presence:

const createQuery = require('to-query')

const toQuery = createQuery({
  url: {
    required: true
  }
})

toQuery('/?foo=bar')
// => TypeError: Expected `string` for `url`, got `undefined`

Custom Error Message

In case you provide an string instead of a boolean it will be used as the message to show under error:

const createQuery = require('to-query')

const toQuery = createQuery({
  url: {
    required: 'You need to provide an URL.'
  }
})

toQuery('/?foo=bar')
// => TypeError: You need to provide an URL.

Validate

If you need granular control for type checking the shape of value in something you expect, you can declare any kind of validation, making easy connect with other packages:

const isUrlHttp = require('is-url-http')
const createQuery = require('to-query')

const toQuery = createQuery({
  url: {
    validate: {
      validator: isUrlHttp,
      message: input => `The value '${input}' is not a valid http(s) URL.`
    }
  }
})

toQuery('/?url=kikobeats.com')
// => TypeError: The value 'kikobeats.com' is not a valid http(s) URL.

Transform

You can mutate an individual value, using one or more functions to produce the final output:

const createQuery = require('to-query')

const split = str => str.split(',').map(item => item.trim())

const toQuery = createQuery({
  url: {
    filters: {
      transform: [split]
    }
  }
})

toQuery('/?filters=prerender,auto,resize')
// => { filters: ['prerender', 'auto', 'resize'] }

Usage

to-query has been designed to do just one thing well.

In this aspect, to-query is framework agnostic, giving you freedom to connect it with the rest of your software.

In case you want ot use it with any HTTP server (Express, Micro, Koa, Hapi, Fastify,...) just provide the url of the request.

const toQuery = require('to-query')()

module.exports = (req, res) => {
  req.query = createQuery(req.url)
  res.end('Your query is', req.query)
}

That's all!

API

toQuery = to-query([options])

It creates to-query instance.

options

Any option provided will be passed to osom, check the documentation to know more.

Additionally you can setup

map

Type: function

A function to run as mapper before process the url.

The default map just convert keys into camel case.

toQuery(input)

input

Required Type: string|object

The input value to convert into a query object.

License

to-query © Kiko Beats, released under the MIT License. Authored and maintained by Kiko Beats with help from contributors.

kikobeats.com · GitHub Kiko Beats · Twitter @Kikobeats