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-easyfier

v1.0.1

Published

To help developers to create http requests easily with axios using a Object Builder Pattern

Downloads

9

Readme

AxiosEasyfier

Objectives

To help developers to create http requests easily with axios using a Object Builder Pattern, and learn basics javascript concepts (i'm from Java world :)).

How to use

To create a simple GET http request:

const AxiosEasyfier = require("axios-easyfier"); // import AxiosEasyfier
const easyfier = new AxiosEasyfier(); // create a AxiosEasyfier instance

//... omitted code

const response = await easyfier
  .withUrl("https://jsonplaceholder.typicode.com/todos/1")
  .GET();

//... omitted code

The response body looks like the follow:

{ "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false }

And the complete example of GET request:

const AxiosEasyfier = require("axios-easyfier");
const easyfier = new AxiosEasyfier();

const makeRequest = async () => {
  try {
    const response = await easyfier
      .withUrl("https://jsonplaceholder.typicode.com/todos/1")
      .GET();

    console.info(response);
  } catch (error) {
    console.error(error.message);
  }
};

makeRequest();

In the Promise way:

const AxiosEasyfier = require("axios-easyfier");
const easyfier = new AxiosEasyfier();

const makeRequest = () => {
  easyfier
    .withUrl("https://jsonplaceholder.typicode.com/todos/1")
    .GET()
    .then((res) => console.log(res))
    .catch((err) => console.error(err));
};

makeRequest();

Common Methods

AxiosEasyfier use Object Build Pattern to create a request object with common attributes to send a http request using axios lib. It's support common https verbs (GET, POST, PUT, PATCH and DELETE) and provide build functions to customize the request parameters.

For example, to make a GET request with custom headers:

const AxiosEasyfier = require("axios-easyfier");
const easyfier = new AxiosEasyfier();

const makeRequest = async () => {
  try {
    const response = await easyfier
      .withUrl("https://jsonplaceholder.typicode.com/todos/1")
      .withHeaders({ "X-App-Custom-Header": "Header Value" })
      .GET();

    console.info(response);
  } catch (error) {
    console.error(error.message);
  }
};

makeRequest();

To send more than 1 header param, just call withHeaders with new key -> value object:

const response = await easyfier
  .withUrl("https://jsonplaceholder.typicode.com/todos/1")
  .withHeaders({ "X-App-Custom-Header": "Header Value" })
  .withHeaders({ Accept: "*/*" })
  .GET();

Or just send an array (with some refactoring...):

const headers = [{ "X-App-Custom-Header": "Header Value" }, { Accept: "*/*" }];
const url = "https://jsonplaceholder.typicode.com/todos/1";

const response = await easyfier.withUrl(url).withHeaders(headers).GET();

To send data we need to call .POST() instead, with additional configuration to send a body in request:

const headers = [{ "Content-type": "application/json" }, { Accept: "*/*" }];
const url = "https://jsonplaceholder.typicode.com/todos";
const body = {
  id: 1,
  description: "Some description",
  another: "Another date here",
};

const response = await easyfier
  .withUrl(url)
  .withHeaders(headers)
  .withBody(body)
  .POST();

If the server requires a Basic ou Bearer authentication, add respective easyfier method to do this:

// ...omitted code
const secretToken = "SECRETTOKEN1234556SECRETTOKEN1234556";

const response = await easyfier
  .withUrl(url)
  .withHeaders(headers)
  .withBody(body)
  .withBasicAuthorization(secretToken) // Basic
  .POST();

// or with Bearer

const response = await easyfier
  .withUrl(url)
  .withHeaders(headers)
  .withBody(body)
  .withBearerAuthorization(secretToken) // Bearer
  .POST();

// ...omitted code

Configuring timeout:

.withTimeoutInSecs(10) // add timeout param with 10 seconds

.withTimeoutInMils(1000) // add timeout param with 1000 miliseconds

Sending request with application/x-www-form-urlencoded format

.withUrlEncondedParams({"param" : "value"})

With a list of parameters:

// ...omitted code
const params = [{ param1: "value1" }, { param2: "value2" }]

  // ...omitted code
  .withUrlEncondedParams(params);

Under the hood, AxiosEasyfier use qs library to configure and parse params.

 

Handle HTTP errors by yourself

By default, axios raise an exception when request not returns success status code (out of range >= 200 and <= 300). In some cases, it's necessary provide a specific error message or redirect process flow, depending on http status. To change that behavior, you can configure withCustomErrorHandling() with a specific satus code to delegates response handling to your function:

const makeRequest = async () => {
  try {
    const url = "https://jsonplaceholder.typicode.com/todos/wrong/1";

    const response = await easyfier
      .withUrl(url)
      .withCustomErrorHandling(404)
      .GET();

    if (response.status === 404) {
      console.info("Nothing to do...");
      return null;
    } else {
      return response.data;
    }

    console.info(response);
  } catch (error) {
    console.error(error.message);
  }
};

IMPORTANT: When withCustomErrorHandling method has configured, easyfier returns a complete axios reponse object:

{
  "config": {},
  "data": {},
  "headers": {},
  "status": 404,
  "status": "Not Found"
}

Addictionaly, you can configure an array of status to be handled:

//... omitted code
const statusToVerify = [400, 404, 405];

//... omitted code
      .withCustomErrorHandling(statusToVerify);
//... omitted code

Or, use without parameters to handle all request status manually;

//... omitted code
      .withCustomErrorHandling();
//... omitted code

 

REPOSITORY

https://github.com/eviccari/axios-easyfier

 

ENVOLVED TECHNOLOGIES

  • NodeJS 14
  • Javascript
  • Node Package Management
  • Axios
  • QS

 

MASTER DEPENDENCIES

  • Axios
  • qs

 

ROADMAP

  • Form request
  • Another complex configurations
  • Fetching data