-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (65 loc) · 2.11 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
const axios = require('axios');
const dataUrl = `http://www.omdbapi.com/?apikey=USERAPIKEY`;
const posterUrl = `http://img.omdbapi.com/?apikey=USERAPIKEY`;
const searchParamKeys = ['s', 'type', 'y', 'r', 'page', 'callback', 'v'];
const specificMovieParamKeys = ['t', 'i', 'plot', 'type', 'y', 'r', 'page', 'callback', 'v'];
const validConfigKeys = {
type: true,
plot: true,
page: true,
r: true,
callback: true,
v: true
}
let userDataUrl, userPosterUrl, userConfigObj = {};
const buildSearchTerms = paramObj => {
const validationArray = paramObj.s ? searchParamKeys : specificMovieParamKeys;
return validationArray.reduce((acc, key) => {
if(paramObj[key]) {
return `${acc}&${key}=${paramObj[key]}`;
}
if(userConfigObj[key]) {
return `${acc}&${key}=${userConfigObj[key]}`;
}
return acc;
}, '');
}
const functions = {
configure(configObj = {}) {
Object.keys(configObj).forEach(key => {
if(!validConfigKeys[key]) {
throw new Error(`Invalid OMDB config key: ${key}`)
}
});
userConfigObj = Object.assign(userConfigObj, configObj);
},
getSpecificMovie(title, imdbId, paramObj = {}) {
if(!title && !imdbId) {
throw new Error('No imdbId or title specified');
}
if (title) paramObj.t = title;
if (imdbId) paramObj.i = imdbId; // Note that OMDB will prioritize title over id
const requestTerms = buildSearchTerms(paramObj);
const requestUrl = `${userDataUrl}${requestTerms}`;
return axios.post(requestUrl)
.then(results => results.data);
},
searchForMovie(title, paramObj = {}){
if(!title) {
throw new Error('No title to search');
}
paramObj.s = title;
const requestTerms = buildSearchTerms(paramObj);
const searchUrl = `${userDataUrl}${requestTerms}`;
return axios.post(searchUrl)
.then((results) => results.data);
},
}
module.exports = apiKey => {
if (typeof apiKey !== 'string') {
throw new Error('Invalid OMDB API key. Should be a string.');
}
userDataUrl = dataUrl.replace('USERAPIKEY', apiKey);
userPosterUrl = posterUrl.replace('USERAPIKEY', apiKey);
return functions;
};