Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.
TypeScript analyticsaudience-segmentationchartscohort-analysisgoogle-analyticsproduct-analyticsstatisticsuser-journeyweb-analytics
import type { QueryFilters } from './types';
export function sanitizeSortFilters<const T extends readonly string[]>(
filters: QueryFilters = {},
allowedFields: T,
defaults: Partial<Pick<QueryFilters, 'orderBy' | 'sortDescending'>> = {},
): QueryFilters {
const { orderBy, sortDescending, ...rest } = filters;
const fallbackOrderBy = defaults.orderBy;
const fallbackSortDescending = defaults.sortDescending;
const isAllowed = orderBy ? allowedFields.includes(orderBy as T[number]) : false;
return {
...rest,
...(isAllowed
? {
orderBy,
sortDescending,
}
: {
...(fallbackOrderBy && { orderBy: fallbackOrderBy }),
...(fallbackSortDescending !== undefined && {
sortDescending: fallbackSortDescending,
}),
}),
};
}