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

@wistle/http-provider

v1.0.0

Published

This package is to handle http provider to make rest api

Downloads

7

Readme

@wistle/http-provider

HttpRequestProvider

HttpRequestProvider is a flexible utility for making HTTP requests in Node.js using Axios. It supports advanced logging, handling PCI-sensitive information, configurable headers, and adding query parameters. It provides full control over request options and the ability to log requests and responses with different levels of verbosity.

Features

  • Supports GET, POST, PUT, DELETE, and other HTTP methods.
  • Handles logging for both sensitive (PCI) and non-sensitive data.
  • Adds custom headers like Content-Type or operation_id.
  • Supports query parameters handling.
  • Can resolve with the full response or just the data field.
  • Catches and logs errors properly.

Installation

npm install @wistle/http-provider

Usage

const { HttpRequest, HttpRequestProvider } = require('./dist');

// Mock logger to display log messages in the console
global['Logger'] = {
    verbose: console.log,
    safeVerbose: console.log,
    error: console.error,
    safeError: console.error,
    info: console.info,
};

(async () => {
  const operation_id = 'op123';

  try {
    // Example: POST request
    const postRequest = new HttpRequest({
      method: 'POST',
      url: 'https://jsonplaceholder.typicode.com/posts',
      data: { title: 'foo', body: 'bar', userId: 1 },
      headers: { 'Content-Type': 'application/json' },
      json: true,
    });

    console.log(`Making POST request to ${postRequest.url}`);
    const postResponse = await HttpRequestProvider.makeRequest(operation_id, postRequest);
    console.log('POST Response:', postResponse);

    // Example: GET request with query parameters
    const queryRequest = new HttpRequest({
      method: 'GET',
      url: 'https://jsonplaceholder.typicode.com/comments',
      query: { postId: 1 },
      json: true,
    });

    console.log(`Making GET request with query params to ${queryRequest.url}`);
    const queryResponse = await HttpRequestProvider.makeRequest(operation_id, queryRequest);
    console.log('GET with Query Params Response:', queryResponse);

  } catch (error) {
    console.error('Error occurred while making HTTP requests:', error);
  }
})();

API

HttpRequestProvider.makeRequest(operation_id, options)

  • operation_id: A unique ID to track requests for logging purposes.
  • options: An object with the following properties:
    • method: HTTP method (GET, POST, PUT, etc.).
    • url: The request URL.
    • data: The request body (for POST, PUT requests).
    • headers: Optional custom headers.
    • query: Object representing query parameters.
    • json: Boolean flag indicating if the Content-Type should be application/json.
    • is_pci: Boolean flag indicating if the request involves sensitive (PCI) data.
    • is_internal_request: Boolean flag to add internal headers.
    • resolve_with_full_response: Boolean flag to resolve the full Axios response or just the data field.

Example of HttpRequest

const getRequest = new HttpRequest({
  method: 'GET',
  url: 'https://jsonplaceholder.typicode.com/posts/1',
  json: true,
});

Logging

HttpRequestProvider uses a global Logger object for logging. You can mock this logger in development or testing, or replace it with your logging framework in production.

Logger Methods

  • Logger.verbose()
  • Logger.safeVerbose()
  • Logger.error()
  • Logger.safeError()
  • Logger.info()

Error Handling

If the request fails, an error is thrown with detailed logs, including the status code and response from the server. If no response is available, a generic error will be thrown.

try {
  const response = await HttpRequestProvider.makeRequest(operation_id, request);
} catch (error) {
  console.error('Request failed:', error);
}

License

This project is licensed under the MIT License - see the [LICENSE] file for details.