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

config-req

v2.1.0

Published

Axios wrapper based on a config file

Downloads

2,819

Readme

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