Socket
Socket
Sign inDemoInstall

gaxios

Package Overview
Dependencies
4
Maintainers
1
Versions
77
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

gaxios


Version published
Weekly downloads
12M
decreased by-6.2%
Maintainers
1
Created
Weekly downloads
 

Package description

What is gaxios?

The gaxios npm package is a lightweight HTTP client based on Axios but with a smaller footprint. It is designed to work in both browser and node environments, providing a simple way to make HTTP requests. It supports all HTTP request methods, automatic JSON data transformation, and custom configuration for requests.

What are gaxios's main functionalities?

GET Request

This feature allows you to make GET requests to retrieve data from a specified resource.

const { request } = require('gaxios');

async function getUser() {
  try {
    const response = await request({ url: 'https://api.example.com/user', method: 'GET' });
    console.log(response.data);
  } catch (error) {
    console.error(error);
  }
}

getUser();

POST Request

This feature allows you to make POST requests to send data to a server to create/update a resource.

const { request } = require('gaxios');

async function createUser(userData) {
  try {
    const response = await request({
      url: 'https://api.example.com/user',
      method: 'POST',
      data: userData
    });
    console.log(response.data);
  } catch (error) {
    console.error(error);
  }
}

createUser({ name: 'New User', email: 'newuser@example.com' });

Interceptors

Interceptors allow you to run your code or modify the request and/or response before the request is sent or after the response is received.

const { Gaxios } = require('gaxios');

const instance = new Gaxios({
  baseURL: 'https://api.example.com'
});

// Add a request interceptor
instance.interceptors.request.use(config => {
  // Do something before request is sent
  config.headers['Authorization'] = 'Bearer token';
  return config;
});

// Add a response interceptor
instance.interceptors.response.use(response => {
  // Do something with response data
  return response;
}, error => {
  // Handle error
  return Promise.reject(error);
});

Custom Configuration

Custom configuration allows you to specify various options for the HTTP request, such as headers, query parameters, timeout, and more.

const { request } = require('gaxios');

async function getCustomData() {
  try {
    const response = await request({
      url: 'https://api.example.com/data',
      method: 'GET',
      timeout: 5000,
      headers: { 'X-Custom-Header': 'foobar' }
    });
    console.log(response.data);
  } catch (error) {
    console.error(error);
  }
}

getCustomData();

Other packages similar to gaxios

Readme

Source

gaxios

Build Status codecov

An HTTP request client that provides an axios like interfance over top of node-fetch. Only really useful if you're trying to migrate from axios to the fetch.

Install

$ npm install gaxios

Example

const {request} = require('gaxios');
const res = await request({
  url: 'https://www.googleapis.com/discovery/v1/apis/'
});

Options

{
  // The url to which the request should be sent.  Required.
  url: string,

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

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

  // The data to base64 encode and send in the body of the request.
  data: {
    some: 'data'
  },

  // The querystring parameters that will be encoded using `qs` and
  // appended to the url
  params: {
    querystring: 'parameters'
  },

  // The timeout for the HTTP request. Defaults to 0.
  timeout: 1000,

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

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

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

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

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

    // The amount of time to initially delay the retry.  Defaults to 100.
    retryDelay?: number;

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

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

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

    // Function to invoke which determines if you should retry
    shouldRetry?: (err: GaxiosError) => boolean;

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

  // Enables default configuration for retries.
  retry: boolean;
}

License

Apache-2.0

Keywords

FAQs

Last updated on 10 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc