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

@paylike/request

v3.1.0

Published

For a higher-level client see https://www.npmjs.com/package/@paylike/client.

Downloads

29

Readme

Paylike low-level request helper

For a higher-level client see https://www.npmjs.com/package/@paylike/client.

This is a low-level library used for making HTTP(s) requests to Paylike APIs. It incorporates the conventions described in the Paylike API reference.

This function is usually put behind a retry mechanism. Paylike APIs will expect any client to gracefully handle a rate limiting response and expects them to retry.

A retry mechanism is not included in this package because it is highly specific to the project and is difficult to implement for streaming requests without further context.

Example

// swap esm.sh for any "npm CJS to ESM CDN"
import request from 'https://esm.sh/@paylike/[email protected]'

const token = request('vault.paylike.io', {
  version: 1,
  data: {type: 'pcn', value: '1000 0000 0000 0000'.replaceAll(' ', '')},
}).first()

request

This package's default export is a function:

request(
  endpoint, // String, required
  {
    log: () => {},
    fetch: globalThis.fetch, // required in older Node.js
    timeout: 10000, // 0 = disabled

    version: String, // required
    query: Object,
    data: Object,

    // mostly relevant during testing
    clock: {
      setTimeout,
      clearTimeout,
    },
  }
})

request returns a pull-stream source (a function):

pull(request(/* ... */), collect(console.log))

For most cases, the below shortcut functions can be used:

request(/* ... */).first().then(console.log, console.error)
request(/* ... */).toArray().then(console.log, console.error)
request(/* ... */).forEach(console.log).catch(console.error)

Error handling

request may throw any of the following error classes as well as any error thrown by the fetch implementation by rejecting the promise returned by a shortcut function or by the error mechanism of a pull-stream.

All error classes can be accessed as request.<error class>, for instance request.RateLimitError.

Example

retry(
  () => request(/* ... */).first(),
  (err, attempts) => {
    if (attempts > 5 || !(err instanceof request.RateLimitError)) return false
    return err.retryAfter || 2 ** attempts * 5000
  }
).then(console.log, console.error)

function retry(fn, should, attempts = 0) {
  return fn().catch((err) => {
    const retryAfter = should(err, attempts)
    if (retryAfter === false) throw err

    return new Promise((resolve) =>
      setTimeout(() => resolve(retry(fn, attempts + 1)), retryAfter)
    )
  })
}

Error classes

  • RateLimitError

    May have a retryAfter (milliseconds) property if sent by the server specifying the minimum delay.

  • TimeoutError

    Has a timeout (milliseconds) property specifying the time waited.

  • ServerError

    Has status and headers properties copied from the fetch response.

  • ResponseError

    These errors correspond to status codes from the API reference. They have at least a code and message property, but may also have other useful properties relevant to the specific error code, such as a minimum and maximum for amounts.

Custom fetch (e.g. Node.js v16 and older)

It is built to work in any JavaScript environment (Node.js, browser) by accepting a Fetch implementation as input. Minor tweaks are implemented to support the node-fetch implementation even though it is not entirely compatible.