2.0.0 • Published 1 year ago

@domotz/chaos-interceptor v2.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

chaos-interceptor

Features

🌪 Randomize response ⏱ Delay response

Usage

Basic Usage

Javascript Fetch

Use chaosInterceptor to random raise error on network request

import {createChaosFetch} from '@domotz/chaos-interceptor';

window.fetch = createChaosFetch();

then

try {
  await fetch("http://api.github.com/");
} catch (error) {
  // may happen fetch errors with random
  console.log(error.status);
  console.log(error.statusText);
}
Apollo-Client

You can specify the fetch to use in HttpLink configuration object.

import {createChaosFetch} from '@domotz/chaos-interceptor';
import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client';

const httpLink = new HttpLink({
   uri: '/graphql',
   fetch: createChaosFetch(simulatedErrors, {logError: true}),
});

const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: httpLink,
});
Axios Fetch

Use chaosInterceptor

import {createChaosInterceptor} from '@domotz/chaos-interceptor';

const client = axios.create();
const chaosInterceptor = createChaosInterceptor();
client.interceptors.response.use(chaosInterceptor);

then

try {
  await axios.get("http://api.github.com/");
} catch (error) {
  // may happen AxiosError with random
  console.log(error.status);
  console.log(error.data);
}
Possible outputs

Possible error.status is one of following

  • 429
  • 500
  • 502
  • 503
  • 504

Possible error.statusText is one of following

  • "Too Many Requests"
  • "Internal Server Error"
  • "Bad Gateway"
  • "Service Unavailable"
  • "Gateway Timeout"

Params

Specify the response that will result in an error

const chaosFetch = createChaosFetch([
  {
    status: 500,
    body: {
      message: "Internal Server Error",
    },
    delay: 500, // delay response (ms)
    rate: 10, // possibilities (%)
  },
  {
    status: 504,
    body: {
      message: "Gateway Timeout",
    },
    delay: 100000,
    rate: 20,
  },
]);

Possible error.status is one of following

  • 500
  • 504

Possible error.statusText is one of following

  • { message: "Internal Server Error" }
  • { message: "Gateway Timeout" }

With {showError: true} it console.log a message everytime a response is rejected