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
47 lines
1.2 KiB
TypeScript
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)}`
|
|
);
|
|
} |