refactor: migrate from @izzyjs/route to @tuyau

This commit is contained in:
Sonny
2024-10-28 01:05:45 +01:00
parent 05c867f44f
commit c308262ee0
44 changed files with 294 additions and 235 deletions

View File

@@ -1,19 +1,43 @@
import { tuyau } from '~/lib/tuyau';
import { Collection, Link } from '~/types/app';
import { RouteName } from '~/types/tuyau';
export const appendCollectionId = (
url: string,
export const routeWithCollectionId = (
routeName: RouteName,
collectionId?: Collection['id'] | null | undefined
) => `${url}${collectionId ? `?collectionId=${collectionId}` : ''}`;
) =>
buildUrlWithQueryParams(routeName, {
collectionId,
});
export const appendLinkId = (
url: string,
export const routeWithLinkId = (
routeName: RouteName,
linkId?: Link['id'] | null | undefined
) => `${url}${linkId ? `?linkId=${linkId}` : ''}`;
) => buildUrlWithQueryParams(routeName, { linkId });
export const appendResourceId = (
url: string,
export const routeWithResourceId = (
routeName: RouteName,
resourceId?: Collection['id'] | Link['id']
) => `${url}${resourceId ? `/${resourceId}` : ''}`;
) => `${routeName}${resourceId ? `/${resourceId}` : ''}`;
export function buildUrlWithQueryParams(
routeName: RouteName,
queryParam: Record<string, string | number | null | undefined>
) {
const path = tuyau.$route(routeName).path;
const [key, value] = Object.entries(queryParam)[0];
if (!value) {
return path;
}
const searchParams = new URLSearchParams({
[key]: value.toString(),
});
return `${path}?${searchParams}`;
}
export const getRoute = (routeName: RouteName) => tuyau.$route(routeName);
export const getPath = (routeName: RouteName) => getRoute(routeName).path;
export function isValidHttpUrl(urlParam: string) {
let url;