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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@termehui/http

v1.0.3

Published

Axios based HTTP client with error handlers

Downloads

0

Readme

Http Client

Axios based HTTP client with error handlers.

Installation

CDN

this package published as httpClient module in umd.

<script src="https://unpkg.com/@termehui/http"></script>

NPM

npm i @termehui/http
import { createAxiosInstance, client } from "@termehui/http";
createAxiosInstance("default error message", myAxiosConfig);
client().get(...)

Error Handling

Http client use parser functions for handling http errors. you can register your own error parser to client.

Each error handled by http client parsed as error record. this record then passed to parser functions for parsing error.

When some error occurred on request, Error parsed and passed throw all registered parser functions. if parser function return true error identifier set as current parser and client continue passing error to the next registered parser. if parser function return false error identifier set as current parser and client stop error parsing. if parser function return undefined parser function skipped!

Error Signature

Error record contains following fields:

  • raw: any
  • type: "response", "request" or undefined
  • identifier: error parser identifier handled this error last time.
  • body: any
  • status: numeric http status code or undefined
  • message: error message set by parser function or used default client message.

Cast Error As Error Interface

You can use ParseAsError function to parse error as error interface. this function returns null if passed parameter not a error object.

import { client, ParseAsError } from "@termehui/http";
client()
  .get("test-url")
  .catch((err: any) => {
    let error = ParseAsError(err);
    if (error == null) {
      // Error not a error interface type
    } else {
      console.log(err.status);
    }
  });

Create Empty Error With Default Message

You can use createError function to create a empty error with default message.

import { createError } from "@termehui/http";
const e = createError();

Register Custom Error Parser

Note: You can register multiple parser with same identifier.

import { registerParser, Error } from "@termehui/http";
registerParser("auth", (err: Error) => true | false | undefined {
  if (err.type === "response" && err.status === 401) {
    err.message = "Authentication required!"
    return false; // set error identifier to "auth" and don't run next parsers
    // return true; // set error identifier to "auth" and run next parsers
  }
  return undefined; // skip this parser
})

Pre Registered Error Parsers

Http client contains pre-defined error parser for common http errors. all parser returns true (continue to other parsers). For register pre-defined error parsers you must pass error message to parser function.

import {
  registerParser,
  AuthorizedParser,
  CSRFParser,
  ForbiddenParser,
  RateLimiterParser,
  MaintenanceParser,
  NotFoundParser,
  ServerParser,
  ValidationParser
} from "@termehui/http";
registerParser("auth", AuthorizedParser("You are not authorized"))
registerParser("auth", (err: Error) => true | false | undefined {
  if (err.type === "response" && err.status === 401) {
    // message set from previous parser
    Redirect("/login")
    return false;
  }
  return undefined;
})