Comparing version 4.4.3 to 5.0.0-beta.1
@@ -1,1 +0,1 @@ | ||
(function(global,factory){'object'==typeof exports&&'undefined'!=typeof module?module.exports=factory(require('babel-runtime/regenerator'),require('babel-runtime/helpers/slicedToArray'),require('babel-runtime/helpers/asyncToGenerator'),require('babel-runtime/helpers/classCallCheck'),require('babel-runtime/helpers/createClass'),require('axios')):'function'==typeof define&&define.amd?define(['babel-runtime/regenerator','babel-runtime/helpers/slicedToArray','babel-runtime/helpers/asyncToGenerator','babel-runtime/helpers/classCallCheck','babel-runtime/helpers/createClass','axios'],factory):global.Kitsu=factory(global._regeneratorRuntime,global._slicedToArray,global._asyncToGenerator,global._classCallCheck,global._createClass,global.axios)})(this,function(_regeneratorRuntime,_slicedToArray,_asyncToGenerator,_classCallCheck,_createClass,axios){'use strict';function error(E){if(E.response){var e=E.response.data;if(e)return e}else if(E.errors)return E;throw E}function query(params){try{var _query='',_loop=function(param){'object'==typeof params[param]?Object.keys(params[param]).forEach(function(value){_query+=`&${param}[${value}]=${params[param][value]}`}):'string'==typeof params[param]&&(_query+=`&${param}=${params[param]}`)};for(var param in params)_loop(param);return params?_query.slice(1):''}catch(E){error(E)}}_regeneratorRuntime=_regeneratorRuntime&&_regeneratorRuntime.hasOwnProperty('default')?_regeneratorRuntime['default']:_regeneratorRuntime,_slicedToArray=_slicedToArray&&_slicedToArray.hasOwnProperty('default')?_slicedToArray['default']:_slicedToArray,_asyncToGenerator=_asyncToGenerator&&_asyncToGenerator.hasOwnProperty('default')?_asyncToGenerator['default']:_asyncToGenerator,_classCallCheck=_classCallCheck&&_classCallCheck.hasOwnProperty('default')?_classCallCheck['default']:_classCallCheck,_createClass=_createClass&&_createClass.hasOwnProperty('default')?_createClass['default']:_createClass,axios=axios&&axios.hasOwnProperty('default')?axios['default']:axios;var deattribute=function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee2(data){var _this=this;return _regeneratorRuntime.wrap(function(_context2){for(;;)switch(_context2.prev=_context2.next){case 0:if(_context2.prev=0,'object'!=typeof data||null===data){_context2.next=8;break}if(!Array.isArray(data)){_context2.next=7;break}return _context2.next=5,data.map(function(){var _ref2=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(el){return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:return _context.abrupt('return',deattribute(el));case 1:case'end':return _context.stop();}},_callee,_this)}));return function(){return _ref2.apply(this,arguments)}}());case 5:_context2.next=8;break;case 7:data.attributes&&data.attributes.constructor===Object&&(Object.keys(data.attributes).forEach(function(key){data[key]=data.attributes[key]}),delete data.attributes);case 8:return _context2.abrupt('return',data);case 11:_context2.prev=11,_context2.t0=_context2['catch'](0),error(_context2.t0);case 14:case'end':return _context2.stop();}},_callee2,this,[[0,11]])}));return function(){return _ref.apply(this,arguments)}}(),deserialiseArray=function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(obj){var _iteratorNormalCompletion,_didIteratorError,_iteratorError,_iterator,_step,value;return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:return _context.prev=0,_iteratorNormalCompletion=!0,_didIteratorError=!1,_iteratorError=void 0,_context.prev=4,_context.next=7,obj.data;case 7:_context.t0=Symbol.iterator,_iterator=_context.sent[_context.t0]();case 9:if(_iteratorNormalCompletion=(_step=_iterator.next()).done){_context.next=23;break}if(value=_step.value,!obj.included){_context.next=15;break}return _context.next=14,linkRelationships(value,obj.included);case 14:value=_context.sent;case 15:if(!value.attributes){_context.next=19;break}return _context.next=18,deattribute(value);case 18:value=_context.sent;case 19:obj.data[obj.data.indexOf(value)]=value;case 20:_iteratorNormalCompletion=!0,_context.next=9;break;case 23:_context.next=29;break;case 25:_context.prev=25,_context.t1=_context['catch'](4),_didIteratorError=!0,_iteratorError=_context.t1;case 29:_context.prev=29,_context.prev=30,!_iteratorNormalCompletion&&_iterator.return&&_iterator.return();case 32:if(_context.prev=32,!_didIteratorError){_context.next=35;break}throw _iteratorError;case 35:return _context.finish(32);case 36:return _context.finish(29);case 37:return _context.abrupt('return',obj);case 40:_context.prev=40,_context.t2=_context['catch'](0),error(_context.t2);case 43:case'end':return _context.stop();}},_callee,this,[[0,40],[4,25,29,37],[30,,32,36]])}));return function(){return _ref.apply(this,arguments)}}(),deserialise=function(){var _ref2=_asyncToGenerator(_regeneratorRuntime.mark(function _callee2(obj){return _regeneratorRuntime.wrap(function(_context2){for(;;)switch(_context2.prev=_context2.next){case 0:if(_context2.prev=0,!(obj.data&&obj.data.constructor===Array)){_context2.next=7;break}return _context2.next=4,deserialiseArray(obj);case 4:obj=_context2.sent,_context2.next=11;break;case 7:if(!obj.included){_context2.next=11;break}return _context2.next=10,linkRelationships(obj.data,obj.included);case 10:obj.data=_context2.sent;case 11:if(delete obj.included,!obj.data.attributes){_context2.next=16;break}return _context2.next=15,deattribute(obj.data);case 15:obj.data=_context2.sent;case 16:return _context2.abrupt('return',obj);case 19:_context2.prev=19,_context2.t0=_context2['catch'](0),error(_context2.t0);case 22:case'end':return _context2.stop();}},_callee2,this,[[0,19]])}));return function(){return _ref2.apply(this,arguments)}}(),filterIncludes=function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(included,_ref2){var id=_ref2.id,type=_ref2.type;return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:return _context.prev=0,_context.abrupt('return',included.filter(function(el){return el.id===id&&el.type===type})[0]||{id,type});case 4:_context.prev=4,_context.t0=_context['catch'](0),error(_context.t0);case 7:case'end':return _context.stop();}},_callee,this,[[0,4]])}));return function(){return _ref.apply(this,arguments)}}(),linkRelationships=function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(data,included){var removeRelationshipsAttribute,relationships,key,_iteratorNormalCompletion,_didIteratorError,_iteratorError,_iterator,_step,_ref2,id,type,filtered,deattributed,_relationships$key$da,_id,_type,_filtered,_deattributed;return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:return removeRelationshipsAttribute=!1,_context.prev=1,relationships=data.relationships,_context.next=5,relationships;case 5:_context.t0=_regeneratorRuntime.keys(_context.sent);case 6:if((_context.t1=_context.t0()).done){_context.next=66;break}if(key=_context.t1.value,!(relationships[key].data&&Array.isArray(relationships[key].data))){_context.next=50;break}return _iteratorNormalCompletion=!0,_didIteratorError=!1,_iteratorError=void 0,_context.prev=12,_context.next=15,relationships[key].data;case 15:_context.t2=Symbol.iterator,_iterator=_context.sent[_context.t2]();case 17:if(_iteratorNormalCompletion=(_step=_iterator.next()).done){_context.next=33;break}return _ref2=_step.value,id=_ref2.id,type=_ref2.type,_context.next=22,filterIncludes(included,{id,type});case 22:if(filtered=_context.sent,!filtered.relationships){_context.next=26;break}return _context.next=26,linkRelationships(filtered,included);case 26:return _context.next=28,deattribute(filtered);case 28:deattributed=_context.sent,'undefined'!=typeof deattributed&&(!data[key]&&(data[key]=[]),data[key].push(deattributed));case 30:_iteratorNormalCompletion=!0,_context.next=17;break;case 33:_context.next=39;break;case 35:_context.prev=35,_context.t3=_context['catch'](12),_didIteratorError=!0,_iteratorError=_context.t3;case 39:_context.prev=39,_context.prev=40,!_iteratorNormalCompletion&&_iterator.return&&_iterator.return();case 42:if(_context.prev=42,!_didIteratorError){_context.next=45;break}throw _iteratorError;case 45:return _context.finish(42);case 46:return _context.finish(39);case 47:removeRelationshipsAttribute=!0,_context.next=64;break;case 50:if(!relationships[key].data){_context.next=64;break}return _relationships$key$da=relationships[key].data,_id=_relationships$key$da.id,_type=_relationships$key$da.type,_context.next=54,filterIncludes(included,{id:_id,type:_type});case 54:if(_filtered=_context.sent,!_filtered.relationships){_context.next=58;break}return _context.next=58,linkRelationships(_filtered,included);case 58:return _context.next=60,deattribute(_filtered);case 60:_deattributed=_context.sent,'undefined'==typeof _deattributed||data[key]||(data[key]=_deattributed),delete data[key].relationships,removeRelationshipsAttribute=!0;case 64:_context.next=6;break;case 66:return removeRelationshipsAttribute&&delete data.relationships,_context.abrupt('return',data);case 70:_context.prev=70,_context.t4=_context['catch'](1),error(_context.t4);case 73:case'end':return _context.stop();}},_callee,this,[[1,70],[12,35,39,47],[40,,42,46]])}));return function(){return _ref.apply(this,arguments)}}(),serialise=function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(model){var type,data,_loop,prop,_this=this,obj=1<arguments.length&&arguments[1]!==void 0?arguments[1]:{},method=2<arguments.length&&arguments[2]!==void 0?arguments[2]:'POST';return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:if(_context.prev=0,obj.constructor===Object&&0!==Object.keys(obj).length){_context.next=3;break}throw new Error(`${method} requires a JSON object body`);case 3:if(type=this.plural(this.camel(model)),data={type},'POST'===method||'undefined'!=typeof obj.id){_context.next=7;break}throw new Error(`${method} requires an ID for the ${type} type`);case 7:for(prop in'POST'!==method&&(data.id=obj.id.toString()),_loop=function(prop){if(null!==obj[prop]&&obj[prop].constructor===Object){if('string'==typeof obj[prop].id)'undefined'==typeof data.relationships&&(data.relationships={}),'undefined'==typeof obj[prop].type&&(obj[prop].type=_this.plural(_this.camel(prop))),data.relationships[prop]={data:Object.assign(obj[prop])};else throw new Error(`${method} requires an ID for the ${prop} relationships`);}else if(null!==obj[prop]&&Array.isArray(obj[prop])){var ptype=_this.plural(_this.camel(prop));'undefined'==typeof data.relationships&&(data.relationships={}),data.relationships[prop]={data:obj[prop].map(function(elem){if('undefined'==typeof elem.id)throw new Error(`${method} requires an ID for the ${prop} relationships`);return{id:elem.id,type:elem.type||ptype}})}}else'id'!==prop&&'type'!==prop&&('undefined'==typeof data.attributes&&(data.attributes={}),data.attributes[prop]=obj[prop])},obj)_loop(prop);return _context.abrupt('return',{data});case 13:throw _context.prev=13,_context.t0=_context['catch'](0),_context.t0;case 16:case'end':return _context.stop();}},_callee,this,[[0,13]])}));return function(){return _ref.apply(this,arguments)}}(),camel=function(s){return s.replace(/[-_][a-z\u00E0-\u00F6\u00F8-\u00FE]/g,function(match){return match.slice(1).toUpperCase()})},kebab=function(s){return s.charAt(0).toLowerCase()+s.slice(1).replace(/[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g,function(match){return'-'+match.toLowerCase()})},snake=function(s){return s.charAt(0).toLowerCase()+s.slice(1).replace(/[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g,function(match){return'_'+match.toLowerCase()})},jsonAPI='application/vnd.api+json',jsonAPIHeader={Accept:jsonAPI,"Content-Type":jsonAPI},Kitsu=function(){function Kitsu(){var options=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{};_classCallCheck(this,Kitsu),this.fetch=this.get,this.update=this.patch,this.create=this.post,this.camel=!1===options.camelCaseTypes?function(s){return s}:camel,this.resCase='none'===options.resourceCase?function(s){return s}:'snake'===options.resourceCase?snake:kebab,this.plural=!1===options.pluralize?function(s){return s}:require('pluralize'),this.headers=Object.assign({},options.headers,jsonAPIHeader),this.axios=axios.create({baseURL:options.baseURL||'https://kitsu.io/api/edge',timeout:options.timeout||3e4})}return _createClass(Kitsu,[{key:'get',value:function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(model){var _model$split,_model$split2,res,id,relationship,url,_ref2,data,params=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},headers=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:return _context.prev=0,_model$split=model.split('/'),_model$split2=_slicedToArray(_model$split,3),res=_model$split2[0],id=_model$split2[1],relationship=_model$split2[2],url=this.plural(this.resCase(res)),id&&(url+=`/${id}`),relationship&&(url+=`/${this.resCase(relationship)}`),_context.next=7,this.axios.get(url,{params,paramsSerializer:function(p){return query(p)},headers:Object.assign(this.headers,headers,jsonAPIHeader)});case 7:return _ref2=_context.sent,data=_ref2.data,_context.abrupt('return',deserialise(data));case 12:return _context.prev=12,_context.t0=_context['catch'](0),_context.abrupt('return',error(_context.t0));case 15:case'end':return _context.stop();}},_callee,this,[[0,12]])}));return function(){return _ref.apply(this,arguments)}}()},{key:'patch',value:function(){var _ref3=_asyncToGenerator(_regeneratorRuntime.mark(function _callee2(model,body){var url,_ref4,data,headers=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};return _regeneratorRuntime.wrap(function(_context2){for(;;)switch(_context2.prev=_context2.next){case 0:if(_context2.prev=0,headers=Object.assign(this.headers,headers,jsonAPIHeader),this.isAuth){_context2.next=4;break}throw new Error('Not logged in');case 4:if('undefined'!=typeof body.id){_context2.next=6;break}throw new Error('Updating a resource requires an ID');case 6:return url=this.plural(this.resCase(model))+'/'+body.id,_context2.t0=this.axios,_context2.t1=url,_context2.next=11,serialise.apply(this,[model,body,'PATCH']);case 11:return _context2.t2=_context2.sent,_context2.t3={headers},_context2.next=15,_context2.t0.patch.call(_context2.t0,_context2.t1,_context2.t2,_context2.t3);case 15:return _ref4=_context2.sent,data=_ref4.data,_context2.abrupt('return',data);case 20:return _context2.prev=20,_context2.t4=_context2['catch'](0),_context2.abrupt('return',error(_context2.t4));case 23:case'end':return _context2.stop();}},_callee2,this,[[0,20]])}));return function(){return _ref3.apply(this,arguments)}}()},{key:'post',value:function(){var _ref5=_asyncToGenerator(_regeneratorRuntime.mark(function _callee3(model,body){var url,_ref6,data,headers=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};return _regeneratorRuntime.wrap(function(_context3){for(;;)switch(_context3.prev=_context3.next){case 0:if(_context3.prev=0,headers=Object.assign(this.headers,headers,jsonAPIHeader),this.isAuth){_context3.next=4;break}throw new Error('Not logged in');case 4:return url=this.plural(this.resCase(model)),_context3.t0=this.axios,_context3.t1=url,_context3.next=9,serialise.apply(this,[model,body]);case 9:return _context3.t2=_context3.sent,_context3.t3={headers},_context3.next=13,_context3.t0.post.call(_context3.t0,_context3.t1,_context3.t2,_context3.t3);case 13:return _ref6=_context3.sent,data=_ref6.data,_context3.abrupt('return',data);case 18:return _context3.prev=18,_context3.t4=_context3['catch'](0),_context3.abrupt('return',error(_context3.t4));case 21:case'end':return _context3.stop();}},_callee3,this,[[0,18]])}));return function(){return _ref5.apply(this,arguments)}}()},{key:'remove',value:function(){var _ref7=_asyncToGenerator(_regeneratorRuntime.mark(function _callee4(model,id){var url,_ref8,data,headers=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};return _regeneratorRuntime.wrap(function(_context4){for(;;)switch(_context4.prev=_context4.next){case 0:if(_context4.prev=0,headers=Object.assign(this.headers,headers,jsonAPIHeader),this.isAuth){_context4.next=4;break}throw new Error('Not logged in');case 4:return url=this.plural(this.resCase(model))+'/'+id,_context4.t0=this.axios,_context4.t1=url,_context4.next=9,serialise.apply(this,[model,{id},'DELETE']);case 9:return _context4.t2=_context4.sent,_context4.t3=headers,_context4.t4={data:_context4.t2,headers:_context4.t3},_context4.next=14,_context4.t0.delete.call(_context4.t0,_context4.t1,_context4.t4);case 14:return _ref8=_context4.sent,data=_ref8.data,_context4.abrupt('return',data);case 19:return _context4.prev=19,_context4.t5=_context4['catch'](0),_context4.abrupt('return',error(_context4.t5));case 22:case'end':return _context4.stop();}},_callee4,this,[[0,19]])}));return function(){return _ref7.apply(this,arguments)}}()},{key:'self',value:function(){var _ref9=_asyncToGenerator(_regeneratorRuntime.mark(function _callee5(){var res,params=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},headers=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};return _regeneratorRuntime.wrap(function(_context5){for(;;)switch(_context5.prev=_context5.next){case 0:return _context5.prev=0,_context5.next=3,this.get('users',Object.assign({filter:{self:!0}},params),headers);case 3:if(res=_context5.sent,!res.errors){_context5.next=6;break}throw res;case 6:return _context5.abrupt('return',res.data[0]);case 9:return _context5.prev=9,_context5.t0=_context5['catch'](0),_context5.abrupt('return',error(_context5.t0));case 12:case'end':return _context5.stop();}},_callee5,this,[[0,9]])}));return function(){return _ref9.apply(this,arguments)}}()},{key:'isAuth',get:function(){return!!this.headers.Authorization}}]),Kitsu}();return Kitsu}); | ||
'use strict';function _interopDefault(ex){return ex&&'object'==typeof ex&&'default'in ex?ex['default']:ex}var _regeneratorRuntime=_interopDefault(require('babel-runtime/regenerator')),_asyncToGenerator=_interopDefault(require('babel-runtime/helpers/asyncToGenerator')),_slicedToArray=_interopDefault(require('babel-runtime/helpers/slicedToArray')),_classCallCheck=_interopDefault(require('babel-runtime/helpers/classCallCheck')),_createClass=_interopDefault(require('babel-runtime/helpers/createClass')),axios=_interopDefault(require('axios')),deattribute=function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee2(data){var _this=this;return _regeneratorRuntime.wrap(function(_context2){for(;;)switch(_context2.prev=_context2.next){case 0:if('object'!=typeof data||null===data){_context2.next=7;break}if(!Array.isArray(data)){_context2.next=6;break}return _context2.next=4,data.map(function(){var _ref2=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(el){return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:return _context.abrupt('return',deattribute(el));case 1:case'end':return _context.stop();}},_callee,_this)}));return function(){return _ref2.apply(this,arguments)}}());case 4:_context2.next=7;break;case 6:data.attributes&&data.attributes.constructor===Object&&(Object.keys(data.attributes).forEach(function(key){data[key]=data.attributes[key]}),delete data.attributes);case 7:return _context2.abrupt('return',data);case 8:case'end':return _context2.stop();}},_callee2,this)}));return function(){return _ref.apply(this,arguments)}}(),deserialiseArray=function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(obj){var _iteratorNormalCompletion,_didIteratorError,_iteratorError,_iterator,_step,value;return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:return _iteratorNormalCompletion=!0,_didIteratorError=!1,_iteratorError=void 0,_context.prev=3,_context.next=6,obj.data;case 6:_context.t0=Symbol.iterator,_iterator=_context.sent[_context.t0]();case 8:if(_iteratorNormalCompletion=(_step=_iterator.next()).done){_context.next=22;break}if(value=_step.value,!obj.included){_context.next=14;break}return _context.next=13,linkRelationships(value,obj.included);case 13:value=_context.sent;case 14:if(!value.attributes){_context.next=18;break}return _context.next=17,deattribute(value);case 17:value=_context.sent;case 18:obj.data[obj.data.indexOf(value)]=value;case 19:_iteratorNormalCompletion=!0,_context.next=8;break;case 22:_context.next=28;break;case 24:_context.prev=24,_context.t1=_context['catch'](3),_didIteratorError=!0,_iteratorError=_context.t1;case 28:_context.prev=28,_context.prev=29,!_iteratorNormalCompletion&&_iterator.return&&_iterator.return();case 31:if(_context.prev=31,!_didIteratorError){_context.next=34;break}throw _iteratorError;case 34:return _context.finish(31);case 35:return _context.finish(28);case 36:return _context.abrupt('return',obj);case 37:case'end':return _context.stop();}},_callee,this,[[3,24,28,36],[29,,31,35]])}));return function(){return _ref.apply(this,arguments)}}(),deserialise=function(){var _ref2=_asyncToGenerator(_regeneratorRuntime.mark(function _callee2(obj){return _regeneratorRuntime.wrap(function(_context2){for(;;)switch(_context2.prev=_context2.next){case 0:if(!(obj.data&&obj.data.constructor===Array)){_context2.next=6;break}return _context2.next=3,deserialiseArray(obj);case 3:obj=_context2.sent,_context2.next=10;break;case 6:if(!obj.included){_context2.next=10;break}return _context2.next=9,linkRelationships(obj.data,obj.included);case 9:obj.data=_context2.sent;case 10:if(delete obj.included,!obj.data.attributes){_context2.next=15;break}return _context2.next=14,deattribute(obj.data);case 14:obj.data=_context2.sent;case 15:return _context2.abrupt('return',obj);case 16:case'end':return _context2.stop();}},_callee2,this)}));return function(){return _ref2.apply(this,arguments)}}();function error(E){if(E.response){var e=E.response.data;if(e&&e.errors)return E.errors=e.errors,E}else if(E.errors)return E;throw E}var filterIncludes=function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(included,_ref2){var id=_ref2.id,type=_ref2.type;return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:return _context.prev=0,_context.abrupt('return',included.filter(function(el){return el.id===id&&el.type===type})[0]||{id,type});case 4:_context.prev=4,_context.t0=_context['catch'](0),error(_context.t0);case 7:case'end':return _context.stop();}},_callee,this,[[0,4]])}));return function(){return _ref.apply(this,arguments)}}(),link=function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(id,type,included){var filtered;return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:return _context.next=2,filterIncludes(included,{id,type});case 2:if(filtered=_context.sent,!filtered.relationships){_context.next=6;break}return _context.next=6,linkRelationships(filtered,included);case 6:return _context.abrupt('return',deattribute(filtered));case 7:case'end':return _context.stop();}},_callee,this)}));return function(){return _ref.apply(this,arguments)}}(),linkArray=function(){var _ref2=_asyncToGenerator(_regeneratorRuntime.mark(function _callee2(data,included,key){var _iteratorNormalCompletion,_didIteratorError,_iteratorError,_iterator,_step,_ref3,id,type;return _regeneratorRuntime.wrap(function(_context2){for(;;)switch(_context2.prev=_context2.next){case 0:return data[key]=[],_iteratorNormalCompletion=!0,_didIteratorError=!1,_iteratorError=void 0,_context2.prev=4,_context2.next=7,data.relationships[key].data;case 7:_context2.t0=Symbol.iterator,_iterator=_context2.sent[_context2.t0]();case 9:if(_iteratorNormalCompletion=(_step=_iterator.next()).done){_context2.next=20;break}return _ref3=_step.value,id=_ref3.id,type=_ref3.type,_context2.t1=data[key],_context2.next=15,link(id,type,included);case 15:_context2.t2=_context2.sent,_context2.t1.push.call(_context2.t1,_context2.t2);case 17:_iteratorNormalCompletion=!0,_context2.next=9;break;case 20:_context2.next=26;break;case 22:_context2.prev=22,_context2.t3=_context2['catch'](4),_didIteratorError=!0,_iteratorError=_context2.t3;case 26:_context2.prev=26,_context2.prev=27,!_iteratorNormalCompletion&&_iterator.return&&_iterator.return();case 29:if(_context2.prev=29,!_didIteratorError){_context2.next=32;break}throw _iteratorError;case 32:return _context2.finish(29);case 33:return _context2.finish(26);case 34:case'end':return _context2.stop();}},_callee2,this,[[4,22,26,34],[27,,29,33]])}));return function(){return _ref2.apply(this,arguments)}}(),linkObject=function(){var _ref4=_asyncToGenerator(_regeneratorRuntime.mark(function _callee3(data,included,key){var _data$relationships$k,id,type;return _regeneratorRuntime.wrap(function(_context3){for(;;)switch(_context3.prev=_context3.next){case 0:return _data$relationships$k=data.relationships[key].data,id=_data$relationships$k.id,type=_data$relationships$k.type,_context3.next=3,link(id,type,included);case 3:data[key]=_context3.sent,delete data[key].relationships;case 5:case'end':return _context3.stop();}},_callee3,this)}));return function(){return _ref4.apply(this,arguments)}}(),linkRelationships=function(){var _ref5=_asyncToGenerator(_regeneratorRuntime.mark(function _callee4(data,included){var relationships,removeRelationships,key;return _regeneratorRuntime.wrap(function(_context4){for(;;)switch(_context4.prev=_context4.next){case 0:return relationships=data.relationships,removeRelationships=!1,_context4.next=4,relationships;case 4:_context4.t0=_regeneratorRuntime.keys(_context4.sent);case 5:if((_context4.t1=_context4.t0()).done){_context4.next=19;break}if(key=_context4.t1.value,!(relationships[key].data&&Array.isArray(relationships[key].data))){_context4.next=13;break}return _context4.next=10,linkArray(data,included,key);case 10:removeRelationships=!0,_context4.next=17;break;case 13:if(!relationships[key].data){_context4.next=17;break}return _context4.next=16,linkObject(data,included,key);case 16:removeRelationships=!0;case 17:_context4.next=5;break;case 19:return removeRelationships&&delete data.relationships,_context4.abrupt('return',data);case 21:case'end':return _context4.stop();}},_callee4,this)}));return function(){return _ref5.apply(this,arguments)}}();function query(params){var query='',_loop=function(param){'object'==typeof params[param]?Object.keys(params[param]).forEach(function(value){query+=`&${param}[${value}]=${params[param][value]}`}):query+=`&${param}=${params[param]}`};for(var param in params)_loop(param);return params?query.slice(1):''}var serialise=function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(model){var type,data,_loop,prop,_this=this,obj=1<arguments.length&&arguments[1]!==void 0?arguments[1]:{},method=2<arguments.length&&arguments[2]!==void 0?arguments[2]:'POST';return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:if(_context.prev=0,obj.constructor===Object&&0!==Object.keys(obj).length){_context.next=3;break}throw new Error(`${method} requires a JSON object body`);case 3:if(type=this.plural(this.camel(model)),data={type},'POST'===method||'undefined'!=typeof obj.id){_context.next=7;break}throw new Error(`${method} requires an ID for the ${type} type`);case 7:for(prop in'POST'!==method&&(data.id=obj.id.toString()),_loop=function(prop){if(null!==obj[prop]&&obj[prop].constructor===Object){if('string'==typeof obj[prop].id)'undefined'==typeof data.relationships&&(data.relationships={}),'undefined'==typeof obj[prop].type&&(obj[prop].type=_this.plural(_this.camel(prop))),data.relationships[prop]={data:Object.assign(obj[prop])};else throw new Error(`${method} requires an ID for the ${prop} relationships`);}else if(null!==obj[prop]&&Array.isArray(obj[prop])){var ptype=_this.plural(_this.camel(prop));'undefined'==typeof data.relationships&&(data.relationships={}),data.relationships[prop]={data:obj[prop].map(function(elem){if('undefined'==typeof elem.id)throw new Error(`${method} requires an ID for the ${prop} relationships`);return{id:elem.id,type:elem.type||ptype}})}}else'id'!==prop&&'type'!==prop&&('undefined'==typeof data.attributes&&(data.attributes={}),data.attributes[prop]=obj[prop])},obj)_loop(prop);return _context.abrupt('return',{data});case 13:throw _context.prev=13,_context.t0=_context['catch'](0),_context.t0;case 16:case'end':return _context.stop();}},_callee,this,[[0,13]])}));return function(){return _ref.apply(this,arguments)}}(),camel=function(s){return s.replace(/[-_][a-z\u00E0-\u00F6\u00F8-\u00FE]/g,function(match){return match.slice(1).toUpperCase()})},kebab=function(s){return s.charAt(0).toLowerCase()+s.slice(1).replace(/[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g,function(match){return'-'+match.toLowerCase()})},snake=function(s){return s.charAt(0).toLowerCase()+s.slice(1).replace(/[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g,function(match){return'_'+match.toLowerCase()})},Kitsu=function(){function Kitsu(){var options=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{};_classCallCheck(this,Kitsu),this.fetch=this.get,this.update=this.patch,this.create=this.post,this.camel=!1===options.camelCaseTypes?function(s){return s}:camel,this.resCase='none'===options.resourceCase?function(s){return s}:'snake'===options.resourceCase?snake:kebab,this.plural=!1===options.pluralize?function(s){return s}:require('pluralize'),this.headers=Object.assign({},options.headers,{Accept:'application/vnd.api+json',"Content-Type":'application/vnd.api+json'}),this.axios=axios.create({baseURL:options.baseURL||'https://kitsu.io/api/edge',timeout:options.timeout||3e4})}return _createClass(Kitsu,[{key:'get',value:function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(model){var _model$split,_model$split2,res,id,relationship,url,_ref2,data,params=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},headers=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:return _context.prev=0,_model$split=model.split('/'),_model$split2=_slicedToArray(_model$split,3),res=_model$split2[0],id=_model$split2[1],relationship=_model$split2[2],url=this.plural(this.resCase(res)),id&&(url+=`/${id}`),relationship&&(url+=`/${this.resCase(relationship)}`),_context.next=7,this.axios.get(url,{params,paramsSerializer:function(p){return query(p)},headers:Object.assign(this.headers,headers)});case 7:return _ref2=_context.sent,data=_ref2.data,_context.abrupt('return',deserialise(data));case 12:throw _context.prev=12,_context.t0=_context['catch'](0),error(_context.t0);case 15:case'end':return _context.stop();}},_callee,this,[[0,12]])}));return function(){return _ref.apply(this,arguments)}}()},{key:'patch',value:function(){var _ref3=_asyncToGenerator(_regeneratorRuntime.mark(function _callee2(model,body){var url,_ref4,data,headers=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};return _regeneratorRuntime.wrap(function(_context2){for(;;)switch(_context2.prev=_context2.next){case 0:if(_context2.prev=0,headers=Object.assign(this.headers,headers),this.isAuth){_context2.next=4;break}throw new Error('Not logged in');case 4:if('undefined'!=typeof body.id){_context2.next=6;break}throw new Error('Updating a resource requires an ID');case 6:return url=this.plural(this.resCase(model))+'/'+body.id,_context2.t0=this.axios,_context2.t1=url,_context2.next=11,serialise.apply(this,[model,body,'PATCH']);case 11:return _context2.t2=_context2.sent,_context2.t3={headers},_context2.next=15,_context2.t0.patch.call(_context2.t0,_context2.t1,_context2.t2,_context2.t3);case 15:return _ref4=_context2.sent,data=_ref4.data,_context2.abrupt('return',data);case 20:throw _context2.prev=20,_context2.t4=_context2['catch'](0),error(_context2.t4);case 23:case'end':return _context2.stop();}},_callee2,this,[[0,20]])}));return function(){return _ref3.apply(this,arguments)}}()},{key:'post',value:function(){var _ref5=_asyncToGenerator(_regeneratorRuntime.mark(function _callee3(model,body){var url,_ref6,data,headers=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};return _regeneratorRuntime.wrap(function(_context3){for(;;)switch(_context3.prev=_context3.next){case 0:if(_context3.prev=0,headers=Object.assign(this.headers,headers),this.isAuth){_context3.next=4;break}throw new Error('Not logged in');case 4:return url=this.plural(this.resCase(model)),_context3.t0=this.axios,_context3.t1=url,_context3.next=9,serialise.apply(this,[model,body]);case 9:return _context3.t2=_context3.sent,_context3.t3={headers},_context3.next=13,_context3.t0.post.call(_context3.t0,_context3.t1,_context3.t2,_context3.t3);case 13:return _ref6=_context3.sent,data=_ref6.data,_context3.abrupt('return',data);case 18:throw _context3.prev=18,_context3.t4=_context3['catch'](0),error(_context3.t4);case 21:case'end':return _context3.stop();}},_callee3,this,[[0,18]])}));return function(){return _ref5.apply(this,arguments)}}()},{key:'remove',value:function(){var _ref7=_asyncToGenerator(_regeneratorRuntime.mark(function _callee4(model,id){var url,_ref8,data,headers=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};return _regeneratorRuntime.wrap(function(_context4){for(;;)switch(_context4.prev=_context4.next){case 0:if(_context4.prev=0,headers=Object.assign(this.headers,headers),this.isAuth){_context4.next=4;break}throw new Error('Not logged in');case 4:return url=this.plural(this.resCase(model))+'/'+id,_context4.t0=this.axios,_context4.t1=url,_context4.next=9,serialise.apply(this,[model,{id},'DELETE']);case 9:return _context4.t2=_context4.sent,_context4.t3=headers,_context4.t4={data:_context4.t2,headers:_context4.t3},_context4.next=14,_context4.t0.delete.call(_context4.t0,_context4.t1,_context4.t4);case 14:return _ref8=_context4.sent,data=_ref8.data,_context4.abrupt('return',data);case 19:throw _context4.prev=19,_context4.t5=_context4['catch'](0),error(_context4.t5);case 22:case'end':return _context4.stop();}},_callee4,this,[[0,19]])}));return function(){return _ref7.apply(this,arguments)}}()},{key:'self',value:function(){var _ref9=_asyncToGenerator(_regeneratorRuntime.mark(function _callee5(){var res,params=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},headers=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};return _regeneratorRuntime.wrap(function(_context5){for(;;)switch(_context5.prev=_context5.next){case 0:return _context5.prev=0,_context5.next=3,this.get('users',Object.assign({filter:{self:!0}},params),headers);case 3:return res=_context5.sent,_context5.abrupt('return',res.data[0]);case 7:throw _context5.prev=7,_context5.t0=_context5['catch'](0),error(_context5.t0);case 10:case'end':return _context5.stop();}},_callee5,this,[[0,7]])}));return function(){return _ref9.apply(this,arguments)}}()},{key:'isAuth',get:function(){return!!this.headers.Authorization}}]),Kitsu}();module.exports=Kitsu; |
@@ -1,1 +0,1 @@ | ||
(function(global,factory){'object'==typeof exports&&'undefined'!=typeof module?module.exports=factory(require('babel-runtime/regenerator'),require('babel-runtime/helpers/slicedToArray'),require('babel-runtime/helpers/asyncToGenerator'),require('babel-runtime/helpers/classCallCheck'),require('babel-runtime/helpers/createClass'),require('axios'),require('babel-runtime/helpers/typeof')):'function'==typeof define&&define.amd?define(['babel-runtime/regenerator','babel-runtime/helpers/slicedToArray','babel-runtime/helpers/asyncToGenerator','babel-runtime/helpers/classCallCheck','babel-runtime/helpers/createClass','axios','babel-runtime/helpers/typeof'],factory):global.Kitsu=factory(global._regeneratorRuntime,global._slicedToArray,global._asyncToGenerator,global._classCallCheck,global._createClass,global.axios,global._typeof)})(this,function(_regeneratorRuntime,_slicedToArray,_asyncToGenerator,_classCallCheck,_createClass,axios,_typeof){'use strict';function error(E){if(E.response){var e=E.response.data;if(e)return e}else if(E.errors)return E;throw E}function query(params){try{var _query='',_loop=function(param){'object'===_typeof(params[param])?Object.keys(params[param]).forEach(function(value){_query+='&'+param+'['+value+']='+params[param][value]}):'string'==typeof params[param]&&(_query+='&'+param+'='+params[param])};for(var param in params)_loop(param);return params?_query.slice(1):''}catch(E){error(E)}}_regeneratorRuntime=_regeneratorRuntime&&_regeneratorRuntime.hasOwnProperty('default')?_regeneratorRuntime['default']:_regeneratorRuntime,_slicedToArray=_slicedToArray&&_slicedToArray.hasOwnProperty('default')?_slicedToArray['default']:_slicedToArray,_asyncToGenerator=_asyncToGenerator&&_asyncToGenerator.hasOwnProperty('default')?_asyncToGenerator['default']:_asyncToGenerator,_classCallCheck=_classCallCheck&&_classCallCheck.hasOwnProperty('default')?_classCallCheck['default']:_classCallCheck,_createClass=_createClass&&_createClass.hasOwnProperty('default')?_createClass['default']:_createClass,axios=axios&&axios.hasOwnProperty('default')?axios['default']:axios,_typeof=_typeof&&_typeof.hasOwnProperty('default')?_typeof['default']:_typeof;var deattribute=function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee2(data){var _this=this;return _regeneratorRuntime.wrap(function(_context2){for(;;)switch(_context2.prev=_context2.next){case 0:if(_context2.prev=0,'object'!==('undefined'==typeof data?'undefined':_typeof(data))||null===data){_context2.next=8;break}if(!Array.isArray(data)){_context2.next=7;break}return _context2.next=5,data.map(function(){var _ref2=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(el){return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:return _context.abrupt('return',deattribute(el));case 1:case'end':return _context.stop();}},_callee,_this)}));return function(){return _ref2.apply(this,arguments)}}());case 5:_context2.next=8;break;case 7:data.attributes&&data.attributes.constructor===Object&&(Object.keys(data.attributes).forEach(function(key){data[key]=data.attributes[key]}),delete data.attributes);case 8:return _context2.abrupt('return',data);case 11:_context2.prev=11,_context2.t0=_context2['catch'](0),error(_context2.t0);case 14:case'end':return _context2.stop();}},_callee2,this,[[0,11]])}));return function(){return _ref.apply(this,arguments)}}(),deserialiseArray=function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(obj){var _iteratorNormalCompletion,_didIteratorError,_iteratorError,_iterator,_step,value;return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:return _context.prev=0,_iteratorNormalCompletion=!0,_didIteratorError=!1,_iteratorError=void 0,_context.prev=4,_context.next=7,obj.data;case 7:_context.t0=Symbol.iterator,_iterator=_context.sent[_context.t0]();case 9:if(_iteratorNormalCompletion=(_step=_iterator.next()).done){_context.next=23;break}if(value=_step.value,!obj.included){_context.next=15;break}return _context.next=14,linkRelationships(value,obj.included);case 14:value=_context.sent;case 15:if(!value.attributes){_context.next=19;break}return _context.next=18,deattribute(value);case 18:value=_context.sent;case 19:obj.data[obj.data.indexOf(value)]=value;case 20:_iteratorNormalCompletion=!0,_context.next=9;break;case 23:_context.next=29;break;case 25:_context.prev=25,_context.t1=_context['catch'](4),_didIteratorError=!0,_iteratorError=_context.t1;case 29:_context.prev=29,_context.prev=30,!_iteratorNormalCompletion&&_iterator.return&&_iterator.return();case 32:if(_context.prev=32,!_didIteratorError){_context.next=35;break}throw _iteratorError;case 35:return _context.finish(32);case 36:return _context.finish(29);case 37:return _context.abrupt('return',obj);case 40:_context.prev=40,_context.t2=_context['catch'](0),error(_context.t2);case 43:case'end':return _context.stop();}},_callee,this,[[0,40],[4,25,29,37],[30,,32,36]])}));return function(){return _ref.apply(this,arguments)}}(),deserialise=function(){var _ref2=_asyncToGenerator(_regeneratorRuntime.mark(function _callee2(obj){return _regeneratorRuntime.wrap(function(_context2){for(;;)switch(_context2.prev=_context2.next){case 0:if(_context2.prev=0,!(obj.data&&obj.data.constructor===Array)){_context2.next=7;break}return _context2.next=4,deserialiseArray(obj);case 4:obj=_context2.sent,_context2.next=11;break;case 7:if(!obj.included){_context2.next=11;break}return _context2.next=10,linkRelationships(obj.data,obj.included);case 10:obj.data=_context2.sent;case 11:if(delete obj.included,!obj.data.attributes){_context2.next=16;break}return _context2.next=15,deattribute(obj.data);case 15:obj.data=_context2.sent;case 16:return _context2.abrupt('return',obj);case 19:_context2.prev=19,_context2.t0=_context2['catch'](0),error(_context2.t0);case 22:case'end':return _context2.stop();}},_callee2,this,[[0,19]])}));return function(){return _ref2.apply(this,arguments)}}(),filterIncludes=function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(included,_ref2){var id=_ref2.id,type=_ref2.type;return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:return _context.prev=0,_context.abrupt('return',included.filter(function(el){return el.id===id&&el.type===type})[0]||{id:id,type:type});case 4:_context.prev=4,_context.t0=_context['catch'](0),error(_context.t0);case 7:case'end':return _context.stop();}},_callee,this,[[0,4]])}));return function(){return _ref.apply(this,arguments)}}(),linkRelationships=function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(data,included){var removeRelationshipsAttribute,relationships,key,_iteratorNormalCompletion,_didIteratorError,_iteratorError,_iterator,_step,_ref2,id,type,filtered,deattributed,_relationships$key$da,_id,_type,_filtered,_deattributed;return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:return removeRelationshipsAttribute=!1,_context.prev=1,relationships=data.relationships,_context.next=5,relationships;case 5:_context.t0=_regeneratorRuntime.keys(_context.sent);case 6:if((_context.t1=_context.t0()).done){_context.next=66;break}if(key=_context.t1.value,!(relationships[key].data&&Array.isArray(relationships[key].data))){_context.next=50;break}return _iteratorNormalCompletion=!0,_didIteratorError=!1,_iteratorError=void 0,_context.prev=12,_context.next=15,relationships[key].data;case 15:_context.t2=Symbol.iterator,_iterator=_context.sent[_context.t2]();case 17:if(_iteratorNormalCompletion=(_step=_iterator.next()).done){_context.next=33;break}return _ref2=_step.value,id=_ref2.id,type=_ref2.type,_context.next=22,filterIncludes(included,{id:id,type:type});case 22:if(filtered=_context.sent,!filtered.relationships){_context.next=26;break}return _context.next=26,linkRelationships(filtered,included);case 26:return _context.next=28,deattribute(filtered);case 28:deattributed=_context.sent,'undefined'!=typeof deattributed&&(!data[key]&&(data[key]=[]),data[key].push(deattributed));case 30:_iteratorNormalCompletion=!0,_context.next=17;break;case 33:_context.next=39;break;case 35:_context.prev=35,_context.t3=_context['catch'](12),_didIteratorError=!0,_iteratorError=_context.t3;case 39:_context.prev=39,_context.prev=40,!_iteratorNormalCompletion&&_iterator.return&&_iterator.return();case 42:if(_context.prev=42,!_didIteratorError){_context.next=45;break}throw _iteratorError;case 45:return _context.finish(42);case 46:return _context.finish(39);case 47:removeRelationshipsAttribute=!0,_context.next=64;break;case 50:if(!relationships[key].data){_context.next=64;break}return _relationships$key$da=relationships[key].data,_id=_relationships$key$da.id,_type=_relationships$key$da.type,_context.next=54,filterIncludes(included,{id:_id,type:_type});case 54:if(_filtered=_context.sent,!_filtered.relationships){_context.next=58;break}return _context.next=58,linkRelationships(_filtered,included);case 58:return _context.next=60,deattribute(_filtered);case 60:_deattributed=_context.sent,'undefined'==typeof _deattributed||data[key]||(data[key]=_deattributed),delete data[key].relationships,removeRelationshipsAttribute=!0;case 64:_context.next=6;break;case 66:return removeRelationshipsAttribute&&delete data.relationships,_context.abrupt('return',data);case 70:_context.prev=70,_context.t4=_context['catch'](1),error(_context.t4);case 73:case'end':return _context.stop();}},_callee,this,[[1,70],[12,35,39,47],[40,,42,46]])}));return function(){return _ref.apply(this,arguments)}}(),serialise=function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(model){var type,data,_loop,prop,_this=this,obj=1<arguments.length&&arguments[1]!==void 0?arguments[1]:{},method=2<arguments.length&&arguments[2]!==void 0?arguments[2]:'POST';return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:if(_context.prev=0,obj.constructor===Object&&0!==Object.keys(obj).length){_context.next=3;break}throw new Error(method+' requires a JSON object body');case 3:if(type=this.plural(this.camel(model)),data={type:type},'POST'===method||'undefined'!=typeof obj.id){_context.next=7;break}throw new Error(method+' requires an ID for the '+type+' type');case 7:for(prop in'POST'!==method&&(data.id=obj.id.toString()),_loop=function(prop){if(null!==obj[prop]&&obj[prop].constructor===Object){if('string'==typeof obj[prop].id)'undefined'==typeof data.relationships&&(data.relationships={}),'undefined'==typeof obj[prop].type&&(obj[prop].type=_this.plural(_this.camel(prop))),data.relationships[prop]={data:Object.assign(obj[prop])};else throw new Error(method+' requires an ID for the '+prop+' relationships');}else if(null!==obj[prop]&&Array.isArray(obj[prop])){var ptype=_this.plural(_this.camel(prop));'undefined'==typeof data.relationships&&(data.relationships={}),data.relationships[prop]={data:obj[prop].map(function(elem){if('undefined'==typeof elem.id)throw new Error(method+' requires an ID for the '+prop+' relationships');return{id:elem.id,type:elem.type||ptype}})}}else'id'!==prop&&'type'!==prop&&('undefined'==typeof data.attributes&&(data.attributes={}),data.attributes[prop]=obj[prop])},obj)_loop(prop);return _context.abrupt('return',{data:data});case 13:throw _context.prev=13,_context.t0=_context['catch'](0),_context.t0;case 16:case'end':return _context.stop();}},_callee,this,[[0,13]])}));return function(){return _ref.apply(this,arguments)}}(),camel=function(s){return s.replace(/[-_][a-z\u00E0-\u00F6\u00F8-\u00FE]/g,function(match){return match.slice(1).toUpperCase()})},kebab=function(s){return s.charAt(0).toLowerCase()+s.slice(1).replace(/[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g,function(match){return'-'+match.toLowerCase()})},snake=function(s){return s.charAt(0).toLowerCase()+s.slice(1).replace(/[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g,function(match){return'_'+match.toLowerCase()})},jsonAPI='application/vnd.api+json',jsonAPIHeader={Accept:jsonAPI,"Content-Type":jsonAPI},Kitsu=function(){function Kitsu(){var options=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{};_classCallCheck(this,Kitsu),this.fetch=this.get,this.update=this.patch,this.create=this.post,this.camel=!1===options.camelCaseTypes?function(s){return s}:camel,this.resCase='none'===options.resourceCase?function(s){return s}:'snake'===options.resourceCase?snake:kebab,this.plural=!1===options.pluralize?function(s){return s}:require('pluralize'),this.headers=Object.assign({},options.headers,jsonAPIHeader),this.axios=axios.create({baseURL:options.baseURL||'https://kitsu.io/api/edge',timeout:options.timeout||3e4})}return _createClass(Kitsu,[{key:'get',value:function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(model){var _model$split,_model$split2,res,id,relationship,url,_ref2,data,params=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},headers=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:return _context.prev=0,_model$split=model.split('/'),_model$split2=_slicedToArray(_model$split,3),res=_model$split2[0],id=_model$split2[1],relationship=_model$split2[2],url=this.plural(this.resCase(res)),id&&(url+='/'+id),relationship&&(url+='/'+this.resCase(relationship)),_context.next=7,this.axios.get(url,{params:params,paramsSerializer:function(p){return query(p)},headers:Object.assign(this.headers,headers,jsonAPIHeader)});case 7:return _ref2=_context.sent,data=_ref2.data,_context.abrupt('return',deserialise(data));case 12:return _context.prev=12,_context.t0=_context['catch'](0),_context.abrupt('return',error(_context.t0));case 15:case'end':return _context.stop();}},_callee,this,[[0,12]])}));return function(){return _ref.apply(this,arguments)}}()},{key:'patch',value:function(){var _ref3=_asyncToGenerator(_regeneratorRuntime.mark(function _callee2(model,body){var url,_ref4,data,headers=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};return _regeneratorRuntime.wrap(function(_context2){for(;;)switch(_context2.prev=_context2.next){case 0:if(_context2.prev=0,headers=Object.assign(this.headers,headers,jsonAPIHeader),this.isAuth){_context2.next=4;break}throw new Error('Not logged in');case 4:if('undefined'!=typeof body.id){_context2.next=6;break}throw new Error('Updating a resource requires an ID');case 6:return url=this.plural(this.resCase(model))+'/'+body.id,_context2.t0=this.axios,_context2.t1=url,_context2.next=11,serialise.apply(this,[model,body,'PATCH']);case 11:return _context2.t2=_context2.sent,_context2.t3={headers:headers},_context2.next=15,_context2.t0.patch.call(_context2.t0,_context2.t1,_context2.t2,_context2.t3);case 15:return _ref4=_context2.sent,data=_ref4.data,_context2.abrupt('return',data);case 20:return _context2.prev=20,_context2.t4=_context2['catch'](0),_context2.abrupt('return',error(_context2.t4));case 23:case'end':return _context2.stop();}},_callee2,this,[[0,20]])}));return function(){return _ref3.apply(this,arguments)}}()},{key:'post',value:function(){var _ref5=_asyncToGenerator(_regeneratorRuntime.mark(function _callee3(model,body){var url,_ref6,data,headers=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};return _regeneratorRuntime.wrap(function(_context3){for(;;)switch(_context3.prev=_context3.next){case 0:if(_context3.prev=0,headers=Object.assign(this.headers,headers,jsonAPIHeader),this.isAuth){_context3.next=4;break}throw new Error('Not logged in');case 4:return url=this.plural(this.resCase(model)),_context3.t0=this.axios,_context3.t1=url,_context3.next=9,serialise.apply(this,[model,body]);case 9:return _context3.t2=_context3.sent,_context3.t3={headers:headers},_context3.next=13,_context3.t0.post.call(_context3.t0,_context3.t1,_context3.t2,_context3.t3);case 13:return _ref6=_context3.sent,data=_ref6.data,_context3.abrupt('return',data);case 18:return _context3.prev=18,_context3.t4=_context3['catch'](0),_context3.abrupt('return',error(_context3.t4));case 21:case'end':return _context3.stop();}},_callee3,this,[[0,18]])}));return function(){return _ref5.apply(this,arguments)}}()},{key:'remove',value:function(){var _ref7=_asyncToGenerator(_regeneratorRuntime.mark(function _callee4(model,id){var url,_ref8,data,headers=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};return _regeneratorRuntime.wrap(function(_context4){for(;;)switch(_context4.prev=_context4.next){case 0:if(_context4.prev=0,headers=Object.assign(this.headers,headers,jsonAPIHeader),this.isAuth){_context4.next=4;break}throw new Error('Not logged in');case 4:return url=this.plural(this.resCase(model))+'/'+id,_context4.t0=this.axios,_context4.t1=url,_context4.next=9,serialise.apply(this,[model,{id:id},'DELETE']);case 9:return _context4.t2=_context4.sent,_context4.t3=headers,_context4.t4={data:_context4.t2,headers:_context4.t3},_context4.next=14,_context4.t0.delete.call(_context4.t0,_context4.t1,_context4.t4);case 14:return _ref8=_context4.sent,data=_ref8.data,_context4.abrupt('return',data);case 19:return _context4.prev=19,_context4.t5=_context4['catch'](0),_context4.abrupt('return',error(_context4.t5));case 22:case'end':return _context4.stop();}},_callee4,this,[[0,19]])}));return function(){return _ref7.apply(this,arguments)}}()},{key:'self',value:function(){var _ref9=_asyncToGenerator(_regeneratorRuntime.mark(function _callee5(){var res,params=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},headers=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};return _regeneratorRuntime.wrap(function(_context5){for(;;)switch(_context5.prev=_context5.next){case 0:return _context5.prev=0,_context5.next=3,this.get('users',Object.assign({filter:{self:!0}},params),headers);case 3:if(res=_context5.sent,!res.errors){_context5.next=6;break}throw res;case 6:return _context5.abrupt('return',res.data[0]);case 9:return _context5.prev=9,_context5.t0=_context5['catch'](0),_context5.abrupt('return',error(_context5.t0));case 12:case'end':return _context5.stop();}},_callee5,this,[[0,9]])}));return function(){return _ref9.apply(this,arguments)}}()},{key:'isAuth',get:function(){return!!this.headers.Authorization}}]),Kitsu}();return Kitsu}); | ||
'use strict';function _interopDefault(ex){return ex&&'object'==typeof ex&&'default'in ex?ex['default']:ex}var _regeneratorRuntime=_interopDefault(require('babel-runtime/regenerator')),_typeof=_interopDefault(require('babel-runtime/helpers/typeof')),_asyncToGenerator=_interopDefault(require('babel-runtime/helpers/asyncToGenerator')),_slicedToArray=_interopDefault(require('babel-runtime/helpers/slicedToArray')),_classCallCheck=_interopDefault(require('babel-runtime/helpers/classCallCheck')),_createClass=_interopDefault(require('babel-runtime/helpers/createClass')),axios=_interopDefault(require('axios')),deattribute=function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee2(data){var _this=this;return _regeneratorRuntime.wrap(function(_context2){for(;;)switch(_context2.prev=_context2.next){case 0:if('object'!==('undefined'==typeof data?'undefined':_typeof(data))||null===data){_context2.next=7;break}if(!Array.isArray(data)){_context2.next=6;break}return _context2.next=4,data.map(function(){var _ref2=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(el){return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:return _context.abrupt('return',deattribute(el));case 1:case'end':return _context.stop();}},_callee,_this)}));return function(){return _ref2.apply(this,arguments)}}());case 4:_context2.next=7;break;case 6:data.attributes&&data.attributes.constructor===Object&&(Object.keys(data.attributes).forEach(function(key){data[key]=data.attributes[key]}),delete data.attributes);case 7:return _context2.abrupt('return',data);case 8:case'end':return _context2.stop();}},_callee2,this)}));return function(){return _ref.apply(this,arguments)}}(),deserialiseArray=function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(obj){var _iteratorNormalCompletion,_didIteratorError,_iteratorError,_iterator,_step,value;return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:return _iteratorNormalCompletion=!0,_didIteratorError=!1,_iteratorError=void 0,_context.prev=3,_context.next=6,obj.data;case 6:_context.t0=Symbol.iterator,_iterator=_context.sent[_context.t0]();case 8:if(_iteratorNormalCompletion=(_step=_iterator.next()).done){_context.next=22;break}if(value=_step.value,!obj.included){_context.next=14;break}return _context.next=13,linkRelationships(value,obj.included);case 13:value=_context.sent;case 14:if(!value.attributes){_context.next=18;break}return _context.next=17,deattribute(value);case 17:value=_context.sent;case 18:obj.data[obj.data.indexOf(value)]=value;case 19:_iteratorNormalCompletion=!0,_context.next=8;break;case 22:_context.next=28;break;case 24:_context.prev=24,_context.t1=_context['catch'](3),_didIteratorError=!0,_iteratorError=_context.t1;case 28:_context.prev=28,_context.prev=29,!_iteratorNormalCompletion&&_iterator.return&&_iterator.return();case 31:if(_context.prev=31,!_didIteratorError){_context.next=34;break}throw _iteratorError;case 34:return _context.finish(31);case 35:return _context.finish(28);case 36:return _context.abrupt('return',obj);case 37:case'end':return _context.stop();}},_callee,this,[[3,24,28,36],[29,,31,35]])}));return function(){return _ref.apply(this,arguments)}}(),deserialise=function(){var _ref2=_asyncToGenerator(_regeneratorRuntime.mark(function _callee2(obj){return _regeneratorRuntime.wrap(function(_context2){for(;;)switch(_context2.prev=_context2.next){case 0:if(!(obj.data&&obj.data.constructor===Array)){_context2.next=6;break}return _context2.next=3,deserialiseArray(obj);case 3:obj=_context2.sent,_context2.next=10;break;case 6:if(!obj.included){_context2.next=10;break}return _context2.next=9,linkRelationships(obj.data,obj.included);case 9:obj.data=_context2.sent;case 10:if(delete obj.included,!obj.data.attributes){_context2.next=15;break}return _context2.next=14,deattribute(obj.data);case 14:obj.data=_context2.sent;case 15:return _context2.abrupt('return',obj);case 16:case'end':return _context2.stop();}},_callee2,this)}));return function(){return _ref2.apply(this,arguments)}}();function error(E){if(E.response){var e=E.response.data;if(e&&e.errors)return E.errors=e.errors,E}else if(E.errors)return E;throw E}var filterIncludes=function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(included,_ref2){var id=_ref2.id,type=_ref2.type;return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:return _context.prev=0,_context.abrupt('return',included.filter(function(el){return el.id===id&&el.type===type})[0]||{id:id,type:type});case 4:_context.prev=4,_context.t0=_context['catch'](0),error(_context.t0);case 7:case'end':return _context.stop();}},_callee,this,[[0,4]])}));return function(){return _ref.apply(this,arguments)}}(),link=function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(id,type,included){var filtered;return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:return _context.next=2,filterIncludes(included,{id:id,type:type});case 2:if(filtered=_context.sent,!filtered.relationships){_context.next=6;break}return _context.next=6,linkRelationships(filtered,included);case 6:return _context.abrupt('return',deattribute(filtered));case 7:case'end':return _context.stop();}},_callee,this)}));return function(){return _ref.apply(this,arguments)}}(),linkArray=function(){var _ref2=_asyncToGenerator(_regeneratorRuntime.mark(function _callee2(data,included,key){var _iteratorNormalCompletion,_didIteratorError,_iteratorError,_iterator,_step,_ref3,id,type;return _regeneratorRuntime.wrap(function(_context2){for(;;)switch(_context2.prev=_context2.next){case 0:return data[key]=[],_iteratorNormalCompletion=!0,_didIteratorError=!1,_iteratorError=void 0,_context2.prev=4,_context2.next=7,data.relationships[key].data;case 7:_context2.t0=Symbol.iterator,_iterator=_context2.sent[_context2.t0]();case 9:if(_iteratorNormalCompletion=(_step=_iterator.next()).done){_context2.next=20;break}return _ref3=_step.value,id=_ref3.id,type=_ref3.type,_context2.t1=data[key],_context2.next=15,link(id,type,included);case 15:_context2.t2=_context2.sent,_context2.t1.push.call(_context2.t1,_context2.t2);case 17:_iteratorNormalCompletion=!0,_context2.next=9;break;case 20:_context2.next=26;break;case 22:_context2.prev=22,_context2.t3=_context2['catch'](4),_didIteratorError=!0,_iteratorError=_context2.t3;case 26:_context2.prev=26,_context2.prev=27,!_iteratorNormalCompletion&&_iterator.return&&_iterator.return();case 29:if(_context2.prev=29,!_didIteratorError){_context2.next=32;break}throw _iteratorError;case 32:return _context2.finish(29);case 33:return _context2.finish(26);case 34:case'end':return _context2.stop();}},_callee2,this,[[4,22,26,34],[27,,29,33]])}));return function(){return _ref2.apply(this,arguments)}}(),linkObject=function(){var _ref4=_asyncToGenerator(_regeneratorRuntime.mark(function _callee3(data,included,key){var _data$relationships$k,id,type;return _regeneratorRuntime.wrap(function(_context3){for(;;)switch(_context3.prev=_context3.next){case 0:return _data$relationships$k=data.relationships[key].data,id=_data$relationships$k.id,type=_data$relationships$k.type,_context3.next=3,link(id,type,included);case 3:data[key]=_context3.sent,delete data[key].relationships;case 5:case'end':return _context3.stop();}},_callee3,this)}));return function(){return _ref4.apply(this,arguments)}}(),linkRelationships=function(){var _ref5=_asyncToGenerator(_regeneratorRuntime.mark(function _callee4(data,included){var relationships,removeRelationships,key;return _regeneratorRuntime.wrap(function(_context4){for(;;)switch(_context4.prev=_context4.next){case 0:return relationships=data.relationships,removeRelationships=!1,_context4.next=4,relationships;case 4:_context4.t0=_regeneratorRuntime.keys(_context4.sent);case 5:if((_context4.t1=_context4.t0()).done){_context4.next=19;break}if(key=_context4.t1.value,!(relationships[key].data&&Array.isArray(relationships[key].data))){_context4.next=13;break}return _context4.next=10,linkArray(data,included,key);case 10:removeRelationships=!0,_context4.next=17;break;case 13:if(!relationships[key].data){_context4.next=17;break}return _context4.next=16,linkObject(data,included,key);case 16:removeRelationships=!0;case 17:_context4.next=5;break;case 19:return removeRelationships&&delete data.relationships,_context4.abrupt('return',data);case 21:case'end':return _context4.stop();}},_callee4,this)}));return function(){return _ref5.apply(this,arguments)}}();function query(params){var query='',_loop=function(param){'object'===_typeof(params[param])?Object.keys(params[param]).forEach(function(value){query+='&'+param+'['+value+']='+params[param][value]}):query+='&'+param+'='+params[param]};for(var param in params)_loop(param);return params?query.slice(1):''}var serialise=function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(model){var type,data,_loop,prop,_this=this,obj=1<arguments.length&&arguments[1]!==void 0?arguments[1]:{},method=2<arguments.length&&arguments[2]!==void 0?arguments[2]:'POST';return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:if(_context.prev=0,obj.constructor===Object&&0!==Object.keys(obj).length){_context.next=3;break}throw new Error(method+' requires a JSON object body');case 3:if(type=this.plural(this.camel(model)),data={type:type},'POST'===method||'undefined'!=typeof obj.id){_context.next=7;break}throw new Error(method+' requires an ID for the '+type+' type');case 7:for(prop in'POST'!==method&&(data.id=obj.id.toString()),_loop=function(prop){if(null!==obj[prop]&&obj[prop].constructor===Object){if('string'==typeof obj[prop].id)'undefined'==typeof data.relationships&&(data.relationships={}),'undefined'==typeof obj[prop].type&&(obj[prop].type=_this.plural(_this.camel(prop))),data.relationships[prop]={data:Object.assign(obj[prop])};else throw new Error(method+' requires an ID for the '+prop+' relationships');}else if(null!==obj[prop]&&Array.isArray(obj[prop])){var ptype=_this.plural(_this.camel(prop));'undefined'==typeof data.relationships&&(data.relationships={}),data.relationships[prop]={data:obj[prop].map(function(elem){if('undefined'==typeof elem.id)throw new Error(method+' requires an ID for the '+prop+' relationships');return{id:elem.id,type:elem.type||ptype}})}}else'id'!==prop&&'type'!==prop&&('undefined'==typeof data.attributes&&(data.attributes={}),data.attributes[prop]=obj[prop])},obj)_loop(prop);return _context.abrupt('return',{data:data});case 13:throw _context.prev=13,_context.t0=_context['catch'](0),_context.t0;case 16:case'end':return _context.stop();}},_callee,this,[[0,13]])}));return function(){return _ref.apply(this,arguments)}}(),camel=function(s){return s.replace(/[-_][a-z\u00E0-\u00F6\u00F8-\u00FE]/g,function(match){return match.slice(1).toUpperCase()})},kebab=function(s){return s.charAt(0).toLowerCase()+s.slice(1).replace(/[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g,function(match){return'-'+match.toLowerCase()})},snake=function(s){return s.charAt(0).toLowerCase()+s.slice(1).replace(/[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g,function(match){return'_'+match.toLowerCase()})},Kitsu=function(){function Kitsu(){var options=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{};_classCallCheck(this,Kitsu),this.fetch=this.get,this.update=this.patch,this.create=this.post,this.camel=!1===options.camelCaseTypes?function(s){return s}:camel,this.resCase='none'===options.resourceCase?function(s){return s}:'snake'===options.resourceCase?snake:kebab,this.plural=!1===options.pluralize?function(s){return s}:require('pluralize'),this.headers=Object.assign({},options.headers,{Accept:'application/vnd.api+json',"Content-Type":'application/vnd.api+json'}),this.axios=axios.create({baseURL:options.baseURL||'https://kitsu.io/api/edge',timeout:options.timeout||3e4})}return _createClass(Kitsu,[{key:'get',value:function(){var _ref=_asyncToGenerator(_regeneratorRuntime.mark(function _callee(model){var _model$split,_model$split2,res,id,relationship,url,_ref2,data,params=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},headers=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};return _regeneratorRuntime.wrap(function(_context){for(;;)switch(_context.prev=_context.next){case 0:return _context.prev=0,_model$split=model.split('/'),_model$split2=_slicedToArray(_model$split,3),res=_model$split2[0],id=_model$split2[1],relationship=_model$split2[2],url=this.plural(this.resCase(res)),id&&(url+='/'+id),relationship&&(url+='/'+this.resCase(relationship)),_context.next=7,this.axios.get(url,{params:params,paramsSerializer:function(p){return query(p)},headers:Object.assign(this.headers,headers)});case 7:return _ref2=_context.sent,data=_ref2.data,_context.abrupt('return',deserialise(data));case 12:throw _context.prev=12,_context.t0=_context['catch'](0),error(_context.t0);case 15:case'end':return _context.stop();}},_callee,this,[[0,12]])}));return function(){return _ref.apply(this,arguments)}}()},{key:'patch',value:function(){var _ref3=_asyncToGenerator(_regeneratorRuntime.mark(function _callee2(model,body){var url,_ref4,data,headers=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};return _regeneratorRuntime.wrap(function(_context2){for(;;)switch(_context2.prev=_context2.next){case 0:if(_context2.prev=0,headers=Object.assign(this.headers,headers),this.isAuth){_context2.next=4;break}throw new Error('Not logged in');case 4:if('undefined'!=typeof body.id){_context2.next=6;break}throw new Error('Updating a resource requires an ID');case 6:return url=this.plural(this.resCase(model))+'/'+body.id,_context2.t0=this.axios,_context2.t1=url,_context2.next=11,serialise.apply(this,[model,body,'PATCH']);case 11:return _context2.t2=_context2.sent,_context2.t3={headers:headers},_context2.next=15,_context2.t0.patch.call(_context2.t0,_context2.t1,_context2.t2,_context2.t3);case 15:return _ref4=_context2.sent,data=_ref4.data,_context2.abrupt('return',data);case 20:throw _context2.prev=20,_context2.t4=_context2['catch'](0),error(_context2.t4);case 23:case'end':return _context2.stop();}},_callee2,this,[[0,20]])}));return function(){return _ref3.apply(this,arguments)}}()},{key:'post',value:function(){var _ref5=_asyncToGenerator(_regeneratorRuntime.mark(function _callee3(model,body){var url,_ref6,data,headers=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};return _regeneratorRuntime.wrap(function(_context3){for(;;)switch(_context3.prev=_context3.next){case 0:if(_context3.prev=0,headers=Object.assign(this.headers,headers),this.isAuth){_context3.next=4;break}throw new Error('Not logged in');case 4:return url=this.plural(this.resCase(model)),_context3.t0=this.axios,_context3.t1=url,_context3.next=9,serialise.apply(this,[model,body]);case 9:return _context3.t2=_context3.sent,_context3.t3={headers:headers},_context3.next=13,_context3.t0.post.call(_context3.t0,_context3.t1,_context3.t2,_context3.t3);case 13:return _ref6=_context3.sent,data=_ref6.data,_context3.abrupt('return',data);case 18:throw _context3.prev=18,_context3.t4=_context3['catch'](0),error(_context3.t4);case 21:case'end':return _context3.stop();}},_callee3,this,[[0,18]])}));return function(){return _ref5.apply(this,arguments)}}()},{key:'remove',value:function(){var _ref7=_asyncToGenerator(_regeneratorRuntime.mark(function _callee4(model,id){var url,_ref8,data,headers=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};return _regeneratorRuntime.wrap(function(_context4){for(;;)switch(_context4.prev=_context4.next){case 0:if(_context4.prev=0,headers=Object.assign(this.headers,headers),this.isAuth){_context4.next=4;break}throw new Error('Not logged in');case 4:return url=this.plural(this.resCase(model))+'/'+id,_context4.t0=this.axios,_context4.t1=url,_context4.next=9,serialise.apply(this,[model,{id:id},'DELETE']);case 9:return _context4.t2=_context4.sent,_context4.t3=headers,_context4.t4={data:_context4.t2,headers:_context4.t3},_context4.next=14,_context4.t0.delete.call(_context4.t0,_context4.t1,_context4.t4);case 14:return _ref8=_context4.sent,data=_ref8.data,_context4.abrupt('return',data);case 19:throw _context4.prev=19,_context4.t5=_context4['catch'](0),error(_context4.t5);case 22:case'end':return _context4.stop();}},_callee4,this,[[0,19]])}));return function(){return _ref7.apply(this,arguments)}}()},{key:'self',value:function(){var _ref9=_asyncToGenerator(_regeneratorRuntime.mark(function _callee5(){var res,params=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},headers=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};return _regeneratorRuntime.wrap(function(_context5){for(;;)switch(_context5.prev=_context5.next){case 0:return _context5.prev=0,_context5.next=3,this.get('users',Object.assign({filter:{self:!0}},params),headers);case 3:return res=_context5.sent,_context5.abrupt('return',res.data[0]);case 7:throw _context5.prev=7,_context5.t0=_context5['catch'](0),error(_context5.t0);case 10:case'end':return _context5.stop();}},_callee5,this,[[0,7]])}));return function(){return _ref9.apply(this,arguments)}}()},{key:'isAuth',get:function(){return!!this.headers.Authorization}}]),Kitsu}();module.exports=Kitsu; |
@@ -1,1 +0,1 @@ | ||
(function(global,factory){'object'==typeof exports&&'undefined'!=typeof module?module.exports=factory(require('babel-runtime/helpers/slicedToArray'),require('babel-runtime/helpers/asyncToGenerator'),require('axios')):'function'==typeof define&&define.amd?define(['babel-runtime/helpers/slicedToArray','babel-runtime/helpers/asyncToGenerator','axios'],factory):global.Kitsu=factory(global._slicedToArray,global._asyncToGenerator,global.axios)})(this,function(_slicedToArray,_asyncToGenerator,axios){'use strict';function error(E){if(E.response){const e=E.response.data;if(e)return e}else if(E.errors)return E;throw E}function query(params){try{let query='';for(let param in params)'object'==typeof params[param]?Object.keys(params[param]).forEach(value=>{query+=`&${param}[${value}]=${params[param][value]}`}):'string'==typeof params[param]&&(query+=`&${param}=${params[param]}`);return params?query.slice(1):''}catch(E){error(E)}}_slicedToArray=_slicedToArray&&_slicedToArray.hasOwnProperty('default')?_slicedToArray['default']:_slicedToArray,_asyncToGenerator=_asyncToGenerator&&_asyncToGenerator.hasOwnProperty('default')?_asyncToGenerator['default']:_asyncToGenerator,axios=axios&&axios.hasOwnProperty('default')?axios['default']:axios;let deattribute=(()=>{var _ref=_asyncToGenerator(function*(data){try{return'object'==typeof data&&null!==data&&(Array.isArray(data)?yield data.map((()=>{var _ref2=_asyncToGenerator(function*(el){return deattribute(el)});return function(){return _ref2.apply(this,arguments)}})()):data.attributes&&data.attributes.constructor===Object&&(Object.keys(data.attributes).forEach(function(key){data[key]=data.attributes[key]}),delete data.attributes)),data}catch(E){error(E)}});return function(){return _ref.apply(this,arguments)}})(),deserialiseArray=(()=>{var _ref=_asyncToGenerator(function*(obj){try{var _iteratorError,_iteratorNormalCompletion=!0,_didIteratorError=!1;try{for(var _step,_iterator=(yield obj.data)[Symbol.iterator]();!(_iteratorNormalCompletion=(_step=_iterator.next()).done);_iteratorNormalCompletion=!0){let value=_step.value;obj.included&&(value=yield linkRelationships(value,obj.included)),value.attributes&&(value=yield deattribute(value)),obj.data[obj.data.indexOf(value)]=value}}catch(err){_didIteratorError=!0,_iteratorError=err}finally{try{!_iteratorNormalCompletion&&_iterator.return&&_iterator.return()}finally{if(_didIteratorError)throw _iteratorError}}return obj}catch(E){error(E)}});return function(){return _ref.apply(this,arguments)}})(),deserialise=(()=>{var _ref2=_asyncToGenerator(function*(obj){try{return obj.data&&obj.data.constructor===Array?obj=yield deserialiseArray(obj):obj.included&&(obj.data=yield linkRelationships(obj.data,obj.included)),delete obj.included,obj.data.attributes&&(obj.data=yield deattribute(obj.data)),obj}catch(E){error(E)}});return function(){return _ref2.apply(this,arguments)}})(),filterIncludes=(()=>{var _ref=_asyncToGenerator(function*(included,{id,type}){try{return included.filter(function(el){return el.id===id&&el.type===type})[0]||{id,type}}catch(E){error(E)}});return function(){return _ref.apply(this,arguments)}})(),linkRelationships=(()=>{var _ref=_asyncToGenerator(function*(data,included){let removeRelationshipsAttribute=!1;try{const relationships=data.relationships;for(let key in yield relationships)if(relationships[key].data&&Array.isArray(relationships[key].data)){var _iteratorNormalCompletion=!0,_didIteratorError=!1,_iteratorError=void 0;try{for(var _step,_iterator=(yield relationships[key].data)[Symbol.iterator]();!(_iteratorNormalCompletion=(_step=_iterator.next()).done);_iteratorNormalCompletion=!0){let _ref2=_step.value,id=_ref2.id,type=_ref2.type;const filtered=yield filterIncludes(included,{id,type});filtered.relationships&&(yield linkRelationships(filtered,included));const deattributed=yield deattribute(filtered);'undefined'!=typeof deattributed&&(!data[key]&&(data[key]=[]),data[key].push(deattributed))}}catch(err){_didIteratorError=!0,_iteratorError=err}finally{try{!_iteratorNormalCompletion&&_iterator.return&&_iterator.return()}finally{if(_didIteratorError)throw _iteratorError}}removeRelationshipsAttribute=!0}else if(relationships[key].data){var _relationships$key$da=relationships[key].data;const id=_relationships$key$da.id,type=_relationships$key$da.type,filtered=yield filterIncludes(included,{id,type});filtered.relationships&&(yield linkRelationships(filtered,included));const deattributed=yield deattribute(filtered);'undefined'==typeof deattributed||data[key]||(data[key]=deattributed),delete data[key].relationships,removeRelationshipsAttribute=!0}return removeRelationshipsAttribute&&delete data.relationships,data}catch(E){error(E)}});return function(){return _ref.apply(this,arguments)}})(),serialise=(()=>{var _ref=_asyncToGenerator(function*(model,obj={},method='POST'){try{if(obj.constructor!==Object||0===Object.keys(obj).length)throw new Error(`${method} requires a JSON object body`);const type=this.plural(this.camel(model)),data={type};if('POST'!==method&&'undefined'==typeof obj.id)throw new Error(`${method} requires an ID for the ${type} type`);for(let prop in'POST'!==method&&(data.id=obj.id.toString()),obj)if(null!==obj[prop]&&obj[prop].constructor===Object){if('string'==typeof obj[prop].id)'undefined'==typeof data.relationships&&(data.relationships={}),'undefined'==typeof obj[prop].type&&(obj[prop].type=this.plural(this.camel(prop))),data.relationships[prop]={data:Object.assign(obj[prop])};else throw new Error(`${method} requires an ID for the ${prop} relationships`);}else if(null!==obj[prop]&&Array.isArray(obj[prop])){const ptype=this.plural(this.camel(prop));'undefined'==typeof data.relationships&&(data.relationships={}),data.relationships[prop]={data:obj[prop].map(function(elem){if('undefined'==typeof elem.id)throw new Error(`${method} requires an ID for the ${prop} relationships`);return{id:elem.id,type:elem.type||ptype}})}}else'id'!=prop&&'type'!=prop&&('undefined'==typeof data.attributes&&(data.attributes={}),data.attributes[prop]=obj[prop]);return{data}}catch(e){throw e}});return function(){return _ref.apply(this,arguments)}})();var camel=s=>s.replace(/[-_][a-z\u00E0-\u00F6\u00F8-\u00FE]/g,match=>match.slice(1).toUpperCase()),kebab=s=>s.charAt(0).toLowerCase()+s.slice(1).replace(/[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g,match=>'-'+match.toLowerCase()),snake=s=>s.charAt(0).toLowerCase()+s.slice(1).replace(/[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g,match=>'_'+match.toLowerCase());const jsonAPI='application/vnd.api+json',jsonAPIHeader={Accept:jsonAPI,"Content-Type":jsonAPI};class Kitsu{constructor(options={}){this.fetch=this.get,this.update=this.patch,this.create=this.post,this.camel=!1===options.camelCaseTypes?s=>s:camel,this.resCase='none'===options.resourceCase?s=>s:'snake'===options.resourceCase?snake:kebab,this.plural=!1===options.pluralize?s=>s:require('pluralize'),this.headers=Object.assign({},options.headers,jsonAPIHeader),this.axios=axios.create({baseURL:options.baseURL||'https://kitsu.io/api/edge',timeout:options.timeout||3e4})}get isAuth(){return!!this.headers.Authorization}get(model,params={},headers={}){var _this=this;return _asyncToGenerator(function*(){try{var _model$split=model.split('/'),_model$split2=_slicedToArray(_model$split,3);let res=_model$split2[0],id=_model$split2[1],relationship=_model$split2[2],url=_this.plural(_this.resCase(res));id&&(url+=`/${id}`),relationship&&(url+=`/${_this.resCase(relationship)}`);var _ref=yield _this.axios.get(url,{params,paramsSerializer:function(p){return query(p)},headers:Object.assign(_this.headers,headers,jsonAPIHeader)});const data=_ref.data;return deserialise(data)}catch(E){return error(E)}})()}patch(model,body,headers={}){var _this2=this;return _asyncToGenerator(function*(){try{if(headers=Object.assign(_this2.headers,headers,jsonAPIHeader),!_this2.isAuth)throw new Error('Not logged in');if('undefined'==typeof body.id)throw new Error('Updating a resource requires an ID');const url=_this2.plural(_this2.resCase(model))+'/'+body.id;var _ref2=yield _this2.axios.patch(url,(yield serialise.apply(_this2,[model,body,'PATCH'])),{headers});const data=_ref2.data;return data}catch(E){return error(E)}})()}post(model,body,headers={}){var _this3=this;return _asyncToGenerator(function*(){try{if(headers=Object.assign(_this3.headers,headers,jsonAPIHeader),!_this3.isAuth)throw new Error('Not logged in');const url=_this3.plural(_this3.resCase(model));var _ref3=yield _this3.axios.post(url,(yield serialise.apply(_this3,[model,body])),{headers});const data=_ref3.data;return data}catch(E){return error(E)}})()}remove(model,id,headers={}){var _this4=this;return _asyncToGenerator(function*(){try{if(headers=Object.assign(_this4.headers,headers,jsonAPIHeader),!_this4.isAuth)throw new Error('Not logged in');const url=_this4.plural(_this4.resCase(model))+'/'+id;var _ref4=yield _this4.axios.delete(url,{data:yield serialise.apply(_this4,[model,{id},'DELETE']),headers});const data=_ref4.data;return data}catch(E){return error(E)}})()}self(params={},headers={}){var _this5=this;return _asyncToGenerator(function*(){try{const res=yield _this5.get('users',Object.assign({filter:{self:!0}},params),headers);if(res.errors)throw res;return res.data[0]}catch(E){return error(E)}})()}}return Kitsu}); | ||
'use strict';function _interopDefault(ex){return ex&&'object'==typeof ex&&'default'in ex?ex['default']:ex}var _asyncToGenerator=_interopDefault(require('babel-runtime/helpers/asyncToGenerator')),_slicedToArray=_interopDefault(require('babel-runtime/helpers/slicedToArray')),axios=_interopDefault(require('axios'));let deattribute=(()=>{var _ref=_asyncToGenerator(function*(data){return'object'==typeof data&&null!==data&&(Array.isArray(data)?yield data.map((()=>{var _ref2=_asyncToGenerator(function*(el){return deattribute(el)});return function(){return _ref2.apply(this,arguments)}})()):data.attributes&&data.attributes.constructor===Object&&(Object.keys(data.attributes).forEach(function(key){data[key]=data.attributes[key]}),delete data.attributes)),data});return function(){return _ref.apply(this,arguments)}})(),deserialiseArray=(()=>{var _ref=_asyncToGenerator(function*(obj){var _iteratorError,_iteratorNormalCompletion=!0,_didIteratorError=!1;try{for(var _step,_iterator=(yield obj.data)[Symbol.iterator]();!(_iteratorNormalCompletion=(_step=_iterator.next()).done);_iteratorNormalCompletion=!0){let value=_step.value;obj.included&&(value=yield linkRelationships(value,obj.included)),value.attributes&&(value=yield deattribute(value)),obj.data[obj.data.indexOf(value)]=value}}catch(err){_didIteratorError=!0,_iteratorError=err}finally{try{!_iteratorNormalCompletion&&_iterator.return&&_iterator.return()}finally{if(_didIteratorError)throw _iteratorError}}return obj});return function(){return _ref.apply(this,arguments)}})(),deserialise=(()=>{var _ref2=_asyncToGenerator(function*(obj){return obj.data&&obj.data.constructor===Array?obj=yield deserialiseArray(obj):obj.included&&(obj.data=yield linkRelationships(obj.data,obj.included)),delete obj.included,obj.data.attributes&&(obj.data=yield deattribute(obj.data)),obj});return function(){return _ref2.apply(this,arguments)}})();function error(E){if(E.response){const e=E.response.data;if(e&&e.errors)return E.errors=e.errors,E}else if(E.errors)return E;throw E}let filterIncludes=(()=>{var _ref=_asyncToGenerator(function*(included,{id,type}){try{return included.filter(function(el){return el.id===id&&el.type===type})[0]||{id,type}}catch(E){error(E)}});return function(){return _ref.apply(this,arguments)}})(),link=(()=>{var _ref=_asyncToGenerator(function*(id,type,included){const filtered=yield filterIncludes(included,{id,type});return filtered.relationships&&(yield linkRelationships(filtered,included)),deattribute(filtered)});return function(){return _ref.apply(this,arguments)}})(),linkArray=(()=>{var _ref2=_asyncToGenerator(function*(data,included,key){data[key]=[];var _iteratorError,_iteratorNormalCompletion=!0,_didIteratorError=!1;try{for(var _step,_iterator=(yield data.relationships[key].data)[Symbol.iterator]();!(_iteratorNormalCompletion=(_step=_iterator.next()).done);_iteratorNormalCompletion=!0){let _ref3=_step.value,id=_ref3.id,type=_ref3.type;data[key].push((yield link(id,type,included)))}}catch(err){_didIteratorError=!0,_iteratorError=err}finally{try{!_iteratorNormalCompletion&&_iterator.return&&_iterator.return()}finally{if(_didIteratorError)throw _iteratorError}}});return function(){return _ref2.apply(this,arguments)}})(),linkObject=(()=>{var _ref4=_asyncToGenerator(function*(data,included,key){var _data$relationships$k=data.relationships[key].data;const id=_data$relationships$k.id,type=_data$relationships$k.type;data[key]=yield link(id,type,included),delete data[key].relationships});return function(){return _ref4.apply(this,arguments)}})(),linkRelationships=(()=>{var _ref5=_asyncToGenerator(function*(data,included){const relationships=data.relationships;let removeRelationships=!1;for(let key in yield relationships)relationships[key].data&&Array.isArray(relationships[key].data)?(yield linkArray(data,included,key),removeRelationships=!0):relationships[key].data&&(yield linkObject(data,included,key),removeRelationships=!0);return removeRelationships&&delete data.relationships,data});return function(){return _ref5.apply(this,arguments)}})();function query(params){let query='';for(let param in params)'object'==typeof params[param]?Object.keys(params[param]).forEach(value=>{query+=`&${param}[${value}]=${params[param][value]}`}):query+=`&${param}=${params[param]}`;return params?query.slice(1):''}let serialise=(()=>{var _ref=_asyncToGenerator(function*(model,obj={},method='POST'){try{if(obj.constructor!==Object||0===Object.keys(obj).length)throw new Error(`${method} requires a JSON object body`);const type=this.plural(this.camel(model)),data={type};if('POST'!==method&&'undefined'==typeof obj.id)throw new Error(`${method} requires an ID for the ${type} type`);for(let prop in'POST'!==method&&(data.id=obj.id.toString()),obj)if(null!==obj[prop]&&obj[prop].constructor===Object){if('string'==typeof obj[prop].id)'undefined'==typeof data.relationships&&(data.relationships={}),'undefined'==typeof obj[prop].type&&(obj[prop].type=this.plural(this.camel(prop))),data.relationships[prop]={data:Object.assign(obj[prop])};else throw new Error(`${method} requires an ID for the ${prop} relationships`);}else if(null!==obj[prop]&&Array.isArray(obj[prop])){const ptype=this.plural(this.camel(prop));'undefined'==typeof data.relationships&&(data.relationships={}),data.relationships[prop]={data:obj[prop].map(function(elem){if('undefined'==typeof elem.id)throw new Error(`${method} requires an ID for the ${prop} relationships`);return{id:elem.id,type:elem.type||ptype}})}}else'id'!=prop&&'type'!=prop&&('undefined'==typeof data.attributes&&(data.attributes={}),data.attributes[prop]=obj[prop]);return{data}}catch(e){throw e}});return function(){return _ref.apply(this,arguments)}})();var camel=s=>s.replace(/[-_][a-z\u00E0-\u00F6\u00F8-\u00FE]/g,match=>match.slice(1).toUpperCase()),kebab=s=>s.charAt(0).toLowerCase()+s.slice(1).replace(/[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g,match=>'-'+match.toLowerCase()),snake=s=>s.charAt(0).toLowerCase()+s.slice(1).replace(/[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g,match=>'_'+match.toLowerCase());class Kitsu{constructor(options={}){this.fetch=this.get,this.update=this.patch,this.create=this.post,this.camel=!1===options.camelCaseTypes?s=>s:camel,this.resCase='none'===options.resourceCase?s=>s:'snake'===options.resourceCase?snake:kebab,this.plural=!1===options.pluralize?s=>s:require('pluralize'),this.headers=Object.assign({},options.headers,{Accept:'application/vnd.api+json',"Content-Type":'application/vnd.api+json'}),this.axios=axios.create({baseURL:options.baseURL||'https://kitsu.io/api/edge',timeout:options.timeout||3e4})}get isAuth(){return!!this.headers.Authorization}get(model,params={},headers={}){var _this=this;return _asyncToGenerator(function*(){try{var _model$split=model.split('/'),_model$split2=_slicedToArray(_model$split,3);let res=_model$split2[0],id=_model$split2[1],relationship=_model$split2[2],url=_this.plural(_this.resCase(res));id&&(url+=`/${id}`),relationship&&(url+=`/${_this.resCase(relationship)}`);var _ref=yield _this.axios.get(url,{params,paramsSerializer:function(p){return query(p)},headers:Object.assign(_this.headers,headers)});const data=_ref.data;return deserialise(data)}catch(E){throw error(E)}})()}patch(model,body,headers={}){var _this2=this;return _asyncToGenerator(function*(){try{if(headers=Object.assign(_this2.headers,headers),!_this2.isAuth)throw new Error('Not logged in');if('undefined'==typeof body.id)throw new Error('Updating a resource requires an ID');const url=_this2.plural(_this2.resCase(model))+'/'+body.id;var _ref2=yield _this2.axios.patch(url,(yield serialise.apply(_this2,[model,body,'PATCH'])),{headers});const data=_ref2.data;return data}catch(E){throw error(E)}})()}post(model,body,headers={}){var _this3=this;return _asyncToGenerator(function*(){try{if(headers=Object.assign(_this3.headers,headers),!_this3.isAuth)throw new Error('Not logged in');const url=_this3.plural(_this3.resCase(model));var _ref3=yield _this3.axios.post(url,(yield serialise.apply(_this3,[model,body])),{headers});const data=_ref3.data;return data}catch(E){throw error(E)}})()}remove(model,id,headers={}){var _this4=this;return _asyncToGenerator(function*(){try{if(headers=Object.assign(_this4.headers,headers),!_this4.isAuth)throw new Error('Not logged in');const url=_this4.plural(_this4.resCase(model))+'/'+id;var _ref4=yield _this4.axios.delete(url,{data:yield serialise.apply(_this4,[model,{id},'DELETE']),headers});const data=_ref4.data;return data}catch(E){throw error(E)}})()}self(params={},headers={}){var _this5=this;return _asyncToGenerator(function*(){try{const res=yield _this5.get('users',Object.assign({filter:{self:!0}},params),headers);return res.data[0]}catch(E){throw error(E)}})()}}module.exports=Kitsu; |
{ | ||
"name": "kitsu", | ||
"version": "4.4.3", | ||
"version": "5.0.0-beta.1", | ||
"description": "Simple & lightweight JSON-API client for Kitsu and other compliant APIs", | ||
@@ -105,3 +105,3 @@ "license": "MIT", | ||
"js-beautify": "~1.7.4", | ||
"rollup": "~0.54.0", | ||
"rollup": "~0.55.0", | ||
"rollup-plugin-babel": "~3.0.1", | ||
@@ -108,0 +108,0 @@ "rollup-plugin-babel-minify": "~3.1.2", |
@@ -250,2 +250,26 @@ # Kitsu | ||
```javascript | ||
// Handling errors (async/await) | ||
// http://jsonapi.org/format/#error-objects | ||
try { | ||
const { data } = api.get('anime') | ||
} catch (err) { | ||
if (err.errors) err.errors.forEach(error => { | ||
console.log(error) // Prints JSON:API error object | ||
}) | ||
} | ||
``` | ||
```javascript | ||
// Handling errors (Promise) | ||
// http://jsonapi.org/format/#error-objects | ||
api.get('anime') | ||
.then(res => res.data) | ||
.catch(err => { | ||
if (err.errors) err.errors.forEach(error => { | ||
console.log(error) // Prints JSON:API error object | ||
}) | ||
}) | ||
``` | ||
Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** JSON-parsed response | ||
@@ -252,0 +276,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
479
127128
264
4
1