Gaxios

An HTTP request client that provides an axios like interface over top of no...

README

gaxios


An HTTP request client that provides an axios like interface over top of node-fetch.


Install


  1. ```sh
  2. $ npm install gaxios
  3. ```

Example


  1. ```js
  2. const {request} = require('gaxios');
  3. const res = await request({
  4.   url: 'https://www.googleapis.com/discovery/v1/apis/',
  5. });
  6. ```

Setting Defaults


Gaxios supports setting default properties both on the default instance, and on additional instances. This is often useful when making many requests to the same domain with the same base settings. For example:

  1. ```js
  2. const gaxios = require('gaxios');
  3. gaxios.instance.defaults = {
  4.   baseURL: 'https://example.com'
  5.   headers: {
  6.     Authorization: 'SOME_TOKEN'
  7.   }
  8. }
  9. gaxios.request({url: '/data'}).then(...);
  10. ```

Note that setting default values will take precedence
over other authentication methods, i.e., application default credentials.

Request Options


  1. ```js
  2. {
  3.   // The url to which the request should be sent.  Required.
  4.   url: string,

  5.   // The HTTP method to use for the request.  Defaults to `GET`.
  6.   method: 'GET',

  7.   // The base Url to use for the request. Prepended to the `url` property above.
  8.   baseURL: 'https://example.com';

  9.   // The HTTP methods to be sent with the request.
  10.   headers: { 'some': 'header' },

  11.   // The data to send in the body of the request. Data objects will be
  12.   // serialized as JSON.
  13.   //
  14.   // Note: if you would like to provide a Content-Type header other than
  15.   // application/json you you must provide a string or readable stream, rather
  16.   // than an object:
  17.   // data: JSON.stringify({some: 'data'})
  18.   // data: fs.readFile('./some-data.jpeg')
  19.   data: {
  20.     some: 'data'
  21.   },

  22.   // The max size of the http response content in bytes allowed.
  23.   // Defaults to `0`, which is the same as unset.
  24.   maxContentLength: 2000,

  25.   // The max number of HTTP redirects to follow.
  26.   // Defaults to 100.
  27.   maxRedirects: 100,

  28.   // The querystring parameters that will be encoded using `qs` and
  29.   // appended to the url
  30.   params: {
  31.     querystring: 'parameters'
  32.   },

  33.   // By default, we use the `querystring` package in node core to serialize
  34.   // querystring parameters.  You can override that and provide your
  35.   // own implementation.
  36.   paramsSerializer: (params) => {
  37.     return qs.stringify(params);
  38.   },

  39.   // The timeout for the HTTP request in milliseconds. Defaults to 0.
  40.   timeout: 1000,

  41.   // Optional method to override making the actual HTTP request. Useful
  42.   // for writing tests and instrumentation
  43.   adapter?: async (options, defaultAdapter) => {
  44.     const res = await defaultAdapter(options);
  45.     res.data = {
  46.       ...res.data,
  47.       extraProperty: 'your extra property',
  48.     };
  49.     return res;
  50.   };

  51.   // The expected return type of the request.  Options are:
  52.   // json | stream | blob | arraybuffer | text | unknown
  53.   // Defaults to `unknown`.
  54.   responseType: 'unknown',

  55.   // The node.js http agent to use for the request.
  56.   agent: someHttpsAgent,

  57.   // Custom function to determine if the response is valid based on the
  58.   // status code.  Defaults to (>= 200 && < 300)
  59.   validateStatus: (status: number) => true,

  60.   // Implementation of `fetch` to use when making the API call. By default,
  61.   // will use the browser context if available, and fall back to `node-fetch`
  62.   // in node.js otherwise.
  63.   fetchImplementation?: typeof fetch;

  64.   // Configuration for retrying of requests.
  65.   retryConfig: {
  66.     // The number of times to retry the request.  Defaults to 3.
  67.     retry?: number;

  68.     // The number of retries already attempted.
  69.     currentRetryAttempt?: number;

  70.     // The HTTP Methods that will be automatically retried.
  71.     // Defaults to ['GET','PUT','HEAD','OPTIONS','DELETE']
  72.     httpMethodsToRetry?: string[];

  73.     // The HTTP response status codes that will automatically be retried.
  74.     // Defaults to: [[100, 199], [429, 429], [500, 599]]
  75.     statusCodesToRetry?: number[][];

  76.     // Function to invoke when a retry attempt is made.
  77.     onRetryAttempt?: (err: GaxiosError) => Promise<void> | void;

  78.     // Function to invoke which determines if you should retry
  79.     shouldRetry?: (err: GaxiosError) => Promise<boolean> | boolean;

  80.     // When there is no response, the number of retries to attempt. Defaults to 2.
  81.     noResponseRetries?: number;

  82.     // The amount of time to initially delay the retry, in ms.  Defaults to 100ms.
  83.     retryDelay?: number;
  84.   },

  85.   // Enables default configuration for retries.
  86.   retry: boolean,

  87.   // Cancelling a request requires the `abort-controller` library.
  88.   // See https://github.com/bitinn/node-fetch#request-cancellation-with-abortsignal
  89.   signal?: AbortSignal

  90.   /**
  91.    * An experimental, customizable error redactor.
  92.    *
  93.    * Set `false` to disable.
  94.    *
  95.    * @experimental
  96.    */
  97.   errorRedactor?: typeof defaultErrorRedactor | false;
  98. }
  99. ```

License