UNPKG

radius-read

Version:

Realtime Dashboard

102 lines 3.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CallbackService = void 0; const tslib_1 = require("tslib"); const request_1 = tslib_1.__importDefault(require("request")); const result_model_1 = require("../models/result.model"); /** * CallbackService is a service class that handles sending callback events to a specified URL. */ class CallbackService { /** * Creates an instance of CallbackService. */ constructor() { } /** * Registers a callback URL to the service. * @param callbackURL */ static register(callbackURL) { if (callbackURL && !this._callbackURLs.includes(callbackURL)) { this._callbackURLs.push(callbackURL); } } /** * Sends callback events to all registered URLs with the provided payload. * @param payload */ static sendCallbackEvents(payload) { var _a; (_a = this._callbackURLs) === null || _a === void 0 ? void 0 : _a.forEach((callbackURL) => { this.sendCallbackEvent(callbackURL, payload); }); } /** * Sends callback events to a specified URL with the provided payload. * @param callbackURL * @param payload * @returns */ static sendCallbackEvent(callbackURL, payload) { return new Promise((resolve) => { var result = new result_model_1.EntityResult(); let options = { url: callbackURL, headers: this._defaultHeaders, strictSSL: false, json: payload }; request_1.default.post(options, (error, response, body) => { if (error) { result.ResultType = result_model_1.EntityResultType.Failed; result.Exception = error; resolve(result); } if (response) { if (typeof (response === null || response === void 0 ? void 0 : response.body) === 'string') response = JSON.parse(response === null || response === void 0 ? void 0 : response.body); else response = response === null || response === void 0 ? void 0 : response.body; if (response.RespType === "Failed" || response.EvType === "Failed") { result.ResultType = result_model_1.EntityResultType.Failed; result.Exception = response; } else { result.ResultType = result_model_1.EntityResultType.Success; result.Response = response; } resolve(result); } }); }); } /** * Gets the registered callback URLs. * @returns */ static getCallbackURLs() { return this._callbackURLs; } } exports.CallbackService = CallbackService; /** * Default headers for API requests. * @date Jun 26, 2025 01:26:08 PM * @author Biswaranjan Nayak * * @private * @type {*} */ CallbackService._defaultHeaders = { 'Content-Type': 'application/json' }; /** * Callback URLs to which events can be sent. * @date Jun 26, 2025 01:27:37 PM * @author Biswaranjan Nayak * * @private * @type {string[]} */ CallbackService._callbackURLs = []; //# sourceMappingURL=callback.service.js.map