@harshilrajput/universal-api-errors
v0.1.0
Published
One error object, no matter where it came from — core + Axios + fetch adapters bundled in one install. Using React Query too? Add @harshilrajput/universal-api-errors-react-query.
Downloads
89
Maintainers
Readme
@harshilrajput/universal-api-errors
One error object, no matter where it came from.
This is the batteries-included install: core, the Axios adapter, and the fetch adapter, bundled into one package. If you'd rather install only the piece you need — say, just the fetch adapter with nothing Axios-related in your dependency tree — install that package directly instead; they're all standalone.
Install
npm install @harshilrajput/universal-api-errorspnpm add @harshilrajput/universal-api-errorsyarn add @harshilrajput/universal-api-errorsaxios is an optional peer dependency — install it if you use it. The fetch adapter needs
nothing extra; it works off the platform's built-in fetch/Response.
Usage
import { parseAxiosError, parseFetchError, parseError } from "@harshilrajput/universal-api-errors";
// Axios
axios.get("/api/user").catch((err) => {
const error = parseAxiosError(err);
if (error.isUnauthorized) return redirectToLogin();
});
// fetch
const response = await fetch("/api/user");
if (!response.ok) {
const error = await parseFetchError(response);
}
// anything else — works via duck-typing, no adapter needed
const error = parseError(err);Using React Query?
This package deliberately does not bundle the React Query integration — importing it would
force a react peer dependency onto every consumer, including plain Node/backend usage that
never touches React. If you use React Query, add it separately:
npm install @harshilrajput/universal-api-errors-react-querySee @harshilrajput/universal-api-errors-react-query
for useApiError() and createRetry().
License
MIT
