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
223 lines
5.3 KiB
TypeScript
223 lines
5.3 KiB
TypeScript
import type {
|
|
CollectionResult,
|
|
CollocationsResult,
|
|
DictionaryResult,
|
|
ExamplesResult,
|
|
Language,
|
|
TranslationResult,
|
|
UsageResult,
|
|
} from "@/types/api";
|
|
|
|
export type ApiErrorKind = "network" | "auth" | "quota" | "not-found" | "server";
|
|
|
|
export interface ApiError {
|
|
kind: ApiErrorKind;
|
|
}
|
|
|
|
export type ErrorWithKind = Error & ApiError;
|
|
|
|
export interface RawCollection {
|
|
id: string;
|
|
name: string;
|
|
lang: Language;
|
|
user_id: string;
|
|
words: string[];
|
|
type: "auto" | "user";
|
|
created_at: string;
|
|
updated_at: string;
|
|
}
|
|
|
|
export function apiError(kind: ApiErrorKind): ErrorWithKind {
|
|
const err = new Error(kind) as ErrorWithKind;
|
|
err.kind = kind;
|
|
return err;
|
|
}
|
|
|
|
export function isApiError(err: unknown): err is ErrorWithKind {
|
|
return (
|
|
err instanceof Error &&
|
|
"kind" in err &&
|
|
typeof (err as ErrorWithKind).kind === "string"
|
|
);
|
|
}
|
|
|
|
export function normalizeCollection(raw: RawCollection): CollectionResult {
|
|
return {
|
|
id: raw.id,
|
|
name: raw.name,
|
|
lang: raw.lang,
|
|
userId: raw.user_id,
|
|
words: raw.words,
|
|
type: raw.type,
|
|
created_at: raw.created_at,
|
|
updated_at: raw.updated_at,
|
|
};
|
|
}
|
|
|
|
export async function parseBody<T>(response: Response): Promise<T> {
|
|
const text = await response.text();
|
|
if (!text) return undefined as T;
|
|
return JSON.parse(text) as T;
|
|
}
|
|
|
|
export function errorKind(status: number): ApiErrorKind {
|
|
if (status === 401) return "auth";
|
|
if (status === 429) return "quota";
|
|
if (status === 404) return "not-found";
|
|
return "server";
|
|
}
|
|
|
|
async function request<T>(path: string, init?: RequestInit): Promise<T> {
|
|
let response: Response;
|
|
try {
|
|
response = await fetch(path, init);
|
|
} catch (err) {
|
|
if (err instanceof Error && err.name === "AbortError") throw err;
|
|
throw apiError("network");
|
|
}
|
|
|
|
if (!response.ok) throw apiError(errorKind(response.status));
|
|
|
|
return parseBody<T>(response);
|
|
}
|
|
|
|
const jsonHeaders = { "Content-Type": "application/json" };
|
|
|
|
export function translateText(
|
|
{ text, from, to }: { text: string; from: Language; to: Language },
|
|
signal?: AbortSignal
|
|
): Promise<TranslationResult> {
|
|
return request<TranslationResult>("/api/translate", {
|
|
method: "POST",
|
|
headers: jsonHeaders,
|
|
body: JSON.stringify({ text, from, to }),
|
|
signal,
|
|
});
|
|
}
|
|
|
|
export function lookupDictionary(
|
|
lang: Language,
|
|
word: string,
|
|
signal?: AbortSignal
|
|
): Promise<DictionaryResult> {
|
|
return request<DictionaryResult>(
|
|
`/api/dictionary/${lang}/${encodeURIComponent(word)}`,
|
|
{ signal }
|
|
);
|
|
}
|
|
|
|
export function getCollocations(
|
|
lang: Language,
|
|
word: string,
|
|
signal?: AbortSignal
|
|
): Promise<CollocationsResult> {
|
|
return request<CollocationsResult>(
|
|
`/api/collocations/${lang}/${encodeURIComponent(word)}`,
|
|
{ signal }
|
|
);
|
|
}
|
|
|
|
export function getExamples(
|
|
lang: Language,
|
|
word: string,
|
|
signal?: AbortSignal
|
|
): Promise<ExamplesResult> {
|
|
return request<ExamplesResult>(
|
|
`/api/examples/${lang}/${encodeURIComponent(word)}`,
|
|
{ signal }
|
|
);
|
|
}
|
|
|
|
export async function getCollections(
|
|
signal?: AbortSignal
|
|
): Promise<CollectionResult[]> {
|
|
try {
|
|
const raw = await request<RawCollection[]>("/api/collections", { signal });
|
|
return raw.map(normalizeCollection);
|
|
} catch (err) {
|
|
if (isApiError(err) && err.kind === "not-found") return [];
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
export async function createCollection(
|
|
name: string,
|
|
lang: Language,
|
|
signal?: AbortSignal
|
|
): Promise<CollectionResult> {
|
|
const raw = await request<RawCollection>("/api/collections", {
|
|
signal,
|
|
method: "POST",
|
|
headers: jsonHeaders,
|
|
body: JSON.stringify({ name, lang }),
|
|
});
|
|
return normalizeCollection(raw);
|
|
}
|
|
|
|
export async function getCollectionsByWord(
|
|
lang: Language,
|
|
word: string,
|
|
signal?: AbortSignal
|
|
): Promise<CollectionResult[]> {
|
|
try {
|
|
const raw = await request<RawCollection[]>(
|
|
`/api/collections/search/${lang}/${encodeURIComponent(word)}`,
|
|
{ signal }
|
|
);
|
|
return raw.map(normalizeCollection);
|
|
} catch (err) {
|
|
if (isApiError(err) && err.kind === "not-found") return [];
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
export function deleteCollection(id: string, signal?: AbortSignal) {
|
|
return request<void>(`/api/collections/${id}`, {
|
|
signal,
|
|
method: "DELETE",
|
|
});
|
|
}
|
|
|
|
export async function updateCollection(
|
|
id: string,
|
|
name: string,
|
|
signal?: AbortSignal
|
|
): Promise<CollectionResult> {
|
|
const raw = await request<RawCollection>(`/api/collections/${id}`, {
|
|
signal,
|
|
method: "PATCH",
|
|
headers: jsonHeaders,
|
|
body: JSON.stringify({ name }),
|
|
});
|
|
return normalizeCollection(raw);
|
|
}
|
|
|
|
export async function addWordsToCollection(
|
|
id: string,
|
|
words: string[],
|
|
signal?: AbortSignal
|
|
): Promise<CollectionResult> {
|
|
const raw = await request<RawCollection>(`/api/collections/${id}/words`, {
|
|
signal,
|
|
method: "POST",
|
|
headers: jsonHeaders,
|
|
body: JSON.stringify({ words }),
|
|
});
|
|
return normalizeCollection(raw);
|
|
}
|
|
|
|
export async function deleteWordFromCollection(
|
|
id: string,
|
|
word: string,
|
|
signal?: AbortSignal
|
|
): Promise<CollectionResult> {
|
|
const raw = await request<RawCollection>(
|
|
`/api/collections/${id}/words?word=${encodeURIComponent(word)}`,
|
|
{ signal, method: "DELETE" }
|
|
);
|
|
return normalizeCollection(raw);
|
|
}
|
|
|
|
export function getUsage(signal?: AbortSignal): Promise<UsageResult> {
|
|
return request<UsageResult>("/api/usage", { signal });
|
|
} |