-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-narrative-api.min.js
2 lines (2 loc) · 12.6 KB
/
angular-narrative-api.min.js
1
2
/*! angular-narrative-api 2016-02-18 */
!function(a,b,c){"use strict";b.module("api.narrative",[])}(window,window.angular),function(a,b,c){"use strict";function d(){this.keySerializer=function(a,b){return"narrative."+a+"."+b};var a,b=this.keySerializer;a={initialize:function(a,b){this.name=a,this.$cache=b},put:function(a,c){return j.setItem(b(this.name,a),f(c)),this.$cache.put(a,c)},get:function(a){var d=this.$cache.get(a);return d||(d=g(j.getItem(b(this.name,a))),d&&this.$cache.put(a,d)),null===d?c:d},remove:function(a){var c=this.get(a);return j.removeItem(b(this.name,a)),this.$cache.remove(a),c},removeAll:function(){var a=b(this.name,"");return h(j,function(b,c){0===c.indexOf(a)&&j.removeItem(c)}),this.$cache.removeAll()}},this.$get=["$cacheFactory",function(b){var c={};return i(j)?b:function(d){var f=c[d];return f||(f=e(a),f.initialize(d,b(d)),c[d]=f),f}}]}var e=b.copy,f=b.toJson,g=b.fromJson,h=b.forEach,i=b.isUndefined,j=a.localStorage;b.module("api.narrative").provider("NarrativeCache",d)}(window,window.angular),function(a,b,c){"use strict";function d(a){return{Authorization:a.token_type+" "+a.access_token}}function e(a){return a.hasOwnProperty("token")&&a.hasOwnProperty("getOauthToken")}function f(){function a(a,b){return a.proxy+a.baseUrl+a.apiSuffix+b}function b(a,b){return b.replace(a.baseUrl+a.apiSuffix,"")}this.defaults={api:{proxy:"",baseUrl:"https://narrativeapp.com/",apiSuffix:"api/v2/"},cacheFactory:"$cacheFactory",paramSerializer:"NarrativeParamSerializer"};var c=this.defaults;this.$get=["$http","NarrativeAuth","$injector",function(f,g,h){function j(j,l,n,o){var p,q,r,s,t=o||{};return e(t)&&(t={auth:t}),p=i({},c,t),p.auth||(p.auth=g()),k(p.cache)&&(p.cacheFactory?(r=p.cacheFactory,m(r)&&(r=h.get(r)),s=p.auth.config().name,p.cache=r.get(s)||r(s)):p.cache=!1),q={method:j,cache:p.cache,url:a(p.api,l)},p.data&&(q.data=p.data),p.auth.token()&&(q.headers=d(p.auth.token())),k(n)||(q=i(q,{params:n,paramSerializer:p.paramSerializer})),f(q).then(function(a){var d=a.data;return d.next&&(d.next=b(c.api,d.next)),d})}return j}]}function g(){this.$get=function(){return function(a){if(!a)return"";var b=[];return h(a,function(a,c){if(null!==a&&!k(a)){var d=a;l(a)&&(d=j(a)),b.push(encodeURIComponent(c)+"="+encodeURIComponent(d))}}),b.join("&")}}}var h=b.forEach,i=b.extend,j=b.toJson,k=b.isUndefined,l=b.isArray,m=b.isString;b.module("api.narrative").provider("NarrativeParamSerializer",g).provider("NarrativeRequest",f)}(window,window.angular),function(a,b,c){"use strict";function d(a,b){return function(c){return b(a(c))}}function e(a,b,c,d,e){this._path=a,this._auth=b,this._config=c||{},this._request=d,this.$q=e,this._transform=j}function f(a,b,c,d,f){e.call(this,a,b,c,d,f)}function g(a,b,c,d,f){e.call(this,a,b,c,d,f),this._itemTransform=j}function h(a,b){return function(c,d,e){return new f(c,d,e,a,b)}}function i(a,b){return function(c,d,e){return new g(c,d,e,a,b)}}var j=b.identity,k=b.bind,l=b.extend,m=b.isUndefined;e.prototype={construct:function(a){return this._options=a||{},this._obj={q:k(this,this.q),get:k(this,this.get),path:k(this,this.path),transform:k(this,this.transform)},this._obj},_constructFromObject:function(a,b){return this._obj=l(this.construct(a),b),this._obj},_object:function(){if(!this._obj)throw"Need to invoke construct() before calling this method";return this._obj},q:function(){throw"Abstract method q() must be overriden."},path:function(){return this._path},get:function(){return this.q(),this._obj},transform:function(a){return m(a)?this._transform(this._object()):(this._transform=d(this._transform,a),this)}},f.prototype=l({},e.prototype,{_super:e.prototype,construct:function(a,b){return this.uuid=a,l(this._super.construct.call(this,b),{qGet:k(this,this.q),update:k(this,this.update),qUpdate:k(this,this.qUpdate)})},_constructFromObject:function(a,b,c){return this._obj=l(this.construct(a,c),b),this._obj},_q:function(a,b,c,d){if(this._qPromise){var e=this;this._qPromise.then(function(){return e._request(a,b,c,d)})}else this._qPromise=this._request(a,b,c,d);return this._qPromise},q:function(){if(!this._qPromise){var a=this;this._qPromise=this._request("GET",this.path(),this._options,this._auth).then(function(b){try{return l(a._object(),b)}catch(c){return a.$q.reject(c)}})}return this._qPromise},qUpdate:function(a,b){if(b||(b=a,a="PATCH"),"PATCH"!==a&&"PUT"!==a)throw"Updating objects only supports PATCH and PUT.";var c=this;return this._q(a,c.path(),c._options,{auth:c._auth,data:b}).then(function(a){return 200===a.status?l(c._object(),a):(c._qPromise=c._request("GET",c.path(),c._options,c._auth).then(function(a){try{return l(c._object(),a)}catch(b){return c.$q.reject(b)}}),c._qPromise)})},update:function(a,b){return this.qUpdate(a,b),this},path:function(){return this._super.path.call(this).replace(":uuid",this.uuid||"")}}),f.prototype.constructor=f,g.prototype=l({},e.prototype,{_super:e.prototype,construct:function(a){return this._next=this.path(),this._previous=null,this._count=0,this.results=[],l(this._super.construct.call(this,a),{nextPage:k(this,this.nextPage),forEach:k(this,this.forEach),itemTransform:k(this,this.itemTransform),results:this.results})},itemTransform:function(a){return m(a)?this._itemTransform:(this._itemTransform=d(this._itemTransform,a),this)},q:function(){if(m(this._qPromise))try{this._qPromise=this.nextPage()}catch(a){return this.$q.reject(a)}return this._qPromise},nextPage:function(){if(this._object()&&null===this._next)throw"No more entries to get";var a=this;return this._request("GET",a._next,a.results.length?null:a._options,a._auth).then(function(b){a._next=b.next,a._count=b.count||b.results.length,b.results=b.results.map(function(b){var c=new f(b.type+"s/:uuid/",a._auth,{},a._request,a._$q);return c.transform(a.itemTransform())._constructFromObject(b.uuid,b).transform()}),a.results.push.apply(a.results,b.results);try{return a._object()}catch(c){return a.$q.reject(c)}})},forEach:function(a){function b(){e=!0}function c(){for(;d<g.results.length;)if(a(g.results[d],d++,b),e)return void f.reject("FOREACH_ABORTED");null!==g._next?g.nextPage().then(c):f.resolve(g)}var d=0,e=!1,f=this.$q.defer(),g=this;return c(),f.promise}}),g.prototype.constructor=g,b.module("api.narrative").factory("NarrativeItemFactory",["NarrativeRequest","$q",h]).factory("NarrativeArrayFactory",["NarrativeRequest","$q",i])}(window,window.angular),function(a,b,c){"use strict";function d(a,b){return b+"."+a}function e(){function b(a,b,d,e,g,h){var j,k,l;this.$http=b,this.$q=d,this.$window=e,this.$rootScope=g,this._onAuthCallbacks=[],a=f(a)?{name:a}:a||{},this._config=i({},c,a),j=this.$q.defer(),j.resolve(this),this._initialRequest=j.promise,k=this._config.cacheFactory,f(k)&&(k=h.get(k)),this._cache=k(this._config.name),this._token=null,l=this._cache.get("token"),l&&this.token(l)}this.defaults={name:"global",cacheFactory:"NarrativeCache",oauthApplication:{clientID:null,redirectURI:null,clientSecret:null},oauthRoutes:{authorize:"https://narrativeapp.com/oauth2/authorize/",token:"https://narrativeapp.com/oauth2/token/"}};var c=this.defaults;b.prototype={construct:function(){return this._object={getOauthToken:g(this,this.getOauthToken),oauthAuthorizationCode:g(this,this.oauthAuthorizationCode),oauthImplicit:g(this,this.oauthImplicit),oauthClientCredentials:g(this,this.oauthClientCredentials),oauthResourceOwner:g(this,this.oauthResourceOwner),oauthRefreshToken:g(this,this.oauthRefreshToken),waitForAuth:g(this,this.waitForAuth),requireAuth:g(this,this.requireAuth),onAuth:g(this,this.onAuth),token:g(this,this.token),unauth:g(this,this.unauth),config:g(this,this.config)},this._object},_oauthInitRedirect:function(a,b){var c={config:this._config},d=this._config.oauthApplication;b&&(c.parameters=b),this.$window.location.replace(this._config.oauthRoutes.authorize+"?redirect_uri="+d.redirectURI+"&response_type="+a+"&client_id="+d.clientID+"&state="+encodeURIComponent(j(c)))},config:function(){return this._config},getOauthToken:function(b,c){var d=this,e=this.$q.defer();return this._initialRequest=e.promise,this.$http({url:this._config.oauthRoutes.token,method:"POST",params:{grant_type:"authorization_code",code:b,redirect_uri:this._config.oauthApplication.redirectURI,client_id:this._config.oauthApplication.clientID},headers:{Authorization:"Basic "+a.btoa(this._config.oauthApplication.clientID+":"+this._config.oauthApplication.clientSecret)}}).then(function(a){return d.token(a.data),d.token()?(e.resolve(d.token()),k(d._onAuthCallbacks,function(a){a.callback.call(a.context,d._object,c)}),d):this.$q.reject("MALFORMED_TOKEN")},e.reject)},oauthResourceOwner:function(a,b,c){var d=this;return this.$http({url:this._config.oauthRoutes.token,method:"POST",params:{grant_type:"password",client_id:this._config.oauthApplication.clientID,username:a,password:b}}).then(function(a){return d.token(a.data),d.token()?(k(d._onAuthCallbacks,function(a){a.callback.call(a.context,d._object,c)}),d):this.$q.reject("MALFORMED_TOKEN")})},oauthAuthorizationCode:function(a){this._oauthInitRedirect("code",a)},oauthImplicit:function(){throw"Implicit Grant flow is not supported yet."},oauthClientCredentials:function(){throw"Client Credentials Grant flow is not supported yet."},oauthRefreshToken:function(){throw"Refresh Token Grant flow is not supported yet."},waitForAuth:function(){var a=this;return this._initialRequest.then(function(){if(a.token())return a._object;var b=a.$q.defer();return a.$rootScope.$on(d(a._config.name,"auth"),function(){b.resolve(a._object)}),b.promise})},requireAuth:function(){var a=this;return this._initialRequest.then(function(){return a.token()?a._object:a.$q.reject("AUTH_REQUIRED")})},onAuth:function(a,b){return this._onAuthCallbacks.push({callback:a,context:b||this}),this._object},_validateToken:function(a){return a.hasOwnProperty("token_type")&&a.hasOwnProperty("access_token")},token:function(a){if(!h(a)){if(!this._validateToken(a))throw"The given token is not valid!";this._cache.put("token",a),this._token=a,this.$rootScope.$emit(d(this._config.name,"auth"),this._object)}return this._token},unauth:function(){this._cache&&this._cache.removeAll(),this._token=null,this.$rootScope.$emit(d(this._config.name,"unauth"),this._object)}},this.$get=["$http","$q","$window","$rootScope","$injector",function(a,d,e,g,h){var i={};return function(j){var k,l=j||{},m=f(l)?l:l.name||c.name;return i.hasOwnProperty(m)||(k=new b(j,a,d,e,g,h),i[m]=k.construct()),i[m]}}]}var f=b.isString,g=b.bind,h=b.isUndefined,i=b.extend,j=b.toJson,k=b.forEach,l=b.fromJson;b.module("api.narrative").provider("NarrativeAuth",e).factory("NarrativeUrlObserverFactory",["NarrativeAuth","$location","$window",function(a,b,c){function d(a,d){delete a.state,b.$$html5?b.search(a).replace():h(d)?c.location.replace(e(b.absUrl(),a)):d["finally"](function(){c.location.replace(e(b.absUrl(),a))})}this.rewriteSearchParams=function(a,b){var c=a.indexOf("?"),d=a.lastIndexOf("#"),e=[],f=c>=0?a.substring(0,c):d>=0?a.substring(0,d):a,g=d>=0?a.substring(d):"";return k(b,function(a,b){e.push(b+"="+a)}),f+(e.length?"?"+e.join("&"):"")+g};var e=this.rewriteSearchParams;this.locationSearch=function(a){var b,c=a.indexOf("?"),d={};return-1!==c&&(b=a.substring(c+1).split("#")[0],k(b?b.split("&"):[],function(a){var b=a.split("=");d[b[0]]=b[1].replace(/\/+$/,"")})),d};var f=this.locationSearch;return function(){var c=b.$$html5?b.search():f(b.absUrl()),e=null;if(c.hasOwnProperty("state")){try{e=l(decodeURIComponent(c.state))}catch(g){return}c.hasOwnProperty("error")?(a(e.config).unauth(),delete c.error,d(c)):c.hasOwnProperty("code")&&(d(c,a(e.config).getOauthToken(c.code,e.parameters)),delete c.code)}}}]).run(["NarrativeUrlObserverFactory",function(a){a()}])}(window,window.angular),function(a,b,c){"use strict";function d(a,c,d,e){return function(f,g){var h=a(c,d,g);return b.forEach(e,function(a){h.transform(a)}),h.construct(f,g).transform()}}function e(a,c,d,e,f){return function(g){var h=a(c,d,g);return b.forEach(e,function(a){h.transform(a)}),b.forEach(f,function(a){h.itemTransform(a)}),h.construct(g).transform()}}function f(){function a(a,f,g){return function(h){function i(a){return b.extend(a,{positions:e(f,a.path()+"positions/",h.auth,[],[]),photos:e(f,a.path()+"photos/",h.auth,[],[])})}function j(a){return"moment"===a.type?i(a):a}var k={};return h=b.extend(c,h||{}),h.auth||(h.auth=g()),k.moments=e(f,"moments/",h.auth,[],[i]),k.timeline=e(f,"timeline/",h.auth,[],[j]),k.moment=d(a,"moments/:uuid/",h.auth,[i]),k.video=d(a,"video/:uuid/",h.auth,[]),k.photos=e(f,"photos/",h.auth,[],[]),k.users=e(f,"users/",h.auth,[],[]),k.user=d(a,"users/:uuid/",h.auth,[]),k.favorites=e(f,"favorites/",h.auth,[],[]),k.me=function(a){return k.user("me",a)},k}}this.defaults={auth:null};var c=this.defaults;this.$get=["NarrativeItemFactory","NarrativeArrayFactory","NarrativeAuth",a]}b.module("api.narrative").provider("NarrativeApi",f)}(window,window.angular);