diff --git a/src/components/utlis/apiHelper.ts b/src/components/utlis/apiHelper.ts index b3fa3517..151cd364 100644 --- a/src/components/utlis/apiHelper.ts +++ b/src/components/utlis/apiHelper.ts @@ -7,31 +7,31 @@ import { GLOBAL } from '../../constants/Global'; import { _map } from '../../../RN-UI-LIB/src/utlis/common'; import { BASE_AV_APP_URL } from '../../constants/config'; import { logError } from './errorUtils'; -import { ClickstreamAPIToMonitor, CLICKSTREAM_EVENT_NAMES } from '../../common/Constants'; import { addClickstreamEvent } from '../../services/clickstreamEventService'; import { resetCasesData } from '../../reducer/allCasesSlice'; +import { getEventNameFromAPIKey, getKeyByValue } from './commonFunctions'; export enum ApiKeys { - GENERATE_OTP, - VERIFY_OTP, - ALL_CASES, - CASE_DETAIL, - PINNED_CASES, - LOGOUT, - FEEDBACK, - FILTERS, - JANUS, - GENERATE_PAYMENT_LINK, - ADDRESSES_GEOLOCATION, - NEW_ADDRESS, - GET_SIGNED_URL, - CASE_UNIFIED_DETAILS, - EMI_SCHEDULES, - PAST_FEEDBACK, - NOTIFICATIONS, - NOTIFICATION_ACTION, - NOTIFICATION_DELIVERED, - SEND_LOCATION, + GENERATE_OTP = 'GENERATE_OTP', + VERIFY_OTP = 'VERIFY_OTP', + ALL_CASES = 'ALL_CASES', + CASE_DETAIL = 'CASE_DETAIL', + PINNED_CASES = 'PINNED_CASES', + LOGOUT = 'LOGOUT', + FEEDBACK = 'FEEDBACK', + FILTERS = 'FILTERS', + JANUS = 'JANUS', + GENERATE_PAYMENT_LINK = 'GENERATE_PAYMENT_LINK', + ADDRESSES_GEOLOCATION = 'ADDRESSES_GEOLOCATION', + NEW_ADDRESS = 'NEW_ADDRESS', + GET_SIGNED_URL = 'GET_SIGNED_URL', + CASE_UNIFIED_DETAILS = 'CASE_UNIFIED_DETAILS', + EMI_SCHEDULES = 'EMI_SCHEDULES', + PAST_FEEDBACK = 'PAST_FEEDBACK', + NOTIFICATIONS = 'NOTIFICATIONS', + NOTIFICATION_ACTION = 'NOTIFICATION_ACTION', + NOTIFICATION_DELIVERED = 'NOTIFICATION_DELIVERED', + SEND_LOCATION = 'SEND_LOCATION', } export const API_URLS: Record = {} as Record; @@ -139,12 +139,11 @@ axiosInstance.interceptors.response.use( const milliseconds = end - Number(start); response.headers['request-duration'] = String(milliseconds); if(response?.config?.url) { - const url = response?.config?.url; - if(ClickstreamAPIToMonitor[url]) { - const eventName = ClickstreamAPIToMonitor[url] + '_SUCCESS' - if(CLICKSTREAM_EVENT_NAMES[eventName]) { - addClickstreamEvent(CLICKSTREAM_EVENT_NAMES[eventName], {timeTaken: milliseconds}) - } + const url = response.config.url; + const apiKey = getKeyByValue(API_URLS, url); + if(apiKey) { + const eventName = getEventNameFromAPIKey(apiKey, true); + addClickstreamEvent({name: eventName, description: eventName}, {timeTaken: milliseconds}) } } } @@ -153,23 +152,20 @@ axiosInstance.interceptors.response.use( error => { const {config, response} = error; logError(error as Error, config?.baseURL+config?.url); - if(config.headers.donotHandleError) { - return; - } - if (config?.headers) { + if(response?.config?.url && response.status >= API_STATUS_CODE.INTERNAL_SERVER_ERROR) { const start = response.config.headers['request-start-time']; const end = Date.now(); const milliseconds = end - Number(start); - if(config?.url) { - const url = response?.config?.url; - if(ClickstreamAPIToMonitor[url]) { - const eventName = ClickstreamAPIToMonitor[url] + '_FAILED' - if(CLICKSTREAM_EVENT_NAMES[eventName]) { - addClickstreamEvent(CLICKSTREAM_EVENT_NAMES[eventName], {timeTaken: milliseconds, response}) - } - } + const url = response.config.url; + const apiKey = getKeyByValue(API_URLS, url); + if(apiKey) { + const eventName = getEventNameFromAPIKey(apiKey); + addClickstreamEvent({name: eventName, description: eventName}, {timeTaken: milliseconds}); } } + if(config.headers.donotHandleError) { + return; + } if ( !config || config.retry <= 1 || @@ -189,8 +185,6 @@ axiosInstance.interceptors.response.use( if ([API_STATUS_CODE.UNAUTHORIZED, API_STATUS_CODE.FORBIDDEN].includes(response.status)) { // Reset user info - // const apiURL = `${config?.baseURL}${config?.url}`; - // toast({type:'error', text1: `API failed: 401 or 403: ${apiURL}`, visibilityTime: 9999999}); dispatch && dispatch( setAuthData({ diff --git a/src/components/utlis/commonFunctions.ts b/src/components/utlis/commonFunctions.ts index c483e390..f8e33dc1 100644 --- a/src/components/utlis/commonFunctions.ts +++ b/src/components/utlis/commonFunctions.ts @@ -206,4 +206,31 @@ export function debounce(func : any, timeout = 1000){ clearTimeout(timer); timer = setTimeout(() => { func.apply(this, args); }, timeout); }; +} + +function memoizeValue any>(func: T): T { + const cache: Record = {}; + return function(...args: Parameters): ReturnType { + const key = JSON.stringify(args); + if (cache[key]) { + return cache[key]; + } + const result = func.apply(this, args); + cache[key] = result; + return result; + } as T; +} + +export const getKeyByValue = memoizeValue(function(obj: Record, value: string) { + for (let key in obj) { + const regex = /\{.*?\}/g; + const str = obj[key].replace(regex, ""); + if (value.includes(str)) { + return key; + } + } +}); + +export const getEventNameFromAPIKey = (apiKey: string, isSuccess?: boolean) => { + return `FA_${apiKey}_${isSuccess ? 'SUCCESS' : 'FAILED'}`; } \ No newline at end of file