inspector.restDocs

axios

Record every axios request with instrument_axios. It wraps the axios adapter, so the real headers and body that go on the wire are captured, including interceptor changes.

JavaScript
import axios from 'axios';
import instrument_axios from '@inspector-rest/sdk/axios';

const eject = instrument_axios(axios, inspector, {
  labels: { client: 'billing-api' }, // added to every request of this instance
  status_as_error: true,             // default: 4xx/5xx responses are reported as errors
});

axios.get(url, { inspector_labels: { op: 'list' } }); // labels of a single request

eject(); // restores the original adapter

How it works

The adapter wraps axios’ adapter. The request headers and body that are recorded are the ones that go on the wire, including headers added by interceptors registered before or after instrument_axios (for example an authorization header). The body is never serialized a second time, and getUri is not called.

Good to know

  • It works with any axios adapter (http, fetch, xhr) and in every runtime.
  • Pass the default axios export or an instance from axios.create(). For created instances the SDK resolves the adapter through the installed axios package, so keep a single copy of axios.
  • A request that sets its own adapter option is not recorded.
  • 4xx and 5xx responses that your validateStatus accepts are still reported with an error named HttpError (the class is exported from the package). With status_as_error: false they settle as ok.
  • Streaming responses (responseType: 'stream') are recorded without a body.
  • axios has no retry of its own, so every call is one attempt. Retries done by a library that calls axios again are separate records.
  • Labels: the instance labels, the context set with with_labels and inspector_labels of a request are merged. See Labels.