Giter VIP home page Giter VIP logo

axios-error-redact's Introduction

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 {getErrorInterceptor} from 'axios-error-redact'

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

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

// instance.get()

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 object can be initiated 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 boolean values to enable or disable these

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

const redactor = new AxiosErrorRedactor(
  false, //redactRequestData
  false, //redactResponseData
  false, //redactQueryData
  )

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 and 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 {
  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.

axios-error-redact's People

Contributors

aminfazlmondo avatar github-actions[bot] avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

scottmondo

axios-error-redact's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.