- Added browser version (moviedb.js and moviedb.min.js)

- Request token now returns an error instead of throwing.
This commit is contained in:
Dan Zajdband
2015-03-07 12:14:30 -03:00
parent b5ef11ed61
commit b9ce9fba94
5 changed files with 1495 additions and 23 deletions

View File

@@ -1,11 +0,0 @@
ALL_TESTS = $(shell find test/ -name '*.test.js')
run-tests:
@./node_modules/.bin/mocha \
$(TESTFLAGS) \
$(TESTS)
test:
@$(MAKE) NODE_PATH=lib TESTS="$(ALL_TESTS)" run-tests
.PHONY: test

View File

@@ -3,14 +3,14 @@
* Module dependencies * Module dependencies
*/ */
var request = require('superagent') var request = require('superagent');
, endpoints = require('./endpoints.json'); var endpoints = require('./endpoints.json');
/* /*
* Exports the constructor * Exports the constructor
*/ */
module.exports = function(api_key, base_url){ module.exports = function(api_key, base_url) {
if(api_key) return new MovieDB(api_key, base_url); if(api_key) return new MovieDB(api_key, base_url);
else throw new Error('Bad api key'); else throw new Error('Bad api key');
}; };
@@ -37,11 +37,11 @@ MovieDB.prototype.requestToken = function(fn){
.query({'api_key': self.api_key}) .query({'api_key': self.api_key})
.set('Accept', 'application/json') .set('Accept', 'application/json')
.end(function(res){ .end(function(res){
if(res.ok) self.token = res.body; if(!res.ok) return fn(res.error);
else throw res.error; self.token = res.body;
fn(); fn();
}).on('error', function(error){ }).on('error', function(error){
throw error; fn(error);
}); });
return this; return this;
@@ -63,7 +63,8 @@ Object.keys(endpoints.methods).forEach(function(method){
} }
if(!this.token || Date.now() > +new Date(this.token.expires_at)) { if(!this.token || Date.now() > +new Date(this.token.expires_at)) {
this.requestToken(function(){ this.requestToken(function(err){
if(err) return fn(err);
execMethod.call(self, met[m].method, params, met[m].resource, fn); execMethod.call(self, met[m].method, params, met[m].resource, fn);
}); });
} else { } else {

1484
moviedb.js Normal file

File diff suppressed because it is too large Load Diff

1
moviedb.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@@ -1,7 +1,7 @@
{ {
"name": "moviedb", "name": "moviedb",
"description": "Library for interacting with themoviedb.com API", "description": "Library for interacting with themoviedb.com API",
"version": "0.1.9", "version": "0.2.0",
"keywords": [ "keywords": [
"themoviedb", "themoviedb",
"api" "api"
@@ -12,9 +12,6 @@
"main": "index.js", "main": "index.js",
"author": "Dan Zajdband <dan.zajdband@gmail.com>", "author": "Dan Zajdband <dan.zajdband@gmail.com>",
"dependencies": { "dependencies": {
"superagent": "0.20.0" "superagent": "0.21.0"
},
"devDependencies": {
"mocha": "1.0.x"
} }
} }