@livup/buffered-requests

1.0.2 • Public • Published

Buffered Requests

Instalation

npm install @livup/buffered-requests

Setup

const bufferedRequests = require('@livup/buffered-requests');
await bufferedRequests.initialize({
	mongoAddress: 'mongodb://localhost/my_db',
})

Methods

async initialize({ mongoAddress, mongoOptions, processInterval, maxRequests, maxAttempts })

Initializes the module.

  • mongoAddress [String] (required): Address of mongo database
  • mongoOptions [String]: Mongo connection options
  • verbose [Boolean]: Show logs
  • processInterval [Number]: Time between requests
  • maxRequests [Number]: Max number of simultaneous requests
  • maxAttempts [Number]: Max attempts to repeat a request after an error
  • eventEmitter [EventEmitter]: Event that listens to requests success/failure

Example:

const bufferedRequests = require('@livup/buffered-requests');
await bufferedRequest.initialize({
	mongoAddress: 'mongodb://localhost/my_db',
	verbose: true,
	processInterval: 1000
})

async addRequest({ url, method, body, headers, json, retryOnError, successEvent, errorEvent })

Add a request to the buffer

  • url [String] (required): Request endpoint
  • method [String] (required): GET, POST, PUT or DELETE
  • body [Object]: Request data
  • headers [Object]: Request headers
  • json [Boolean]: Sets body and response to JSON and add Content-Type: application/json header
  • retryOnError [Boolean]: Keep retrying the request on error until reaching 'maxAttempts' from config.
  • successEvent [String]: Success event name
  • errorEvent [String]: Error event name

Example:

const bufferedRequests = require('@livup/buffered-requests');
bufferedRequest.addRequest({
	url: 'http://www.example.com',
	method: 'POST',
	body: {
		something: 'some data'
	},
	headers: {
		'Content-Type': 'text/plain'
	},
	successEvent: 'success_event'
})

async updateRequest({ _id, status, attempt })

Update a specific request

  • _id [ObjectId] (required): Request id
  • status [String] (required): queued, success or failed
  • attempt [Object]: Request attempt info, with timestamp [Date], response [String] and status [Number]

getWaiting()

Returns all queued requests

getProcessing()

Returns all processing requests

Dependencies (4)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i @livup/buffered-requests

    Weekly Downloads

    0

    Version

    1.0.2

    License

    ISC

    Unpacked Size

    10.7 kB

    Total Files

    7

    Last publish

    Collaborators

    • livupservices
    • rafaelcorreiapoli
    • mayerlevy
    • andreebr
    • martinand
    • caiolivup