UNPKG

pink-bears

Version:

Intelligent rate limiting middleware with MongoDB integration and caching for Node.js applications

212 lines (199 loc) 9.73 kB
const {emitEvent} = require('./errorEmitter'); const {errorConstants} = require('./constants'); const axiosWrapper = require('./AxiosWrapper'); let Axios; class Schedule { constructor(domain, token, iframeUrl, productId, traceId, appVersion, userInstalledId, accountId, userId) { this.domain = domain; this.token = token; this.iframeUrl = iframeUrl; this.productId = productId; this.traceId = traceId; this.appVersion = appVersion; this.userInstalledId = userInstalledId; this.accountId = accountId; this.userId = userId; this.initializeAxiosWrapper(); } initializeAxiosWrapper = () => { const AxiosWrapper = new axiosWrapper(this.domain, this.userId); Axios = AxiosWrapper.getAxiosInstance(); } create = async ({ data = {}, name, schedule_at, cronExpression, repeat, type = "ONE_TIME" }) => { try { const request = { productId: this.productId, method: "POST", data, accountId: this.accountId, iframeUrl: this.iframeUrl, appVersion: this.appVersion, userInstalledId: this.userInstalledId, runAt: schedule_at, name, type, cronExpression, repeat } let response; if (this.traceId) { response = await Axios.post(`${this.domain}/api/sparrow-apps/schedule`, { ...request }, { headers: { "Content-type": "application/json; charset=UTF-8", "auth": this.token, "traceId": this.traceId } }); } else { response = await Axios.post(`${this.domain}/api/sparrow-apps/schedule`, { "request": request }, { headers: { "Content-type": "application/json; charset=UTF-8", "auth": this.token, } }); } console.log("RESPONSE FOR THE API CALL", response?.data) return { data: response?.data, status: response?.status }; } catch (error) { console.log("Error occured while making API call", error); await emitEvent(errorConstants.scheduler, error); return { data: error.response?.data || error, status: error.response?.status || error }; } } delete = async ({ name, type = "ONE_TIME" }) => { try { const request = { productId: this.productId, method: "DELETE", accountId: this.accountId, iframeUrl: this.iframeUrl, appVersion: this.appVersion, userInstalledId: this.userInstalledId, name, type } let response; if (this.traceId) { response = await Axios.post(`${this.domain}/api/sparrow-apps/schedule`, { ...request }, { headers: { "Content-type": "application/json; charset=UTF-8", "auth": this.token, "traceId": this.traceId } }); } else { response = await Axios.post(`${this.domain}/api/sparrow-apps/schedule`, { "request": request }, { headers: { "Content-type": "application/json; charset=UTF-8", "auth": this.token, } }); } console.log("RESPONSE FOR THE API CALL", response?.data) return { data: response?.data, status: response?.status }; } catch (error) { console.log("Error occured while making API call", error); await emitEvent(errorConstants.scheduler, error); return { data: error.response?.data || error, status: error.response?.status || error }; } } fetch = async ({ name, type = "ONE_TIME" }) => { try { const request = { productId: this.productId, method: "GET", accountId: this.accountId, iframeUrl: this.iframeUrl, appVersion: this.appVersion, userInstalledId: this.userInstalledId, name, type } let response; if (this.traceId) { response = await Axios.post(`${this.domain}/api/sparrow-apps/schedule`, { ...request }, { headers: { "Content-type": "application/json; charset=UTF-8", "auth": this.token, "traceId": this.traceId } }); } else { response = await Axios.post(`${this.domain}/api/sparrow-apps/schedule`, { "request": request }, { headers: { "Content-type": "application/json; charset=UTF-8", "auth": this.token, } }); } console.log("RESPONSE FOR THE API CALL", response?.data) return { data: response?.data, status: response?.status }; } catch (error) { console.log("Error occured while making API call", error); await emitEvent(errorConstants.scheduler, error); return { data: error.response?.data || error, status: error.response?.status || error }; } } put = async ({ data = {}, name, schedule_at, cronExpression, repeat, type = "ONE_TIME" }) => { try { const request = { productId: this.productId, method: "PUT", data, accountId: this.accountId, iframeUrl: this.iframeUrl, appVersion: this.appVersion, userInstalledId: this.userInstalledId, runAt: schedule_at, name, type, cronExpression, repeat } let response; if (this.traceId) { response = await Axios.post(`${this.domain}/api/sparrow-apps/schedule`, { ...request }, { headers: { "Content-type": "application/json; charset=UTF-8", "auth": this.token, "traceId": this.traceId } }); } else { response = await Axios.post(`${this.domain}/api/sparrow-apps/schedule`, { "request": request }, { headers: { "Content-type": "application/json; charset=UTF-8", "auth": this.token, } }); } console.log("RESPONSE FOR THE API CALL", response?.data) return { data: response?.data, status: response?.status }; } catch (error) { console.log("Error occured while making API call", error); await emitEvent(errorConstants.scheduler, error); return { data: error.response?.data || error, status: error.response?.status || error }; } } pause = async ({ name, type = "ONE_TIME" }) => { try { const request = { productId: this.productId, method: "PAUSE", accountId: this.accountId, iframeUrl: this.iframeUrl, appVersion: this.appVersion, userInstalledId: this.userInstalledId, name, type } let response; if (this.traceId) { response = await Axios.post(`${this.domain}/api/sparrow-apps/schedule`, { ...request }, { headers: { "Content-type": "application/json; charset=UTF-8", "auth": this.token, "traceId": this.traceId } }); } else { response = await Axios.post(`${this.domain}/api/sparrow-apps/schedule`, { "request": request }, { headers: { "Content-type": "application/json; charset=UTF-8", "auth": this.token, } }); } console.log("RESPONSE FOR THE API CALL", response?.data) return { data: response?.data, status: response?.status }; } catch (error) { console.log("Error occured while making API call", error); await emitEvent(errorConstants.scheduler, error); return { data: error.response?.data || error, status: error.response?.status || error }; } } resume = async ({ name, type = "ONE_TIME" }) => { try { const request = { productId: this.productId, method: "RESUME", accountId: this.accountId, iframeUrl: this.iframeUrl, appVersion: this.appVersion, userInstalledId: this.userInstalledId, name, type } let response; if (this.traceId) { response = await Axios.post(`${this.domain}/api/sparrow-apps/schedule`, { ...request }, { headers: { "Content-type": "application/json; charset=UTF-8", "auth": this.token, "traceId": this.traceId } }); } else { response = await Axios.post(`${this.domain}/api/sparrow-apps/schedule`, { "request": request }, { headers: { "Content-type": "application/json; charset=UTF-8", "auth": this.token, } }); } console.log("RESPONSE FOR THE API CALL", response?.data) return { data: response?.data, status: response?.status }; } catch (error) { console.log("Error occured while making API call", error); await emitEvent(errorConstants.scheduler, error); return { data: error.response?.data || error, status: error.response?.status || error }; } } } module.exports = Schedule;