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

axios-error-redact

v1.0.30

Published

Library to redact sensitive information from Axios errors

Downloads

5,853

Readme

Axios-Error-Redact

Library to redact sensitive information from Axios errors. This can be used as an response interceptor for axios instances, or can be used standalone.

Compatibility

Works with

  • axios@^0
  • axios@^1

Getting started

npm i axios-error-redact

Interceptor usage

Simple Interceptor

The redactor can simply be used in an interceptor to extract non-sensitive data from error and continue

import axios from 'axios'
import {createErrorInterceptor} from 'axios-error-redact'

const instance = axios.create({baseURL: 'http://example.com'})

instance.interceptors.response.use(undefined, createErrorInterceptor())

try {
  await instance.get()
} catch(error) {
  // The isHttpErrorResponse helper can be used to ensure the thrown error is a redacted error
  if (isHttpErrorResponse(error)) {
    console.error(error.response.statusMessage, error.message)
  }
}

Custom Interceptor

The redactor can be used in an interceptor to extract non-sensitive data from error and continue, with this approach the interceptor can be created with some custom logic

import axios from 'axios'
import {AxiosErrorRedactor} from 'axios-error-redact'

const instance = axios.create({baseURL: 'http://example.com'})

const redactor = new AxiosErrorRedactor()

function errorInterceptor(error: any): any {
  const redactedError = redactor.redactError(error)
  // You may want to add more logic here; for example logging
  return Promise.reject(redactedError)
}

instance.interceptors.response.use(undefined, errorInterceptor)

// instance.get()

Standalone usage

The library can be used on its own without using any interceptor as well.

import axios from 'axios'
import {AxiosErrorRedactor} from 'axios-error-redact'

const redactor = new AxiosErrorRedactor()

const result = axios.get('http://example.com')
  .catch(error => redactor.redactError(error))

API

Constructor

The redactor is initialized with some defaults; in which all of the sensitive data will be redacted (request, response, query)

import {AxiosErrorRedactor} from 'axios-error-redact'

const redactor = new AxiosErrorRedactor()

The constructor also accepts options to enable or disable these

import {AxiosErrorRedactor} from 'axios-error-redact'

const redactor = new AxiosErrorRedactor({
  redactRequestDataEnabled: false,
  redactResponseDataEnabled: false,
  redactQueryDataEnabled: false,
})

Main Function

The main function that can be called on the initiated object is redactError which accepts the error as the input and returns the redacted information in an object of type HttpErrorResponse

import axios from 'axios'
import {AxiosErrorRedactor} from 'axios-error-redact'

const redactor = new AxiosErrorRedactor()

const result = axios.get('http://example.com')
  .catch(error => redactor.redactError(error))

Changing Flags

There are three functions that can be used and chained after the initiated object in order to change what sort of data should be skipped to be redacted.

import {AxiosErrorRedactor} from 'axios-error-redact'

const redactor = new AxiosErrorRedactor()
  .skipRequestData()
  .skipQueryData()

Response

The redact library will extract information from axios error and return an object with following details.

HttpErrorResponse {
  isErrorRedactedResponse: true;
  message: string;
  fullURL: string;
  response: {
    statusCode?: number;
    statusMessage: string;
    data: any;
  };
  request: {
    baseURL: string;
    path: string;
    method: string;
    data: any;
  };
}

If the error is not an axios error, then the same error will be returned.

Type guard

The isHttpErrorResponse() function can be used as a type guard in TypeScript to narrow the error type.

This can be useful when multiple error types can be thrown from the try block.

Be sure not to use the isAxiosError() type guard provided by Axios since all intercepted Axios errors will be transformed into a HttpErrorResponse

try {
  ...
} catch(error: unknown) {
  if (isHttpErrorResponse(error)) {
    // error is narrowed to type HttpErrorResponse
  }
}