feat(proj): init
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
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, sourceId: string): string {
|
||||
if (isPersonal(access)) {
|
||||
return serviceUrl(
|
||||
`/u/${encodeURIComponent(access.slug)}/styles/${encodeURIComponent(name)}?source=${encodeURIComponent(sourceId)}&token=${encodeURIComponent(access.token)}`
|
||||
);
|
||||
}
|
||||
return serviceUrl(`/api/v1/styles/${encodeURIComponent(name)}?source=${encodeURIComponent(sourceId)}`);
|
||||
}
|
||||
|
||||
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}`;
|
||||
}
|
||||
Reference in New Issue
Block a user