@livup/buffered-requests
v1.0.2
Published
Store HTTP requests in buffers and send them periodically
Downloads
3
Keywords
Readme
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 databasemongoOptions [String]
: Mongo connection optionsverbose [Boolean]
: Show logsprocessInterval [Number]
: Time between requestsmaxRequests [Number]
: Max number of simultaneous requestsmaxAttempts [Number]
: Max attempts to repeat a request after an erroreventEmitter [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 endpointmethod [String] (required)
: GET, POST, PUT or DELETEbody [Object]
: Request dataheaders [Object]
: Request headersjson [Boolean]
: Sets body and response to JSON and add Content-Type: application/json headerretryOnError [Boolean]
: Keep retrying the request on error until reaching 'maxAttempts' from config.successEvent [String]
: Success event nameerrorEvent [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 idstatus [String] (required)
: queued, success or failedattempt [Object]
: Request attempt info, with timestamp [Date], response [String] and status [Number]
getWaiting()
Returns all queued requests
getProcessing()
Returns all processing requests