Skip to content

Commit

Permalink
update to support Callbacks and UMD
Browse files Browse the repository at this point in the history
  • Loading branch information
lacymorrow committed Feb 16, 2018
1 parent 03bbbcc commit 2b302cb
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 24 deletions.
55 changes: 31 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
"use strict";
'use strict';

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

if (typeof movie !== "string") {
throw new Error("Expected a string");
} else if (typeof year !== "string") {
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') {
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 : '')
);

// Promise
return fetch(url, {
method: "GET",
mode: "no-cors"
// Request
var response = fetch(url, {
method: 'GET',
})
.then(
function(response) {
Expand All @@ -52,26 +52,33 @@
}
)
.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.results[0];
return json && json.results[0];
}
})
.catch(function(error) {
return error;
});

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

// exposed public method
// exposed public method
return movieInfo;
});
33 changes: 33 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,39 @@
import test from 'ava';
var movieInfo = require('./index');

test('calls the callback without a year', async t => {
t.plan(1);

const title = await new Promise((resolve, reject) => {movieInfo('crash', r => {
resolve(r.title)
});
});

t.is(title, 'Crash');
});

test('calls the callback with a year', async t => {
t.plan(1);

const title = await new Promise((resolve, reject) => {movieInfo('crash', '2005', r => {
resolve(r.title)
});
});

t.is(title, 'Crash');
});

test('calls the callback with a numeric year', async t => {
t.plan(1);

const title = await new Promise((resolve, reject) => {movieInfo('crash', 2005, r => {
resolve(r.title)
});
});

t.is(title, 'Crash');
});

test('returns an object', async t => {
t.plan(1);

Expand Down

0 comments on commit 2b302cb

Please sign in to comment.