/
kreyndelHam
/
Konstructorium
Обзор
Документация
Войти
/
kreyndelHam
/
Konstructorium
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
1
CI/CD
Аналитика
Безопасность
dev
frontend/src/utils/normalizeApiPath.js
33 строки
1 KB
MihahamYT
tests added
25 май 2026, 13:06
25 май 2026, 13:06
728a833
Код
Авторство
О чём код?
/** * Normalize API URL path for grouping (mask numeric ids). * * @param {string} url * @returns {{path: string, apiService: string}} */ export const normalizeApiPath = (url) => { if (!url) { return { path: '/', apiService: '' } } try { const origin = typeof window !== 'undefined' && window.location?.origin ? window.location.origin : 'http://localhost' const base = origin || 'http://localhost' const parsed = url.startsWith('http://') || url.startsWith('https://') ? new URL(url) : new URL(url, base) const segments = parsed.pathname.split('/').filter(Boolean) const apiIndex = segments.indexOf('api') const apiService = apiIndex >= 0 && segments[apiIndex + 1] ? segments[apiIndex + 1] : '' const normalized = segments.map((segment) => (/^\d+$/.test(segment) ? ':id' : segment)).join('/') const path = normalized ? `/${normalized}` : '/' return { path: path.replace(/\/+$/, '') || '/', apiService, } } catch { return { path: url, apiService: '' } } }