pink-bears
Version:
Intelligent rate limiting middleware with MongoDB integration and caching for Node.js applications
63 lines (55 loc) • 2.3 kB
JavaScript
const {emitEvent} = require('./errorEmitter');
const {errorConstants} = require('./constants');
const axiosWrapper = require('./AxiosWrapper');
let Axios;
class Webhook {
constructor(domain, token, productId, traceId, userInstalledId, accountId, userId){
this.domain = domain;
this.token = token;
this.productId = productId;
this.traceId = traceId;
this.userInstalledId = userInstalledId;
this.accountId = accountId;
this.userId = userId;
this.initializeAxiosWrapper();
}
initializeAxiosWrapper = () => {
const AxiosWrapper = new axiosWrapper(this.domain, this.userId);
Axios = AxiosWrapper.getAxiosInstance();
}
generate = async () => {
try {
const response = await Axios.get(`${this.domain}/api/generate-incoming-webhook-url?userInstalledId=${this.userInstalledId}`, {
headers: {
"Content-type": "application/json; charset=UTF-8",
"auth": this.token,
"traceId": this.traceId
}
});
console.log("RESPONSE FOR THE API CALL GENERATE WEBHOOK URL", response?.data)
return response?.data;
} catch (error) {
console.log(`Error occured while making API call for account Id ${this.accountId}`, error)
await emitEvent(errorConstants.fetch, error);
throw error;
}
}
revoke = async () => {
try {
const response = await Axios.delete(`${this.domain}/api/revoke-incoming-webhook-url?userInstalledId=${this.userInstalledId}`, {
headers: {
"Content-type": "application/json; charset=UTF-8",
"auth": this.token,
"traceId": this.traceId
}
});
console.log("RESPONSE FOR THE API CALL REVOKE WEBHOOK URL", response?.data)
return response?.data;
} catch (error) {
console.log(`Error occured while making API call for account Id ${this.accountId}`, error);
await emitEvent(errorConstants.fetch, error);
throw error;
}
}
}
module.exports = Webhook;