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

pretender-query-param-handler

v1.4.0

Published

Library to help with working with Pretender and Query Param heavy APIs

Readme

pretender-query-param-handler

This package provides a helper function and wrapped Pretender class with support for matching query params as part of a registered handler.

Installation

# npm
npm install pretender-query-param-handler

# yarn
yarn add pretender-query-param-handler

Usage

Nothing beats a quick demo:

import { QueryParamAwarePretender } from 'pretender-query-param-handler';

let server = new QueryParamAwarePretender();

server.get('/api/graphql?foo=bar', () => [ 200, {}, '{ "query": "bar" }']);
server.get('/api/graphql?foo=baz', () => [ 200, {}, '{ "query": "baz" }']);
server.get('/api/graphql?foo=*', () => [ 200, {}, '{ "query": "xyz" }']);
server.get('/api/graphql', () => [ 200, {}, '{ "query": "none" }']);

let result;
result = await fetch('/api/graphql?foo=baz');
console.log(await result.json());
//=> { query: 'baz' }

result = await fetch('/api/graphql?foo=xyz');
console.log(await result.json());
//=> { query: 'xyz' }

result = await fetch('/api/graphql?foo=bar');
console.log(await result.json());
//=> { query: 'bar' }

result = await fetch('/api/graphql?foo=xyz&bar=123');
console.log(await result.json());
//=> { query: 'none' }

result = await fetch('/api/graphql');
console.log(await result.json());
//=> { query: 'none' }

Pattern Matching

Simple pattern based matching is supported in query string. Currently the only pattern suported is '*', which matches anything, even an empty string. Multiple patterns can be used in a query string, e.g.:

'/api/graphql?foo=*&bar=*&var=1234'

Please note: any other forms of paterns, including regular expressions, are not supported.

Fallback

Fallback means doing pathname based matching while ignoring the query string altogether, that is, '/api/graphql?foo=abc&...' is equivalent to '/api/graphql' for the purpose of matching.

pretender-query-param-handler performs URL matching in the following stages:

  1. Returns an error message if no handler found for the pathname.
  2. Performs a search against pre-registered query string literals and returns the handler if there's a match.
  3. Performa a search against a pre-registered query string patterns and returns the handler if there's a match.
  4. Returns the handler for pathname.

E.g.:

server.get('/api/graphql?foo=abc', /* handlerA */ () => [ 200, {}, '{ "query": "abc" }']);
server.get('/api/graphql?foo=*', /* handlerB */ () => [ 200, {}, '{ "query": "xyz" }']);
server.get('/api/graphql', /* handlerC */ () => [ 200, {}, '{ "query": "none" }']);

result = await fetch('/api/graphql?foo=abc');  // matches handlerA
console.log(await result.json());
//=> { query: 'abc' }

result = await fetch('/api/graphql?foo=xyz'); // matches handlerB
console.log(await result.json());
//=> { query: 'xyz' }

result = await fetch('/api/graphql?bar=xyz'); // fallback: matches handlerC
console.log(await result.json());
//=> { query: 'none' }

result = await fetch('/api/graphql?foo=xyz&bar=1234'); // fallback: matches handlerC
console.log(await result.json());
//=> { query: 'none' }

result = await fetch('/api/json?foo=xyz'); // no match
console.log(await result.json());
//=> Error message

Normalize URLs

In some cases, the order of the query params doesn't matter. Then you can set normalizeURLs to true and the server will normalize (sort) the query params while mocking and matching

import { QueryParamAwarePretender } from 'pretender-query-param-handler';

let server = new QueryParamAwarePretender({
  normalizeURLs: true
});

server.get('/api/graphql?foo=bar&bar=baz', () => [ 200, {}, '{ "query": { "foo": "bar", "bar": "baz" } }'),

let result;
result = await fetch('/api/graphql?bar=baz&foo=bar');
console.log(await result.json());
//=> { query: { foo: 'bar', bar: 'baz' } }

Unhandled Requests


const parentUnhandledRequest = this.server.unhandledRequest;
server.unhandledRequest = function () {
  try {
    parentUnhandledRequest.apply(this, arguments);
  } catch (error) {
    error.requestedQueryParams  // Map<ParamName,ParamValue> unhandled request
    error.existingQueryParams   // Map<ParamName,ParamValue> registered handlers

    // You can customize error messages here
    throw error;
  }
};

Limitations

Internet Explorer/IE is not supported due to the lack of proper Api support.

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.