Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

config-req

Package Overview
Dependencies
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

config-req

Axios wrapper based on a config file

  • 2.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
292
increased by170.37%
Maintainers
2
Weekly downloads
 
Created
Source

config-req

npm CircleCI Known Vulnerabilities Greenkeeper badge

Axios wrapper based on a config file

How it works

This module allows to set up a programmatic HTTP client based on axios. To set it up, it just the url and the HTTP method per environment to be setup ¡et voilà!, a new and shiny HTTP client is ready to be used.

Axios options can be found here;

Install package

npm install config-req

Basic Example

const request = require('config-req');

// Your env configuration
const config = {
  activateAccount: {
    url: 'http://localhost:5000/v1/account/activate',
    method: 'post',
  },
};

const api = request(config);

console.log(api); // returns an object like this { activateAccount: <Promise> }

// Api instance contains a request with the method and URL already configured
api.activateAccount()
  .then(response => {
    console.log(response); // Axios response
  });

Nesting Configuration Example

const request = require('config-req');

// Your env configuration with nested objects
const nestedOptions = {
  registration: {
    activateAccount: {
      url: 'http://localhost:5000/v1/account/activate',
      method: 'post',
    },
  },
};

const api = request(nestedOptions);

console.log(api); // returns an object like this { registration: { activateAccount: <Promise> } }

// Api instance contains a request with the method and URL already configured
api.registration.activateAccount()
  .then(response => {
    console.log(response); // Axios response
  });

Customizer request parameters

const request = require('config-req');

const options = {
  advanced: {
    withBodyInfo: {
      url: 'http://localhost:5000/v1/account/activate',
      method: 'post',
    },
    withURLParams: {
      url: 'http://localhost:5000/v1/account/:id/activate',
      method: 'get',
    },
    withBasicAuth: { // This will affect each call to this endpoint
      url: 'http://localhost:5000/v1/account/:id/activate',
      method: 'get',
      auth: { password: 'pwd', username: 'nickname' },
    }
  },
};

const api = request(options);

api.advanced.withBodyInfo({
  body: { example: 'example' }, // this is how to send body params
  query: { example: 'example' }, // this is how to send query params
  headers: { Authorization: 'Bearer example' }, // this is how to send header params
})
  .then(response => {
    console.log(response); // Axios response
  });

// This is when we want to handle dynamic url params like this
// http://localhost:5000/v1/account/:id/activate
// To change that ID we need to setup the urlParams
api.advanced.withURLParams({
  body: { example: 'example' }, // this is how to send body params
  query: { example: 'example' }, // this is how to send query params
  headers: { Authorization: 'Bearer example' }, // this is how to send header params
  params: { id: 'urlParam' },
})
  .then(response => {
    console.log(response); // Axios response
  });

// Basic auth
api.advanced.withURLParams({
  body: { example: 'example' }, // this is how to send body params
  query: { example: 'example' }, // this is how to send query params
  headers: { Authorization: 'Bearer example' }, // this is how to send header params
  params: { id: 'urlParam' }, // This is how to send path params
  auth: { password: 'pwd', username: 'nickname' }, // this is how you add basic auth for each request
})
  .then(response => {
    console.log(response); // Axios response
  });

Intercepting requests

In some scenarios, it might be needed to have a fine-grained control on request or the responses. For example, to refresh a token when it is expired or to handle errors in a specific way. This can be achieved by using the interceptors option provided by Axios. These interceptors can be set-up in the following way:

const request = require('config-req');

// Your env configuration
const config = {
  activateAccount: {
    url: 'http://localhost:5000/v1/account/activate',
    method: 'post',
  },

};

const api = request(config, {
  interceptors: {
    request: (config) => {
      // Do something before request is sent
      return config;
    },
    response: {
      success: (response) => {
        // Do something with response data
        return response;
      },
      error: (error) => {
        // Do something with response error
        return Promise.reject(error);
      }
    }
  }
});

How to contribute

To contribute you must send a PR. This is how you can run the project as a developer.

Run as contributor

Install dependencies

npm install

Execute tests

npm run test

Keywords

FAQs

Package last updated on 21 Aug 2023

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc