npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

axios-api-request

v1.0.1

Published

An axios handler that helps facilitate requests to endpoints

Downloads

13

Readme

axios-api-request

This package creates an axios handler that helps facilitate requests to endpoints with a single instance for all.

Table of Contents

Installation

Using npm:

  npm i -D axios-api-request

  npm install --save-dev axios-api-request

Using yarn:

  yarn add axios-api-request -D

  yarn add axios-api-request --dev

Quick use

This line of code generates the same instance for all requests. This makes it easier to work with requests to an API, since the baseUrl is only declared once in this file and the methods make use of this configuration.

  • Sample:

    It is important to call 'AxiosApiRequest.init' from a high-level input file that is called only once so as not to overwrite the installation

    index.js - It could be any other input file

      import AxiosApiRequest from "axios-api-request";
    
      const main = () => {
        AxiosApiRequest.init({
          baseURL: "https://my-url-api/api",
          headers: { "Content-Type": "text/html" }
          timeout: 20000
        });
    
        // Do something...
      }

    From another file, we can make a request with traditional http methods. This retrieves the instance created in the entry file.

    other.js

      import AxiosApiRequest from "axios-api-request";
    
      const myApiRequest = async () => {
        const client = AxiosApiRequest.getClient();
        const response = await client.get("/something");
    
        /*
          * Generate a request in the following way
          axios.request({
            baseURL: 'https://my-url-api/api',
            method: 'GET',
            url: '/something',
            headers: { "Content-Type": "text/html" },
            timeout: 20000
          });
    
          * Returns the service response without having to access 'data' from the axios response
          response = { ... }
        */
    
        // Do something...
      }

It is also possible to overwrite the value of a specified property in the config of the 'init' method and add new values.

  import AxiosApiRequest from "axios-api-request";

  const myApiRequest = async () => {
    const client = AxiosApiRequest.getClient();
    const response = await client.post(
      "/product/1",
      { name: "example", id: "k39-123nb19319-asdcloj234" },
      {
        headers: { Connection: "Keep-Alive", "Content-Type": "application/json" },
        timeout: 40000
      }
    );

    /*
      * Generate a request in the following way
      axios.request({
        baseURL: 'https://my-url-api/api',
        method: 'POST',
        url: '/product/1',
        headers: { Connection: "Keep-Alive", "Content-Type": "application/json" },
        data: { name: "example", id: "k39-123nb19319-asdcloj234" },
        timeout: 40000
      });

      * Returns the service response without having to access 'data' from the axios response
      response = { ... }
    */

    // Do something...
  }

Please review the tests of each of the methods in the tests to get a clearer idea of ​​the capabilities of their use.

Methods reference

Description of each of the methods. To use each of these methods, you must first call 'AxiosApiRequest.init' to generate the instance with axios.

The current instance to use the following methods must first be recovered as follows:

  const client = AxiosApiRequest.getClient();

get()

  client.get(url, config, configResponse)

  // Sample
  const client = AxiosApiRequest.getClient();
  const response = await client.get(
    "/products",
    { headers: { "X-Requested-With": "XMLHttpRequest" } },
    ["headers"]
  );

  /*
    response = {
      data: { ... }, -> response that the service returns
      headers: { ... } -> headers that the service returns
    };
  */

The traditional 'GET' method of http with some improvements

Parameters:

| Name | Type | required | Default | Description | | -----|------|----------|---------|------------ | | url | string | yes | N/A | Server URL that will be used for the request | | config | AxiosRequestConfig | no | {} | Axios configuration available to make the request, headers added from the 'init' method are added to those specified here. If the 'method' or 'url' attribute is specified here, they will be ignored since these are configured in each of the methods to make http requests. Review Axios config for more information. | | configResponse | ConfigResponseDataType | no | [] | Array with the name of the attributes that the request could return. By default it always returns the content of 'data', review Axios response schema for more information. |


post()

  client.post(url, data, config, configResponse)

  // Sample
  const client = AxiosApiRequest.getClient();
  const response = await client.post(
    "/product/1",
    { name: "example", id: "k39-123nb19319-asdcloj234" },
    { headers: { "Content-Type": "application/json" } },
    ["headers", "status"]
  );

  /*
    response = {
      data: { ... }, -> response that the service returns
      headers: { ... } -> headers that the service returns
      status: { ... } -> status that the service returns
    };
  */

The traditional 'POST' method of http with some improvements

Parameters:

