1 | (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.MDB = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
|
2 |
|
3 | module.exports = require('./lib/moviedb');
|
4 |
|
5 | },{"./lib/moviedb":3}],2:[function(require,module,exports){
|
6 | module.exports={
|
7 | "base_url": "https://api.themoviedb.org/3/"
|
8 | , "authentication" : {
|
9 | "requestToken" : "authentication/token/new"
|
10 | , "sessionId" : "authentication/session/new"
|
11 | }
|
12 | , "methods" : {
|
13 | "configuration" : {
|
14 | "": { "resource":"configuration", "method": "get" }
|
15 | }
|
16 | , "find" : {
|
17 | "" : { "resource": "find/:id", "method": "get" }
|
18 | }
|
19 | , "search" : {
|
20 | "Movie" : { "resource": "search/movie", "method": "get" }
|
21 | , "Tv" : { "resource": "search/tv", "method": "get" }
|
22 | , "Multi" : { "resource": "search/multi", "method": "get" }
|
23 | , "Collection" : { "resource": "search/collection", "method": "get" }
|
24 | , "Person" : { "resource": "search/person", "method": "get" }
|
25 | , "List" : { "resource": "search/list", "method": "get" }
|
26 | , "Company" : { "resource": "search/company", "method": "get" }
|
27 | , "Keyword" : { "resource": "search/keyword", "method": "get" }
|
28 | }
|
29 | , "collection" : {
|
30 | "Info" : { "resource": "collection/:id", "method": "get" }
|
31 | , "Images" : { "resource": "collection/:id/images", "method": "get" }
|
32 | }
|
33 | , "discover" : {
|
34 | "Movie" : { "resource": "discover/movie", "method": "get" }
|
35 | , "Tv" : { "resource": "discover/tv", "method": "get" }
|
36 | }
|
37 | , "movie" : {
|
38 | "Info" : { "resource": "movie/:id", "method": "get" }
|
39 | , "AlternativeTitles" : { "resource": "movie/:id/alternative_titles", "method": "get" }
|
40 | , "Credits" : { "resource": "movie/:id/credits", "method": "get" }
|
41 | , "Images" : { "resource": "movie/:id/images", "method": "get" }
|
42 | , "Videos" : { "resource": "movie/:id/videos", "method": "get" }
|
43 | , "Keywords" : { "resource": "movie/:id/keywords", "method": "get" }
|
44 | , "Releases" : { "resource":"movie/:id/releases", "method": "get" }
|
45 | , "Trailers" : { "resource": "movie/:id/trailers", "method": "get" }
|
46 | , "Translations" : { "resource": "movie/:id/translations", "method": "get" }
|
47 | , "Similar" : { "resource": "movie/:id/similar_movies", "method": "get" }
|
48 | , "Reviews" : { "resource": "movie/:id/reviews", "method": "get" }
|
49 | , "Lists" : { "resource": "movie/:id/lists", "method": "get" }
|
50 | , "Changes" : { "resource": "movie/:id/changes", "method": "get" }
|
51 | , "RatingUpdate" : { "resource": "movie/:id/rating", "method": "post" }
|
52 | }
|
53 | , "tv" : {
|
54 | "Info" : { "resource": "tv/:id", "method": "get" }
|
55 | , "Credits" : { "resource": "tv/:id/credits", "method": "get" }
|
56 | , "ExternalIds" : { "resource": "tv/:id/external_ids", "method": "get" }
|
57 | , "Images" : { "resource": "tv/:id/images", "method": "get" }
|
58 | , "Videos" : { "resource": "tv/:id/videos", "method": "get" }
|
59 | , "Similar" : { "resource": "tv/:id/similar", "method": "get" }
|
60 | , "Translations" : { "resource": "tv/:id/translations", "method": "get" }
|
61 | , "SeasonInfo" : { "resource": "tv/:id/season/:season_number", "method": "get" }
|
62 | , "SeasonCredits" : { "resource": "tv/:id/season/:season_number/credits", "method": "get" }
|
63 | , "SeasonExternalIds" : { "resource": "tv/:id/season/:season_number/external_ids", "method": "get" }
|
64 | , "SeasonImages" : { "resource": "tv/:id/season/:season_number/images", "method": "get" }
|
65 | , "EpisodeInfo" : { "resource": "tv/:id/season/:season_number/episode/:episode_number", "method": "get" }
|
66 | , "EpisodeCredits" : { "resource": "tv/:id/season/:season_number/episode/:episode_number/credits", "method": "get" }
|
67 | , "EpisodeExternalIds" : { "resource": "tv/:id/season/:season_number/episode/:episode_number/external_ids", "method": "get" }
|
68 | , "EpisodeImages" : { "resource": "tv/:id/season/:season_number/episode/:episode_number/images", "method": "get" }
|
69 | }
|
70 | , "person" : {
|
71 | "Info" : { "resource": "person/:id", "method": "get" }
|
72 | , "Credits" : { "resource": "person/:id/credits", "method": "get" }
|
73 | , "CombinedCredits" : {"resource":"person/:id/combined_credits", "method":"get"}
|
74 | , "Images" : { "resource": "person/:id/images", "method": "get" }
|
75 | , "Changes" : { "resource": "person/:id/changes", "method": "get" }
|
76 | , "Latest" : { "resource": "person/latest", "method": "get" }
|
77 | , "Popular" : { "resource": "person/popular", "method": "get" }
|
78 | }
|
79 | , "list" : {
|
80 | "Info" : { "resource": "list/:id", "method": "get" }
|
81 | }
|
82 | , "genre" : {
|
83 | "List" : { "resource": "genre/list", "method": "get" }
|
84 | , "Movies" : { "resource": "genre/:id/movies", "method": "get" }
|
85 | }
|
86 | , "keyword" : {
|
87 | "Info" : { "resource": "keyword/:id", "method": "get" }
|
88 | , "Movies" : { "resource": "keyword/:id/movies", "method": "get" }
|
89 | }
|
90 | , "company" : {
|
91 | "Info" : { "resource": "company/:id", "method": "get" }
|
92 | , "Movies" : { "resource": "company/:id/movies", "method": "get" }
|
93 | }
|
94 | , "account" : {
|
95 | "Info" : { "resource": "account", "method": "get" }
|
96 | , "Lists" : { "resource": "account/:id/lists", "method": "get" }
|
97 | , "FavoriteMovies" : { "resource": "account/:id/favorite_movies", "method": "get" }
|
98 | , "FavoriteUpdate" : { "resource": "account/:id/favorite", "method": "post" }
|
99 | , "RatedMovies" : { "resource": "account/:id/rated_movies", "method": "get" }
|
100 | , "MovieWatchlist" : { "resource": "account/:id/movie_watchlist", "method": "get" }
|
101 | , "MovieWatchlistUpdate" : { "resource": "account/:id/movie_watchlist", "method": "post" }
|
102 | }
|
103 | , "misc" : {
|
104 | "LatestMovies" : { "resource": "movie/latest", "method": "get" }
|
105 | , "UpcomingMovies" : { "resource": "movie/upcoming", "method": "get" }
|
106 | , "NowPlayingMovies" : { "resource": "movie/now_playing", "method": "get" }
|
107 | , "PopularMovies" : { "resource": "movie/popular", "method": "get" }
|
108 | , "TopRatedMovies" : { "resource": "movie/top_rated", "method": "get" }
|
109 | , "ChangedMovies" : { "resource": "movie/changes", "method": "get" }
|
110 | , "ChangedPeople" : { "resource": "person/changes", "method": "get" }
|
111 | , "TopRatedTvs" : { "resource": "tv/top_rated", "method": "get" }
|
112 | , "PopularTvs" : { "resource": "tv/popular", "method": "get" }
|
113 | }
|
114 | }
|
115 | }
|
116 |
|
117 | },{}],3:[function(require,module,exports){
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | var request = require('superagent')
|
124 | , endpoints = require('./endpoints.json');
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | module.exports = function(api_key, base_url){
|
131 | if(api_key) return new MovieDB(api_key, base_url);
|
132 | else throw new Error('Bad api key');
|
133 | };
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | function MovieDB(api_key, base_url) {
|
140 | this.api_key = api_key;
|
141 | if(base_url) endpoints.base_url = base_url;
|
142 | return this;
|
143 | }
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | MovieDB.prototype.requestToken = function(fn){
|
150 | var self = this;
|
151 |
|
152 | request
|
153 | .get(endpoints.base_url + endpoints.authentication.requestToken)
|
154 | .query({'api_key': self.api_key})
|
155 | .set('Accept', 'application/json')
|
156 | .end(function(res){
|
157 | if(res.ok) self.token = res.body;
|
158 | else throw res.error;
|
159 | fn();
|
160 | }).on('error', function(error){
|
161 | throw error;
|
162 | });
|
163 |
|
164 | return this;
|
165 | };
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | Object.keys(endpoints.methods).forEach(function(method){
|
172 | var met = endpoints.methods[method];
|
173 | Object.keys(met).forEach(function(m){
|
174 | MovieDB.prototype[method + m] = function(params, fn){
|
175 | var self = this;
|
176 |
|
177 | if("function" == typeof params) {
|
178 | fn = params;
|
179 | params = {};
|
180 | }
|
181 |
|
182 | if(!this.token || Date.now() > +new Date(this.token.expires_at)) {
|
183 | this.requestToken(function(){
|
184 | execMethod.call(self, met[m].method, params, met[m].resource, fn);
|
185 | });
|
186 | } else {
|
187 | execMethod.call(this, met[m].method, params, met[m].resource, fn);
|
188 | }
|
189 |
|
190 | return this;
|
191 | };
|
192 | });
|
193 | });
|
194 |
|
195 | var execMethod = function(type, params, endpoint, fn){
|
196 | params = params || {};
|
197 | endpoint = endpoint.replace(':id', params.id).replace(':season_number', params.season_number).replace(':episode_number', params.episode_number);
|
198 | type = type.toUpperCase();
|
199 |
|
200 | var req = request(type, endpoints.base_url + endpoint)
|
201 | .query({api_key : this.api_key})
|
202 | .set('Accept', 'application/json');
|
203 |
|
204 | if(type === 'GET')
|
205 | req.query(params);
|
206 | else
|
207 | req.send(params);
|
208 |
|
209 | req.end(function(res){
|
210 | if(res.ok) fn(null, res.body);
|
211 | else fn(res.error, null);
|
212 | });
|
213 |
|
214 | req.on('error', fn);
|
215 | };
|
216 |
|
217 | },{"./endpoints.json":2,"superagent":4}],4:[function(require,module,exports){
|
218 |
|
219 |
|
220 |
|
221 |
|
222 | var Emitter = require('emitter');
|
223 | var reduce = require('reduce');
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 | var root = 'undefined' == typeof window
|
230 | ? this
|
231 | : window;
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | function noop(){};
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 | function isHost(obj) {
|
251 | var str = {}.toString.call(obj);
|
252 |
|
253 | switch (str) {
|
254 | case '[object File]':
|
255 | case '[object Blob]':
|
256 | case '[object FormData]':
|
257 | return true;
|
258 | default:
|
259 | return false;
|
260 | }
|
261 | }
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 | function getXHR() {
|
268 | if (root.XMLHttpRequest
|
269 | && ('file:' != root.location.protocol || !root.ActiveXObject)) {
|
270 | return new XMLHttpRequest;
|
271 | } else {
|
272 | try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch(e) {}
|
273 | try { return new ActiveXObject('Msxml2.XMLHTTP.6.0'); } catch(e) {}
|
274 | try { return new ActiveXObject('Msxml2.XMLHTTP.3.0'); } catch(e) {}
|
275 | try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch(e) {}
|
276 | }
|
277 | return false;
|
278 | }
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 | var trim = ''.trim
|
289 | ? function(s) { return s.trim(); }
|
290 | : function(s) { return s.replace(/(^\s*|\s*$)/g, ''); };
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 | function isObject(obj) {
|
301 | return obj === Object(obj);
|
302 | }
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 | function serialize(obj) {
|
313 | if (!isObject(obj)) return obj;
|
314 | var pairs = [];
|
315 | for (var key in obj) {
|
316 | if (null != obj[key]) {
|
317 | pairs.push(encodeURIComponent(key)
|
318 | + '=' + encodeURIComponent(obj[key]));
|
319 | }
|
320 | }
|
321 | return pairs.join('&');
|
322 | }
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 | request.serializeObject = serialize;
|
329 |
|
330 | |
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 | function parseString(str) {
|
339 | var obj = {};
|
340 | var pairs = str.split('&');
|
341 | var parts;
|
342 | var pair;
|
343 |
|
344 | for (var i = 0, len = pairs.length; i < len; ++i) {
|
345 | pair = pairs[i];
|
346 | parts = pair.split('=');
|
347 | obj[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
|
348 | }
|
349 |
|
350 | return obj;
|
351 | }
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 | request.parseString = parseString;
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 | request.types = {
|
367 | html: 'text/html',
|
368 | json: 'application/json',
|
369 | xml: 'application/xml',
|
370 | urlencoded: 'application/x-www-form-urlencoded',
|
371 | 'form': 'application/x-www-form-urlencoded',
|
372 | 'form-data': 'application/x-www-form-urlencoded'
|
373 | };
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 | request.serialize = {
|
385 | 'application/x-www-form-urlencoded': serialize,
|
386 | 'application/json': JSON.stringify
|
387 | };
|
388 |
|
389 | |
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 | request.parse = {
|
399 | 'application/x-www-form-urlencoded': parseString,
|
400 | 'application/json': JSON.parse
|
401 | };
|
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 | function parseHeader(str) {
|
413 | var lines = str.split(/\r?\n/);
|
414 | var fields = {};
|
415 | var index;
|
416 | var line;
|
417 | var field;
|
418 | var val;
|
419 |
|
420 | lines.pop();
|
421 |
|
422 | for (var i = 0, len = lines.length; i < len; ++i) {
|
423 | line = lines[i];
|
424 | index = line.indexOf(':');
|
425 | field = line.slice(0, index).toLowerCase();
|
426 | val = trim(line.slice(index + 1));
|
427 | fields[field] = val;
|
428 | }
|
429 |
|
430 | return fields;
|
431 | }
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 |
|
441 | function type(str){
|
442 | return str.split(/ *; */).shift();
|
443 | };
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 | function params(str){
|
454 | return reduce(str.split(/ *; */), function(obj, str){
|
455 | var parts = str.split(/ *= */)
|
456 | , key = parts.shift()
|
457 | , val = parts.shift();
|
458 |
|
459 | if (key && val) obj[key] = val;
|
460 | return obj;
|
461 | }, {});
|
462 | };
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 |
|
497 |
|
498 |
|
499 |
|
500 |
|
501 |
|
502 |
|
503 |
|
504 |
|
505 |
|
506 |
|
507 |
|
508 |
|
509 |
|
510 | function Response(req, options) {
|
511 | options = options || {};
|
512 | this.req = req;
|
513 | this.xhr = this.req.xhr;
|
514 | this.text = this.xhr.responseText;
|
515 | this.setStatusProperties(this.xhr.status);
|
516 | this.header = this.headers = parseHeader(this.xhr.getAllResponseHeaders());
|
517 |
|
518 |
|
519 |
|
520 | this.header['content-type'] = this.xhr.getResponseHeader('content-type');
|
521 | this.setHeaderProperties(this.header);
|
522 | this.body = this.req.method != 'HEAD'
|
523 | ? this.parseBody(this.text)
|
524 | : null;
|
525 | }
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 | Response.prototype.get = function(field){
|
536 | return this.header[field.toLowerCase()];
|
537 | };
|
538 |
|
539 |
|
540 |
|
541 |
|
542 |
|
543 |
|
544 |
|
545 |
|
546 |
|
547 |
|
548 |
|
549 |
|
550 |
|
551 | Response.prototype.setHeaderProperties = function(header){
|
552 |
|
553 | var ct = this.header['content-type'] || '';
|
554 | this.type = type(ct);
|
555 |
|
556 |
|
557 | var obj = params(ct);
|
558 | for (var key in obj) this[key] = obj[key];
|
559 | };
|
560 |
|
561 |
|
562 |
|
563 |
|
564 |
|
565 |
|
566 |
|
567 |
|
568 |
|
569 |
|
570 |
|
571 |
|
572 | Response.prototype.parseBody = function(str){
|
573 | var parse = request.parse[this.type];
|
574 | return parse && str && str.length
|
575 | ? parse(str)
|
576 | : null;
|
577 | };
|
578 |
|
579 |
|
580 |
|
581 |
|
582 |
|
583 |
|
584 |
|
585 |
|
586 |
|
587 |
|
588 |
|
589 |
|
590 |
|
591 |
|
592 |
|
593 |
|
594 |
|
595 |
|
596 |
|
597 |
|
598 |
|
599 |
|
600 | Response.prototype.setStatusProperties = function(status){
|
601 | var type = status / 100 | 0;
|
602 |
|
603 |
|
604 | this.status = status;
|
605 | this.statusType = type;
|
606 |
|
607 |
|
608 | this.info = 1 == type;
|
609 | this.ok = 2 == type;
|
610 | this.clientError = 4 == type;
|
611 | this.serverError = 5 == type;
|
612 | this.error = (4 == type || 5 == type)
|
613 | ? this.toError()
|
614 | : false;
|
615 |
|
616 |
|
617 | this.accepted = 202 == status;
|
618 | this.noContent = 204 == status || 1223 == status;
|
619 | this.badRequest = 400 == status;
|
620 | this.unauthorized = 401 == status;
|
621 | this.notAcceptable = 406 == status;
|
622 | this.notFound = 404 == status;
|
623 | this.forbidden = 403 == status;
|
624 | };
|
625 |
|
626 |
|
627 |
|
628 |
|
629 |
|
630 |
|
631 |
|
632 |
|
633 | Response.prototype.toError = function(){
|
634 | var req = this.req;
|
635 | var method = req.method;
|
636 | var url = req.url;
|
637 |
|
638 | var msg = 'cannot ' + method + ' ' + url + ' (' + this.status + ')';
|
639 | var err = new Error(msg);
|
640 | err.status = this.status;
|
641 | err.method = method;
|
642 | err.url = url;
|
643 |
|
644 | return err;
|
645 | };
|
646 |
|
647 |
|
648 |
|
649 |
|
650 |
|
651 | request.Response = Response;
|
652 |
|
653 |
|
654 |
|
655 |
|
656 |
|
657 |
|
658 |
|
659 |
|
660 |
|
661 | function Request(method, url) {
|
662 | var self = this;
|
663 | Emitter.call(this);
|
664 | this._query = this._query || [];
|
665 | this.method = method;
|
666 | this.url = url;
|
667 | this.header = {};
|
668 | this._header = {};
|
669 | this.on('end', function(){
|
670 | try {
|
671 | var res = new Response(self);
|
672 | if ('HEAD' == method) res.text = null;
|
673 | self.callback(null, res);
|
674 | } catch(e) {
|
675 | var err = new Error('Parser is unable to parse the response');
|
676 | err.parse = true;
|
677 | err.original = e;
|
678 | self.callback(err);
|
679 | }
|
680 | });
|
681 | }
|
682 |
|
683 |
|
684 |
|
685 |
|
686 |
|
687 | Emitter(Request.prototype);
|
688 |
|
689 |
|
690 |
|
691 |
|
692 |
|
693 | Request.prototype.use = function(fn) {
|
694 | fn(this);
|
695 | return this;
|
696 | }
|
697 |
|
698 |
|
699 |
|
700 |
|
701 |
|
702 |
|
703 |
|
704 |
|
705 |
|
706 | Request.prototype.timeout = function(ms){
|
707 | this._timeout = ms;
|
708 | return this;
|
709 | };
|
710 |
|
711 |
|
712 |
|
713 |
|
714 |
|
715 |
|
716 |
|
717 |
|
718 | Request.prototype.clearTimeout = function(){
|
719 | this._timeout = 0;
|
720 | clearTimeout(this._timer);
|
721 | return this;
|
722 | };
|
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 |
|
729 |
|
730 |
|
731 | Request.prototype.abort = function(){
|
732 | if (this.aborted) return;
|
733 | this.aborted = true;
|
734 | this.xhr.abort();
|
735 | this.clearTimeout();
|
736 | this.emit('abort');
|
737 | return this;
|
738 | };
|
739 |
|
740 |
|
741 |
|
742 |
|
743 |
|
744 |
|
745 |
|
746 |
|
747 |
|
748 |
|
749 |
|
750 |
|
751 |
|
752 |
|
753 |
|
754 |
|
755 |
|
756 |
|
757 |
|
758 |
|
759 |
|
760 | Request.prototype.set = function(field, val){
|
761 | if (isObject(field)) {
|
762 | for (var key in field) {
|
763 | this.set(key, field[key]);
|
764 | }
|
765 | return this;
|
766 | }
|
767 | this._header[field.toLowerCase()] = val;
|
768 | this.header[field] = val;
|
769 | return this;
|
770 | };
|
771 |
|
772 |
|
773 |
|
774 |
|
775 |
|
776 |
|
777 |
|
778 |
|
779 |
|
780 |
|
781 |
|
782 |
|
783 |
|
784 |
|
785 |
|
786 | Request.prototype.unset = function(field){
|
787 | delete this._header[field.toLowerCase()];
|
788 | delete this.header[field];
|
789 | return this;
|
790 | };
|
791 |
|
792 |
|
793 |
|
794 |
|
795 |
|
796 |
|
797 |
|
798 |
|
799 |
|
800 | Request.prototype.getHeader = function(field){
|
801 | return this._header[field.toLowerCase()];
|
802 | };
|
803 |
|
804 |
|
805 |
|
806 |
|
807 |
|
808 |
|
809 |
|
810 |
|
811 |
|
812 |
|
813 |
|
814 |
|
815 |
|
816 |
|
817 |
|
818 |
|
819 |
|
820 |
|
821 |
|
822 |
|
823 |
|
824 |
|
825 |
|
826 | Request.prototype.type = function(type){
|
827 | this.set('Content-Type', request.types[type] || type);
|
828 | return this;
|
829 | };
|
830 |
|
831 |
|
832 |
|
833 |
|
834 |
|
835 |
|
836 |
|
837 |
|
838 |
|
839 |
|
840 |
|
841 |
|
842 |
|
843 |
|
844 |
|
845 |
|
846 |
|
847 |
|
848 |
|
849 |
|
850 |
|
851 | Request.prototype.accept = function(type){
|
852 | this.set('Accept', request.types[type] || type);
|
853 | return this;
|
854 | };
|
855 |
|
856 |
|
857 |
|
858 |
|
859 |
|
860 |
|
861 |
|
862 |
|
863 |
|
864 |
|
865 | Request.prototype.auth = function(user, pass){
|
866 | var str = btoa(user + ':' + pass);
|
867 | this.set('Authorization', 'Basic ' + str);
|
868 | return this;
|
869 | };
|
870 |
|
871 |
|
872 |
|
873 |
|
874 |
|
875 |
|
876 |
|
877 |
|
878 |
|
879 |
|
880 |
|
881 |
|
882 |
|
883 |
|
884 |
|
885 | Request.prototype.query = function(val){
|
886 | if ('string' != typeof val) val = serialize(val);
|
887 | if (val) this._query.push(val);
|
888 | return this;
|
889 | };
|
890 |
|
891 |
|
892 |
|
893 |
|
894 |
|
895 |
|
896 |
|
897 |
|
898 |
|
899 |
|
900 |
|
901 |
|
902 |
|
903 |
|
904 |
|
905 |
|
906 |
|
907 | Request.prototype.field = function(name, val){
|
908 | if (!this._formData) this._formData = new FormData();
|
909 | this._formData.append(name, val);
|
910 | return this;
|
911 | };
|
912 |
|
913 |
|
914 |
|
915 |
|
916 |
|
917 |
|
918 |
|
919 |
|
920 |
|
921 |
|
922 |
|
923 |
|
924 |
|
925 |
|
926 |
|
927 |
|
928 |
|
929 |
|
930 | Request.prototype.attach = function(field, file, filename){
|
931 | if (!this._formData) this._formData = new FormData();
|
932 | this._formData.append(field, file, filename);
|
933 | return this;
|
934 | };
|
935 |
|
936 |
|
937 |
|
938 |
|
939 |
|
940 |
|
941 |
|
942 |
|
943 |
|
944 |
|
945 |
|
946 |
|
947 |
|
948 |
|
949 |
|
950 |
|
951 |
|
952 |
|
953 |
|
954 |
|
955 |
|
956 |
|
957 |
|
958 |
|
959 |
|
960 |
|
961 |
|
962 |
|
963 |
|
964 |
|
965 |
|
966 |
|
967 |
|
968 |
|
969 |
|
970 |
|
971 |
|
972 |
|
973 |
|
974 |
|
975 |
|
976 |
|
977 |
|
978 |
|
979 |
|
980 |
|
981 |
|
982 |
|
983 |
|
984 |
|
985 |
|
986 |
|
987 | Request.prototype.send = function(data){
|
988 | var obj = isObject(data);
|
989 | var type = this.getHeader('Content-Type');
|
990 |
|
991 |
|
992 | if (obj && isObject(this._data)) {
|
993 | for (var key in data) {
|
994 | this._data[key] = data[key];
|
995 | }
|
996 | } else if ('string' == typeof data) {
|
997 | if (!type) this.type('form');
|
998 | type = this.getHeader('Content-Type');
|
999 | if ('application/x-www-form-urlencoded' == type) {
|
1000 | this._data = this._data
|
1001 | ? this._data + '&' + data
|
1002 | : data;
|
1003 | } else {
|
1004 | this._data = (this._data || '') + data;
|
1005 | }
|
1006 | } else {
|
1007 | this._data = data;
|
1008 | }
|
1009 |
|
1010 | if (!obj) return this;
|
1011 | if (!type) this.type('json');
|
1012 | return this;
|
1013 | };
|
1014 |
|
1015 |
|
1016 |
|
1017 |
|
1018 |
|
1019 |
|
1020 |
|
1021 |
|
1022 |
|
1023 |
|
1024 | Request.prototype.callback = function(err, res){
|
1025 | var fn = this._callback;
|
1026 | if (2 == fn.length) return fn(err, res);
|
1027 | if (err) return this.emit('error', err);
|
1028 | fn(res);
|
1029 | };
|
1030 |
|
1031 |
|
1032 |
|
1033 |
|
1034 |
|
1035 |
|
1036 |
|
1037 | Request.prototype.crossDomainError = function(){
|
1038 | var err = new Error('Origin is not allowed by Access-Control-Allow-Origin');
|
1039 | err.crossDomain = true;
|
1040 | this.callback(err);
|
1041 | };
|
1042 |
|
1043 |
|
1044 |
|
1045 |
|
1046 |
|
1047 |
|
1048 |
|
1049 | Request.prototype.timeoutError = function(){
|
1050 | var timeout = this._timeout;
|
1051 | var err = new Error('timeout of ' + timeout + 'ms exceeded');
|
1052 | err.timeout = timeout;
|
1053 | this.callback(err);
|
1054 | };
|
1055 |
|
1056 |
|
1057 |
|
1058 |
|
1059 |
|
1060 |
|
1061 |
|
1062 |
|
1063 |
|
1064 |
|
1065 |
|
1066 |
|
1067 | Request.prototype.withCredentials = function(){
|
1068 | this._withCredentials = true;
|
1069 | return this;
|
1070 | };
|
1071 |
|
1072 |
|
1073 |
|
1074 |
|
1075 |
|
1076 |
|
1077 |
|
1078 |
|
1079 |
|
1080 |
|
1081 | Request.prototype.end = function(fn){
|
1082 | var self = this;
|
1083 | var xhr = this.xhr = getXHR();
|
1084 | var query = this._query.join('&');
|
1085 | var timeout = this._timeout;
|
1086 | var data = this._formData || this._data;
|
1087 |
|
1088 |
|
1089 | this._callback = fn || noop;
|
1090 |
|
1091 |
|
1092 | xhr.onreadystatechange = function(){
|
1093 | if (4 != xhr.readyState) return;
|
1094 | if (0 == xhr.status) {
|
1095 | if (self.aborted) return self.timeoutError();
|
1096 | return self.crossDomainError();
|
1097 | }
|
1098 | self.emit('end');
|
1099 | };
|
1100 |
|
1101 |
|
1102 | if (xhr.upload) {
|
1103 | xhr.upload.onprogress = function(e){
|
1104 | e.percent = e.loaded / e.total * 100;
|
1105 | self.emit('progress', e);
|
1106 | };
|
1107 | }
|
1108 |
|
1109 |
|
1110 | if (timeout && !this._timer) {
|
1111 | this._timer = setTimeout(function(){
|
1112 | self.abort();
|
1113 | }, timeout);
|
1114 | }
|
1115 |
|
1116 |
|
1117 | if (query) {
|
1118 | query = request.serializeObject(query);
|
1119 | this.url += ~this.url.indexOf('?')
|
1120 | ? '&' + query
|
1121 | : '?' + query;
|
1122 | }
|
1123 |
|
1124 |
|
1125 | xhr.open(this.method, this.url, true);
|
1126 |
|
1127 |
|
1128 | if (this._withCredentials) xhr.withCredentials = true;
|
1129 |
|
1130 |
|
1131 | if ('GET' != this.method && 'HEAD' != this.method && 'string' != typeof data && !isHost(data)) {
|
1132 |
|
1133 | var serialize = request.serialize[this.getHeader('Content-Type')];
|
1134 | if (serialize) data = serialize(data);
|
1135 | }
|
1136 |
|
1137 |
|
1138 | for (var field in this.header) {
|
1139 | if (null == this.header[field]) continue;
|
1140 | xhr.setRequestHeader(field, this.header[field]);
|
1141 | }
|
1142 |
|
1143 |
|
1144 | this.emit('request', this);
|
1145 | xhr.send(data);
|
1146 | return this;
|
1147 | };
|
1148 |
|
1149 |
|
1150 |
|
1151 |
|
1152 |
|
1153 | request.Request = Request;
|
1154 |
|
1155 |
|
1156 |
|
1157 |
|
1158 |
|
1159 |
|
1160 |
|
1161 |
|
1162 |
|
1163 |
|
1164 |
|
1165 |
|
1166 |
|
1167 |
|
1168 |
|
1169 |
|
1170 | function request(method, url) {
|
1171 |
|
1172 | if ('function' == typeof url) {
|
1173 | return new Request('GET', method).end(url);
|
1174 | }
|
1175 |
|
1176 |
|
1177 | if (1 == arguments.length) {
|
1178 | return new Request('GET', method);
|
1179 | }
|
1180 |
|
1181 | return new Request(method, url);
|
1182 | }
|
1183 |
|
1184 |
|
1185 |
|
1186 |
|
1187 |
|
1188 |
|
1189 |
|
1190 |
|
1191 |
|
1192 |
|
1193 |
|
1194 | request.get = function(url, data, fn){
|
1195 | var req = request('GET', url);
|
1196 | if ('function' == typeof data) fn = data, data = null;
|
1197 | if (data) req.query(data);
|
1198 | if (fn) req.end(fn);
|
1199 | return req;
|
1200 | };
|
1201 |
|
1202 |
|
1203 |
|
1204 |
|
1205 |
|
1206 |
|
1207 |
|
1208 |
|
1209 |
|
1210 |
|
1211 |
|
1212 | request.head = function(url, data, fn){
|
1213 | var req = request('HEAD', url);
|
1214 | if ('function' == typeof data) fn = data, data = null;
|
1215 | if (data) req.send(data);
|
1216 | if (fn) req.end(fn);
|
1217 | return req;
|
1218 | };
|
1219 |
|
1220 |
|
1221 |
|
1222 |
|
1223 |
|
1224 |
|
1225 |
|
1226 |
|
1227 |
|
1228 |
|
1229 | request.del = function(url, fn){
|
1230 | var req = request('DELETE', url);
|
1231 | if (fn) req.end(fn);
|
1232 | return req;
|
1233 | };
|
1234 |
|
1235 |
|
1236 |
|
1237 |
|
1238 |
|
1239 |
|
1240 |
|
1241 |
|
1242 |
|
1243 |
|
1244 |
|
1245 | request.patch = function(url, data, fn){
|
1246 | var req = request('PATCH', url);
|
1247 | if ('function' == typeof data) fn = data, data = null;
|
1248 | if (data) req.send(data);
|
1249 | if (fn) req.end(fn);
|
1250 | return req;
|
1251 | };
|
1252 |
|
1253 |
|
1254 |
|
1255 |
|
1256 |
|
1257 |
|
1258 |
|
1259 |
|
1260 |
|
1261 |
|
1262 |
|
1263 | request.post = function(url, data, fn){
|
1264 | var req = request('POST', url);
|
1265 | if ('function' == typeof data) fn = data, data = null;
|
1266 | if (data) req.send(data);
|
1267 | if (fn) req.end(fn);
|
1268 | return req;
|
1269 | };
|
1270 |
|
1271 |
|
1272 |
|
1273 |
|
1274 |
|
1275 |
|
1276 |
|
1277 |
|
1278 |
|
1279 |
|
1280 |
|
1281 | request.put = function(url, data, fn){
|
1282 | var req = request('PUT', url);
|
1283 | if ('function' == typeof data) fn = data, data = null;
|
1284 | if (data) req.send(data);
|
1285 | if (fn) req.end(fn);
|
1286 | return req;
|
1287 | };
|
1288 |
|
1289 |
|
1290 |
|
1291 |
|
1292 |
|
1293 | module.exports = request;
|
1294 |
|
1295 | },{"emitter":5,"reduce":6}],5:[function(require,module,exports){
|
1296 |
|
1297 |
|
1298 |
|
1299 |
|
1300 |
|
1301 | module.exports = Emitter;
|
1302 |
|
1303 |
|
1304 |
|
1305 |
|
1306 |
|
1307 |
|
1308 |
|
1309 | function Emitter(obj) {
|
1310 | if (obj) return mixin(obj);
|
1311 | };
|
1312 |
|
1313 |
|
1314 |
|
1315 |
|
1316 |
|
1317 |
|
1318 |
|
1319 |
|
1320 |
|
1321 | function mixin(obj) {
|
1322 | for (var key in Emitter.prototype) {
|
1323 | obj[key] = Emitter.prototype[key];
|
1324 | }
|
1325 | return obj;
|
1326 | }
|
1327 |
|
1328 |
|
1329 |
|
1330 |
|
1331 |
|
1332 |
|
1333 |
|
1334 |
|
1335 |
|
1336 |
|
1337 | Emitter.prototype.on =
|
1338 | Emitter.prototype.addEventListener = function(event, fn){
|
1339 | this._callbacks = this._callbacks || {};
|
1340 | (this._callbacks[event] = this._callbacks[event] || [])
|
1341 | .push(fn);
|
1342 | return this;
|
1343 | };
|
1344 |
|
1345 |
|
1346 |
|
1347 |
|
1348 |
|
1349 |
|
1350 |
|
1351 |
|
1352 |
|
1353 |
|
1354 |
|
1355 | Emitter.prototype.once = function(event, fn){
|
1356 | var self = this;
|
1357 | this._callbacks = this._callbacks || {};
|
1358 |
|
1359 | function on() {
|
1360 | self.off(event, on);
|
1361 | fn.apply(this, arguments);
|
1362 | }
|
1363 |
|
1364 | on.fn = fn;
|
1365 | this.on(event, on);
|
1366 | return this;
|
1367 | };
|
1368 |
|
1369 |
|
1370 |
|
1371 |
|
1372 |
|
1373 |
|
1374 |
|
1375 |
|
1376 |
|
1377 |
|
1378 |
|
1379 | Emitter.prototype.off =
|
1380 | Emitter.prototype.removeListener =
|
1381 | Emitter.prototype.removeAllListeners =
|
1382 | Emitter.prototype.removeEventListener = function(event, fn){
|
1383 | this._callbacks = this._callbacks || {};
|
1384 |
|
1385 |
|
1386 | if (0 == arguments.length) {
|
1387 | this._callbacks = {};
|
1388 | return this;
|
1389 | }
|
1390 |
|
1391 |
|
1392 | var callbacks = this._callbacks[event];
|
1393 | if (!callbacks) return this;
|
1394 |
|
1395 |
|
1396 | if (1 == arguments.length) {
|
1397 | delete this._callbacks[event];
|
1398 | return this;
|
1399 | }
|
1400 |
|
1401 |
|
1402 | var cb;
|
1403 | for (var i = 0; i < callbacks.length; i++) {
|
1404 | cb = callbacks[i];
|
1405 | if (cb === fn || cb.fn === fn) {
|
1406 | callbacks.splice(i, 1);
|
1407 | break;
|
1408 | }
|
1409 | }
|
1410 | return this;
|
1411 | };
|
1412 |
|
1413 |
|
1414 |
|
1415 |
|
1416 |
|
1417 |
|
1418 |
|
1419 |
|
1420 |
|
1421 | Emitter.prototype.emit = function(event){
|
1422 | this._callbacks = this._callbacks || {};
|
1423 | var args = [].slice.call(arguments, 1)
|
1424 | , callbacks = this._callbacks[event];
|
1425 |
|
1426 | if (callbacks) {
|
1427 | callbacks = callbacks.slice(0);
|
1428 | for (var i = 0, len = callbacks.length; i < len; ++i) {
|
1429 | callbacks[i].apply(this, args);
|
1430 | }
|
1431 | }
|
1432 |
|
1433 | return this;
|
1434 | };
|
1435 |
|
1436 |
|
1437 |
|
1438 |
|
1439 |
|
1440 |
|
1441 |
|
1442 |
|
1443 |
|
1444 | Emitter.prototype.listeners = function(event){
|
1445 | this._callbacks = this._callbacks || {};
|
1446 | return this._callbacks[event] || [];
|
1447 | };
|
1448 |
|
1449 |
|
1450 |
|
1451 |
|
1452 |
|
1453 |
|
1454 |
|
1455 |
|
1456 |
|
1457 | Emitter.prototype.hasListeners = function(event){
|
1458 | return !! this.listeners(event).length;
|
1459 | };
|
1460 |
|
1461 | },{}],6:[function(require,module,exports){
|
1462 |
|
1463 |
|
1464 |
|
1465 |
|
1466 |
|
1467 |
|
1468 |
|
1469 |
|
1470 |
|
1471 |
|
1472 |
|
1473 | module.exports = function(arr, fn, initial){
|
1474 | var idx = 0;
|
1475 | var len = arr.length;
|
1476 | var curr = arguments.length == 3
|
1477 | ? initial
|
1478 | : arr[idx++];
|
1479 |
|
1480 | while (idx < len) {
|
1481 | curr = fn.call(null, curr, arr[idx], ++idx, arr);
|
1482 | }
|
1483 |
|
1484 | return curr;
|
1485 | };
|
1486 | },{}]},{},[1])(1)
|
1487 | });
|