axios-storage-adapter
Version:
This is an Axios adapter designed to cache API calls
2 lines (1 loc) • 3.56 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var c=(n,e,t)=>new Promise((r,a)=>{var i=l=>{try{d(t.next(l))}catch(m){a(m)}},o=l=>{try{d(t.throw(l))}catch(m){a(m)}},d=l=>l.done?r(l.value):Promise.resolve(l.value).then(i,o);d((t=t.apply(n,e)).next())});var _axios = require('axios'); var _axios2 = _interopRequireDefault(_axios);var s=class s{static setupDB(){return c(this,arguments,function*(e="application-cache",t="api-response",r=[]){this.skipParams=[],this.dbName=e,this.tableName=t;var a;this.addTab(),a=yield this.IndxDb.open(this.dbName,3),a.onupgradeneeded=yield this.AddTables,a.onsuccess=i=>{this.db=i.target.result},window.addEventListener("beforeunload",s.resetDB)})}static AddTables(e){return c(this,null,function*(){var a;this.db=e.target.result;let t={autoIncrement:!0,keyPath:"url"},r=yield(a=this.db)==null?void 0:a.createObjectStore(s.tableName,t);r==null||r.createIndex("url","url",{unique:!0})})}static saveData(e,t){return c(this,null,function*(){this.db&&(yield(yield this.db.transaction(s.tableName,"readwrite")).objectStore(s.tableName)).put({url:e,content:t})})}static validateUrl(e){return s.cacheUrl.filter(t=>URL+t==e)}static getData(e){return c(this,null,function*(){return new Promise((t,r)=>{if(!this.db||s.refreshData){t(!1);return}let o=this.db.transaction(s.tableName,"readonly").objectStore(s.tableName).get(e);o.onsuccess=d=>{d.target.result?t(d.target.result):t(!1)},o.onerror=d=>{t(!1)}})})}static addTab(){if(!sessionStorage.getItem("tabCount")){let e=localStorage.getItem("tabCount"),t=e?parseInt(e)+1:1;localStorage.setItem("tabCount",t.toString()),sessionStorage.setItem("tabCount","1")}}static removeTabItem(e){let t=localStorage.getItem("tabCount"),r=t?parseInt(t)-1:0;localStorage.setItem("tabCount",r.toString())}static resetDB(e){let t=localStorage.getItem("tabCount"),r=t?parseInt(t)-1:0;localStorage.setItem("tabCount",r.toString()),sessionStorage.removeItem("tabCount"),r<=0&&this.db&&(s.db.close(),s.IndxDb.deleteDatabase(s.dbName))}static deleteDB(){this.db&&(s.db.close(),s.IndxDb.deleteDatabase(s.dbName))}static deleteData(){return c(this,null,function*(){this.db&&(yield(yield(yield this.db.transaction(s.tableName,"readwrite")).objectStore(s.tableName)).clear())})}};s.IndxDb=window.indexedDB,s.dbName="",s.tableName="",s.refreshData=!1,s.cacheUrl=[],s.skipParams=[];var u=s;var g=typeof _axios2.default.defaults.adapter=="object"?_axios2.default.defaults.adapter[0]:"xhr",b={adapter:_axios2.default.getAdapter(g),cache:!1};function f(n,e){let t=e.split("?")[0],r,a=[],i=e.indexOf("?")!==-1?e.split("?")[1]:"";if(i!==""){a=i.split("&");for(let o=a.length-1;o>=0;o-=1)r=a[o].split("=")[0],r===n&&a.splice(o,1);a.length&&(t=t+"?"+a.join("&"))}return t}function y(n,e){return c(this,null,function*(){try{let r=yield(n.method==="get"?_axios2.default.get:_axios2.default.post)(n.url,n.data,{headers:n.headers}),a=r.data;return a&&u.saveData(e,JSON.stringify(a)),r}catch(t){throw console.error(t),t}})}function h(n){return c(this,null,function*(){let{url:e,method:t}=n;if(((t==null?void 0:t.toLowerCase())==="get"||(t==null?void 0:t.toLowerCase())==="post")&&b.cache){let r=u.skipParams,a=e;r.forEach(o=>{a=f(o,a)});let i=yield u.getData(a);return i&&i.content?{data:JSON.parse(i.content),status:200,statusText:"OK",_from_localstorage:!0,config:n}:y(n,a)}return b.adapter(n)})}function A(n){return Object.assign(b,n),b.cache?h:b.adapter}exports.CacheService = u; exports.StorageAdapter = A;