-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchKaikki.ts
62 lines (60 loc) · 1.81 KB
/
fetchKaikki.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import axios from "axios";
import { WordEntry } from "./types";
export type FetchKaikkiResponse = {
data?: WordEntry[];
error?: { message: string };
};
export const fetchKaikki = async (
query: string,
lang: string = "English"
): Promise<FetchKaikkiResponse> => {
// Partly based on: https://github.com/calcit-lang/calcit-runner.nim/blob/master/src/calcit_runner/codegen/emit_js.nim
const converted = query
.replaceAll(".", "_dot_")
.replaceAll("?", "_ques_")
.replaceAll("*", "_star_")
.replaceAll("%", "") // Error 400
.replaceAll("/", "_slash_")
.replaceAll("#", "_hash_")
.replaceAll("\\", "_backslash_");
if (converted === "") {
return { error: { message: "Empty query" } };
}
// Handle "/a/a/a" as in "https://kaikki.org/dictionary/English/meaning/a/a/a.html"
const url = `https://kaikki.org/dictionary/${lang}/meaning/${converted.slice(
0,
1
)}/${converted.slice(0, Math.min(2, converted.length))}/${converted}.json`;
console.log("URL:", url);
// Fetch response
try {
const { data, status } = await axios.get<string | WordEntry>(url, {
headers: {
Accept: "application/json",
},
});
console.log("Status:", status);
// Response is either a JSON or a string containing JSONs
const parsedData =
typeof data === "string"
? data
.split("\n")
.filter(Boolean)
.map((jsonString) => JSON.parse(jsonString))
: [data];
console.log("parsed data:", parsedData);
return {
data: parsedData,
};
} catch (err) {
if (axios.isAxiosError(err)) {
console.error(err.message);
return {
error: { message: err.message },
};
} else {
console.error(err);
return { error: { message: "Could not fetch data: unexpected error" } };
}
}
};