refactor: improve language normalization and date handling utilities
All checks were successful
CI / lint-and-test (push) Successful in 21s

- Introduced a new `normalize_lang` function to standardize language codes across the application, ensuring consistent handling of user language preferences.
- Refactored date handling utilities by adding `parse_utc_iso` and `parse_utc_iso_naive` functions for better parsing of ISO 8601 date strings, enhancing timezone awareness.
- Updated various modules to utilize the new language normalization and date parsing functions, improving code clarity and maintainability.
- Enhanced error handling in date validation to raise specific `DateRangeValidationError` exceptions, providing clearer feedback on validation issues.
- Improved test coverage for date range validation and language normalization functionalities, ensuring robustness and reliability.
This commit is contained in:
2026-02-20 22:42:54 +03:00
parent f53ef81306
commit d02d0a1835
19 changed files with 216 additions and 158 deletions

View File

@@ -21,6 +21,27 @@ export function buildFetchOptions(initData) {
return { headers, signal: controller.signal, timeoutId };
}
/**
* GET request to API with init data, Accept-Language, timeout.
* Caller checks res.ok, res.status, res.json().
* @param {string} path - e.g. "/api/duties"
* @param {{ from?: string, to?: string }} params - query params
* @returns {Promise<Response>} - raw response
*/
export async function apiGet(path, params = {}) {
const base = window.location.origin;
const query = new URLSearchParams(params).toString();
const url = query ? `${base}${path}?${query}` : `${base}${path}`;
const initData = getInitData();
const opts = buildFetchOptions(initData);
try {
const res = await fetch(url, { headers: opts.headers, signal: opts.signal });
return res;
} finally {
clearTimeout(opts.timeoutId);
}
}
/**
* Fetch duties for date range. Throws ACCESS_DENIED error on 403.
* @param {string} from - YYYY-MM-DD
@@ -28,17 +49,8 @@ export function buildFetchOptions(initData) {
* @returns {Promise<object[]>}
*/
export async function fetchDuties(from, to) {
const base = window.location.origin;
const url =
base +
"/api/duties?from=" +
encodeURIComponent(from) +
"&to=" +
encodeURIComponent(to);
const initData = getInitData();
const opts = buildFetchOptions(initData);
try {
const res = await fetch(url, { headers: opts.headers, signal: opts.signal });
const res = await apiGet("/api/duties", { from, to });
if (res.status === 403) {
let detail = t(state.lang, "access_denied");
try {
@@ -63,8 +75,6 @@ export async function fetchDuties(from, to) {
throw new Error(t(state.lang, "error_network"));
}
throw e;
} finally {
clearTimeout(opts.timeoutId);
}
}
@@ -75,22 +85,11 @@ export async function fetchDuties(from, to) {
* @returns {Promise<object[]>}
*/
export async function fetchCalendarEvents(from, to) {
const base = window.location.origin;
const url =
base +
"/api/calendar-events?from=" +
encodeURIComponent(from) +
"&to=" +
encodeURIComponent(to);
const initData = getInitData();
const opts = buildFetchOptions(initData);
try {
const res = await fetch(url, { headers: opts.headers, signal: opts.signal });
const res = await apiGet("/api/calendar-events", { from, to });
if (!res.ok) return [];
return res.json();
} catch (e) {
return [];
} finally {
clearTimeout(opts.timeoutId);
}
}