57 lines
1.7 KiB
TypeScript
57 lines
1.7 KiB
TypeScript
import { AnalyticsEventNameConstants, EVENT_NAMES } from "../constants";
|
|
import { sendAsAnalyticsEvent } from "../hooks/useAnalyticsEvent";
|
|
|
|
export function parseValue(value: any, targetType: string): any | null {
|
|
if (targetType === "string") {
|
|
return String(value);
|
|
} else if (targetType === "number") {
|
|
return Number(value);
|
|
} else if (targetType === "object") {
|
|
if (typeof value === "string") {
|
|
try {
|
|
return JSON.parse(value);
|
|
} catch (error) {
|
|
sendAsAnalyticsEvent({
|
|
name: AnalyticsEventNameConstants.HI_RN_INVALID_JSON_ERROR,
|
|
properties: {
|
|
errorType: EVENT_NAMES.JSON_PARSING_ERROR,
|
|
error: `${error}`,
|
|
methodName: `${arguments.callee.name}`,
|
|
},
|
|
});
|
|
return null;
|
|
}
|
|
} else {
|
|
sendAsAnalyticsEvent({
|
|
name: AnalyticsEventNameConstants.HI_RN_INVALID_JSON_ERROR,
|
|
properties: {
|
|
errorType: EVENT_NAMES.JSON_PARSING_ERROR,
|
|
error: `${value}`,
|
|
methodName: `${arguments.callee.name}`,
|
|
},
|
|
});
|
|
return null;
|
|
}
|
|
} else {
|
|
sendAsAnalyticsEvent({
|
|
name: AnalyticsEventNameConstants.HI_RN_INVALID_JSON_ERROR,
|
|
properties: {
|
|
errorType: EVENT_NAMES.INVALID_TARGET_TYPE_ERROR,
|
|
error: `${targetType}`,
|
|
methodName: `${arguments.callee.name}`,
|
|
},
|
|
});
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export const buildUrlWithParams = (
|
|
baseUrl: string,
|
|
params: Record<string, any>,
|
|
): string => {
|
|
const queryString = Object.keys(params)
|
|
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
|
|
.join("&");
|
|
return `${baseUrl}?${queryString}`;
|
|
};
|