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

@mokup/query

v2.0.0

Published

TanStack Query integrations for mokup client switching.

Readme

@mokup/query

TanStack Query integrations for Mokup request switching.

Requirements

  • Node.js >=22.12.0

Upgrade notes

  • This package now ships ESM-only output.
  • The default Fetch executor now rejects non-2xx responses with MokupHttpError, so TanStack Query treats HTTP failures as errors. A custom transformResponse supplies the complete response policy.
  • The Fetch executor now JSON-encodes plain object and array request bodies. It adds Content-Type: application/json only when no content type is supplied.
  • Full migration guide: ../../docs/guide/migration-v1.md

Install

pnpm add @mokup/query @mokup/client

Quick start (global defaults)

import { applyMokupToQueryClient } from '@mokup/query'
import { QueryClient } from '@tanstack/react-query'

const queryClient = new QueryClient()

applyMokupToQueryClient(queryClient, {
  resolverOptions: {
    mockBase: 'http://localhost:3300',
    realBase: 'https://api.example.com',
    pathMap: [{ from: '/api/*', to: '/*' }],
    markers: { header: true },
  },
})

Query key convention

Default queryKey shapes:

const keyA = ['GET', '/users', { params, headers, body, mock: true }]
const keyB = ['/users', { params, mock: true }]

Per-request override using meta:

useQuery({
  queryKey: ['GET', '/users'],
  meta: { mokup: true },
})

If your app already has custom query keys, provide buildRequest to map them.

Custom request mapping

import { createMokupQueryClient } from '@mokup/query'

const { queryFn, mutationFn } = createMokupQueryClient({
  buildRequest(queryKey, meta) {
    const [resource, params] = queryKey as [string, Record<string, unknown>?]
    return {
      url: resource,
      method: 'GET',
      params,
      meta,
    }
  },
})

Using axios

import { applyMokupToQueryClient, createAxiosExecutor } from '@mokup/query'
import axios from 'axios'

const executor = createAxiosExecutor({
  axios,
  resolverOptions: {
    mockBase: 'http://localhost:3300',
    realBase: 'https://api.example.com',
  },
})

applyMokupToQueryClient(queryClient, { executor })

You can also pass an Axios instance created with axios.create(). The executor reads its current defaults.baseURL and defaults.allowAbsoluteUrls on every request, combines the URL using Axios rules, then applies the Mokup resolver. For example, a base URL ending in /api/v1 and a query URL /users retain the /api/v1/users path. No request interceptor is installed on the instance.

Using fetch

import { applyMokupToQueryClient, createFetchExecutor } from '@mokup/query'

const executor = createFetchExecutor({
  resolverOptions: {
    mockBase: 'http://localhost:3300',
    realBase: 'https://api.example.com',
  },
})

applyMokupToQueryClient(queryClient, { executor })

The default Fetch executor rejects non-2xx responses with MokupHttpError before reading the response body. Successful HEAD requests and HTTP 204/205 responses return an empty string, including when a JSON content type is present. Other successful responses use response.json() when the content type contains application/json, or response.text() otherwise. Empty or malformed JSON on a regular successful response still rejects with its JSON parse error. This also applies when createMokupQueryClient or applyMokupToQueryClient creates the default executor.

Reading HTTP errors

MokupHttpError extends Error, has the name MokupHttpError, and exposes readonly response, status, and statusText properties. Its response is the original Fetch Response, whose body is still available to read:

import { createFetchExecutor, MokupHttpError } from '@mokup/query'

const executor = createFetchExecutor()

try {
  await executor({ url: '/users', method: 'GET' })
}
catch (error) {
  if (!(error instanceof MokupHttpError)) {
    throw error
  }
  const details = await error.response.text()
  console.error(error.status, error.statusText, details)
}

Custom response handling

transformResponse receives every response, including non-2xx responses, and completely replaces the default status check and body parsing. For example, this executor treats a missing user as null and rejects other HTTP failures:

import { createFetchExecutor, MokupHttpError } from '@mokup/query'

const executor = createFetchExecutor({
  async transformResponse(response) {
    if (response.status === 404) {
      return null
    }
    if (!response.ok) {
      throw new MokupHttpError(response)
    }
    return response.json()
  },
})

You can also pass transformResponse to createMokupQueryClient or applyMokupToQueryClient when using their default Fetch executor.

Request bodies

Plain objects, including objects with a null prototype or from another realm, and arrays are encoded with JSON.stringify. The executor adds Content-Type: application/json only if the request has no content type; an explicitly supplied content type is preserved. Custom resolvers still receive the original body and may supply a content type; JSON encoding happens after resolution, immediately before Fetch.

await executor({
  url: '/users',
  method: 'POST',
  body: { name: 'Ada' },
})

Native Fetch BodyInit values, such as strings, FormData, URLSearchParams, Blob, buffers, typed arrays, and streams, pass through without JSON encoding or an added JSON content type. These response and body rules belong to the Query Fetch executor; @mokup/client's raw Fetch adapter retains native Fetch response semantics, and the Axios executor uses Axios behavior.