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

rtk-query-html-guard

v1.0.2

Published

Detects HTML responses in RTK Query and converts PARSING_ERROR into a normalized, debuggable error.

Readme

rtk-query-html-guard

Detect HTML responses in RTK Query and convert opaque parsing failures into a clear, normalized error.

When an API unexpectedly returns HTML (login redirect page, proxy error page, gateway timeout page), fetchBaseQuery may surface a generic PARSING_ERROR with little context. This package wraps your baseQuery and upgrades that case to a specific HTML_RESPONSE_ERROR you can handle intentionally.

Why this package exists

If your frontend expects JSON but receives HTML, you often see errors like:

  • SyntaxError: Unexpected token < in JSON at position 0
  • RTK Query PARSING_ERROR

Those errors are common when:

  • auth middleware redirects to an HTML login page
  • reverse proxy/load balancer returns an HTML error page
  • backend route is misconfigured and responds with HTML

This package helps you detect that condition reliably and handle it with clear logic.

Installation

npm install rtk-query-html-guard

Peer dependency:

  • @reduxjs/toolkit ^2.12.0

Quick start

import { fetchBaseQuery } from "@reduxjs/toolkit/query/react";
import { createHtmlGuardBaseQuery } from "rtk-query-html-guard";

const baseQuery = createHtmlGuardBaseQuery(fetchBaseQuery({ baseUrl: "/api" }));

Use baseQuery in your API setup as usual:

import { createApi } from "@reduxjs/toolkit/query/react";

export const api = createApi({
  reducerPath: "api",
  baseQuery,
  endpoints: (builder) => ({
    getProfile: builder.query<{ id: string; name: string }, void>({
      query: () => "/profile",
    }),
  }),
});

What changes

Before wrapping:

{
	status: "PARSING_ERROR",
	originalStatus: 502,
	data: "<!doctype html><html>..."
}

After wrapping:

{
	status: "HTML_RESPONSE_ERROR",
	originalStatus: 502,
	data: {
		message:
			"Expected JSON but received an HTML response. This usually means an auth redirect, gateway timeout, or misconfigured proxy.",
		rawBodyPreview: "<!doctype html><html>..."
	}
}

Only this specific case is transformed. Successful responses and all other errors pass through unchanged.

Error type

export interface HtmlGuardError {
  status: "HTML_RESPONSE_ERROR";
  originalStatus: number | string;
  data: {
    message: string;
    rawBodyPreview: string;
  };
}

Example error handling

const result = await baseQuery({ url: "/profile" }, api, extraOptions);

if ("error" in result && result.error?.status === "HTML_RESPONSE_ERROR") {
  // Example: treat as session/auth infrastructure issue
  console.error(result.error.data.message);
  console.debug(result.error.data.rawBodyPreview);
}

Exports

  • createHtmlGuardBaseQuery
  • isHtmlResponse
  • HtmlGuardError (TypeScript type)

How detection works

isHtmlResponse checks whether the raw response body is a string that starts with common HTML markers, for example <!doctype html>, <html>, <head>, <body>, and similar tags.

Notes

  • rawBodyPreview is truncated to the first 300 characters.
  • This package does not alter successful JSON responses.
  • This package only normalizes PARSING_ERROR cases that look like HTML.

License

MIT