| Name | Type | required | Default | Description | | -----|------|----------|---------|------------ | | url | string | yes | N/A | Server URL that will be used for the request | | data | object | yes | N/A | These are the data to be sent as the body of the request. It must always be an object, if this is not required send an empty object | | config | AxiosRequestConfig | no | {} | Axios configuration available to make the request, headers added from the 'init' method are added to those specified here. If the 'method' or 'url' attribute is specified here, they will be ignored since these are configured in each of the methods to make http requests. Review Axios config for more information.. | | configResponse | ConfigResponseDataType | no | [] | Array with the name of the attributes that the request could return. By default it always returns the content of 'data', review Axios response schema for more information. |


put()

  client.put(url, data, config, configResponse)

  // Sample
  const client = AxiosApiRequest.getClient();
  const response = await client.put(
    "/product/1",
    { name: "example", id: "k39-123nb19319-asdcloj234" },
    { headers: { "Content-Type": "application/json" } },
    ["headers", "status"]
  );

  /*
    response = {
      data: { ... }, -> response that the service returns
      headers: { ... } -> headers that the service returns
      status: { ... } -> status that the service returns
    };
  */

The traditional 'PUT' method of http with some improvements

Parameters:

| Name | Type | required | Default | Description | | -----|------|----------|---------|------------ | | url | string | yes | N/A | Server URL that will be used for the request | | data | object | yes | N/A | These are the data to be sent as the body of the request. It must always be an object, if this is not required send an empty object | | config | AxiosRequestConfig | no | {} | Axios configuration available to make the request, headers added from the 'init' method are added to those specified here. If the 'method' or 'url' attribute is specified here, they will be ignored since these are configured in each of the methods to make http requests. Review Axios config for more information.. | | configResponse | ConfigResponseDataType | no | [] | Array with the name of the attributes that the request could return. By default it always returns the content of 'data', review Axios response schema for more information. |


patch()

  client.patch(url, data, config, configResponse)

  // Sample
  const client = AxiosApiRequest.getClient();
  const response = await client.patch(
    "/product/1",
    { name: "example", id: "k39-123nb19319-asdcloj234" },
    { headers: { "Content-Type": "application/json" } },
    ["headers", "status"]
  );

  /*
    response = {
      data: { ... }, -> response that the service returns
      headers: { ... } -> headers that the service returns
      status: { ... } -> status that the service returns
    };
  */

The traditional 'PATCH' method of http with some improvements

Parameters:

| Name | Type | required | Default | Description | | -----|------|----------|---------|------------ | | url | string | yes | N/A | Server URL that will be used for the request | | data | object | yes | N/A | These are the data to be sent as the body of the request. It must always be an object, if this is not required send an empty object | | config | AxiosRequestConfig | no | {} | Axios configuration available to make the request, headers added from the 'init' method are added to those specified here. If the 'method' or 'url' attribute is specified here, they will be ignored since these are configured in each of the methods to make http requests. Review Axios config for more information.. | | configResponse | ConfigResponseDataType | no | [] | Array with the name of the attributes that the request could return. By default it always returns the content of 'data', review Axios response schema for more information. |


delete()

  client.delete(url, config, configResponse)

  // Sample
  const client = AxiosApiRequest.getClient();
  const response = await client.delete(
    "/products",
    { headers: { "X-Requested-With": "XMLHttpRequest" } },
    ["headers"]
  );

  /*
    response = {
      data: { ... }, -> response that the service returns
      headers: { ... } -> headers that the service returns
    };
  */

The traditional 'DELETE' method of http with some improvements

Parameters:

| Name | Type | required | Default | Description | | -----|------|----------|---------|------------ | | url | string | yes | N/A | Server URL that will be used for the request | | config | AxiosRequestConfig | no | {} | Axios configuration available to make the request, headers added from the 'init' method are added to those specified here. If the 'method' or 'url' attribute is specified here, they will be ignored since these are configured in each of the methods to make http requests. Review Axios config for more information.. | | configResponse | ConfigResponseDataType | no | [] | Array with the name of the attributes that the request could return. By default it always returns the content of 'data', review Axios response schema for more information. |

 

Data Types

These types can be destructured from the package to be used in their interfaces.

ConfigResponseDataType

Array of type ConfigResponseTypes with the name of the attributes that the request could return. By default it always returns the content of 'data', review Axios response schema for more information.

ConfigResponseTypes

Name of elements that can be returned in the request response or error, review Axios response schema for more information.

Interface:

| Type | Values ​​you can take | Description | | -----|---------------------|------------ | | string | "status" \| "statusText" \| "headers" \| "config" \| "request" | Name of the attributes that the request could return. |

 

Resources

License

MIT