UNPKG

mid-apis

Version:

Apis的中间层,接收数据并上传给服务器

2 lines (1 loc) 2.05 kB
var MidApis=function(){"use strict";const t=Array.isArray,e="apis_data";class s extends class{init(){this.data=Object.create(null),"undefined"!=typeof window?this._this=window.localStorage:this._this=null}getItem(t){return t?this._this?JSON.parse(this._this.getItem(t)):this.data[t]:this.data}setItem(t,e){this._this?this._this.setItem(t,JSON.stringify(e)):this.data[t]=e}removeItem(t){this._this?this._this.removeItem(t):delete this.data[t]}clear(){this._this?this._this.clear():this.init()}}{constructor(){super(),this.init(),this.get()}get(){return this.arr=this.getItem(e)||[],this.arr}set(t){this.arr.push(t),this.setItem(e,this.arr)}clearAll(){this.removeItem(e)}}class i{constructor(t){this.init(t)}init(t){const{project:e,pageUrl:s,realUrl:i,resourceUrl:r,category:h,sec_category:o,level:a,content:n,rowNum:c,colNum:l,timestamp:g,unionId:m,tags:d}=t||{};this.project=e||"",this.pageUrl=s||"",this.realUrl=i||"",this.resourceUrl=r||"",this.category=h||"jsError",this.sec_category=o||"",this.level=a||"error",this.content=n||"",this.rowNum=c||NaN,this.colNum=l||NaN,this.timestamp=g||NaN,this.unionId=m||"",this.tags=d||{}}}return class{constructor(){this.init()}init(){this.storage=new s,this.preReporter(),this._opt=new i}preReporter(){const e=this.storage.get();t(e)&&e.length>0&&this.reporter(e),this.isTimeOut(),this.isMaxCache()}debug(t){if("dev"===t)return this.storage.get();throw new Error("this mode is not dev")}addError(t,e,s=!1){if(!t)throw new Error("message is not defined");if(!e)throw new Error("opt is not defined");Object.assign(this._opt,e),s||(this.storage.set({message:t,...this._opt}),this.time||(this.time=(new Date).getTime()))}isTimeOut(){const t=this.storage.get();if(this.time){const e=(new Date).getTime();60===Math.floor(e/1e3-this.time/1e3)&&this.reporter(t)}}isMaxCache(){const e=this.storage.get();t(e)&&100===e.length&&this.reporter(e)}reporter(e){if(!e)throw new Error("data is not defined");if(!t(e))throw new Error("reporter data is error");if(0===e.length)throw new Error("reporter data is null")}}}();