-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathURL.js
90 lines (73 loc) · 2.22 KB
/
URL.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
// packages
const axios = require('axios');
// URL will store needed information for compiling URL before API call
const URL = function(key = "", baseURL, dataIndex, text, value){
if (!key.length) {
this.key = "";
}else {
this.key = "&apikey="+key;
}
this.baseURL = baseURL;
this.url;
// index of object to pull desired data from
this.dataIndex = dataIndex;
// key needed for capturing text to display for each <li>
this.text = text;
// value needed for capturing id
this.value = value;
this.numOfResults = 10;
}
// put the HTTP endpoint together with needed parameters
URL.prototype.compileURL = function(query){
if (!query) return "";
if (this.key === ""){
let compiledUrl = this.baseURL+query
this.url = compiledUrl;
}else{
this.url = this.baseURL+query+this.key;
}
};
// fires off api call
// callback function from Autocomplete.updateDropdown
URL.prototype.getAPI = function(query, getResults, updateDropdownCB){
axios
.get(this.url)
.then(response => {
//grab needed index of response data
let resData = response.data
let objKey = Object.keys(resData)[this.dataIndex]
// const rows = response.data[objKey];
const rows = response.data[objKey];
// iterate over rows of data
const APIresults = rows.map(item => ({
text: item[this.text],
value: item[this.value]
}))
// data array is set to value of APIresults array
let data = APIresults;
// filter results
let results = getResults(query, data);
results = results.slice(0, this.numOfResults);
//callback function from Autocomplete
updateDropdownCB(results);
})
.catch(error => {
console.log(error);
});
}
export default URL;
/*
const updateDropdownCB = (results) => console.log(results);
// Test 1 - Github
let data = new URL ("", "https://api.github.com/search/users?q=", 2, "login", "id");
console.log(data);
data.compileURL('foo', data.key);
console.log(data);
data.getAPI('foo', updateDropdownCB);
//Test 2 - OMDB
let data = new URL (keys.OMDB.apiKey, "http://www.omdbapi.com/?s=", 0, "Title", "imdbID");
console.log(data);
data.compileURL('foo', data.key);
console.log(data);
data.getAPI('foo', updateDropdownCB);
*/