# advancedrequest

> ES6 JS class for intervaled requests with retries and timeouts

Latest version **2.1.0** (published 2024-12-30) · 0 weekly downloads

## Install

```sh
npm install advancedrequest
pnpm add advancedrequest
yarn add advancedrequest
bun add advancedrequest
```

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2024-12-30 |
| First published | 2017-05-18 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 16.7 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | falconscript |
| Keywords | advancedrequest, advanced request, http, request, interval, interval request, api, developer api, rate, limit, rate limit, rate limiting |

## Links

- npm: https://www.npmjs.com/package/advancedrequest
- Repository: https://github.com/falconscript/advancedrequest
- Homepage: https://github.com/falconscript/advancedrequest#readme
- Issues: https://github.com/falconscript/advancedrequest/issues
- npm.io page: https://npm.io/package/advancedrequest

## Dependencies (1)

- [request](https://npm.io/package/request.md) ^2.88.2

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 2.1.0 (latest) — 2024-12-30
- 2.0.1 — 2024-12-28
- 2.0.0 — 2024-12-28
- 1.0.15 — 2020-08-07
- 1.0.14 — 2018-11-13
- 1.0.13 — 2018-10-25
- 1.0.12 — 2018-02-07
- 1.0.11 — 2017-10-12
- 1.0.10 — 2017-07-26
- 1.0.9 — 2017-07-20
- 1.0.8 — 2017-07-20
- 1.0.7 — 2017-06-18
- 1.0.6 — 2017-05-22
- 1.0.3 — 2017-05-18

## README

# advancedrequest

> ES6 JS class for intervaled requests with retries and timeouts

## Installation

```sh
npm install advancedrequest --save
```

## Usage

Manager for sending HTTP requests with multiple tries, wait intervals, and timeouts  
Uses node module 'request'  

This can be configured by adding names as keys and values with requiredInterval  
in order to WAIT before sending another request of that name. If you wanted to  
only send one request with name DevAPI_SendFriendRequest per minute, set the  
requiredInterval to 1000*60 and the minute will automatically be enforced  

All 'lastruns' start with the moment of script start  
to make sure script restarts don't accidentally disobey the interval  


## Simple GET request example
```js
let advancedrequest = require('advancedrequest');

let requestData = await new advancedrequest.AdvancedRequest({
  url: "http://api.somefriendsite.com/addFriend"
}).runAsync();

console.log("[+] API response received!", requestData);
```

## Inheritance example

Here you can define our own logic to determine if a request was successful  
or needs to be retried by subclassing advancedrequest.AdvancedRequest  
Calling this.fail(timeoutInMs, messageToLog); will retry the request up to  
10 times. This max can be passed in as an integer argument named maxRetries.  
Call this.onFinish(this.data); in order to complete the request
```js
class DevApiRequest extends advancedrequest.AdvancedRequest {
  constructor (args) {
    super(args); // always call superclass constructor!
  }
  // Override the postProcess function to provide your own checks
  postProcess () {
    // this.data is the body of the request.
    if (!this.data) {
      // If the response was blank, call this.fail to retry the same request in 10 seconds
      this.fail(10, "Request was blank! Is connection to internet broken?");
    } else if (this.data.indexOf('Too many requests in the last hour!') != -1) {
      // If the API has a limit and tells you about it, you can call this.fail and retry in 1 hour
      this.fail(60*60, "Hit rate limit. Waiting 1 hour to retry");
    } else {
      // All was clear! MAKE SURE YOU HAVE this line at the end of your request!
      this.onFinish(this.data);
    }
  }
};


// Then once you've written your DevApiRequest class, maybe you'd call it so:
function sendFriendRequestTo(targetId, callback) {
  let sendFriendRequestReq = new DevApiRequest({
      url: "http://api.somefriendsite.com/addFriend",
      method: "POST",
      name: "sendFriendRequest", // NOTE: used as identifier for interval
      postData: { targetId: targetId, auth_token: "24t4534token42i5h2"},
      callback: callback
  });

  sendFriendRequestReq.runAsync().then(callback);
}

sendFriendRequestTo(252452, function(json) {
  console.log("[+] API response received!", json);
});
```

## Intervaled request example

Enforce a defined interval between requests with the same 'name' passed into them.
Using the DevApiRequest class, we first set lastRunHash with the proper interval
```js
// Set interval timeouts for the requests of a particular name
advancedrequest.setLastRunHash({
  'sendFriendRequest': { requiredInterval: 1000*15 }, // 15 seconds
  'removeFriend':      { requiredInterval: 1000*5, lastReqTime: new Date().getTime() }
});

sendFriendRequestTo(34, function(json) {
  // 15 seconds will be waited before next request is sent out
  sendFriendRequestTo(35, function(json) {
    // 15 seconds will be waited before next request is sent out
    sendFriendRequestTo(36, function(json) {
      console.log("[+] 3 friend requests sent successfully with 15 seconds between them");
    });
  });
});
```

## Credits
http://x64projects.tk/

---
_Source: https://npm.io/package/advancedrequest · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
