mirror of
https://github.com/ToolJet/ToolJet
synced 2026-05-03 21:38:42 +00:00
54 lines
1.3 KiB
TypeScript
54 lines
1.3 KiB
TypeScript
|
|
import { QueryError } from './query.error';
|
||
|
|
|
||
|
|
const CACHED_CONNECTIONS: any = {};
|
||
|
|
|
||
|
|
export function parseJson(jsonString: string, errorMessage?: string): object {
|
||
|
|
try {
|
||
|
|
return JSON.parse(jsonString);
|
||
|
|
} catch (err) {
|
||
|
|
throw new QueryError(errorMessage, err.message, {});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function cacheConnection(dataSourceId: string, connection: any): any {
|
||
|
|
const updatedAt = new Date();
|
||
|
|
CACHED_CONNECTIONS[dataSourceId] = { connection, updatedAt };
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getCachedConnection(dataSourceId: string | number, dataSourceUpdatedAt: any): any {
|
||
|
|
const cachedData = CACHED_CONNECTIONS[dataSourceId];
|
||
|
|
|
||
|
|
if (cachedData) {
|
||
|
|
const updatedAt = new Date(dataSourceUpdatedAt || null);
|
||
|
|
const cachedAt = new Date(cachedData.updatedAt || null);
|
||
|
|
|
||
|
|
const diffTime = (cachedAt.getTime() - updatedAt.getTime()) / 1000;
|
||
|
|
|
||
|
|
if (diffTime < 0) {
|
||
|
|
return null;
|
||
|
|
} else {
|
||
|
|
return cachedData['connection'];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function cleanSensitiveData(data, keys) {
|
||
|
|
if (!data || typeof data !== 'object') return;
|
||
|
|
|
||
|
|
const dataObj = { ...data };
|
||
|
|
clearData(dataObj, keys);
|
||
|
|
return dataObj;
|
||
|
|
}
|
||
|
|
|
||
|
|
function clearData(data, keys) {
|
||
|
|
if (!data || typeof data !== 'object') return;
|
||
|
|
|
||
|
|
for (const key in data) {
|
||
|
|
if (keys.includes(key)) {
|
||
|
|
delete data[key];
|
||
|
|
} else {
|
||
|
|
clearData(data[key], keys);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|