larapi 0.2.45 → 0.2.46
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +258 -0
- package/dist/index.d.ts +258 -0
- package/dist/index.js +981 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +952 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +17 -10
- package/dist/index.js.LICENSE.txt +0 -6
- package/lib/Core.d.ts +0 -39
- package/lib/Http.d.ts +0 -44
- package/lib/Queue.d.ts +0 -13
- package/lib/index.d.ts +0 -72
- package/lib/types.d.ts +0 -130
package/dist/index.js
CHANGED
|
@@ -1,3 +1,982 @@
|
|
|
1
|
-
|
|
2
|
-
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.API=t():e.API=t()}("undefined"!=typeof self?self:this,(()=>{return e={487:e=>{var t={utf8:{stringToBytes:function(e){return t.bin.stringToBytes(unescape(encodeURIComponent(e)))},bytesToString:function(e){return decodeURIComponent(escape(t.bin.bytesToString(e)))}},bin:{stringToBytes:function(e){for(var t=[],i=0;i<e.length;i++)t.push(255&e.charCodeAt(i));return t},bytesToString:function(e){for(var t=[],i=0;i<e.length;i++)t.push(String.fromCharCode(e[i]));return t.join("")}}};e.exports=t},12:e=>{var t,i;t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",i={rotl:function(e,t){return e<<t|e>>>32-t},rotr:function(e,t){return e<<32-t|e>>>t},endian:function(e){if(e.constructor==Number)return 16711935&i.rotl(e,8)|4278255360&i.rotl(e,24);for(var t=0;t<e.length;t++)e[t]=i.endian(e[t]);return e},randomBytes:function(e){for(var t=[];e>0;e--)t.push(Math.floor(256*Math.random()));return t},bytesToWords:function(e){for(var t=[],i=0,s=0;i<e.length;i++,s+=8)t[s>>>5]|=e[i]<<24-s%32;return t},wordsToBytes:function(e){for(var t=[],i=0;i<32*e.length;i+=8)t.push(e[i>>>5]>>>24-i%32&255);return t},bytesToHex:function(e){for(var t=[],i=0;i<e.length;i++)t.push((e[i]>>>4).toString(16)),t.push((15&e[i]).toString(16));return t.join("")},hexToBytes:function(e){for(var t=[],i=0;i<e.length;i+=2)t.push(parseInt(e.substr(i,2),16));return t},bytesToBase64:function(e){for(var i=[],s=0;s<e.length;s+=3)for(var r=e[s]<<16|e[s+1]<<8|e[s+2],a=0;a<4;a++)8*s+6*a<=8*e.length?i.push(t.charAt(r>>>6*(3-a)&63)):i.push("=");return i.join("")},base64ToBytes:function(e){e=e.replace(/[^A-Z0-9+\/]/gi,"");for(var i=[],s=0,r=0;s<e.length;r=++s%4)0!=r&&i.push((t.indexOf(e.charAt(s-1))&Math.pow(2,-2*r+8)-1)<<2*r|t.indexOf(e.charAt(s))>>>6-2*r);return i}},e.exports=i},2:function(e){var t;t=()=>(()=>{"use strict";var e={828:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.default=class{constructor(e){Object.defineProperty(this,"listeners",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"events",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),e?(this.listeners=e.listeners,this.events=e.events):(this.listeners=new Map,this.events={})}}},987:function(e,t,i){var s=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const r=s(i(828));class a extends r.default{constructor(e,t,i,s=!1){super(i),Object.defineProperty(this,"isStack",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_name",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"handlers",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this.isStack=s,this._name=e,this.handlers={callback:t,emit:[],remove:[]},this.push(this)}push(e){var t;this._name&&!this.events[this._name]&&(this.events[this._name]=new Map),this.events[this._name].set(e,e),this.listeners.has(e.handlers.callback)?null===(t=this.listeners.get(e.handlers.callback))||void 0===t||t.pushFork(e):this.listeners.set(e.handlers.callback,e)}pushFork(e){this.handlers.forks||(this.handlers.forks=[]),this.handlers.forks.push(e)}get name(){return"string"==typeof this._name?this._name:void 0}emit(e){if("function"==typeof this.handlers.callback){this.handlers.callback(e);for(let e=0;e<this.handlers.emit.length;e++)this.handlers.emit[e]()}}hasHandler(e){return e===this.handlers.callback}count(){var e,t;return null!==(t=null===(e=this.events[this._name])||void 0===e?void 0:e.size)&&void 0!==t?t:0}remove(){if(!(this&&this.events&&this._name&&this.events[this._name]))return;this.events[this._name].delete(this),0===this.events[this._name].size&&delete this.events[this._name];const e=this.listeners.get(this.handlers.callback);if(e&&e.handlers.forks){const t=e.handlers.forks.indexOf(this);-1!==t&&e.handlers.forks.splice(t,1)}this.listeners.delete(this.handlers.callback),this.isStack||(this._name=void 0,this.handlers.callback=void 0);for(let e=0;e<this.handlers.remove.length;e++)this.handlers.remove[e]()}onEmit(e){this.handlers.emit.splice(0,1,e)}onRemove(e){this.handlers.remove.splice(0,1,e)}}t.default=a},518:function(e,t,i){var s=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const r=s(i(987)),a=s(i(828));class n extends a.default{constructor(e){super(),this.emit=this.emit.bind(this),this.addListener=this.addListener.bind(this),this.removeAllListeners=this.removeAllListeners.bind(this),"boolean"==typeof e&&!0===e?this.events=n._events[n.globalName]:"string"==typeof e&&e!==n.globalName&&(n._events[e]||(n._events[e]={}),this.events=n._events[e])}emit(e,t){this.events[e]&&this.events[e].forEach((e=>e.emit(t)))}addListener(e,t){return new r.default(e,t,this)}listenerCount(e){var t,i;let s=0;return s=e?null!==(i=null===(t=this.events[e])||void 0===t?void 0:t.size)&&void 0!==i?i:0:this.listeners.size,s}removeListener(e){var t;null===(t=this.listeners.get(e))||void 0===t||t.remove()}removeAllListeners(){this.listeners.forEach((e=>{e.remove()}))}}Object.defineProperty(n,"globalName",{enumerable:!0,configurable:!0,writable:!0,value:"__global"}),Object.defineProperty(n,"_events",{enumerable:!0,configurable:!0,writable:!0,value:{[n.globalName]:{}}}),t.default=n},457:function(e,t,i){var s=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const r=s(i(987)),a=s(i(607));class n extends r.default{constructor(e=[]){super("__stack",(e=>{this.listeners.forEach((t=>t.isStack?null:t.emit(e)))}),new a.default,!0);for(let t=0;t<e.length;t++)this.push(e[t])}push(e){super.push(e)}remove(e){if(this&&this.events&&this._name&&this.events[this._name]){this.events[this._name].delete(null!=e?e:this);for(const[t,i]of this.listeners)e?i===e&&i.remove():i!==this&&i.remove();e||super.remove()}}get length(){return Number(this.listeners.size-1)}}t.default=n},607:function(e,t,i){var s,r=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const a=r(i(518)),n=r(i(457));class h extends a.default{}s=h,Object.defineProperty(h,"instance",{enumerable:!0,configurable:!0,writable:!0,value:new h(!0)}),Object.defineProperty(h,"emit",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.emit}),Object.defineProperty(h,"addListener",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.addListener}),Object.defineProperty(h,"removeListener",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.removeListener}),Object.defineProperty(h,"removeAllListeners",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.removeAllListeners}),Object.defineProperty(h,"Stack",{enumerable:!0,configurable:!0,writable:!0,value:n.default}),t.default=h}},t={};return function i(s){var r=t[s];if(void 0!==r)return r.exports;var a=t[s]={exports:{}};return e[s].call(a.exports,a,a.exports,i),a.exports}(607).default})(),e.exports=t()},738:e=>{function t(e){return!!e.constructor&&"function"==typeof e.constructor.isBuffer&&e.constructor.isBuffer(e)}e.exports=function(e){return null!=e&&(t(e)||function(e){return"function"==typeof e.readFloatLE&&"function"==typeof e.slice&&t(e.slice(0,0))}(e)||!!e._isBuffer)}},568:(e,t,i)=>{var s,r,a,n,h;s=i(12),r=i(487).utf8,a=i(738),n=i(487).bin,(h=function(e,t){e.constructor==String?e=t&&"binary"===t.encoding?n.stringToBytes(e):r.stringToBytes(e):a(e)?e=Array.prototype.slice.call(e,0):Array.isArray(e)||e.constructor===Uint8Array||(e=e.toString());for(var i=s.bytesToWords(e),o=8*e.length,u=1732584193,l=-271733879,c=-1732584194,d=271733878,p=0;p<i.length;p++)i[p]=16711935&(i[p]<<8|i[p]>>>24)|4278255360&(i[p]<<24|i[p]>>>8);i[o>>>5]|=128<<o%32,i[14+(o+64>>>9<<4)]=o;var f=h._ff,b=h._gg,m=h._hh,v=h._ii;for(p=0;p<i.length;p+=16){var g=u,y=l,j=c,O=d;u=f(u,l,c,d,i[p+0],7,-680876936),d=f(d,u,l,c,i[p+1],12,-389564586),c=f(c,d,u,l,i[p+2],17,606105819),l=f(l,c,d,u,i[p+3],22,-1044525330),u=f(u,l,c,d,i[p+4],7,-176418897),d=f(d,u,l,c,i[p+5],12,1200080426),c=f(c,d,u,l,i[p+6],17,-1473231341),l=f(l,c,d,u,i[p+7],22,-45705983),u=f(u,l,c,d,i[p+8],7,1770035416),d=f(d,u,l,c,i[p+9],12,-1958414417),c=f(c,d,u,l,i[p+10],17,-42063),l=f(l,c,d,u,i[p+11],22,-1990404162),u=f(u,l,c,d,i[p+12],7,1804603682),d=f(d,u,l,c,i[p+13],12,-40341101),c=f(c,d,u,l,i[p+14],17,-1502002290),u=b(u,l=f(l,c,d,u,i[p+15],22,1236535329),c,d,i[p+1],5,-165796510),d=b(d,u,l,c,i[p+6],9,-1069501632),c=b(c,d,u,l,i[p+11],14,643717713),l=b(l,c,d,u,i[p+0],20,-373897302),u=b(u,l,c,d,i[p+5],5,-701558691),d=b(d,u,l,c,i[p+10],9,38016083),c=b(c,d,u,l,i[p+15],14,-660478335),l=b(l,c,d,u,i[p+4],20,-405537848),u=b(u,l,c,d,i[p+9],5,568446438),d=b(d,u,l,c,i[p+14],9,-1019803690),c=b(c,d,u,l,i[p+3],14,-187363961),l=b(l,c,d,u,i[p+8],20,1163531501),u=b(u,l,c,d,i[p+13],5,-1444681467),d=b(d,u,l,c,i[p+2],9,-51403784),c=b(c,d,u,l,i[p+7],14,1735328473),u=m(u,l=b(l,c,d,u,i[p+12],20,-1926607734),c,d,i[p+5],4,-378558),d=m(d,u,l,c,i[p+8],11,-2022574463),c=m(c,d,u,l,i[p+11],16,1839030562),l=m(l,c,d,u,i[p+14],23,-35309556),u=m(u,l,c,d,i[p+1],4,-1530992060),d=m(d,u,l,c,i[p+4],11,1272893353),c=m(c,d,u,l,i[p+7],16,-155497632),l=m(l,c,d,u,i[p+10],23,-1094730640),u=m(u,l,c,d,i[p+13],4,681279174),d=m(d,u,l,c,i[p+0],11,-358537222),c=m(c,d,u,l,i[p+3],16,-722521979),l=m(l,c,d,u,i[p+6],23,76029189),u=m(u,l,c,d,i[p+9],4,-640364487),d=m(d,u,l,c,i[p+12],11,-421815835),c=m(c,d,u,l,i[p+15],16,530742520),u=v(u,l=m(l,c,d,u,i[p+2],23,-995338651),c,d,i[p+0],6,-198630844),d=v(d,u,l,c,i[p+7],10,1126891415),c=v(c,d,u,l,i[p+14],15,-1416354905),l=v(l,c,d,u,i[p+5],21,-57434055),u=v(u,l,c,d,i[p+12],6,1700485571),d=v(d,u,l,c,i[p+3],10,-1894986606),c=v(c,d,u,l,i[p+10],15,-1051523),l=v(l,c,d,u,i[p+1],21,-2054922799),u=v(u,l,c,d,i[p+8],6,1873313359),d=v(d,u,l,c,i[p+15],10,-30611744),c=v(c,d,u,l,i[p+6],15,-1560198380),l=v(l,c,d,u,i[p+13],21,1309151649),u=v(u,l,c,d,i[p+4],6,-145523070),d=v(d,u,l,c,i[p+11],10,-1120210379),c=v(c,d,u,l,i[p+2],15,718787259),l=v(l,c,d,u,i[p+9],21,-343485551),u=u+g>>>0,l=l+y>>>0,c=c+j>>>0,d=d+O>>>0}return s.endian([u,l,c,d])})._ff=function(e,t,i,s,r,a,n){var h=e+(t&i|~t&s)+(r>>>0)+n;return(h<<a|h>>>32-a)+t},h._gg=function(e,t,i,s,r,a,n){var h=e+(t&s|i&~s)+(r>>>0)+n;return(h<<a|h>>>32-a)+t},h._hh=function(e,t,i,s,r,a,n){var h=e+(t^i^s)+(r>>>0)+n;return(h<<a|h>>>32-a)+t},h._ii=function(e,t,i,s,r,a,n){var h=e+(i^(t|~s))+(r>>>0)+n;return(h<<a|h>>>32-a)+t},h._blocksize=16,h._digestsize=16,e.exports=function(e,t){if(null==e)throw new Error("Illegal argument "+e);var i=s.wordsToBytes(h(e,t));return t&&t.asBytes?i:t&&t.asString?n.bytesToString(i):s.bytesToHex(i)}},828:function(e,t,i){"use strict";var s=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const r=s(i(2));t.default=class{constructor(e){Object.defineProperty(this,"cache",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"initData",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"events",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"config",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"headers",{enumerable:!0,configurable:!0,writable:!0,value:{}}),this.events=new r.default,this.config={host:"http://127.0.0.1/"},this.cache={},this.initData={},this.setConfig=this.setConfig.bind(this),this.getConfig=this.getConfig.bind(this),this.setHost=this.setHost.bind(this),this.setInitData=this.setInitData.bind(this),this.triggerByCacheGroup=this.triggerByCacheGroup.bind(this),this.clearCacheGroup=this.clearCacheGroup.bind(this),this.updateCacheGroup=this.updateCacheGroup.bind(this),this.setGlobalInitData=this.setGlobalInitData.bind(this),e&&Object.assign(this,{config:null==e?void 0:e.config,events:null==e?void 0:e.events,cache:null==e?void 0:e.cache,initData:null==e?void 0:e.initData,headers:e.headers})}setConfig(e){this.config=e}getConfig(){return this.config}getCache(e){if(this.cache[e]){if((new Date).getTime()<=this.cache[e].time+36e8)return this.cache[e].data;delete this.cache[e]}}isJsonString(e){try{JSON.parse(e)}catch(e){return!1}return!0}compileRevivers(){const e={},t=[],i=this.config.dataReviver;if(!i)return{fieldRevivers:e,objectRevivers:t};for(const[s,r]of Object.entries(i))"function"==typeof r&&(s.includes("&")?t.push({keys:new Set(s.split("&")),fn:r}):e[s]=r);return{fieldRevivers:e,objectRevivers:t}}applyObjectRevivers(e,t,i=0,s=!1){if(!e||"object"!=typeof e)return e;if(Array.isArray(e)){for(let s=0;s<e.length;s++)e[s]=this.applyObjectRevivers(e[s],t,i+1,!0);return e}let r=e;if(0===i||s)for(const e of t){const t=Object.keys(r);if(e.keys.size>t.length)continue;let i=!0;for(const t of e.keys)if(!(t in r)){i=!1;break}i&&(r=e.fn(r))}for(const e of Object.keys(r))r[e]=this.applyObjectRevivers(r[e],t,i+1,!1);return r}dataPrepare(e){const t=JSON.stringify(e);return this.jsonParse(t)}jsonParse(e){if("function"==typeof this.config.reviver)return JSON.parse(e,this.config.reviver);const{fieldRevivers:t,objectRevivers:i}=this.compileRevivers(),s=JSON.parse(e,((e,i)=>{const s=t[e];return s?s(i):i}));return this.applyObjectRevivers(s,i)}setHost(e){this.config.host=e}setHeader(e,t){this.headers[e]=t,this.events.emit("header-update",{name:e,value:t})}getHeader(e){return this.headers[e]}getHeaders(){return Object.entries(this.headers).map((([e,t])=>({name:e,value:t})))}hasHeader(e){return!!this.headers[e]}deleteHeader(e){delete this.headers[e]}setCache(e,t,i){this.cache[e]={time:(new Date).getTime(),data:t,group:i}}groupFromArray(e,t,i=!1){const s=e=>{if("string"!=typeof e||i)t(e);else{const i=e.split("."),s=i.shift();t(s,i)}};Array.isArray(e)?e.map(s):"string"==typeof e&&s(e)}triggerByCacheGroup(e){this.groupFromArray(e,(e=>this.events.emit("trigger-request-by-cache-"+e,void 0)))}clearCacheGroup(e,t,i="id"){this.groupFromArray(e,(e=>{if(t){const s=e=>{if(i&&(null==e?void 0:e[i])===(null==t?void 0:t[i])||null===i)for(const i in e)if(i in t)return!1;return!0};for(const t in this.cache)this.cache[t].group===e&&(Array.isArray(this.cache[t].data)?this.cache[t].data=Object.values(this.cache[t].data).filter(s):s(this.cache[t].data)||delete this.cache[t])}else for(const t in this.cache)this.cache[t].group===e&&delete this.cache[t]}))}setDataTree(e,t,i){const s=t.split("."),r=s.pop(),a=s.reduce(((e,t)=>e[t]=e[t]||{}),e);return"string"==typeof r&&(a[r]=i),e}updateCacheGroup(e,t,i="id"){const s=e=>{const s=e,r=i.split(".");if(r.length>1)for(const t of r)e=e[t];if(i&&e[i]===t[i]||null===i)for(const i in e)i in t&&(e[i]=t[i]);return s};this.groupFromArray(e,((e,t)=>{for(const t in this.cache)this.cache[t].group===e&&(Array.isArray(this.cache[t].data)?this.cache[t].data=Object.values(this.cache[t].data).map(s):this.cache[t].data=s(this.cache[t].data))}))}appendCacheGroup(e,t){}getCacheByIndex(e){return this.cache[e]}deleteCache(e){if(!this.cache[e])return;const t=this.cache[e].group;t&&this.clearCacheGroup(t),delete this.cache[e]}setGlobalInitData(e,t){this.initData[e]=t}setInitData(e){this.initData=e}}},383:function(e,t,i){"use strict";var s=this&&this.__awaiter||function(e,t,i,s){return new(i||(i=Promise))((function(r,a){function n(e){try{o(s.next(e))}catch(e){a(e)}}function h(e){try{o(s.throw(e))}catch(e){a(e)}}function o(e){var t;e.done?r(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(n,h)}o((s=s.apply(e,t||[])).next())}))},r=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const a=r(i(568)),n=r(i(828)),h=r(i(840)),o=r(i(2));class u extends n.default{constructor(e,t,i){super(i),Object.defineProperty(this,"cacheIndex",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"currentCache",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"currentEvents",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"requestParams",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"method",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"params",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"path",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"queue",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"queueName",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"_promise",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"xhr",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"tryRequestCount",{enumerable:!0,configurable:!0,writable:!0,value:0}),Object.defineProperty(this,"tryRequestTimeout",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this.currentEvents=new o.default,this._promise=new Promise((()=>{})),this.queue=new h.default(this),this.cacheIndex="",this.queueName="",this.method=e,this.headers=Object.assign(Object.assign({},this.headers),t.headers),this.params=Object.assign(Object.assign(Object.assign({},this.config),{headers:this.headers}),t),this.path=this.params.path,this.requestParams={method:this.method},this.handleSuccess=this.handleSuccess.bind(this),this.handleAbort=this.handleAbort.bind(this),this.handleFail=this.handleFail.bind(this),this.handleError=this.handleError.bind(this),this.handleComplete=this.handleComplete.bind(this),this.handleProgress=this.handleProgress.bind(this),this.setEmit=this.setEmit.bind(this),this.request=this.request.bind(this),this.httpRequest=this.httpRequest.bind(this),this.tryHttpRequest=this.tryHttpRequest.bind(this),this.initRequest(),this.initCache(),this.initQueue()}initRequest(){var e;"undefined"!=typeof XMLHttpRequest&&(this.xhr=new XMLHttpRequest),this.params.clearUndifinedData&&(this.params.data=this.params.data?this.cuteUndifinedParams(this.params.data):this.params.data),["GET","HEAD"].includes(this.method)&&(this.params.data&&(this.path=this.params.path+"?"+this.encodeUrlParams(this.params.data)),"HEAD"===this.method&&(this.requestParams.withoutResponse=!0)),["POST","PUT","CONNECT","PATH"].includes(this.method)&&(this.requestParams.body=null!==(e=this.params.body)&&void 0!==e?e:JSON.stringify(this.params.data)),["PUT","DELETE","CONNECT","OPTIONS","TRACE"].includes(this.method)&&(this.params.cache=!1)}initCache(){var e;if(this.cacheIndex=(0,a.default)(this.path+JSON.stringify(this.params.data)),this.params.globalName&&this.initData[this.params.globalName]){const t=this.config.globalDataWithoutDataPrepare?this.initData[this.params.globalName]:this.dataPrepare(this.initData[this.params.globalName]);this.setCache(this.cacheIndex,t,"boolean"==typeof this.params.cache?void 0:this.params.cache),this.params.cache=null===(e=this.params.cache)||void 0===e||e,delete this.initData[this.params.globalName]}this.params.cache?("string"==typeof this.params.cache&&this.events.addListener("trigger-request-by-cache-"+this.params.cache,this.request),this._promise=new Promise((e=>s(this,void 0,void 0,(function*(){(this.currentCache=this.getCache(this.cacheIndex))&&(e(this.currentCache),this.handleSuccess(this.currentCache),this.handleComplete(this.currentCache))}))))):this.deleteCache(this.cacheIndex)}get host(){return this.params.host}get promise(){return this._promise}get url(){return this.params.host+this.path}initQueue(){this.queueName=this.params.queueThrottling?(0,a.default)(this.path):this.cacheIndex}setEmit(e,t){this.currentEvents.emit(e,t),this.events.emit(e,{cacheIndex:this.cacheIndex,data:t})}handleSuccess(e){"function"==typeof this.params.success&&this.xhr&&this.params.success(e,this.xhr),this.params.cacheUpdate&&this.params.data&&this.groupFromArray(this.params.cacheUpdate,((e,t)=>{var i;this.updateCacheGroup("string"==typeof e?e:e.group,null!==(i=this.params.data)&&void 0!==i?i:{},"string"==typeof e?void 0:e.fieldKey)}),!0),this.params.cacheClear&&this.params.data&&this.groupFromArray(this.params.cacheClear,(e=>{this.clearCacheGroup("string"==typeof e?e:e.group,this.params.data,"string"==typeof e?void 0:e.fieldKey)})),this.setEmit("api-request-success",e)}handleAbort(e){"function"==typeof this.params.abort&&this.params.abort(e),this.setEmit("api-request-abort",e)}handleFail(e){"function"==typeof this.params.fail&&this.params.fail(e),this.setEmit("api-request-fail",e)}handleError(e){var t;0===(null===(t=this.xhr)||void 0===t?void 0:t.status)&&4===this.xhr.readyState||("function"==typeof this.params.error&&this.params.error(e),this.setEmit("api-request-error",e))}handleComplete(e){"function"==typeof this.params.complete&&this.params.complete(e),this.setEmit("api-request-complete",e)}handleProgress(e){"function"==typeof this.params.progress&&this.params.progress(e),this.setEmit("api-request-progress",e)}encodeUrlParams(e,t=""){return Object.entries(e).filter((([e,t])=>null!=t)).map((([e,i])=>{const s=t?`${t}[${encodeURIComponent(e)}]`:encodeURIComponent(e);return Array.isArray(i)?0===i.length?`${s}[]=`:i.map((e=>`${s}[]=${encodeURIComponent(String(e))}`)).join("&"):"object"==typeof i&&null!==i?this.encodeUrlParams(i,s):`${s}=${encodeURIComponent(String(i))}`})).filter((e=>""!==e)).join("&")}cuteUndifinedParams(e){if("object"!=typeof e||null===e)return e;for(const t in e)void 0===e[t]&&delete e[t];return e}tryHttpRequest(){return new Promise(((e,t)=>{var i;this.params.tryRequest&&this.params.tryRequest>this.tryRequestCount?(this.tryRequestTimeout&&clearTimeout(this.tryRequestTimeout),this.tryRequestTimeout=setTimeout((()=>{var i;null===(i=this.xhr)||void 0===i||i.abort(),this.httpRequest().then((t=>{e(t),this.tryRequestCount=0})).catch(t)}),null!==(i=this.params.tryRequestDelay)&&void 0!==i?i:1e3),++this.tryRequestCount):t()}))}fileRequest(){return new Promise(((e,t)=>s(this,void 0,void 0,(function*(){if(!this.params.file)return;const i=this.params.file,s=this.params.fileSizeChunk?1024*this.params.fileSizeChunk*1024:this.params.file.byteLength,r=Math.ceil(this.params.file.byteLength/s);let a=this.params.fileIdByServer?void 0:`file-${(new Date).getTime()}`;for(let n=0;n<r;n++)try{const h=n*s,o=Math.min(h+s,i.byteLength),l=i.slice(h,o),c=Object.assign({},this.headers);let d;if("binary"===this.params.fileType){const e=l instanceof Uint8Array?l:new Uint8Array(l);c["Content-Type"]="application/octet-stream",c["Content-Range"]=`bytes ${h}-${o-1}/${this.params.file.byteLength}`,c["X-Chunk-Index"]=String(n),c["X-Chunk-Total"]=String(r),a&&(!this.params.fileIdByServer||this.params.fileIdByServer&&n>0)&&(c["string"==typeof this.params.fileIdByServer?this.params.fileIdByServer:"X-File-ID"]=a),d=e}else{const e=new FormData;if(e.append("file",new Blob([l])),a&&(!this.params.fileIdByServer||this.params.fileIdByServer&&n>0)&&e.append("string"==typeof this.params.fileIdByServer?this.params.fileIdByServer:"file_id",a),e.append("chunk_index",String(n)),e.append("total_chunks",String(r)),this.params.data&&r-1===n)for(const t in this.params.data)e.append(t,this.params.data[t]);delete c["Content-Type"],d=e}delete c.Accept;const p=new u(this.method,Object.assign(Object.assign({},this.params),{file:void 0,headers:c,data:void 0,fileSizeChunk:void 0,body:d,success:r-1>n?void 0:t=>{e(t),this.handleSuccess(t)},complete:r-1>n?void 0:e=>{this.handleComplete(e)},progress:e=>{const t=h+e.loaded;this.handleProgress({percent:t/i.byteLength*100,total:i.byteLength,loaded:t})},fail:t,error:t}),this);this.xhr=p.xhr;const f=yield p.request(!0).promise;(null==f?void 0:f.file_id)&&(a=null==f?void 0:f.file_id)}catch(e){t(e);break}}))))}httpRequest(){return new Promise(((e,t)=>{var i;if(void 0!==this.xhr)try{this.xhr.open(this.method,this.url,null===(i=this.params.requestAsync)||void 0===i||i),this.params.timeout&&(this.xhr.timeout=this.params.timeout);for(const e in this.headers)this.xhr.setRequestHeader(e,this.headers[e]);this.xhr.upload&&(this.xhr.upload.onprogress=({lengthComputable:e,loaded:t,total:i})=>{e&&this.handleProgress({percent:t/i*100,total:i,loaded:t})}),this.xhr.onabort=()=>{t({type:"abort",message:"request abort"}),this.handleAbort({message:"request abort"})},this.xhr.ontimeout=()=>{this.tryHttpRequest().then(e).catch(t)},this.xhr.onerror=i=>s(this,void 0,void 0,(function*(){yield this.tryHttpRequest().then(e).catch(t),t({type:"error",message:"request error",event:i}),this.handleError({message:"request error",event:i})})),this.xhr.onreadystatechange=()=>{var i,s;if((null===(i=this.xhr)||void 0===i?void 0:i.readyState)!==XMLHttpRequest.DONE)return;const r=!this.requestParams.withoutResponse&&this.isJsonString(this.xhr.responseText)?this.jsonParse(this.xhr.responseText):{};(null===(s=this.xhr)||void 0===s?void 0:s.status)>=200&&this.xhr.status<=299?(this.params.cache&&this.setCache(this.cacheIndex,r,"boolean"==typeof this.params.cache?void 0:this.params.cache),e(r),this.handleSuccess(r)):this.xhr.status>=100&&this.xhr.status<=500&&(t(r),this.handleFail(r)),this.handleComplete(r),this.queue.clear(this.queueName)},this.xhr.send(this.requestParams.body)}catch(e){t(e)}}))}streamRequest(){return new Promise(((e,t)=>{fetch(this.url,{method:this.method,headers:this.headers,body:this.requestParams.body}).then((t=>{this.handleSuccess(t),e(t)})).catch((e=>{this.handleFail(e),t(e)}))}))}request(e){return e||this.params.forceRequest||!this.currentCache&&!this.queue.push(this.queueName)?(this._promise=new Promise(((e,t)=>s(this,void 0,void 0,(function*(){try{let t;t=this.params.stream?yield this.streamRequest():this.params.file?yield this.fileRequest():yield this.httpRequest(),e(t)}catch(e){t(e)}})))),this):this}abort(){var e;null===(e=this.xhr)||void 0===e||e.abort()}addListener(e,t){return this.currentEvents.addListener(e,t)}updateCache(e,t="id"){const i=this.getCacheByIndex(this.cacheIndex);i&&i.group&&this.updateCacheGroup(i.group,e,t)}clearCache(e,t="id"){if(e){const i=this.getCacheByIndex(this.cacheIndex);i&&i.group&&this.clearCacheGroup(i.group,e,t)}else this.deleteCache(this.cacheIndex)}}t.default=u},840:function(e,t,i){"use strict";var s=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const r=s(i(383));class a{constructor(e){Object.defineProperty(this,"queue",{enumerable:!0,configurable:!0,writable:!0,value:a._Queue}),Object.defineProperty(this,"http",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this.http=e,this.has=this.has.bind(this),this.get=this.get.bind(this),this.clear=this.clear.bind(this),this.push=this.push.bind(this)}has(e){return e in this.queue}get(e){return this.has(e)?this.queue[e]:void 0}clear(e){this.has(e)&&delete this.queue[e]}push(e){const t=this.get(e);return t?(null==t||t.addListener("api-request-success",this.http.handleSuccess),null==t||t.addListener("api-request-error",this.http.handleError),null==t||t.addListener("api-request-fail",this.http.handleFail),null==t||t.addListener("api-request-complete",this.http.handleComplete),null==t||t.addListener("api-request-abort",this.http.handleAbort)):this.queue[e]=this.http,t instanceof r.default}}Object.defineProperty(a,"_Queue",{enumerable:!0,configurable:!0,writable:!0,value:{}}),t.default=a},607:function(e,t,i){"use strict";var s,r=this&&this.__awaiter||function(e,t,i,s){return new(i||(i=Promise))((function(r,a){function n(e){try{o(s.next(e))}catch(e){a(e)}}function h(e){try{o(s.throw(e))}catch(e){a(e)}}function o(e){var t;e.done?r(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(n,h)}o((s=s.apply(e,t||[])).next())}))},a=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0});const n=a(i(828)),h=a(i(383));class o extends n.default{constructor(e){super(),Object.defineProperty(this,"_user",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"token",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"csrfToken",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this.headers=Object.assign({Accept:"application/json","Content-Type":"application/json"},e.headers),this.setConfig(e),this._user={},this.setToken=this.setToken.bind(this),this.setCSRFToken=this.setCSRFToken.bind(this),this.setLogin=this.setLogin.bind(this),this.setUser=this.setUser.bind(this),this.updateUser=this.updateUser.bind(this),this.logout=this.logout.bind(this),this.getToken=this.getToken.bind(this),this.getCSRFToken=this.getCSRFToken.bind(this),this.http=this.http.bind(this),this.get=this.get.bind(this),this.head=this.head.bind(this),this.post=this.post.bind(this),this.put=this.put.bind(this),this.patch=this.patch.bind(this),this.delete=this.delete.bind(this),this.options=this.options.bind(this),this.connect=this.connect.bind(this),this.trace=this.trace.bind(this),this.getUid=this.getUid.bind(this),this.getUser=this.getUser.bind(this),this.addListener=this.addListener.bind(this),this.getConfig=this.getConfig.bind(this)}http(e,t){const i=new h.default(e,t,this);return this.token&&i.setHeader("Authorization",`Bearer ${this.token}`),this.csrfToken&&i.setHeader("X-CSRF-TOKEN",this.csrfToken),i}get(e){return this.http("GET",e).request()}head(e){return this.http("HEAD",e).request()}post(e){return this.http("POST",e).request()}put(e){return this.http("PUT",e).request()}patch(e){return this.http("PATCH",e).request()}delete(e){return this.http("DELETE",e).request()}options(e){return this.http("OPTIONS",e).request()}connect(e){return this.http("CONNECT",e).request()}trace(e){return this.http("TRACE",e).request()}addListener(e,t){return this.events.addListener(e,t)}setToken(e){this.token=e,this.events.emit("token-update",e)}setCSRFToken(e){this.csrfToken=e,this.events.emit("csrf-token-update",e)}getConfig(){return this.config}getToken(){return this.token}getCSRFToken(){return this.csrfToken}getUid(){return"object"==typeof this._user&&"id"in this._user?Number(this._user.id):0}getUser(){return this._user}get uid(){return this.getUid()}get user(){return this.getUser()}get host(){return this.config.host}setLogin(e,t=""){this.setUser(e),this.setToken(t),this.events.emit("login",{user:e,token:t})}setUser(e){this._user=e,this.events.emit("user-set",e)}updateUser(e){const t=Object.assign(Object.assign({},this.user),e);this.setUser(t),this.events.emit("user-update",t)}logout(){return r(this,void 0,void 0,(function*(){const e=this.user;this.setUser({}),this.setToken(""),this.events.emit("logout",e)}))}}s=o,Object.defineProperty(o,"instance",{enumerable:!0,configurable:!0,writable:!0,value:new o({host:""})}),Object.defineProperty(o,"http",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.http}),Object.defineProperty(o,"get",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.get}),Object.defineProperty(o,"head",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.head}),Object.defineProperty(o,"post",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.post}),Object.defineProperty(o,"put",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.put}),Object.defineProperty(o,"patch",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.patch}),Object.defineProperty(o,"delete",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.delete}),Object.defineProperty(o,"options",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.options}),Object.defineProperty(o,"connect",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.connect}),Object.defineProperty(o,"trace",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.trace}),Object.defineProperty(o,"setConfig",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.setConfig}),Object.defineProperty(o,"setHost",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.setHost}),Object.defineProperty(o,"setUser",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.setUser}),Object.defineProperty(o,"updateUser",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.updateUser}),Object.defineProperty(o,"logout",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.logout}),Object.defineProperty(o,"setToken",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.setToken}),Object.defineProperty(o,"setLogin",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.setLogin}),Object.defineProperty(o,"addListener",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.addListener}),Object.defineProperty(o,"setInitData",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.setInitData}),Object.defineProperty(o,"setCSRFToken",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.setCSRFToken}),Object.defineProperty(o,"getCSRFToken",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.getCSRFToken}),Object.defineProperty(o,"triggerByCacheGroup",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.triggerByCacheGroup}),Object.defineProperty(o,"clearCacheGroup",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.clearCacheGroup}),Object.defineProperty(o,"updateCacheGroup",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.updateCacheGroup}),Object.defineProperty(o,"getToken",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.getToken}),Object.defineProperty(o,"getUid",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.getUid}),Object.defineProperty(o,"getUser",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.getUser}),Object.defineProperty(o,"user",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.user}),Object.defineProperty(o,"uid",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.uid}),Object.defineProperty(o,"deleteCacheGroup",{enumerable:!0,configurable:!0,writable:!0,value:s.instance.clearCacheGroup}),globalThis.apiSetInitData=o.setInitData,t.default=o}},t={},function i(s){var r=t[s];if(void 0!==r)return r.exports;var a=t[s]={exports:{}};return e[s].call(a.exports,a,a.exports,i),a.exports}(607);var e,t}));
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defProps = Object.defineProperties;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
9
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
10
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
12
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
13
|
+
var __spreadValues = (a, b) => {
|
|
14
|
+
for (var prop in b || (b = {}))
|
|
15
|
+
if (__hasOwnProp.call(b, prop))
|
|
16
|
+
__defNormalProp(a, prop, b[prop]);
|
|
17
|
+
if (__getOwnPropSymbols)
|
|
18
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
19
|
+
if (__propIsEnum.call(b, prop))
|
|
20
|
+
__defNormalProp(a, prop, b[prop]);
|
|
21
|
+
}
|
|
22
|
+
return a;
|
|
23
|
+
};
|
|
24
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
25
|
+
var __export = (target, all) => {
|
|
26
|
+
for (var name in all)
|
|
27
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
28
|
+
};
|
|
29
|
+
var __copyProps = (to, from, except, desc) => {
|
|
30
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
31
|
+
for (let key of __getOwnPropNames(from))
|
|
32
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
33
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
34
|
+
}
|
|
35
|
+
return to;
|
|
36
|
+
};
|
|
37
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
38
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
39
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
40
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
41
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
42
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
43
|
+
mod
|
|
44
|
+
));
|
|
45
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
46
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
47
|
+
var __async = (__this, __arguments, generator) => {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
var fulfilled = (value) => {
|
|
50
|
+
try {
|
|
51
|
+
step(generator.next(value));
|
|
52
|
+
} catch (e) {
|
|
53
|
+
reject(e);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
var rejected = (value) => {
|
|
57
|
+
try {
|
|
58
|
+
step(generator.throw(value));
|
|
59
|
+
} catch (e) {
|
|
60
|
+
reject(e);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
64
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// src/index.ts
|
|
69
|
+
var index_exports = {};
|
|
70
|
+
__export(index_exports, {
|
|
71
|
+
default: () => index_default
|
|
72
|
+
});
|
|
73
|
+
module.exports = __toCommonJS(index_exports);
|
|
74
|
+
|
|
75
|
+
// src/Core.ts
|
|
76
|
+
var import_easy_event_emitter = __toESM(require("easy-event-emitter"));
|
|
77
|
+
var Core = class {
|
|
78
|
+
constructor(context) {
|
|
79
|
+
__publicField(this, "cache");
|
|
80
|
+
__publicField(this, "initData");
|
|
81
|
+
__publicField(this, "events");
|
|
82
|
+
__publicField(this, "config");
|
|
83
|
+
__publicField(this, "headers", {});
|
|
84
|
+
this.events = new import_easy_event_emitter.default();
|
|
85
|
+
this.config = {
|
|
86
|
+
host: "http://127.0.0.1/"
|
|
87
|
+
};
|
|
88
|
+
this.cache = {};
|
|
89
|
+
this.initData = {};
|
|
90
|
+
this.setConfig = this.setConfig.bind(this);
|
|
91
|
+
this.getConfig = this.getConfig.bind(this);
|
|
92
|
+
this.setHost = this.setHost.bind(this);
|
|
93
|
+
this.setInitData = this.setInitData.bind(this);
|
|
94
|
+
this.triggerByCacheGroup = this.triggerByCacheGroup.bind(this);
|
|
95
|
+
this.clearCacheGroup = this.clearCacheGroup.bind(this);
|
|
96
|
+
this.updateCacheGroup = this.updateCacheGroup.bind(this);
|
|
97
|
+
this.setGlobalInitData = this.setGlobalInitData.bind(this);
|
|
98
|
+
if (context) {
|
|
99
|
+
Object.assign(this, {
|
|
100
|
+
config: context == null ? void 0 : context.config,
|
|
101
|
+
events: context == null ? void 0 : context.events,
|
|
102
|
+
cache: context == null ? void 0 : context.cache,
|
|
103
|
+
initData: context == null ? void 0 : context.initData,
|
|
104
|
+
headers: context.headers
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
setConfig(config) {
|
|
109
|
+
this.config = config;
|
|
110
|
+
}
|
|
111
|
+
getConfig() {
|
|
112
|
+
return this.config;
|
|
113
|
+
}
|
|
114
|
+
getCache(key) {
|
|
115
|
+
if (this.cache[key]) {
|
|
116
|
+
if ((/* @__PURE__ */ new Date()).getTime() <= this.cache[key].time + 36e8) {
|
|
117
|
+
return this.cache[key].data;
|
|
118
|
+
} else {
|
|
119
|
+
delete this.cache[key];
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
isJsonString(str) {
|
|
124
|
+
try {
|
|
125
|
+
JSON.parse(str);
|
|
126
|
+
} catch (e) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
compileRevivers() {
|
|
132
|
+
const fieldRevivers = {};
|
|
133
|
+
const objectRevivers = [];
|
|
134
|
+
const replace = this.config.dataReviver;
|
|
135
|
+
if (!replace) return { fieldRevivers, objectRevivers };
|
|
136
|
+
for (const [key, fn] of Object.entries(replace)) {
|
|
137
|
+
if (typeof fn !== "function") continue;
|
|
138
|
+
if (key.includes("&")) {
|
|
139
|
+
objectRevivers.push({
|
|
140
|
+
keys: new Set(key.split("&")),
|
|
141
|
+
fn
|
|
142
|
+
});
|
|
143
|
+
} else {
|
|
144
|
+
fieldRevivers[key] = fn;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return { fieldRevivers, objectRevivers };
|
|
148
|
+
}
|
|
149
|
+
applyObjectRevivers(value, objectRevivers, depth = 0, isArrayItem = false) {
|
|
150
|
+
if (!value || typeof value !== "object") {
|
|
151
|
+
return value;
|
|
152
|
+
}
|
|
153
|
+
if (Array.isArray(value)) {
|
|
154
|
+
for (let i = 0; i < value.length; i++) {
|
|
155
|
+
value[i] = this.applyObjectRevivers(value[i], objectRevivers, depth + 1, true);
|
|
156
|
+
}
|
|
157
|
+
return value;
|
|
158
|
+
}
|
|
159
|
+
let obj = value;
|
|
160
|
+
const shouldApply = depth === 0 || isArrayItem;
|
|
161
|
+
if (shouldApply) {
|
|
162
|
+
for (const rule of objectRevivers) {
|
|
163
|
+
const keys = Object.keys(obj);
|
|
164
|
+
if (rule.keys.size > keys.length) continue;
|
|
165
|
+
let match = true;
|
|
166
|
+
for (const k of rule.keys) {
|
|
167
|
+
if (!(k in obj)) {
|
|
168
|
+
match = false;
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (match) {
|
|
173
|
+
obj = rule.fn(obj);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
for (const k of Object.keys(obj)) {
|
|
178
|
+
obj[k] = this.applyObjectRevivers(obj[k], objectRevivers, depth + 1, false);
|
|
179
|
+
}
|
|
180
|
+
return obj;
|
|
181
|
+
}
|
|
182
|
+
dataPrepare(data) {
|
|
183
|
+
const json = JSON.stringify(data);
|
|
184
|
+
return this.jsonParse(json);
|
|
185
|
+
}
|
|
186
|
+
jsonParse(str) {
|
|
187
|
+
if (typeof this.config.reviver === "function") {
|
|
188
|
+
return JSON.parse(str, this.config.reviver);
|
|
189
|
+
}
|
|
190
|
+
const { fieldRevivers, objectRevivers } = this.compileRevivers();
|
|
191
|
+
const data = JSON.parse(str, (key, value) => {
|
|
192
|
+
const reviver = fieldRevivers[key];
|
|
193
|
+
return reviver ? reviver(value) : value;
|
|
194
|
+
});
|
|
195
|
+
return this.applyObjectRevivers(data, objectRevivers);
|
|
196
|
+
}
|
|
197
|
+
setHost(host) {
|
|
198
|
+
this.config.host = host;
|
|
199
|
+
}
|
|
200
|
+
setHeader(name, value) {
|
|
201
|
+
this.headers[name] = value;
|
|
202
|
+
this.events.emit("header-update", {
|
|
203
|
+
name,
|
|
204
|
+
value
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
getHeader(name) {
|
|
208
|
+
return this.headers[name];
|
|
209
|
+
}
|
|
210
|
+
getHeaders() {
|
|
211
|
+
return Object.entries(this.headers).map(([name, value]) => ({ name, value }));
|
|
212
|
+
}
|
|
213
|
+
hasHeader(name) {
|
|
214
|
+
return this.headers[name] ? true : false;
|
|
215
|
+
}
|
|
216
|
+
deleteHeader(name) {
|
|
217
|
+
delete this.headers[name];
|
|
218
|
+
}
|
|
219
|
+
setCache(key, data, group) {
|
|
220
|
+
this.cache[key] = {
|
|
221
|
+
time: (/* @__PURE__ */ new Date()).getTime(),
|
|
222
|
+
data,
|
|
223
|
+
group
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
groupFromArray(groups, callback, withoutSplit = false) {
|
|
227
|
+
const currentCallback = (group) => {
|
|
228
|
+
if (typeof group === "string" && !withoutSplit) {
|
|
229
|
+
const params = group.split(".");
|
|
230
|
+
const current = params.shift();
|
|
231
|
+
callback(current, params);
|
|
232
|
+
} else {
|
|
233
|
+
callback(group);
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
if (Array.isArray(groups)) {
|
|
237
|
+
groups.map(currentCallback);
|
|
238
|
+
} else if (typeof groups === "string") {
|
|
239
|
+
currentCallback(groups);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
triggerByCacheGroup(groups) {
|
|
243
|
+
this.groupFromArray(groups, (group) => this.events.emit("trigger-request-by-cache-" + group, void 0));
|
|
244
|
+
}
|
|
245
|
+
clearCacheGroup(groups, data, fieldKey = "id") {
|
|
246
|
+
this.groupFromArray(groups, (group) => {
|
|
247
|
+
if (data) {
|
|
248
|
+
const clear = (cacheData) => {
|
|
249
|
+
if (fieldKey && (cacheData == null ? void 0 : cacheData[fieldKey]) === (data == null ? void 0 : data[fieldKey]) || fieldKey === null) {
|
|
250
|
+
for (const key in cacheData) {
|
|
251
|
+
if (key in data) {
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return true;
|
|
257
|
+
};
|
|
258
|
+
for (const key in this.cache) {
|
|
259
|
+
if (this.cache[key].group === group) {
|
|
260
|
+
if (Array.isArray(this.cache[key].data)) {
|
|
261
|
+
this.cache[key].data = Object.values(this.cache[key].data).filter(clear);
|
|
262
|
+
} else {
|
|
263
|
+
if (!clear(this.cache[key].data))
|
|
264
|
+
delete this.cache[key];
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
} else {
|
|
269
|
+
for (const key in this.cache) {
|
|
270
|
+
if (this.cache[key].group === group) {
|
|
271
|
+
delete this.cache[key];
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
setDataTree(obj, path, val) {
|
|
278
|
+
const keys = path.split(".");
|
|
279
|
+
const lastKey = keys.pop();
|
|
280
|
+
const lastObj = keys.reduce(
|
|
281
|
+
(obj2, key) => obj2[key] = obj2[key] || {},
|
|
282
|
+
obj
|
|
283
|
+
);
|
|
284
|
+
if (typeof lastKey === "string") {
|
|
285
|
+
lastObj[lastKey] = val;
|
|
286
|
+
}
|
|
287
|
+
return obj;
|
|
288
|
+
}
|
|
289
|
+
updateCacheGroup(groups, data, fieldKey = "id") {
|
|
290
|
+
const update = (cacheData) => {
|
|
291
|
+
const retData = cacheData;
|
|
292
|
+
const fieldList = fieldKey.split(".");
|
|
293
|
+
if (fieldList.length > 1) {
|
|
294
|
+
for (const key of fieldList) {
|
|
295
|
+
cacheData = cacheData[key];
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (fieldKey && cacheData[fieldKey] === data[fieldKey] || fieldKey === null) {
|
|
299
|
+
for (const key in cacheData) {
|
|
300
|
+
if (key in data) {
|
|
301
|
+
cacheData[key] = data[key];
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return retData;
|
|
306
|
+
};
|
|
307
|
+
this.groupFromArray(groups, (group, params) => {
|
|
308
|
+
for (const key in this.cache) {
|
|
309
|
+
if (this.cache[key].group === group) {
|
|
310
|
+
if (Array.isArray(this.cache[key].data)) {
|
|
311
|
+
this.cache[key].data = Object.values(this.cache[key].data).map(update);
|
|
312
|
+
} else {
|
|
313
|
+
this.cache[key].data = update(this.cache[key].data);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
appendCacheGroup(group, data) {
|
|
320
|
+
}
|
|
321
|
+
getCacheByIndex(index) {
|
|
322
|
+
return this.cache[index];
|
|
323
|
+
}
|
|
324
|
+
deleteCache(key) {
|
|
325
|
+
if (!this.cache[key])
|
|
326
|
+
return;
|
|
327
|
+
const group = this.cache[key].group;
|
|
328
|
+
if (group) {
|
|
329
|
+
this.clearCacheGroup(group);
|
|
330
|
+
}
|
|
331
|
+
delete this.cache[key];
|
|
332
|
+
}
|
|
333
|
+
setGlobalInitData(name, data) {
|
|
334
|
+
this.initData[name] = data;
|
|
335
|
+
}
|
|
336
|
+
setInitData(data) {
|
|
337
|
+
this.initData = data;
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
var Core_default = Core;
|
|
341
|
+
|
|
342
|
+
// src/Http.ts
|
|
343
|
+
var import_md5 = __toESM(require("md5"));
|
|
344
|
+
|
|
345
|
+
// src/Queue.ts
|
|
346
|
+
var _Queue = class _Queue {
|
|
347
|
+
constructor(http) {
|
|
348
|
+
__publicField(this, "queue", _Queue._Queue);
|
|
349
|
+
__publicField(this, "http");
|
|
350
|
+
this.http = http;
|
|
351
|
+
this.has = this.has.bind(this);
|
|
352
|
+
this.get = this.get.bind(this);
|
|
353
|
+
this.clear = this.clear.bind(this);
|
|
354
|
+
this.push = this.push.bind(this);
|
|
355
|
+
}
|
|
356
|
+
has(request) {
|
|
357
|
+
return request in this.queue;
|
|
358
|
+
}
|
|
359
|
+
get(request) {
|
|
360
|
+
return this.has(request) ? this.queue[request] : void 0;
|
|
361
|
+
}
|
|
362
|
+
clear(request) {
|
|
363
|
+
if (this.has(request)) {
|
|
364
|
+
delete this.queue[request];
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
push(request) {
|
|
368
|
+
const refInstance = this.get(request);
|
|
369
|
+
if (!refInstance) {
|
|
370
|
+
this.queue[request] = this.http;
|
|
371
|
+
} else {
|
|
372
|
+
refInstance == null ? void 0 : refInstance.addListener("api-request-success", this.http.handleSuccess);
|
|
373
|
+
refInstance == null ? void 0 : refInstance.addListener("api-request-error", this.http.handleError);
|
|
374
|
+
refInstance == null ? void 0 : refInstance.addListener("api-request-fail", this.http.handleFail);
|
|
375
|
+
refInstance == null ? void 0 : refInstance.addListener("api-request-complete", this.http.handleComplete);
|
|
376
|
+
refInstance == null ? void 0 : refInstance.addListener("api-request-abort", this.http.handleAbort);
|
|
377
|
+
}
|
|
378
|
+
return refInstance instanceof Http_default;
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
__publicField(_Queue, "_Queue", {});
|
|
382
|
+
var Queue = _Queue;
|
|
383
|
+
var Queue_default = Queue;
|
|
384
|
+
|
|
385
|
+
// src/Http.ts
|
|
386
|
+
var import_easy_event_emitter2 = __toESM(require("easy-event-emitter"));
|
|
387
|
+
var Http = class _Http extends Core_default {
|
|
388
|
+
constructor(method, params, context) {
|
|
389
|
+
super(context);
|
|
390
|
+
__publicField(this, "cacheIndex");
|
|
391
|
+
__publicField(this, "currentCache");
|
|
392
|
+
__publicField(this, "currentEvents");
|
|
393
|
+
__publicField(this, "requestParams");
|
|
394
|
+
__publicField(this, "method");
|
|
395
|
+
__publicField(this, "params");
|
|
396
|
+
__publicField(this, "path");
|
|
397
|
+
__publicField(this, "queue");
|
|
398
|
+
__publicField(this, "queueName");
|
|
399
|
+
__publicField(this, "_promise");
|
|
400
|
+
__publicField(this, "xhr");
|
|
401
|
+
__publicField(this, "tryRequestCount", 0);
|
|
402
|
+
__publicField(this, "tryRequestTimeout");
|
|
403
|
+
this.currentEvents = new import_easy_event_emitter2.default();
|
|
404
|
+
this._promise = new Promise(() => {
|
|
405
|
+
});
|
|
406
|
+
this.queue = new Queue_default(this);
|
|
407
|
+
this.cacheIndex = "";
|
|
408
|
+
this.queueName = "";
|
|
409
|
+
this.method = method;
|
|
410
|
+
this.headers = __spreadValues(__spreadValues({}, this.headers), params.headers);
|
|
411
|
+
this.params = __spreadValues(__spreadProps(__spreadValues({}, this.config), {
|
|
412
|
+
headers: this.headers
|
|
413
|
+
}), params);
|
|
414
|
+
this.path = this.params.path;
|
|
415
|
+
this.requestParams = {
|
|
416
|
+
method: this.method
|
|
417
|
+
};
|
|
418
|
+
this.handleSuccess = this.handleSuccess.bind(this);
|
|
419
|
+
this.handleAbort = this.handleAbort.bind(this);
|
|
420
|
+
this.handleFail = this.handleFail.bind(this);
|
|
421
|
+
this.handleError = this.handleError.bind(this);
|
|
422
|
+
this.handleComplete = this.handleComplete.bind(this);
|
|
423
|
+
this.handleProgress = this.handleProgress.bind(this);
|
|
424
|
+
this.setEmit = this.setEmit.bind(this);
|
|
425
|
+
this.request = this.request.bind(this);
|
|
426
|
+
this.httpRequest = this.httpRequest.bind(this);
|
|
427
|
+
this.tryHttpRequest = this.tryHttpRequest.bind(this);
|
|
428
|
+
this.initRequest();
|
|
429
|
+
this.initCache();
|
|
430
|
+
this.initQueue();
|
|
431
|
+
}
|
|
432
|
+
initRequest() {
|
|
433
|
+
var _a;
|
|
434
|
+
if (typeof XMLHttpRequest !== "undefined") {
|
|
435
|
+
this.xhr = new XMLHttpRequest();
|
|
436
|
+
}
|
|
437
|
+
if (this.params.clearUndifinedData)
|
|
438
|
+
this.params.data = this.params.data ? this.cuteUndifinedParams(this.params.data) : this.params.data;
|
|
439
|
+
if (["GET", "HEAD"].includes(this.method)) {
|
|
440
|
+
if (this.params.data)
|
|
441
|
+
this.path = this.params.path + "?" + this.encodeUrlParams(this.params.data);
|
|
442
|
+
if (this.method === "HEAD") {
|
|
443
|
+
this.requestParams.withoutResponse = true;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if (["POST", "PUT", "CONNECT", "PATH"].includes(this.method)) {
|
|
447
|
+
this.requestParams.body = (_a = this.params.body) != null ? _a : JSON.stringify(this.params.data);
|
|
448
|
+
}
|
|
449
|
+
if (["PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE"].includes(this.method)) {
|
|
450
|
+
this.params.cache = false;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
initCache() {
|
|
454
|
+
var _a;
|
|
455
|
+
this.cacheIndex = (0, import_md5.default)(this.path + JSON.stringify(this.params.data));
|
|
456
|
+
if (this.params.globalName) {
|
|
457
|
+
if (this.initData[this.params.globalName]) {
|
|
458
|
+
const data = this.config.globalDataWithoutDataPrepare ? this.initData[this.params.globalName] : this.dataPrepare(this.initData[this.params.globalName]);
|
|
459
|
+
this.setCache(this.cacheIndex, data, typeof this.params.cache === "boolean" ? void 0 : this.params.cache);
|
|
460
|
+
this.params.cache = (_a = this.params.cache) != null ? _a : true;
|
|
461
|
+
delete this.initData[this.params.globalName];
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
if (this.params.cache) {
|
|
465
|
+
if (typeof this.params.cache === "string") {
|
|
466
|
+
this.events.addListener("trigger-request-by-cache-" + this.params.cache, this.request);
|
|
467
|
+
}
|
|
468
|
+
this._promise = new Promise((resolve) => __async(this, null, function* () {
|
|
469
|
+
if (this.currentCache = this.getCache(this.cacheIndex)) {
|
|
470
|
+
resolve(this.currentCache);
|
|
471
|
+
this.handleSuccess(this.currentCache);
|
|
472
|
+
this.handleComplete(this.currentCache);
|
|
473
|
+
}
|
|
474
|
+
}));
|
|
475
|
+
} else {
|
|
476
|
+
this.deleteCache(this.cacheIndex);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
get host() {
|
|
480
|
+
return this.params.host;
|
|
481
|
+
}
|
|
482
|
+
get promise() {
|
|
483
|
+
return this._promise;
|
|
484
|
+
}
|
|
485
|
+
get url() {
|
|
486
|
+
return this.params.host + this.path;
|
|
487
|
+
}
|
|
488
|
+
initQueue() {
|
|
489
|
+
this.queueName = this.params.queueThrottling ? (0, import_md5.default)(this.path) : this.cacheIndex;
|
|
490
|
+
}
|
|
491
|
+
setEmit(event, data) {
|
|
492
|
+
this.currentEvents.emit(event, data);
|
|
493
|
+
this.events.emit(event, {
|
|
494
|
+
cacheIndex: this.cacheIndex,
|
|
495
|
+
data
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
//Заголовки от 200 до 299
|
|
499
|
+
handleSuccess(data) {
|
|
500
|
+
if (typeof this.params.success === "function" && this.xhr)
|
|
501
|
+
this.params.success(data, this.xhr);
|
|
502
|
+
if (this.params.cacheUpdate && this.params.data) {
|
|
503
|
+
this.groupFromArray(this.params.cacheUpdate, (cacheUpdate, params) => {
|
|
504
|
+
var _a;
|
|
505
|
+
this.updateCacheGroup(
|
|
506
|
+
typeof cacheUpdate === "string" ? cacheUpdate : cacheUpdate.group,
|
|
507
|
+
(_a = this.params.data) != null ? _a : {},
|
|
508
|
+
typeof cacheUpdate === "string" ? void 0 : cacheUpdate.fieldKey
|
|
509
|
+
);
|
|
510
|
+
}, true);
|
|
511
|
+
}
|
|
512
|
+
if (this.params.cacheClear && this.params.data) {
|
|
513
|
+
this.groupFromArray(this.params.cacheClear, (cacheClear) => {
|
|
514
|
+
this.clearCacheGroup(
|
|
515
|
+
typeof cacheClear === "string" ? cacheClear : cacheClear.group,
|
|
516
|
+
this.params.data,
|
|
517
|
+
typeof cacheClear === "string" ? void 0 : cacheClear.fieldKey
|
|
518
|
+
);
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
this.setEmit("api-request-success", data);
|
|
522
|
+
}
|
|
523
|
+
handleAbort(data) {
|
|
524
|
+
if (typeof this.params.abort === "function") {
|
|
525
|
+
this.params.abort(data);
|
|
526
|
+
}
|
|
527
|
+
this.setEmit("api-request-abort", data);
|
|
528
|
+
}
|
|
529
|
+
//Заголовки от 400 до 499
|
|
530
|
+
handleFail(data) {
|
|
531
|
+
if (typeof this.params.fail === "function") {
|
|
532
|
+
this.params.fail(data);
|
|
533
|
+
}
|
|
534
|
+
this.setEmit("api-request-fail", data);
|
|
535
|
+
}
|
|
536
|
+
//Все остальные
|
|
537
|
+
handleError(data) {
|
|
538
|
+
var _a;
|
|
539
|
+
if (((_a = this.xhr) == null ? void 0 : _a.status) === 0 && this.xhr.readyState === 4) {
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
if (typeof this.params.error === "function") {
|
|
543
|
+
this.params.error(data);
|
|
544
|
+
}
|
|
545
|
+
this.setEmit("api-request-error", data);
|
|
546
|
+
}
|
|
547
|
+
handleComplete(data) {
|
|
548
|
+
if (typeof this.params.complete === "function") {
|
|
549
|
+
this.params.complete(data);
|
|
550
|
+
}
|
|
551
|
+
this.setEmit("api-request-complete", data);
|
|
552
|
+
}
|
|
553
|
+
handleProgress(progress) {
|
|
554
|
+
if (typeof this.params.progress === "function") {
|
|
555
|
+
this.params.progress(progress);
|
|
556
|
+
}
|
|
557
|
+
this.setEmit("api-request-progress", progress);
|
|
558
|
+
}
|
|
559
|
+
encodeUrlParams(data, prefix = "") {
|
|
560
|
+
return Object.entries(data).filter(([_, value]) => value !== void 0 && value !== null).map(([key, value]) => {
|
|
561
|
+
const encodedKey = prefix ? `${prefix}[${encodeURIComponent(key)}]` : encodeURIComponent(key);
|
|
562
|
+
if (Array.isArray(value)) {
|
|
563
|
+
if (value.length === 0) return `${encodedKey}[]=`;
|
|
564
|
+
return value.map((val) => `${encodedKey}[]=${encodeURIComponent(String(val))}`).join("&");
|
|
565
|
+
}
|
|
566
|
+
if (typeof value === "object" && value !== null) {
|
|
567
|
+
return this.encodeUrlParams(value, encodedKey);
|
|
568
|
+
}
|
|
569
|
+
return `${encodedKey}=${encodeURIComponent(String(value))}`;
|
|
570
|
+
}).filter((str) => str !== "").join("&");
|
|
571
|
+
}
|
|
572
|
+
cuteUndifinedParams(data) {
|
|
573
|
+
if (typeof data !== "object" || data === null) {
|
|
574
|
+
return data;
|
|
575
|
+
}
|
|
576
|
+
for (const key in data) {
|
|
577
|
+
if (typeof data[key] === "undefined") {
|
|
578
|
+
delete data[key];
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
return data;
|
|
582
|
+
}
|
|
583
|
+
tryHttpRequest() {
|
|
584
|
+
return new Promise((resolve, reject) => {
|
|
585
|
+
var _a;
|
|
586
|
+
if (this.params.tryRequest && this.params.tryRequest > this.tryRequestCount) {
|
|
587
|
+
if (this.tryRequestTimeout) {
|
|
588
|
+
clearTimeout(this.tryRequestTimeout);
|
|
589
|
+
}
|
|
590
|
+
this.tryRequestTimeout = setTimeout(() => {
|
|
591
|
+
var _a2;
|
|
592
|
+
(_a2 = this.xhr) == null ? void 0 : _a2.abort();
|
|
593
|
+
this.httpRequest().then((value) => {
|
|
594
|
+
resolve(value);
|
|
595
|
+
this.tryRequestCount = 0;
|
|
596
|
+
}).catch(reject);
|
|
597
|
+
}, (_a = this.params.tryRequestDelay) != null ? _a : 1e3);
|
|
598
|
+
++this.tryRequestCount;
|
|
599
|
+
} else {
|
|
600
|
+
reject();
|
|
601
|
+
}
|
|
602
|
+
});
|
|
603
|
+
}
|
|
604
|
+
fileRequest() {
|
|
605
|
+
return new Promise((resolve, reject) => __async(this, null, function* () {
|
|
606
|
+
if (!this.params.file) return;
|
|
607
|
+
const data = this.params.file;
|
|
608
|
+
const chunkSize = this.params.fileSizeChunk ? this.params.fileSizeChunk * 1024 * 1024 : this.params.file.byteLength;
|
|
609
|
+
const totalChunks = Math.ceil(this.params.file.byteLength / chunkSize);
|
|
610
|
+
let fileId = this.params.fileIdByServer ? void 0 : `file-${(/* @__PURE__ */ new Date()).getTime()}`;
|
|
611
|
+
for (let i = 0; i < totalChunks; i++) {
|
|
612
|
+
try {
|
|
613
|
+
const start = i * chunkSize;
|
|
614
|
+
const end = Math.min(start + chunkSize, data.byteLength);
|
|
615
|
+
const chunk = data.slice(start, end);
|
|
616
|
+
const headers = __spreadValues({}, this.headers);
|
|
617
|
+
let body = void 0;
|
|
618
|
+
if (this.params.fileType === "binary") {
|
|
619
|
+
const u8 = chunk instanceof Uint8Array ? chunk : new Uint8Array(chunk);
|
|
620
|
+
headers["Content-Type"] = "application/octet-stream";
|
|
621
|
+
headers["Content-Range"] = `bytes ${start}-${end - 1}/${this.params.file.byteLength}`;
|
|
622
|
+
headers["X-Chunk-Index"] = String(i);
|
|
623
|
+
headers["X-Chunk-Total"] = String(totalChunks);
|
|
624
|
+
if (fileId && (!this.params.fileIdByServer || this.params.fileIdByServer && i > 0)) {
|
|
625
|
+
headers[typeof this.params.fileIdByServer === "string" ? this.params.fileIdByServer : "X-File-ID"] = fileId;
|
|
626
|
+
}
|
|
627
|
+
body = u8;
|
|
628
|
+
} else {
|
|
629
|
+
const formData = new FormData();
|
|
630
|
+
formData.append("file", new Blob([chunk]));
|
|
631
|
+
if (fileId && (!this.params.fileIdByServer || this.params.fileIdByServer && i > 0)) {
|
|
632
|
+
formData.append(typeof this.params.fileIdByServer === "string" ? this.params.fileIdByServer : "file_id", fileId);
|
|
633
|
+
}
|
|
634
|
+
formData.append("chunk_index", String(i));
|
|
635
|
+
formData.append("total_chunks", String(totalChunks));
|
|
636
|
+
if (this.params.data && totalChunks - 1 === i) {
|
|
637
|
+
for (const key in this.params.data) {
|
|
638
|
+
formData.append(key, this.params.data[key]);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
delete headers["Content-Type"];
|
|
642
|
+
body = formData;
|
|
643
|
+
}
|
|
644
|
+
delete headers["Accept"];
|
|
645
|
+
const http = new _Http(this.method, __spreadProps(__spreadValues({}, this.params), {
|
|
646
|
+
file: void 0,
|
|
647
|
+
headers,
|
|
648
|
+
data: void 0,
|
|
649
|
+
fileSizeChunk: void 0,
|
|
650
|
+
body,
|
|
651
|
+
success: totalChunks - 1 > i ? void 0 : (data2) => {
|
|
652
|
+
resolve(data2);
|
|
653
|
+
this.handleSuccess(data2);
|
|
654
|
+
},
|
|
655
|
+
complete: totalChunks - 1 > i ? void 0 : (e) => {
|
|
656
|
+
this.handleComplete(e);
|
|
657
|
+
},
|
|
658
|
+
progress: (progres) => {
|
|
659
|
+
const loaded = start + progres.loaded;
|
|
660
|
+
this.handleProgress({
|
|
661
|
+
percent: loaded / data.byteLength * 100,
|
|
662
|
+
total: data.byteLength,
|
|
663
|
+
loaded
|
|
664
|
+
});
|
|
665
|
+
},
|
|
666
|
+
fail: reject,
|
|
667
|
+
error: reject
|
|
668
|
+
}), this);
|
|
669
|
+
this.xhr = http.xhr;
|
|
670
|
+
const result = yield http.request(true).promise;
|
|
671
|
+
if (result == null ? void 0 : result.file_id) {
|
|
672
|
+
fileId = result == null ? void 0 : result.file_id;
|
|
673
|
+
}
|
|
674
|
+
} catch (e) {
|
|
675
|
+
reject(e);
|
|
676
|
+
break;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
}));
|
|
680
|
+
}
|
|
681
|
+
httpRequest() {
|
|
682
|
+
return new Promise((resolve, reject) => {
|
|
683
|
+
var _a;
|
|
684
|
+
if (typeof this.xhr === "undefined") return;
|
|
685
|
+
try {
|
|
686
|
+
this.xhr.open(this.method, this.url, (_a = this.params.requestAsync) != null ? _a : true);
|
|
687
|
+
if (this.params.timeout) {
|
|
688
|
+
this.xhr.timeout = this.params.timeout;
|
|
689
|
+
}
|
|
690
|
+
for (const header in this.headers) {
|
|
691
|
+
this.xhr.setRequestHeader(header, this.headers[header]);
|
|
692
|
+
}
|
|
693
|
+
if (this.xhr.upload) {
|
|
694
|
+
this.xhr.upload.onprogress = ({ lengthComputable, loaded, total }) => {
|
|
695
|
+
if (lengthComputable) {
|
|
696
|
+
this.handleProgress({
|
|
697
|
+
percent: loaded / total * 100,
|
|
698
|
+
total,
|
|
699
|
+
loaded
|
|
700
|
+
});
|
|
701
|
+
}
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
this.xhr.onabort = () => {
|
|
705
|
+
reject({
|
|
706
|
+
type: "abort",
|
|
707
|
+
message: "request abort"
|
|
708
|
+
});
|
|
709
|
+
this.handleAbort({
|
|
710
|
+
message: "request abort"
|
|
711
|
+
});
|
|
712
|
+
};
|
|
713
|
+
this.xhr.ontimeout = () => {
|
|
714
|
+
this.tryHttpRequest().then(resolve).catch(reject);
|
|
715
|
+
};
|
|
716
|
+
this.xhr.onerror = (event) => __async(this, null, function* () {
|
|
717
|
+
yield this.tryHttpRequest().then(resolve).catch(reject);
|
|
718
|
+
reject({
|
|
719
|
+
type: "error",
|
|
720
|
+
message: "request error",
|
|
721
|
+
event
|
|
722
|
+
});
|
|
723
|
+
this.handleError({
|
|
724
|
+
message: "request error",
|
|
725
|
+
event
|
|
726
|
+
});
|
|
727
|
+
});
|
|
728
|
+
this.xhr.onreadystatechange = () => {
|
|
729
|
+
var _a2, _b;
|
|
730
|
+
if (((_a2 = this.xhr) == null ? void 0 : _a2.readyState) !== XMLHttpRequest.DONE) return;
|
|
731
|
+
const result = !this.requestParams.withoutResponse && this.isJsonString(this.xhr.responseText) ? this.jsonParse(this.xhr.responseText) : {};
|
|
732
|
+
if (((_b = this.xhr) == null ? void 0 : _b.status) >= 200 && this.xhr.status <= 299) {
|
|
733
|
+
if (this.params.cache) {
|
|
734
|
+
this.setCache(this.cacheIndex, result, typeof this.params.cache === "boolean" ? void 0 : this.params.cache);
|
|
735
|
+
}
|
|
736
|
+
resolve(result);
|
|
737
|
+
this.handleSuccess(result);
|
|
738
|
+
} else {
|
|
739
|
+
if (this.xhr.status >= 100 && this.xhr.status <= 500) {
|
|
740
|
+
reject(result);
|
|
741
|
+
this.handleFail(result);
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
this.handleComplete(result);
|
|
745
|
+
this.queue.clear(this.queueName);
|
|
746
|
+
};
|
|
747
|
+
this.xhr.send(this.requestParams.body);
|
|
748
|
+
} catch (e) {
|
|
749
|
+
reject(e);
|
|
750
|
+
}
|
|
751
|
+
});
|
|
752
|
+
}
|
|
753
|
+
streamRequest() {
|
|
754
|
+
return new Promise((resolve, reject) => {
|
|
755
|
+
fetch(this.url, {
|
|
756
|
+
method: this.method,
|
|
757
|
+
headers: this.headers,
|
|
758
|
+
body: this.requestParams.body
|
|
759
|
+
}).then((response) => {
|
|
760
|
+
this.handleSuccess(response);
|
|
761
|
+
resolve(response);
|
|
762
|
+
}).catch((e) => {
|
|
763
|
+
this.handleFail(e);
|
|
764
|
+
reject(e);
|
|
765
|
+
});
|
|
766
|
+
});
|
|
767
|
+
}
|
|
768
|
+
request(force) {
|
|
769
|
+
if (!force && !this.params.forceRequest && (this.currentCache || this.queue.push(this.queueName))) return this;
|
|
770
|
+
this._promise = new Promise((resolve, reject) => __async(this, null, function* () {
|
|
771
|
+
try {
|
|
772
|
+
let result;
|
|
773
|
+
if (this.params.stream) {
|
|
774
|
+
result = yield this.streamRequest();
|
|
775
|
+
} else if (this.params.file) {
|
|
776
|
+
result = yield this.fileRequest();
|
|
777
|
+
} else {
|
|
778
|
+
result = yield this.httpRequest();
|
|
779
|
+
}
|
|
780
|
+
resolve(result);
|
|
781
|
+
} catch (e) {
|
|
782
|
+
reject(e);
|
|
783
|
+
}
|
|
784
|
+
}));
|
|
785
|
+
return this;
|
|
786
|
+
}
|
|
787
|
+
abort() {
|
|
788
|
+
var _a;
|
|
789
|
+
(_a = this.xhr) == null ? void 0 : _a.abort();
|
|
790
|
+
}
|
|
791
|
+
addListener(event, callback) {
|
|
792
|
+
return this.currentEvents.addListener(event, callback);
|
|
793
|
+
}
|
|
794
|
+
updateCache(data, fieldKey = "id") {
|
|
795
|
+
const cache = this.getCacheByIndex(this.cacheIndex);
|
|
796
|
+
if (cache && cache.group) {
|
|
797
|
+
this.updateCacheGroup(cache.group, data, fieldKey);
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
clearCache(data, fieldKey = "id") {
|
|
801
|
+
if (data) {
|
|
802
|
+
const cache = this.getCacheByIndex(this.cacheIndex);
|
|
803
|
+
if (cache && cache.group) {
|
|
804
|
+
this.clearCacheGroup(cache.group, data, fieldKey);
|
|
805
|
+
}
|
|
806
|
+
} else {
|
|
807
|
+
this.deleteCache(this.cacheIndex);
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
};
|
|
811
|
+
var Http_default = Http;
|
|
812
|
+
|
|
813
|
+
// src/index.ts
|
|
814
|
+
var _API = class _API extends Core_default {
|
|
815
|
+
constructor(config) {
|
|
816
|
+
super();
|
|
817
|
+
__publicField(this, "_user");
|
|
818
|
+
__publicField(this, "token");
|
|
819
|
+
__publicField(this, "csrfToken");
|
|
820
|
+
this.headers = __spreadValues({
|
|
821
|
+
"Accept": "application/json",
|
|
822
|
+
"Content-Type": "application/json"
|
|
823
|
+
}, config.headers);
|
|
824
|
+
this.setConfig(config);
|
|
825
|
+
this._user = {};
|
|
826
|
+
this.setToken = this.setToken.bind(this);
|
|
827
|
+
this.setCSRFToken = this.setCSRFToken.bind(this);
|
|
828
|
+
this.setLogin = this.setLogin.bind(this);
|
|
829
|
+
this.setUser = this.setUser.bind(this);
|
|
830
|
+
this.updateUser = this.updateUser.bind(this);
|
|
831
|
+
this.logout = this.logout.bind(this);
|
|
832
|
+
this.getToken = this.getToken.bind(this);
|
|
833
|
+
this.getCSRFToken = this.getCSRFToken.bind(this);
|
|
834
|
+
this.http = this.http.bind(this);
|
|
835
|
+
this.get = this.get.bind(this);
|
|
836
|
+
this.head = this.head.bind(this);
|
|
837
|
+
this.post = this.post.bind(this);
|
|
838
|
+
this.put = this.put.bind(this);
|
|
839
|
+
this.patch = this.patch.bind(this);
|
|
840
|
+
this.delete = this.delete.bind(this);
|
|
841
|
+
this.options = this.options.bind(this);
|
|
842
|
+
this.connect = this.connect.bind(this);
|
|
843
|
+
this.trace = this.trace.bind(this);
|
|
844
|
+
this.getUid = this.getUid.bind(this);
|
|
845
|
+
this.getUser = this.getUser.bind(this);
|
|
846
|
+
this.addListener = this.addListener.bind(this);
|
|
847
|
+
this.getConfig = this.getConfig.bind(this);
|
|
848
|
+
}
|
|
849
|
+
http(method, params) {
|
|
850
|
+
const http = new Http_default(method, params, this);
|
|
851
|
+
if (this.token) http.setHeader("Authorization", `Bearer ${this.token}`);
|
|
852
|
+
if (this.csrfToken) http.setHeader("X-CSRF-TOKEN", this.csrfToken);
|
|
853
|
+
return http;
|
|
854
|
+
}
|
|
855
|
+
get(params) {
|
|
856
|
+
return this.http("GET", params).request();
|
|
857
|
+
}
|
|
858
|
+
head(params) {
|
|
859
|
+
return this.http("HEAD", params).request();
|
|
860
|
+
}
|
|
861
|
+
post(params) {
|
|
862
|
+
return this.http("POST", params).request();
|
|
863
|
+
}
|
|
864
|
+
put(params) {
|
|
865
|
+
return this.http("PUT", params).request();
|
|
866
|
+
}
|
|
867
|
+
patch(params) {
|
|
868
|
+
return this.http("PATCH", params).request();
|
|
869
|
+
}
|
|
870
|
+
delete(params) {
|
|
871
|
+
return this.http("DELETE", params).request();
|
|
872
|
+
}
|
|
873
|
+
options(params) {
|
|
874
|
+
return this.http("OPTIONS", params).request();
|
|
875
|
+
}
|
|
876
|
+
connect(params) {
|
|
877
|
+
return this.http("CONNECT", params).request();
|
|
878
|
+
}
|
|
879
|
+
trace(params) {
|
|
880
|
+
return this.http("TRACE", params).request();
|
|
881
|
+
}
|
|
882
|
+
addListener(event, callback) {
|
|
883
|
+
return this.events.addListener(event, callback);
|
|
884
|
+
}
|
|
885
|
+
setToken(token) {
|
|
886
|
+
this.token = token;
|
|
887
|
+
this.events.emit("token-update", token);
|
|
888
|
+
}
|
|
889
|
+
setCSRFToken(csrfToken) {
|
|
890
|
+
this.csrfToken = csrfToken;
|
|
891
|
+
this.events.emit("csrf-token-update", csrfToken);
|
|
892
|
+
}
|
|
893
|
+
getConfig() {
|
|
894
|
+
return this.config;
|
|
895
|
+
}
|
|
896
|
+
getToken() {
|
|
897
|
+
return this.token;
|
|
898
|
+
}
|
|
899
|
+
getCSRFToken() {
|
|
900
|
+
return this.csrfToken;
|
|
901
|
+
}
|
|
902
|
+
getUid() {
|
|
903
|
+
return typeof this._user === "object" && "id" in this._user ? Number(this._user.id) : 0;
|
|
904
|
+
}
|
|
905
|
+
getUser() {
|
|
906
|
+
return this._user;
|
|
907
|
+
}
|
|
908
|
+
get uid() {
|
|
909
|
+
return this.getUid();
|
|
910
|
+
}
|
|
911
|
+
get user() {
|
|
912
|
+
return this.getUser();
|
|
913
|
+
}
|
|
914
|
+
get host() {
|
|
915
|
+
return this.config.host;
|
|
916
|
+
}
|
|
917
|
+
setLogin(user, token = "") {
|
|
918
|
+
this.setUser(user);
|
|
919
|
+
this.setToken(token);
|
|
920
|
+
this.events.emit("login", {
|
|
921
|
+
user,
|
|
922
|
+
token
|
|
923
|
+
});
|
|
924
|
+
}
|
|
925
|
+
setUser(user) {
|
|
926
|
+
this._user = user;
|
|
927
|
+
this.events.emit("user-set", user);
|
|
928
|
+
}
|
|
929
|
+
updateUser(user) {
|
|
930
|
+
const updatedUser = __spreadValues(__spreadValues({}, this.user), user);
|
|
931
|
+
this.setUser(updatedUser);
|
|
932
|
+
this.events.emit("user-update", updatedUser);
|
|
933
|
+
}
|
|
934
|
+
logout() {
|
|
935
|
+
return __async(this, null, function* () {
|
|
936
|
+
const user = this.user;
|
|
937
|
+
this.setUser({});
|
|
938
|
+
this.setToken("");
|
|
939
|
+
this.events.emit("logout", user);
|
|
940
|
+
});
|
|
941
|
+
}
|
|
942
|
+
};
|
|
943
|
+
__publicField(_API, "instance", new _API({
|
|
944
|
+
host: ""
|
|
945
|
+
}));
|
|
946
|
+
__publicField(_API, "http", _API.instance.http);
|
|
947
|
+
__publicField(_API, "get", _API.instance.get);
|
|
948
|
+
__publicField(_API, "head", _API.instance.head);
|
|
949
|
+
__publicField(_API, "post", _API.instance.post);
|
|
950
|
+
__publicField(_API, "put", _API.instance.put);
|
|
951
|
+
__publicField(_API, "patch", _API.instance.patch);
|
|
952
|
+
__publicField(_API, "delete", _API.instance.delete);
|
|
953
|
+
__publicField(_API, "options", _API.instance.options);
|
|
954
|
+
__publicField(_API, "connect", _API.instance.connect);
|
|
955
|
+
__publicField(_API, "trace", _API.instance.trace);
|
|
956
|
+
__publicField(_API, "setConfig", _API.instance.setConfig);
|
|
957
|
+
__publicField(_API, "setHost", _API.instance.setHost);
|
|
958
|
+
__publicField(_API, "setUser", _API.instance.setUser);
|
|
959
|
+
__publicField(_API, "updateUser", _API.instance.updateUser);
|
|
960
|
+
__publicField(_API, "logout", _API.instance.logout);
|
|
961
|
+
__publicField(_API, "setToken", _API.instance.setToken);
|
|
962
|
+
__publicField(_API, "setLogin", _API.instance.setLogin);
|
|
963
|
+
__publicField(_API, "addListener", _API.instance.addListener);
|
|
964
|
+
__publicField(_API, "setInitData", _API.instance.setInitData);
|
|
965
|
+
__publicField(_API, "setCSRFToken", _API.instance.setCSRFToken);
|
|
966
|
+
__publicField(_API, "getCSRFToken", _API.instance.getCSRFToken);
|
|
967
|
+
__publicField(_API, "triggerByCacheGroup", _API.instance.triggerByCacheGroup);
|
|
968
|
+
__publicField(_API, "clearCacheGroup", _API.instance.clearCacheGroup);
|
|
969
|
+
__publicField(_API, "updateCacheGroup", _API.instance.updateCacheGroup);
|
|
970
|
+
__publicField(_API, "getToken", _API.instance.getToken);
|
|
971
|
+
__publicField(_API, "getUid", _API.instance.getUid);
|
|
972
|
+
__publicField(_API, "getUser", _API.instance.getUser);
|
|
973
|
+
__publicField(_API, "user", _API.instance.user);
|
|
974
|
+
__publicField(_API, "uid", _API.instance.uid);
|
|
975
|
+
/**
|
|
976
|
+
* @deprecated The method should not be used
|
|
977
|
+
*/
|
|
978
|
+
__publicField(_API, "deleteCacheGroup", _API.instance.clearCacheGroup);
|
|
979
|
+
var API = _API;
|
|
980
|
+
globalThis.apiSetInitData = API.setInitData;
|
|
981
|
+
var index_default = API;
|
|
3
982
|
//# sourceMappingURL=index.js.map
|