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

@loxjs/request

v2.0.6

Published

A module that provides a wrapper around the axios library.

Downloads

5

Readme

@loxjs/request

@loxjs/request is a Node.js module that provides a wrapper around the axios HTTP client to facilitate custom request and response interceptors, error handling, and automatic retry of certain network errors.

Installation

Install the module with npm:

npm install @loxjs/request

Or with yarn:

yarn add @loxjs/request

Usage

To use @loxjs/request, you need to create an HTTP client instance with optional configuration and interceptors.

const createHttpClient = require('@loxjs/request');

const httpClient = createHttpClient({
  baseURL: 'https://api.example.com', // Your API base URL
  timeout: 10000, // Request timeout in milliseconds
}, {
  requestInterceptor: (config) => {
    // Modify or augment the request configuration here
    // For example, adding an authorization token:
    config.headers.Authorization = `Bearer your_token_here`;
    return config;
  },
  responseInterceptor: (response) => {
    // Handle or transform the response data here
    // For example, unwrapping data:
    return response.data;
  },
  errorInterceptor: (error) => {
    // Handle errors or retry logic here
    // For example, logging the error:
    console.error(error.message);
    return Promise.reject(error);
  }
});

// Now you can use the httpClient to make requests
httpClient.get('/users')
  .then(users => {
    console.log(users);
  })
  .catch(error => {
    console.error('An error occurred while fetching users', error);
  });

Error Handling

The module enhances error objects with additional properties and a retryable flag for certain network errors that are considered retryable.

// Error handling example
httpClient.get('/non-existent-endpoint')
  .catch(error => {
    if (error.retryable) {
      // Implement retry logic
      console.log('This error is retryable, consider implementing retry logic here.');
    }
    console.error('HTTP Request failed:', error.message);
  });

Custom Interceptors

You can add custom interceptors for requests, responses, and errors. Below is an example showing how to add a request interceptor.

// Custom request interceptor example
httpClient.interceptors.request.use((config) => {
  // Add custom logic before request is sent
  console.log('Request sent with config:', config);
  return config;
});

And here is how you might add a response interceptor.

// Custom response interceptor example
httpClient.interceptors.response.use((response) => {
  // Any status code within the range of 2xx causes this function to trigger
  console.log('Response received:', response);
  return response;
}, (error) => {
  // Any status codes outside the range of 2xx cause this function to trigger
  console.error('Response error:', error);
  return Promise.reject(error);
});

Contributing

Contributions to @loxjs/request are welcome! Please ensure that your contributions adhere to the following guidelines:

  • Write clear, readable, and maintainable code.
  • Follow existing coding styles and practices.
  • Write meaningful commit messages.
  • Update the documentation accordingly.

For more detailed information, please read the contributing guide.

Enjoy using @loxjs/request!