-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiRequest.js
More file actions
49 lines (42 loc) · 1.39 KB
/
ApiRequest.js
File metadata and controls
49 lines (42 loc) · 1.39 KB
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
class Request{
constructor () {
// For Transalte
this.apikey = "<YOUR_KEY>";
}
async getTranslateWord(word,language,slanguage){
let url = `https://translation.googleapis.com/language/translate/v2?key=${this.apikey}`;
const response = await fetch(url,{
method: 'POST',
headers: {
"Content-type": "application/json; charset=UTF-8"
},
body: JSON.stringify({
target:language,
source:slanguage,
format: "text",
q: word
}),
});
const data = await response.json();
return data;
}
async getDictionary(dictonaryword){
let beforeurl = convertURL(dictonaryword);
let url = `https://api.dictionaryapi.dev/api/v2/entries/en/${beforeurl}`;
const response = await fetch(url,{
method: 'GET',
headers: {
"Content-type": "application/json; charset=UTF-8"
}
});
const data = await response.json();
return data;
}
}
// Converting value from "project.js" to url structure
function convertURL(value) {
var a = value;
var b = a.toLowerCase().replace(/ /g, '-')
.replace(/[^\w-]+/g, '');
return b
}