version 1
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
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
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
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)}`
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user