Skip to content

Commit

Permalink
add image base
Browse files Browse the repository at this point in the history
  • Loading branch information
lacymorrow committed Feb 16, 2018
1 parent 2b302cb commit ee81c85
Showing 1 changed file with 31 additions and 27 deletions.
58 changes: 31 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
'use strict';
"use strict";

(function(root, factory) {
if (typeof define === 'function' && define.amd) {
(function(root, cx) {
if (typeof define === "function" && define.amd) {
// AMD
define(['fetch'], factory);
} else if (typeof exports === 'object') {
define(["fetch"], cx);
} else if (typeof exports === "object") {
// Node, CommonJS-like
module.exports = factory(require('node-fetch'));
module.exports = cx(require("node-fetch"));
} else {
// Browser globals (root is window)
root.movieInfo = factory(root.fetch);
root.movieInfo = cx(root.fetch);
}
})(this, function(fetch) {
function movieInfo(movie, year, cb) {
// search parameters
var search = {
key: '9d2bff12ed955c7f1f74b83187f188ae',
base: 'https://api.themoviedb.org',
key: "9d2bff12ed955c7f1f74b83187f188ae",
base: "https://api.themoviedb.org",
image_base: "http://image.tmdb.org/t/p/original/",
year: null,
movie: movie
};

if (typeof movie !== 'string') {
throw new Error('Expected a string');
} else if (typeof year === 'function') {
if (typeof movie !== "string") {
throw new Error("Expected a string");
} else if (typeof year === "function") {
cb = year;
} else if (typeof year === 'string' || typeof year === 'number') {
} else if (typeof year === "string" || typeof year === "number") {
search.year = year;
}

var url =
search.base +
encodeURI(
'/3/search/movie?api_key=' +
"/3/search/movie?api_key=" +
search.key +
'&query=' +
"&query=" +
search.movie +
(search.year !== null ? '&year=' + search.year : '')
(search.year !== null ? "&year=" + search.year : "")
);

// Request
var response = fetch(url, {
method: 'GET',
method: "GET"
})
.then(
function(response) {
Expand All @@ -52,31 +53,34 @@
}
)
.then(function(json) {
if (json && typeof json.status_message !== 'undefined') {
return Promise.reject('JSON Error: ' + json.status_message);
if (json && typeof json.status_message !== "undefined") {
return Promise.reject("JSON Error: " + json.status_message);
}
if (json && json.results && json.results.length === 0) {
// Retry failed search without year
if (search.year !== null) {
search.year = null;
return movieInfo(search.movie, null);
} else {
return Promise.reject('Search Error: No results found');
return Promise.reject("Search Error: No results found");
}
} else {
return json && json.results[0];
// Success
var res = json && json.results[0];
res.image_base = search.image_base;
return res;
}
})
.catch(function(error) {
return error;
});

// Callback or return Promise
if (cb) {
response.then(cb);
} else {
return response;
}
// Callback or return Promise
if (cb) {
response.then(cb);
} else {
return response;
}
}

// exposed public method
Expand Down

0 comments on commit ee81c85

Please sign in to comment.