import jwt from 'jsonwebtoken';
import request from 'request-promise-native';

export default async function makeRequest(
  requestAttributes,
  { apiKey, apiSecret, maxTries = 0 },
  tryNumber = 1,
) {
  const signed = jwt.sign({ key: apiKey }, apiSecret, { header: { kid: apiKey } });
  try {
    const result = await request(
      Object.assign(
        {
          gzip: true,
          auth: {
            bearer: signed,
          },
        },
        requestAttributes,
      ),
    );
    return result;
  } catch (e) {
    if (tryNumber >= maxTries) {
      throw e;
    }

    const { method, url } = requestAttributes;
    console.warn(`Failed ${method} ${url}. Retrying after 2s...`);

    await new Promise((resolve) => setTimeout(resolve, 2000));
    return makeRequest(
      requestAttributes,
      {
        apiKey,
        apiSecret,
        maxTries,
      },
      tryNumber + 1,
    );
  }
}

@@ -72,16 +72,117 @@
 tive';%0A%0A
+function randomIntInRange(min, max) %7B%0A  return Math.floor(Math.random() * (max - min + 1) + min);%0A%7D%0A%0A
 export d
@@ -725,16 +725,214 @@
 ributes;
+%0A%0A    // Exponential backoff with full jitter.%0A    // https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/%0A    const nextRetryIn = randomIntInRange(1000, 1000 * 2 ** tryNumber);
 %0A    con
@@ -982,17 +982,50 @@
 g after 
-2
+$%7B(nextRetryIn / 1000).toFixed(1)%7D
 s...%60);%0A
@@ -1084,12 +1084,19 @@
 ve, 
-2000
+nextRetryIn
 ));%0A

Add exponential backoff with full jitter to makeRequest