pink-bears
Version:
Intelligent rate limiting middleware with MongoDB integration and caching for Node.js applications
187 lines (177 loc) • 6.78 kB
JavaScript
const {emitEvent} = require('./errorEmitter');
const {errorConstants} = require('./constants');
const axiosWrapper = require('./AxiosWrapper');
let Axios;
class Storage {
constructor(accountId, productId, appId, domain, userId, traceId) {
this.accountId = accountId;
this.productId = productId;
this.appId = appId;
this.domain = domain;
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("🚀 ~ Storage ~ get= ~ key:", key)
try {
const data = {
accountId: this.accountId,
productId: this.productId,
appId: this.appId,
method: 'GET',
key
}
console.log("🚀 ~ Storage ~ get= ~ data:", JSON.stringify(data));
return (await Axios.post(`${this.domain}/api/sparrow-apps/db`, data))?.data;
} catch (error) {
console.log("🚀 ~ Storage ~ get= ~ error:", JSON.stringify(error));
await emitEvent(errorConstants.storage, error);
throw error;
}
}
set = async(key,value) => {
console.log("🚀 ~ Storage ~ set=async ~ value:", value)
console.log("🚀 ~ Storage ~ set=async ~ key:", key)
try {
const data = {
accountId: this.accountId,
productId: this.productId,
appId: this.appId,
method: 'SET',
key,
value
}
console.log("🚀 ~ Storage ~ set=async ~ data:", JSON.stringify(data))
return (await Axios.post(`${this.domain}/api/sparrow-apps/db`, data))?.data;
} catch (error) {
console.log("🚀 ~ Storage ~ set=async ~ error:", JSON.stringify(error));
await emitEvent(errorConstants.storage, error);
throw error;
}
}
del = async(key) => {
console.log("🚀 ~ Storage ~ del=async ~ key:", key)
try {
const data = {
accountId: this.accountId,
productId: this.productId,
appId: this.appId,
method: 'DEL',
key
}
console.log("🚀 ~ Storage ~ del=async ~ data:", JSON.stringify(data))
return (await Axios.post(`${this.domain}/api/sparrow-apps/db`, data))?.data;
} catch (error) {
console.log("🚀 ~ Storage ~ del=async ~ error:", JSON.stringify(error));
await emitEvent(errorConstants.storage, error);
throw error;
}
}
getV2 = async (key) => {
console.log("🚀 ~ Storage ~ getV2= ~ key:", key)
try {
const data = {
accountId: this.accountId,
productId: this.productId,
appId: this.appId,
method: 'GET',
key,
v2: true
}
console.log("🚀 ~ Storage ~ getV2= ~ data:", JSON.stringify(data))
return (await Axios.post(`${this.domain}/api/sparrow-apps/db`, data))?.data;
} catch (error) {
console.log("🚀 ~ Storage ~ getV2= ~ error:", JSON.stringify(error))
await emitEvent(errorConstants.storage, error);
throw error;
}
}
getPatternV2 = async (key) => {
console.log("🚀 ~ Storage ~ getPatternV2= ~ key:", key)
try {
const data = {
accountId: this.accountId,
productId: this.productId,
appId: this.appId,
method: 'GET-PAT',
key,
v2: true
}
console.log("🚀 ~ Storage ~ getPatternV2= ~ data:", JSON.stringify(data))
return (await Axios.post(`${this.domain}/api/sparrow-apps/db`, data))?.data;
} catch (error) {
console.log("🚀 ~ Storage ~ getPatternV2= ~ error:", JSON.stringify(error))
await emitEvent(errorConstants.storage, error);
throw error;
}
}
setV2 = async(key,value) => {
console.log("🚀 ~ Storage ~ setV2=async ~ value:", value)
console.log("🚀 ~ Storage ~ setV2=async ~ key:", key)
try {
const data = {
accountId: this.accountId,
productId: this.productId,
appId: this.appId,
method: 'SET',
key,
value,
v2: true
}
console.log("🚀 ~ Storage ~ setV2=async ~ data:", JSON.stringify(data));
return (await Axios.post(`${this.domain}/api/sparrow-apps/db`, data))?.data;
} catch (error) {
console.log("🚀 ~ Storage ~ setV2=async ~ error:", JSON.stringify(error))
await emitEvent(errorConstants.storage, error);
throw error;
}
}
delV2 = async(key) => {
console.log("🚀 ~ Storage ~ delV2=async ~ key:", key)
try {
const data = {
accountId: this.accountId,
productId: this.productId,
appId: this.appId,
method: 'DEL',
key,
v2: true
}
console.log("🚀 ~ Storage ~ delV2=async ~ data:", JSON.stringify(data))
return (await Axios.post(`${this.domain}/api/sparrow-apps/db`, data))?.data;
} catch (error) {
console.log("🚀 ~ Storage ~ delV2=async ~ error:", JSON.stringify(error));
await emitEvent(errorConstants.storage, error);
throw error;
}
}
delPatternV2 = async(key) => {
console.log("🚀 ~ Storage ~ delPatternV2=async ~ key:", key)
try {
const data = {
accountId: this.accountId,
productId: this.productId,
appId: this.appId,
method: 'DEL-PAT',
key,
v2: true
}
console.log("🚀 ~ Storage ~ delPatternV2=async ~ data:", JSON.stringify(data))
return (await Axios.post(`${this.domain}/api/sparrow-apps/db`, data))?.data;
} catch (error) {
console.log("🚀 ~ Storage ~ delPatternV2=async ~ error:", JSON.stringify(error));
await emitEvent(errorConstants.storage, error);
throw error;
}
}
}
module.exports = Storage;