39 lines
1011 B
TypeScript
39 lines
1011 B
TypeScript
import type {
|
|
AnalyticsSummaryParams,
|
|
AnalyticsSummaryResponse,
|
|
ByCategoryParams,
|
|
ByCategoryItem,
|
|
TimeseriesParams,
|
|
TimeseriesItem,
|
|
} from '@family-budget/shared';
|
|
import { api } from './client';
|
|
|
|
function toQuery(params: object): string {
|
|
const sp = new URLSearchParams();
|
|
for (const [key, value] of Object.entries(params) as [string, unknown][]) {
|
|
if (value != null && value !== '') {
|
|
sp.set(key, String(value));
|
|
}
|
|
}
|
|
const qs = sp.toString();
|
|
return qs ? `?${qs}` : '';
|
|
}
|
|
|
|
export async function getSummary(
|
|
params: AnalyticsSummaryParams,
|
|
): Promise<AnalyticsSummaryResponse> {
|
|
return api.get(`/api/analytics/summary${toQuery(params)}`);
|
|
}
|
|
|
|
export async function getByCategory(
|
|
params: ByCategoryParams,
|
|
): Promise<ByCategoryItem[]> {
|
|
return api.get(`/api/analytics/by-category${toQuery(params)}`);
|
|
}
|
|
|
|
export async function getTimeseries(
|
|
params: TimeseriesParams,
|
|
): Promise<TimeseriesItem[]> {
|
|
return api.get(`/api/analytics/timeseries${toQuery(params)}`);
|
|
}
|