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

requestq

v0.9.0

Published

An intelligent queue for ajax requests in the browser.

Downloads

17

Readme

requestq

A tiny, intelligent queue for ajax requests in the browser:

  • Prioritize requests, even cancelling long-running low-priority requests when high-priority requests come in.
  • Throttle requests, executing only a set number at a time
  • Retry requests
  • Only 2KB (minified and gzipped)

Installation

npm install requestq --save

If you need to support most browsers, you will also need a Promise polyfill if you do not already have one:

npm install promise-polyfill --save-exact

Usage

import {RequestQueue} from 'requestq';

const requests = new RequestQueue()
requests.get('https://example.com/terms.txt').then((response) => {
  console.log('Got terms: ', response)  
}).catch(() => {
  console.error('Request failed')
})

Advanced Usage

import {RequestQueue, RequestPriority} from 'requestq';

const requests = new RequestQueue({
    retries: 3,
    concurrency: 5
})

let terms = requests.get('https://example.com/terms.txt), {
  priority: RequestPriority.LOW
}).then((response) => {
  console.log('Got terms: ' + response)  
}).catch(() => {
  console.error('Request failed after 3 retries')
})

let names = requests.get('https://example.com/names.json', {
    priority: RequestPriority.HIGHEST,
    responseType: 'json'
}).then((response) => {
  // Use the response object
}).catch(() => {
  console.error('Request failed after 3 retries')
})
await Promise.all([terms, names])

Documentation

new RequestQueue({options})

Makes a new RequestQueue. Options:

  • retries: number of times a GET request will be retried on errors. (Default: 3)
    • Only GET requests are retried, as retrying POST can create duplicate objects or unwanted effects.
  • concurrency: number of concurrent requests the queue can have in-flight (Default: 6)

RequestQueue.request(method, url, {options})

Puts request in the queue. Returns a promise that resolves when the request is done, or rejects if it fails.

  • method: the HTTP method for the request, e.g. "GET"
  • url: The URL for the request
  • options:
    • priority: Defines the order in which requests get sent. One of the following:
      • RequestQueue.LOW
      • RequestQueue.MEDIUM (default)
      • RequestQueue.HIGH
      • RequestQueue.HIGHEST
      HIGHEST is special: It actually aborts HIGH, MEDIUM or LOW requests to be tried again, if there are too many requests in flight)
    • body: The request body if applicable. Either a string or an object (automatically JSON.stringified)
    • maxRetries: The maximum number of retries that this request will use, overrides the main RequestQueue retries.
    • responseType: One of the following:
      • null (default, uses browser behavior)
      • json: Parses the response as JSON and returns an object. Sets Accept: application/json as a request header.
      • text: Returns the response as text
      • blob: Returns a Blob
      • arraybuffer: Returns an ArrayBuffer
      • image: Returns an Image
    • headers: Object of additional headers to set
    • auth: contents of the 'Authorization' header if supplied
    • withCredentials: Enable or disable sending credentials with cross-site XHR requests (default false)

RequestQueue.get(url, {options})

RequestQueue.post(url, {options})

RequestQueue.patch(url, {options})

RequestQueue.delete(url, {options})

RequestQueue.head(url, {options})

RequestQueue.options(url, {options})

Shorthands for RequestQueue.request("method" ...).

FAQ

Why are you not using the Fetch API?

  • The Fetch API does not support aborting a request, which is required for high priority requests to abort and requeue lower priority requests.

Why does this not support Node?

  • Keeping the library tiny. If you need a universal client, axios is really neat.

Does this library have Typescript declarations?

  • Yup :)

License

MIT