forked from ZCW-Java8-1/WhatsTypescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
72 lines (55 loc) · 1.7 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
// Import stylesheets
// import './styles.css';
import fetch from 'node-fetch'
const defURL : string = 'https://api.dictionaryapi.dev/api/v2/entries/en/';
const form: HTMLFormElement = document.querySelector('#defineform');
const defString = document.getElementById('definitions');
const bigHead = document.getElementById('header');
form.onsubmit = () => {
const formData = new FormData(form);
const text = formData.get('defineword') as string;
const kyle = GetWords(text);
console.log(kyle);
bigHead!.innerHTML = text;
let counter = 1;
defString!.innerHTML = '';
GetWords(text)
.then(defintions => {
defintions.forEach(d => {
defString!.innerHTML += `<p>${counter}. ${d}</p>`;
counter++;
});
})
.catch(_ => {
defString!.innerHTML += `<p class="lead">${text} isn't a word dumdum.</p>`;
});
return false; // prevent reload
};
type Word = {
word: string;};
type GetWord = {
data : Word[];};
async function GetWords(text: string){
try {
const response = await fetch(defURL + text, {
method: 'GET',
headers: {
Accept: 'application/json',
},
});
if(!response.ok){
throw new Error(`Error! status: ${response.status}`);
}
const result = (await response.json());
console.log('result is: ', JSON.stringify(result, null, 4));
return result[0].meanings.flatMap(m => m.definitions).flatMap(d => d.definition);
} catch (error) {
if(error instanceof Error){
console.log('error message: ', error.message);
return error.message;
} else {
console.log('unexpected error: ', error);
return 'An unexpected error occurred';
}
}
}