Files
Ivan Rezchikov 628dd6384b
Build and Deploy / test-backend (push) Successful in 1m11s
Build and Deploy / lint-frontend (push) Successful in 21s
Build and Deploy / build-backend (push) Successful in 58s
Build and Deploy / deploy (push) Canceled after 0s
Build and Deploy / build-frontend (push) Canceled after 5m38s
version 1
2026-09-18 00:45:51 +03:00

47 lines
1.2 KiB
TypeScript

import { backendFetch } from "./backend";
import {
apiError,
errorKind,
isApiError,
normalizeCollection,
parseBody,
type RawCollection,
} from "./api";
import type { CollectionResult, DictionaryResult, Language } from "@/types/api";
async function serverRequest<T>(path: string, init?: RequestInit): Promise<T> {
let response: Response;
try {
response = await backendFetch(path, init);
} catch {
throw apiError("network");
}
if (!response.ok) throw apiError(errorKind(response.status));
return parseBody<T>(response);
}
export async function getCollections(): Promise<CollectionResult[]> {
try {
const raw = await serverRequest<RawCollection[]>("/api/collections");
return raw.map(normalizeCollection);
} catch (err) {
if (isApiError(err) && err.kind === "not-found") return [];
throw err;
}
}
export async function getCollection(id: string): Promise<CollectionResult> {
const raw = await serverRequest<RawCollection>(`/api/collections/${encodeURIComponent(id)}`);
return normalizeCollection(raw);
}
export function lookupDictionary(
lang: Language,
word: string
): Promise<DictionaryResult> {
return serverRequest<DictionaryResult>(
`/api/dictionary/${lang}/${encodeURIComponent(word)}`
);
}