Files
ITWorx-Pulse-Public/apps/web/src/dashboardScope.ts
T
ITWorx Pulse release export bd774932d5
Public source validation / validate (push) Failing after 3m8s
Publish ITWorx Pulse source
2026-09-03 02:09:19 +02:00

25 lines
1.1 KiB
TypeScript

type DashboardVariable = { name?: unknown; default?: unknown };
// Resolve only exact `$name` references declared by the dashboard. The query
// planner remains the authority for accepted scope keys and values; unresolved
// references are preserved so they fail visibly instead of widening a query.
export function resolveDashboardScope(
rawScope: Record<string, unknown>,
variables: unknown[],
): Record<string, string> {
const defaults = new Map<string, string>();
for (const candidate of variables) {
if (!candidate || typeof candidate !== 'object') continue;
const variable = candidate as DashboardVariable;
if (typeof variable.name === 'string' && variable.name !== '' && typeof variable.default === 'string') {
defaults.set(variable.name, variable.default);
}
}
return Object.fromEntries(Object.entries(rawScope).flatMap(([key, value]) => {
if (typeof value !== 'string') return [];
const reference = /^\$([A-Za-z][A-Za-z0-9_-]{0,63})$/.exec(value);
if (!reference) return [[key, value]];
return [[key, defaults.get(reference[1]) ?? value]];
}));
}