Skip to main content
Deno 2 is finally here πŸŽ‰οΈ
Learn more

fetchify

deno.land/x/luminous popularity npm version npm downloads npm license

This package is designed to make the process of interacting with various APIs that have strict limitations more convenient and careful. For example, this could be APIs like Notion or Telegram, which have stringent limits.

πŸ‘‹ πŸ‘‹ ATTENTION!

This package is under development and will be frequently updated. The author would appreciate any help, advice, and pull requests! Thank you for your understanding 😊

Import

Deno:

From deno.land/x:

import fetchify from "https://deno.land/x/fetchify@0.2.7/mod.ts";

Or esm.sh:

import fetchify from "https://esm.sh/gh/sevapp/fetchify@0.2.7/mod.ts";

Node.JS:

Install from npm:

npm i --save @sevapp/fetchify

And import:

import fetchify from "fetchify";

Usage:

The first thing available to you is the fetchify function

const json = await (await fetchify("https://catfact.ninja/fact")).json();
console.log(json);

This function has a similar interface to the classic fetch but extends it with additional options, for example:

const json = await (await fetchify(
  "https://catfact.ninja/fact",
  {
    timeout: 1000, // Now, the waiting for a response will be interrupted after 1000 ms.
  },
)).json();

console.log(json);

But you can also create an instance with a set base URL and rate-limiting constraints:

const jph = fetchify.create({
  limiter: {
    // Number of requests per second
    rps: 3,
    // You can handle the occurrence of a 429 error
    // and return the time in ms that the request loop should delay
    "429": (response) => 1000,
  },
  baseURL: "https://jsonplaceholder.typicode.com",
  headers: {
    "hello": "world",
  },
});

for (let i = 30; i--;) {
  console.log(`send ${i}`);
  // All basic methods supported: get post put delete head patch
  jph.get(`/posts/${i}`).then((data) => console.log(`${i} ${data.status}`))
    .catch((err) => console.log(`${i} ${err}`))
    .finally(() => {
    });
}

Yes, all methods comply with the fetch interface but also extend it with additional options, for example:

await jph.get(`/posts/10`, {
  // Number of attempts
  attempts: 10
  // Time after which we stop waiting for a response
  timeout: 1000
});

If you need to make a request to the configured baseURL but not through the request queue, you can add the flag:

await jph.get(`/posts/10`, { unlimited: true });

LICENCE

LGPL-2.1