pink-bears
Version:
Intelligent rate limiting middleware with MongoDB integration and caching for Node.js applications
358 lines (341 loc) • 13.6 kB
JavaScript
const { emitEvent } = require('./errorEmitter');
const { errorConstants } = require('./constants');
const axiosWrapper = require('./AxiosWrapper');
let Axios;
class Redis {
constructor(accountId, productId, appId, domain, traceId, userInstalledId, token, userId) {
this.accountId = accountId;
this.token = token;
this.productId = productId;
this.appId = appId;
this.domain = domain;
this.userInstalledId = userInstalledId;
this.traceId = traceId;
this.headers = {
headers: {
traceId: traceId
}
};
this.userId = userId;
this.initializeAxiosWrapper();
}
initializeAxiosWrapper = () => {
const AxiosWrapper = new axiosWrapper(this.domain, this.userId);
Axios = AxiosWrapper.getAxiosInstance();
}
get = async (key) => {
console.log("🚀 ~ Redis ~ get= ~ key:", key);
try {
const data = {
accountId: this.accountId,
productId: this.productId,
appId: this.appId,
userInstalledId: this.userInstalledId,
method: 'GET',
key
};
console.log("🚀 ~ Redis ~ get= ~ data:", JSON.stringify(data));
return (await Axios.post(`${this.domain}/api/sparrow-apps/redis-db`, data, {
headers: {
"Content-type": "application/json; charset=UTF-8",
"auth": this.token,
"traceId": this.traceId
}
}))?.data;
} catch (error) {
console.log("🚀 ~ Redis ~ get= ~ error:", JSON.stringify(error));
await emitEvent(errorConstants.redis, error);
throw error;
}
}
set = async (key, value) => {
console.log("🚀 ~ Redis ~ set=async ~ value:", value);
console.log("🚀 ~ Redis ~ set=async ~ key:", key);
try {
const data = {
accountId: this.accountId,
productId: this.productId,
userInstalledId: this.userInstalledId,
appId: this.appId,
method: 'SET',
key,
value
};
console.log("🚀 ~ Redis ~ set=async ~ data:", JSON.stringify(data));
return (await Axios.post(`${this.domain}/api/sparrow-apps/redis-db`, data, {
headers: {
"Content-type": "application/json; charset=UTF-8",
"auth": this.token,
"traceId": this.traceId
}
}))?.data;
} catch (error) {
console.log("🚀 ~ Redis ~ set=async ~ error:", JSON.stringify(error));
await emitEvent(errorConstants.redis, error);
throw error;
}
}
del = async (key) => {
console.log("🚀 ~ Redis ~ del=async ~ key:", key);
try {
const data = {
accountId: this.accountId,
productId: this.productId,
userInstalledId: this.userInstalledId,
appId: this.appId,
method: 'DEL',
key
};
console.log("🚀 ~ Redis ~ del=async ~ data:", JSON.stringify(data));
return (await Axios.post(`${this.domain}/api/sparrow-apps/redis-db`, data, {
headers: {
"Content-type": "application/json; charset=UTF-8",
"auth": this.token,
"traceId": this.traceId
}
}))?.data;
} catch (error) {
console.log("🚀 ~ Redis ~ del=async ~ error:", JSON.stringify(error));
await emitEvent(errorConstants.redis, error);
throw error;
}
}
increment = async (key) => {
console.log("🚀 ~ Redis ~ INCR=async ~ key:", key);
try {
const data = {
accountId: this.accountId,
productId: this.productId,
userInstalledId: this.userInstalledId,
appId: this.appId,
method: 'INCR',
key
};
console.log("🚀 ~ Redis ~ INCR=async ~ data:", JSON.stringify(data));
return (await Axios.post(`${this.domain}/api/sparrow-apps/redis-db`, data, {
headers: {
"Content-type": "application/json; charset=UTF-8",
"auth": this.token,
"traceId": this.traceId
}
}))?.data;
} catch (error) {
console.log("🚀 ~ Redis ~ INCR=async ~ error:", JSON.stringify(error));
await emitEvent(errorConstants.redis, error);
throw error;
}
}
incrementBy = async (key, value) => {
console.log("🚀 ~ Redis ~ INCRBY=async ~ value:", value);
console.log("🚀 ~ Redis ~ INCRBY=async ~ key:", key);
try {
const data = {
accountId: this.accountId,
productId: this.productId,
userInstalledId: this.userInstalledId,
appId: this.appId,
method: 'INCRBY',
key,
incrbyValue: value
};
console.log("🚀 ~ Redis ~ INCRBY=async ~ data:", JSON.stringify(data));
return (await Axios.post(`${this.domain}/api/sparrow-apps/redis-db`, data, {
headers: {
"Content-type": "application/json; charset=UTF-8",
"auth": this.token,
"traceId": this.traceId
}
}))?.data;
} catch (error) {
console.log("🚀 ~ Redis ~ INCRBY=async ~ error:", JSON.stringify(error));
await emitEvent(errorConstants.redis, error);
throw error;
}
}
decrement = async (key) => {
console.log("🚀 ~ Redis ~ DECR=async ~ key:", key);
try {
const data = {
accountId: this.accountId,
productId: this.productId,
userInstalledId: this.userInstalledId,
appId: this.appId,
method: 'DECR',
key
};
console.log("🚀 ~ Redis ~ DECR=async ~ data:", JSON.stringify(data));
return (await Axios.post(`${this.domain}/api/sparrow-apps/redis-db`, data, {
headers: {
"Content-type": "application/json; charset=UTF-8",
"auth": this.token,
"traceId": this.traceId
}
}))?.data;
} catch (error) {
console.log("🚀 ~ Redis ~ DECR=async ~ error:", JSON.stringify(error));
await emitEvent(errorConstants.redis, error);
throw error;
}
}
decrementBy = async (key, value) => {
console.log("🚀 ~ Redis ~ DECRBY=async ~ value:", value);
console.log("🚀 ~ Redis ~ DECRBY=async ~ key:", key);
try {
const data = {
accountId: this.accountId,
productId: this.productId,
userInstalledId: this.userInstalledId,
appId: this.appId,
method: 'DECRBY',
key,
decrbyValue: value
};
console.log("🚀 ~ Redis ~ DECRBY=async ~ data:", JSON.stringify(data));
return (await Axios.post(`${this.domain}/api/sparrow-apps/redis-db`, data, {
headers: {
"Content-type": "application/json; charset=UTF-8",
"auth": this.token,
"traceId": this.traceId
}
}))?.data;
} catch (error) {
console.log("🚀 ~ Redis ~ DECRBY=async ~ error:", JSON.stringify(error));
await emitEvent(errorConstants.redis, error);
throw error;
}
}
getArray = async (key) => {
console.log("🚀 ~ Redis ~ GET_ARRAY= ~ key:", key);
try {
const data = {
accountId: this.accountId,
productId: this.productId,
userInstalledId: this.userInstalledId,
appId: this.appId,
method: 'GET_ARRAY',
key
};
console.log("🚀 ~ Redis ~ GET_ARRAY= ~ data:", JSON.stringify(data));
return (await Axios.post(`${this.domain}/api/sparrow-apps/redis-db`, data, {
headers: {
"Content-type": "application/json; charset=UTF-8",
"auth": this.token,
"traceId": this.traceId
}
}))?.data;
} catch (error) {
console.log("🚀 ~ Redis ~ GET_ARRAY= ~ error:", JSON.stringify(error));
await emitEvent(errorConstants.redis, error);
throw error;
}
}
setArray = async (key, value) => {
console.log("🚀 ~ Redis ~ SET_ARRAY=async ~ value:", value);
console.log("🚀 ~ Redis ~ SET_ARRAY=async ~ key:", key);
try {
const data = {
accountId: this.accountId,
productId: this.productId,
userInstalledId: this.userInstalledId,
appId: this.appId,
method: 'SET_ARRAY',
key,
value
};
console.log("🚀 ~ Redis ~ SET_ARRAY=async ~ data:", JSON.stringify(data));
return (await Axios.post(`${this.domain}/api/sparrow-apps/redis-db`, data, {
headers: {
"Content-type": "application/json; charset=UTF-8",
"auth": this.token,
"traceId": this.traceId
}
}))?.data;
} catch (error) {
console.log("🚀 ~ Redis ~ SET_ARRAY=async ~ error:", JSON.stringify(error));
await emitEvent(errorConstants.redis, error);
throw error;
}
}
setElementArray = async (key, value) => {
console.log("🚀 ~ Redis ~ SET_EL_ARRAY=async ~ value:", value);
console.log("🚀 ~ Redis ~ SET_EL_ARRAY=async ~ key:", key);
try {
const data = {
accountId: this.accountId,
productId: this.productId,
userInstalledId: this.userInstalledId,
appId: this.appId,
method: 'SET_EL_ARRAY',
key,
value
};
console.log("🚀 ~ Redis ~ SET_EL_ARRAY=async ~ data:", JSON.stringify(data));
return (await Axios.post(`${this.domain}/api/sparrow-apps/redis-db`, data, {
headers: {
"Content-type": "application/json; charset=UTF-8",
"auth": this.token,
"traceId": this.traceId
}
}))?.data;
} catch (error) {
console.log("🚀 ~ Redis ~ SET_EL_ARRAY=async ~ error:", JSON.stringify(error));
await emitEvent(errorConstants.redis, error);
throw error;
}
}
setUniqueElementArray = async (key, value) => {
console.log("🚀 ~ Redis ~ SET_UNI_EL_ARRAY=async ~ value:", value);
console.log("🚀 ~ Redis ~ SET_UNI_EL_ARRAY=async ~ key:", key);
try {
const data = {
accountId: this.accountId,
productId: this.productId,
userInstalledId: this.userInstalledId,
appId: this.appId,
method: 'SET_UNI_EL_ARRAY',
key,
value
};
console.log("🚀 ~ Redis ~ SET_UNI_EL_ARRAY=async ~ data:", JSON.stringify(data));
return (await Axios.post(`${this.domain}/api/sparrow-apps/redis-db`, data, {
headers: {
"Content-type": "application/json; charset=UTF-8",
"auth": this.token,
"traceId": this.traceId
}
}))?.data;
} catch (error) {
console.log("🚀 ~ Redis ~ SET_UNI_EL_ARRAY=async ~ error:", JSON.stringify(error));
await emitEvent(errorConstants.redis, error);
throw error;
}
}
deleteElementArray = async (key, value) => {
console.log("🚀 ~ Redis ~ DEL_EL_ARRAY=async ~ value:", value);
console.log("🚀 ~ Redis ~ DEL_EL_ARRAY=async ~ key:", key);
try {
const data = {
accountId: this.accountId,
productId: this.productId,
userInstalledId: this.userInstalledId,
appId: this.appId,
method: 'DEL_EL_ARRAY',
key,
value
};
console.log("🚀 ~ Redis ~ DEL_EL_ARRAY=async ~ data:", JSON.stringify(data));
return (await Axios.post(`${this.domain}/api/sparrow-apps/redis-db`, data, {
headers: {
"Content-type": "application/json; charset=UTF-8",
"auth": this.token,
"traceId": this.traceId
}
}))?.data;
} catch (error) {
console.log("🚀 ~ Redis ~ DEL_EL_ARRAY=async ~ error:", JSON.stringify(error));
await emitEvent(errorConstants.redis, error);
throw error;
}
}
}
module.exports = Redis;