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

htpx

v1.2.1

Published

HTPX is a lightweight, flexible HTTP client for JavaScript and Node.js that supports both `XMLHttpRequest` and `Fetch` in the browser and `http`/`https` in Node.js. It is designed to be modular, promise-based, and fully equipped with modern features like

Downloads

5,907

Readme

HTPX - HTTP Client

Version - 1.2.1

HTPX is a lightweight, flexible HTTP client for JavaScript and Node.js that supports both XMLHttpRequest and Fetch in the browser and http/https in Node.js. It is designed to be modular, promise-based, and fully equipped with modern features like request cancellation, interceptors, and progress tracking for uploads and downloads.

Features

  • Browser and Node.js support:
    • Uses XMLHttpRequest or Fetch in the browser
    • Uses http/https in Node.js
  • Promise-based API: Simple and clean syntax using promises.
  • Request and Response interceptors: Modify requests/responses globally before sending or after receiving them.
  • Timeout support: Automatically abort requests that take too long.
  • Request cancellation: Cancel in-flight requests using CancelToken.
  • Custom headers: Easily add or modify headers.
  • Progress tracking: Monitor upload and download progress.
  • XSRF protection: Automatically add XSRF tokens from cookies to headers.
  • Proxy support: Handle HTTP proxies in Node.js.
  • Encryption support for params and data:
    • Encrypt query string parameters and send them via the q key.
    • Encrypt the entire request payload and send it under the data key.
    • Optional custom encryption functions for both query parameters and request data, with Base64 encoding as the default.
  • more coming soon...

Tutorials

Installation

Install HTPX using npm:

npm install htpx

If you're using TypeScript, the types are already included in the package, so there's no need to install additional type definitions.

Basic Usage

HTPX supports the standard HTTP methods, including GET, POST, PUT, PATCH, and DELETE. Here's how you can make simple HTTP requests:

GET Request

import htpx from 'htpx';

htpx.get('https://jsonplaceholder.typicode.com/posts/1')
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error(error);
  });

GET Request with Query String Encryption

You can also enable encryption for query parameters by using the encryptParams option. The entire query string will be encrypted and sent as a q key.

htpx.get('https://jsonplaceholder.typicode.com/posts', {
  params: { search: 'foo', page: 1 },
  encryptParams: true,  // Enable encryption for query string
  makeParamsEncryption: (params) => customEncryptFunction(params)  // Optional custom encryption function
})
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error(error);
  });

POST Request

htpx.post('https://jsonplaceholder.typicode.com/posts', {
  title: 'foo',
  body: 'bar',
  userId: 1
})
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error(error);
  });

POST Request with Data Encryption

You can enable encryption for the request body (data) by using the encryptData option. The payload will be encrypted and sent under the data key. You can do the same for PUT and PATCH requests.

htpx.post('https://jsonplaceholder.typicode.com/posts', {
  title: 'foo',
  body: 'bar',
  userId: 1
}, {
  encryptData: true,  // Enable encryption for the data
  makeDataEncryption: (data) => customEncryptFunction(data)  // Optional custom encryption function
})
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error(error);
  });

PUT Request

htpx.put('https://jsonplaceholder.typicode.com/posts/1', {
  title: 'updated title',
  body: 'updated body',
  userId: 1
})
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error(error);
  });

DELETE Request

htpx.delete('https://jsonplaceholder.typicode.com/posts/1')
  .then(response => {
    console.log('Deleted successfully');
  })
  .catch(error => {
    console.error(error);
  });

Request Configuration

You can pass a configuration object with additional settings like headers, timeouts, and custom parameters:

htpx.get('https://jsonplaceholder.typicode.com/posts', {
  headers: { 'Authorization': 'Bearer your_token' },
  timeout: 5000 // 5-second timeout
})
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error('Request timed out or failed:', error);
  });

Interceptors

Interceptors allow you to modify requests or responses globally before they are sent or after they are received.

Request Interceptor

htpx.addRequestInterceptor(config => {
  config.headers['Authorization'] = 'Bearer your_token';
  return config;
});

Response Interceptor

htpx.addResponseInterceptor(response => {
  if (response.status === 401) {
    // Handle unauthorized access
    console.log('Unauthorized, redirecting to login...');
  }
  return response;
});

Request Cancellation

HTPX supports request cancellation using CancelToken. This is useful when you need to abort a request that is no longer needed.

const { token, cancel } = htpx.CancelToken.source();

htpx.get('https://jsonplaceholder.typicode.com/posts', {
  cancelToken: token
})
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    if (htpx.isCancel(error)) {
      console.log('Request canceled:', error.message);
    } else {
      console.error('Error:', error);
    }
  });

// Cancel the request
cancel('Operation canceled by the user.');

Timeout

You can set a timeout for each request. If the request takes longer than the specified time, it will be aborted.

htpx.get('https://jsonplaceholder.typicode.com/posts', { timeout: 1000 })
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error('Request timed out or failed:', error);
  });

Progress Tracking

HTPX allows you to track the progress of uploads and downloads.

Download Progress

htpx.get('https://jsonplaceholder.typicode.com/posts', {
  onDownloadProgress: progressEvent => {
    const percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total);
    console.log(`Download progress: ${percentCompleted}%`);
  }
})
  .then(response => {
    console.log(response.data);
  });

Upload Progress

htpx.post('https://jsonplaceholder.typicode.com/posts', {
  title: 'foo',
  body: 'bar',
  userId: 1
}, {
  onUploadProgress: progressEvent => {
    const percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total);
    console.log(`Upload progress: ${percentCompleted}%`);
  }
})
  .then(response => {
    console.log(response.data);
  });

Proxy Support (Node.js)

HTPX supports proxies for Node.js requests. Here’s how you can set up a proxy:

htpx.get('https://example.com/api', {
  proxy: {
    protocol: 'https',
    host: '127.0.0.1',
    port: 9000,
    auth: {
      username: 'proxyUser',
      password: 'proxyPassword'
    }
  }
})
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error(error);
  });

XSRF Protection

HTPX automatically adds XSRF tokens from cookies to your requests. You can configure the cookie and header names:

htpx.post('https://example.com/api', { data: 'foo' }, {
  xsrfCookieName: 'MY-XSRF-COOKIE', // default is 'XSRF-TOKEN'
  xsrfHeaderName: 'MY-XSRF-HEADER' // default is 'X-XSRF-TOKEN'
});

TypeScript Support

HTPX includes built-in TypeScript support. The types are automatically recognized when you import HTPX in a TypeScript project. Here's an example of using HTPX with TypeScript:

TypeScript Example

import { request, get, post, RequestConfig, Response } from 'htpx';

const config: RequestConfig = {
  method: 'GET',
  url: 'https://example.com/api',
  timeout: 5000
};

get(config.url, config).then((response: Response) => {
  console.log(response.data);
});

Methods

  • request(url, config) – Core method for custom requests.
  • get(url, config) – Send a GET request.
  • post(url, data, config) – Send a POST request.
  • put(url, data, config) – Send a PUT request.
  • patch(url, data, config) – Send a PATCH request.
  • delete(url, config) – Send a DELETE request.
  • addRequestInterceptor(interceptor) – Add a global request interceptor.
  • addResponseInterceptor(interceptor) – Add a global response interceptor.
  • CancelToken – Use to cancel requests.

Running Tests

You can run the tests using Jest:

npm test

Issues

If you encounter any errors in this component or have suggestions for improvements, please feel free to open an issue. I’m eager to hear feedback from the community and would greatly appreciate your input!

Author Services

Interested in this library but missing certain features? Contact the author, and he can help implement them for you.

Author

Krishna Paul

Github