92 lines
3.0 KiB
TypeScript
92 lines
3.0 KiB
TypeScript
const SLUG_KEY = "ts.demo.slug";
|
|
const TOKEN_KEY = "ts.demo.token";
|
|
|
|
export const API_ORIGIN = (import.meta.env.VITE_API_ORIGIN ?? "https://tile-server.ru").replace(/\/$/, "");
|
|
|
|
export function serviceUrl(path: string): string {
|
|
if (/^https?:\/\//i.test(path)) {
|
|
return path;
|
|
}
|
|
return `${API_ORIGIN}${path.startsWith("/") ? path : `/${path}`}`;
|
|
}
|
|
|
|
export type Access = {
|
|
slug: string;
|
|
token: string;
|
|
};
|
|
|
|
export function isPersonal(access: Access): boolean {
|
|
return access.slug.length > 0 && access.token.length > 0;
|
|
}
|
|
|
|
export function readAccess(): Access {
|
|
const params = new URLSearchParams(window.location.search);
|
|
const slug = (params.get("slug") ?? sessionStorage.getItem(SLUG_KEY) ?? "").trim();
|
|
const token = (params.get("token") ?? sessionStorage.getItem(TOKEN_KEY) ?? "").trim();
|
|
return { slug, token };
|
|
}
|
|
|
|
export function persistAccess(access: Access): void {
|
|
if (access.slug) {
|
|
sessionStorage.setItem(SLUG_KEY, access.slug);
|
|
} else {
|
|
sessionStorage.removeItem(SLUG_KEY);
|
|
}
|
|
|
|
if (access.token) {
|
|
sessionStorage.setItem(TOKEN_KEY, access.token);
|
|
} else {
|
|
sessionStorage.removeItem(TOKEN_KEY);
|
|
}
|
|
|
|
const url = new URL(window.location.href);
|
|
if (url.searchParams.has("slug") || url.searchParams.has("token")) {
|
|
url.searchParams.delete("slug");
|
|
url.searchParams.delete("token");
|
|
window.history.replaceState(null, "", `${url.pathname}${url.search}${url.hash}`);
|
|
}
|
|
}
|
|
|
|
export function stylesListUrl(access: Access): string {
|
|
if (isPersonal(access)) {
|
|
return serviceUrl(`/u/${encodeURIComponent(access.slug)}/styles?token=${encodeURIComponent(access.token)}`);
|
|
}
|
|
return serviceUrl("/api/v1/styles");
|
|
}
|
|
|
|
export function styleUrl(access: Access, name: string, sourceIds: string[]): string {
|
|
const source = sourceIds.join(",");
|
|
if (isPersonal(access)) {
|
|
return serviceUrl(
|
|
`/u/${encodeURIComponent(access.slug)}/styles/${encodeURIComponent(name)}?source=${encodeURIComponent(source)}&token=${encodeURIComponent(access.token)}`
|
|
);
|
|
}
|
|
return serviceUrl(`/api/v1/styles/${encodeURIComponent(name)}?source=${encodeURIComponent(source)}`);
|
|
}
|
|
|
|
export function graphMetaUrl(access: Access, sourceId: string): string {
|
|
return serviceUrl(
|
|
`/u/${encodeURIComponent(access.slug)}/graph/${encodeURIComponent(sourceId)}?token=${encodeURIComponent(access.token)}`
|
|
);
|
|
}
|
|
|
|
export function graphTilesUrl(access: Access, sourceId: string): string {
|
|
return serviceUrl(
|
|
`/u/${encodeURIComponent(access.slug)}/graph/${encodeURIComponent(sourceId)}/{z}/{x}/{y}.pbf?token=${encodeURIComponent(access.token)}`
|
|
);
|
|
}
|
|
|
|
export function rewriteServiceUrl(url: string, access: Access): string {
|
|
const resolved = new URL(url, `${API_ORIGIN}/`);
|
|
const isService = resolved.pathname.startsWith("/u/") || resolved.pathname.startsWith("/api/");
|
|
if (!isService) {
|
|
return resolved.href;
|
|
}
|
|
|
|
if (isPersonal(access) && resolved.pathname.startsWith("/u/") && !resolved.searchParams.has("token")) {
|
|
resolved.searchParams.set("token", access.token);
|
|
}
|
|
|
|
return `${API_ORIGIN}${resolved.pathname}${resolved.search}`;
|
|
}
|