gaxios

An HTTP request client that provides an axios like interface over top of node-fetch.
Install
$ npm install gaxios
Example
import {request} from 'gaxios';
const res = await request({url: 'https://google.com/'});
fetch-Compatible API Example
We offer a drop-in fetch-compatible API as well.
import {instance} from 'gaxios';
const res = await instance.fetch('https://google.com/');
To disable the auto-processing of the request body in res.data, set {responseType: 'stream'} or .adapter on a Gaxios instance's defaults or per-request.
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:
import {Gaxios} from 'gaxios';
const gaxios = new Gaxios();
gaxios.defaults = {
baseURL: 'https://example.com'
headers: new Headers({
Authorization: 'SOME_TOKEN'
})
}
await gaxios.request({url: '/data'});
Note that setting default values will take precedence
over other authentication methods, i.e., application default credentials.
GaxiosResponse
The GaxiosResponse object extends the fetch API's Response object, with the addition of:
config: the configuration used for the request.
data: the transformed .body, such as JSON, text, arrayBuffer, or more.
Request Options
interface GaxiosOptions = {
url: string | URL,
method: 'GET',
baseURL: 'https://example.com/v1/' | URL;
headers: new Headers(),
data: {
some: 'data'
},
maxContentLength: 2000,
params: {
querystring: 'parameters'
},
timeout: 60000,
adapter?: async (options, defaultAdapter) => {
const res = await defaultAdapter(options);
res.data = {
...res.data,
extraProperty: 'your extra property',
};
return res;
};
responseType: 'unknown',
agent: someHttpsAgent,
validateStatus: (status: number) => true,
fetchImplementation?: typeof fetch;
retryConfig: {
retry?: number;
currentRetryAttempt?: number;
httpMethodsToRetry?: string[];
statusCodesToRetry?: number[][];
onRetryAttempt?: (err: GaxiosError) => Promise<void> | void;
shouldRetry?: (err: GaxiosError) => Promise<boolean> | boolean;
noResponseRetries?: number;
retryDelay?: number;
},
retry: boolean,
signal?: AbortSignal
multipart?: GaxiosMultipartOptions;
proxy?: string | URL;
noProxy?: (string | URL | RegExp)[];
errorRedactor?: typeof defaultErrorRedactor | false;
}
License
Apache-2.0