-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (76 loc) · 3.53 KB
/
index.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const OxfordDictionary = function(obj) {
this.config = {
app_id : obj.app_id,
app_key : obj.app_key,
source_lang: obj.source_lang || 'en-us'
};
};
const path ='https://od-api.oxforddictionaries.com:443/api/v2/';
OxfordDictionary.prototype.find = async function(props) {
const url =path + `entries/${this.config.source_lang}/` + String(props);
const options = new OptionObj(url, this.config.app_id, this.config.app_key);
return buildRequest(options, url);;
} // .find
//GET /entries/{source_lang}/{word_id}/definitions
OxfordDictionary.prototype.definitions = function(props) {
const url =path + `entries/${this.config.source_lang}/` + String(props) + '?fields=definitions';
const options = new OptionObj(url, this.config.app_id, this.config.app_key);
return buildRequest(options, url);
} // .definitions
// GET /entries/{source_lang}/{word_id}/pronunciations
OxfordDictionary.prototype.pronunciations = function(props) {
const url =path + `entries/${this.config.source_lang}/` + String(props) + '?fields=pronunciations';
const options = new OptionObj(url, this.config.app_id, this.config.app_key);
return buildRequest(options, url);
} // .pronunciations
// GET /entries/{source_lang}/{word_id}/examples
OxfordDictionary.prototype.examples = function(props) {
const url =path + `entries/${this.config.source_lang}/` + String(props) + '?fields=examples';
const options = new OptionObj(url, this.config.app_id, this.config.app_key);
return buildRequest(options, url);
} // .examples
// GET /entries/{source_lang}/{word_id}/synonyms
OxfordDictionary.prototype.synonyms = function(props) {
const url =path + `thesaurus/${this.config.source_lang}/` + String(props) + '?fields=synonyms';
const options = new OptionObj(url, this.config.app_id, this.config.app_key);
return buildRequest(options, url);
} // .synonyms
// GET /entries/{source_lang}/{word_id}/antonyms
OxfordDictionary.prototype.antonyms = function(props) {
const url =path + `thesaurus/${this.config.source_lang}/` + String(props) + '?fields=antonyms';
const options = new OptionObj(url, this.config.app_id, this.config.app_key);
return buildRequest(options, url);
} // .antonyms
// GET /entries/{source_lang}/{word_id}/inflections
OxfordDictionary.prototype.inflections = function(props) {
const url =path + `inflections/${this.config.source_lang}/` + String(props);
const options = new OptionObj(url, this.config.app_id, this.config.app_key);
return buildRequest(options, url);
} // .inflections
// GET /entries/{source_lang}/{word_id}/sentences
OxfordDictionary.prototype.sentences = function(props) {
const url =path + `sentences/${this.config.source_lang}/` + String(props);
const options = new OptionObj(url, this.config.app_id, this.config.app_key);
return buildRequest(options, url);
} // .sentences
const buildRequest = function(options, path) {
// const url = 'https://od-api.oxforddictionaries.com:443/api/v2/entries/en-us/';
return fetch(path, options).then((res) => res.json())
.catch(err => console.log(err));
}; // end buildRequest
// Constructor Function for Option Objects
function OptionObj(path, app_id, app_key) {
const options = {
//host : 'od-api.oxforddictionaries.com',
//port : 443,
// path : path,
// method : 'GET',
headers : {
//"Accept": "application/json",
"app_id": app_id,
"app_key": app_key
}
};
return options;
} // end OptionObj
module.exports = OxfordDictionary;