-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary.ts
41 lines (30 loc) · 895 Bytes
/
dictionary.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { Response } from "./type.ts";
const generateURL = (word: string) => {
const url = new URL("https://www.nisanyansozluk.com/api/words");
url.searchParams.set(
"session",
crypto.getRandomValues(new Uint8Array(8)).join("-")
);
url.pathname += `/${word}`;
return url;
};
enum ErrorType {
SERVER_ERROR = "SERVER_ERROR",
NOT_FOUND = "NOT_FOUND",
}
const ErrorMeessage: Record<ErrorType, string> = {
SERVER_ERROR: "Bir seyler yanlis gitti.",
NOT_FOUND: "Aradiginiz sozcuk bulunamadi.",
};
export const getWord = async (word: string): Promise<Response> => {
const url = generateURL(word);
const response = await fetch(url.toString());
if (!response.ok) {
throw new Error(ErrorMeessage.SERVER_ERROR);
}
const json: Response = await response.json();
if (json.isUnsuccessful) {
throw new Error(ErrorMeessage.NOT_FOUND);
}
return json;
};