UNPKG

@mediarithmics/plugins-nodejs-sdk

Version:

This is the mediarithmics nodejs to help plugin developers bootstrapping their plugin without having to deal with most of the plugin boilerplate

52 lines 2.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BatchUpdateHandler = void 0; class BatchUpdateHandler { constructor(app, emptyBodyFilter, logger) { this.app = app; this.emptyBodyFilter = emptyBodyFilter; this.logger = logger; } registerRoute(onbatchUpdateFn) { this.app.post('/v1/batch_update', this.emptyBodyFilter, async (req, res) => { try { this.logger.debug(`POST /v1/batch_update ${JSON.stringify(req.body)}`); const request = req.body; const response = await onbatchUpdateFn(request); this.logger.debug(`Returning: ${JSON.stringify(response)}`); const pluginResponse = { status: response.status, sent_items_in_success: response.sent_items_in_success, sent_items_in_error: response.sent_items_in_error, }; if (response.next_msg_delay_in_ms) { res.set('x-mics-next-msg-delay', response.next_msg_delay_in_ms.toString()); } if (response.message) { pluginResponse.message = response.message; } let statusCode; switch (response.status) { case 'OK': statusCode = 200; break; case 'ERROR': statusCode = 400; break; case 'RETRY': statusCode = 503; break; default: statusCode = 500; } return res.status(statusCode).send(JSON.stringify(pluginResponse)); } catch (err) { this.logger.error(`Something bad happened : ${err.message} - ${err.stack ? err.stack : 'stack undefined'}`); return res.status(500).send({ status: 'error', message: `${err.message}` }); } }); } } exports.BatchUpdateHandler = BatchUpdateHandler; //# sourceMappingURL=BatchUpdateHandler.js.map