pink-bears
Version:
Intelligent rate limiting middleware with MongoDB integration and caching for Node.js applications
164 lines • 4.88 kB
JavaScript
const {
emitEvent
} = require('./errorEmitter');
const {
errorConstants
} = require('./constants');
const axiosWrapper = require('./AxiosWrapper');
let Axios;
class Fetch {
constructor(domain, token, iframeUrl, productId, userId, traceId, appVersion, userInstalledId, accountId) {
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();
};
get = async (url, options, params) => {
try {
const request = {
pId: this.productId,
function: "Request",
method: "GET",
url: url,
options: options,
params: params,
iframeUrl: this.iframeUrl,
appVersion: this.appVersion,
userInstalledId: this.userInstalledId,
accountId: this.accountId
};
let response;
if (this.traceId) {
response = await Axios.post(`${this.domain}/api/sparrow-apps`, {
"request": 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`, {
"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?.body,
status: response?.data?.statusCodeValue
};
} catch (error) {
console.log("Error occured while making API call", error);
await emitEvent(errorConstants.fetch, error);
return {
data: error.response?.data || error,
status: error.response?.status || error
};
}
};
post = async (url, payload, options) => {
try {
return await this._makeAPICall(url, options, payload, 'POST');
} catch (error) {
console.log("Error occured while making API call", error);
await emitEvent(errorConstants.fetch, error);
return {
data: error.response?.data || error,
status: error.response?.status || error
};
}
};
put = async (url, payload, options) => {
try {
return await this._makeAPICall(url, options, payload, 'PUT');
} catch (error) {
console.log("Error occured while making API call", error);
await emitEvent(errorConstants.fetch, error);
return {
data: error.response?.data || error,
status: error.response?.status || error
};
}
};
patch = async (url, payload, options) => {
try {
return await this._makeAPICall(url, options, payload, 'PATCH');
} catch (error) {
console.log("Error occured while making API call", error);
await emitEvent(errorConstants.fetch, error);
return {
data: error.response?.data || error,
status: error.response?.status || error
};
}
};
delete = async (url, payload, options) => {
try {
return await this._makeAPICall(url, options, payload, 'DELETE');
} catch (error) {
console.log("Error occured while making API call", error);
await emitEvent(errorConstants.fetch, error);
return {
data: error.response?.data || error,
status: error.response?.status || error
};
}
};
async _makeAPICall(url, options, payload, method) {
const request = {
pId: this.productId,
function: "Request",
method,
url: url,
options: options,
body: payload,
iframeUrl: this.iframeUrl,
appVersion: this.appVersion,
userInstalledId: this.userInstalledId,
accountId: this.accountId
};
let response;
if (this.traceId) {
response = await Axios.post(`${this.domain}/api/sparrow-apps`, {
"request": 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`, {
"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?.body,
status: response?.data?.statusCodeValue
};
}
}
module.exports = Fetch;