UNPKG

axios-rest-api

Version:

Simple Axios wrapper for simple REST Api requests.

1 lines 17.1 kB
module.exports=function(t){var e={};function r(n){if(e[n])return e[n].exports;var i=e[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,r),i.l=!0,i.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)r.d(n,i,function(e){return t[e]}.bind(null,i));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=13)}([function(t,e){t.exports=require("underscore")},function(t,e){t.exports=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}},function(t,e){function r(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}t.exports=function(t,e,n){return e&&r(t.prototype,e),n&&r(t,n),t}},function(t,e,r){t.exports=r(12)},function(t,e){function r(e){return t.exports=r=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)},r(e)}t.exports=r},function(t,e){function r(t,e,r,n,i,o,u){try{var s=t[o](u),a=s.value}catch(t){return void r(t)}s.done?e(a):Promise.resolve(a).then(n,i)}t.exports=function(t){return function(){var e=this,n=arguments;return new Promise((function(i,o){var u=t.apply(e,n);function s(t){r(u,i,o,s,a,"next",t)}function a(t){r(u,i,o,s,a,"throw",t)}s(void 0)}))}}},function(t,e){t.exports=require("axios")},function(t,e,r){var n=r(9);t.exports=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&n(t,e)}},function(t,e,r){var n=r(10),i=r(11);t.exports=function(t,e){return!e||"object"!==n(e)&&"function"!=typeof e?i(t):e}},function(t,e){function r(e,n){return t.exports=r=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t},r(e,n)}t.exports=r},function(t,e){function r(e){return"function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?t.exports=r=function(t){return typeof t}:t.exports=r=function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},r(e)}t.exports=r},function(t,e){t.exports=function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}},function(t,e,r){var n=function(t){"use strict";var e=Object.prototype,r=e.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},i=n.iterator||"@@iterator",o=n.asyncIterator||"@@asyncIterator",u=n.toStringTag||"@@toStringTag";function s(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{s({},"")}catch(t){s=function(t,e,r){return t[e]=r}}function a(t,e,r,n){var i=e&&e.prototype instanceof l?e:l,o=Object.create(i.prototype),u=new O(n||[]);return o._invoke=function(t,e,r){var n="suspendedStart";return function(i,o){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===i)throw o;return L()}for(r.method=i,r.arg=o;;){var u=r.delegate;if(u){var s=x(u,r);if(s){if(s===f)continue;return s}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var a=c(t,e,r);if("normal"===a.type){if(n=r.done?"completed":"suspendedYield",a.arg===f)continue;return{value:a.arg,done:r.done}}"throw"===a.type&&(n="completed",r.method="throw",r.arg=a.arg)}}}(t,r,u),o}function c(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=a;var f={};function l(){}function h(){}function p(){}var y={};y[i]=function(){return this};var d=Object.getPrototypeOf,v=d&&d(d(j([])));v&&v!==e&&r.call(v,i)&&(y=v);var g=p.prototype=l.prototype=Object.create(y);function b(t){["next","throw","return"].forEach((function(e){s(t,e,(function(t){return this._invoke(e,t)}))}))}function m(t,e){var n;this._invoke=function(i,o){function u(){return new e((function(n,u){!function n(i,o,u,s){var a=c(t[i],t,o);if("throw"!==a.type){var f=a.arg,l=f.value;return l&&"object"==typeof l&&r.call(l,"__await")?e.resolve(l.__await).then((function(t){n("next",t,u,s)}),(function(t){n("throw",t,u,s)})):e.resolve(l).then((function(t){f.value=t,u(f)}),(function(t){return n("throw",t,u,s)}))}s(a.arg)}(i,o,n,u)}))}return n=n?n.then(u,u):u()}}function x(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,x(t,e),"throw"===e.method))return f;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return f}var n=c(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,f;var i=n.arg;return i?i.done?(e[t.resultName]=i.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,f):i:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,f)}function k(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function w(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function O(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(k,this),this.reset(!0)}function j(t){if(t){var e=t[i];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,o=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return o.next=o}}return{next:L}}function L(){return{value:void 0,done:!0}}return h.prototype=g.constructor=p,p.constructor=h,h.displayName=s(p,u,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===h||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,p):(t.__proto__=p,s(t,u,"GeneratorFunction")),t.prototype=Object.create(g),t},t.awrap=function(t){return{__await:t}},b(m.prototype),m.prototype[o]=function(){return this},t.AsyncIterator=m,t.async=function(e,r,n,i,o){void 0===o&&(o=Promise);var u=new m(a(e,r,n,i),o);return t.isGeneratorFunction(r)?u:u.next().then((function(t){return t.done?t.value:u.next()}))},b(g),s(g,u,"Generator"),g[i]=function(){return this},g.toString=function(){return"[object Generator]"},t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=j,O.prototype={constructor:O,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(w),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return u.type="throw",u.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var i=this.tryEntries.length-1;i>=0;--i){var o=this.tryEntries[i],u=o.completion;if("root"===o.tryLoc)return n("end");if(o.tryLoc<=this.prev){var s=r.call(o,"catchLoc"),a=r.call(o,"finallyLoc");if(s&&a){if(this.prev<o.catchLoc)return n(o.catchLoc,!0);if(this.prev<o.finallyLoc)return n(o.finallyLoc)}else if(s){if(this.prev<o.catchLoc)return n(o.catchLoc,!0)}else{if(!a)throw new Error("try statement without catch or finally");if(this.prev<o.finallyLoc)return n(o.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var i=this.tryEntries[n];if(i.tryLoc<=this.prev&&r.call(i,"finallyLoc")&&this.prev<i.finallyLoc){var o=i;break}}o&&("break"===t||"continue"===t)&&o.tryLoc<=e&&e<=o.finallyLoc&&(o=null);var u=o?o.completion:{};return u.type=t,u.arg=e,o?(this.method="next",this.next=o.finallyLoc,f):this.complete(u)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),f},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),w(r),f}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var i=n.arg;w(r)}return i}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:j(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),f}},t}(t.exports);try{regeneratorRuntime=n}catch(t){Function("r","regeneratorRuntime = r")(n)}},function(t,e,r){"use strict";r.r(e),r.d(e,"default",(function(){return j}));var n=r(1),i=r.n(n),o=r(2),u=r.n(o),s=r(0),a=r(7),c=r.n(a),f=r(8),l=r.n(f),h=r(4),p=r.n(h),y={get:"{id}",cget:"",post:"",delete:"{id}",put:"{id}",patch:"{id}",option:"{id}",load:"",send:"",new:"new",save:"save",edit:"edit/{id}",remove:"remove/{id}"},d=function(){function t(){i()(this,t),this.root="/",this.url="",this.routing=y,this.params={},this.subresource,this.subId}return u()(t,[{key:"init",value:function(t,e,r,n){var i=r?"".concat(r,"/"):"",o=e?"".concat(e,"/"):"",u=n?"".concat(n,"/"):"";return this.root="".concat(t,"/").concat(o).concat(i).concat(u),this.subresource=null,this.subId=null,this}},{key:"setRouting",value:function(t,e){return this.routing={route:e},Object(s.extend)(this.routing,y,t),this}},{key:"setParams",value:function(t,e){var r=e.replace(/\{(.*?)\}/gi,(function(e,r){var n=t[r];return delete t[r],n}));return t&&t.subId&&(this.setSubId(subId),delete t.subId),this.params=t||{},r}},{key:"setSubId",value:function(t){t&&(this.subId=t)}},{key:"setSubResource",value:function(t,e){this.setSubId(e),this.subresource=t}},{key:"getUrlPath",value:function(t){var e,r=this.routing.route,n=this.routing[t];return(e=r||"")&&n?e="".concat(e,"/").concat(n):n&&(e=n),this.subresource&&(e="".concat(e,"/").concat(this.subresource),this.subId&&(e="".concat(e,"/").concat(this.subId))),e}},{key:"setUrl",value:function(t,e,r){var n,i=this.root;return this.isAbsolute(r)?(i="",n=r):n=r||this.getUrlPath(t),n=this.setParams(e,n),this.url=i+n,this.url}},{key:"isAbsolute",value:function(t){return new RegExp("(www|://)").test(t)}}]),t}(),v=r(3),g=r.n(v),b=r(5),m=r.n(b),x=r(6),k=r.n(x);function w(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return!1}}();return function(){var r,n=p()(t);if(e){var i=p()(this).constructor;r=Reflect.construct(n,arguments,i)}else r=n.apply(this,arguments);return l()(this,r)}}var O=function(t){c()(r,t);var e=w(r);function r(t){var n,o=t.defaults,u=t.axiosConfig,s=t.omitEmptyParams;return i()(this,r),(n=e.call(this,u,s)).name="default",n.config={},n.defaults=o,n.router=new d,n}return u()(r,[{key:"setApi",value:function(t,e){return this.router.init(t.host,t.prefix,t.version,t.postfix),this.setDelay(t.delay),this.isAuth()||e||!1===t.authorized||this.auth(t.key),this}},{key:"setResource",value:function(t,e){return this.name=e,this.router.setRouting(t.routing||{},e),this}},{key:"sub",value:function(t,e){return this.router.setSubResource(t,e),this}},{key:"setDelay",value:function(t){return this.wait=t||0,this}},{key:"setHeaders",value:function(t){return t&&(Object(s.defaults)(this.config,{headers:{}}),Object(s.extend)(this.config.headers,t)),this}},{key:"setBaseURL",value:function(t){var e=t||this.defaults.baseURL||null;return e&&(this.config.baseURL=e),this}},{key:"isAuth",value:function(){return this.config.headers&&this.config.headers.Authorization}},{key:"auth",value:function(t){var e=t||this.defaults.key;return e&&this.setHeaders({Authorization:"Bearer ".concat(e)}),this}},{key:"public",value:function(){return this.isAuth()&&delete this.config.headers.Authorization,this}},{key:"private",value:function(t){return this.isAuth()||this.auth(t.key),this}},{key:"baseURL",value:function(){return this.root}},{key:"lastURL",value:function(){return this.router.url}},{key:"setAxiosConfig",value:function(t){return Object(s.extend)(this.config,t),this}},{key:"clear",value:function(){return this.lastConfig=Object.assign({},this.config),this.config={},this}},{key:"dumpAxiosConfig",value:function(){return this.config}},{key:"lastAxiosConfig",value:function(){return this.lastConfig}}]),r}(function(){function t(e,r){i()(this,t),this.omitEmpty=!0===r,this.axiosInstance=k.a.create(e||{}),this.CancelToken=k.a.CancelToken}var e,r;return u()(t,[{key:"axios",value:function(){return this.axiosInstance}},{key:"get",value:function(t,e,r){return this.request("get","get",null,t,e,r)}},{key:"load",value:function(t,e,r){return this.request("get","load",null,t,e,r)}},{key:"cget",value:function(t,e,r){return this.request("get","cget",null,t,e,r)}},{key:"send",value:function(t,e,r){return this.request("get","send",null,t,e,r)}},{key:"post",value:function(t,e,r,n){return this.request("post","post",t,e,r,n)}},{key:"save",value:function(t,e,r,n){return this.request("post","save",t,e,r,n)}},{key:"new",value:function(t,e,r,n){return this.request("post","new",t,e,r,n)}},{key:"edit",value:function(t,e,r,n){return this.request("post","edit",t,e,r,n)}},{key:"remove",value:function(t,e,r,n){return this.request("post","remove",t,e,r,n)}},{key:"delete",value:function(t,e,r){return this.request("delete","delete",null,t,e,r)}},{key:"put",value:function(t,e,r,n){return this.request("put","put",t,e,r,n)}},{key:"patch",value:function(t,e,r,n){return this.request("patch","patch",t||{},e,r,n)}},{key:"option",value:function(t,e,r){return this.request("option","option",null,t,e,r)}},{key:"axiosData",value:function(t,e){return t||["post","put","patch"].includes(e)}},{key:"adaptParams",value:function(t){if(!this.omitEmpty)return t;return function t(e){return e=Object(s.mapObject)(e,(function(e){return Object(s.isObject)(e)?Object(s.isEmpty)(e)?null:t(e):e})),Object(s.pick)(e,(function(t){return""!==t&&!Object(s.isNull)(t)&&!Object(s.isNaN)(t)&&!Object(s.isUndefined)(t)}))}(t)}},{key:"request",value:(r=m()(g.a.mark((function t(e,r,n,i,o,u){var s,a,c=this;return g.a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return s=null,a=this.router.setUrl(r,i,o),this.config.params=this.adaptParams(this.router.params),this.currentRequest=this.CancelToken.source(),this.config.cancelToken=this.currentRequest.token,s=(s=this.axiosData(n,e)?this.axiosInstance[e](a,n,this.config):this.axiosInstance[e](a,this.config)).finally((function(){c.clear()})),t.next=9,this.delay(u);case 9:return t.abrupt("return",s);case 10:case"end":return t.stop()}}),t,this)}))),function(t,e,n,i,o,u){return r.apply(this,arguments)})},{key:"current",value:function(){return this.currentRequest}},{key:"cancel",value:function(t){(t=t||this.current()).cancel()}},{key:"delay",value:(e=m()(g.a.mark((function t(e){var r;return g.a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if((r=e||this.wait||0)<this.wait&&(r=this.wait),r){t.next=4;break}return t.abrupt("return",Promise.resolve());case 4:return t.abrupt("return",new Promise((function(t){Object(s.delay)(t,1e3*r)})));case 5:case"end":return t.stop()}}),t,this)}))),function(t){return e.apply(this,arguments)})}]),t}()),j=function(){function t(e,r,n){i()(this,t),this.resources=e||[],this.defaults={},this.config={},this.axiosConfig=r,this.omitEmptyParams=n,this.builder}return u()(t,[{key:"default",value:function(t,e){return void 0!==e&&(this.defaults[t]=e),this.defaults[t]}},{key:"define",value:function(t,e){return this.resources[t]=e,this}},{key:"extend",value:function(t,e){return Object(s.extend)(this.resources[t],e),this}},{key:"getConfig",value:function(t){var e=this.resources[t]||{},r=Object.assign({},e);return Object(s.defaults)(r,this.resources.default)}},{key:"res",value:function(t,e,r){return this.builder=new O(this),this.config=this.getConfig(e||t),this.builder.setApi(this.config,r),this.builder.setResource(this.getConfig(t),t),this.builder}},{key:"auth",value:function(t,e){return t&&(e?this.extend(e,{key:t}):this.default("key",t)),this}},{key:"public",value:function(){return this.builder.public(),this.builder}},{key:"private",value:function(){return this.builder.private(this.config),this.builder}}]),t}()}]);