UNPKG

@heimdallr-sdk/wx-request

Version:
2 lines (1 loc) 4.19 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).HEIMDALLR_WX_REQUEST=t()}(this,(function(){"use strict";var e,t=((e=t||{}).NAME="@heimdallr-sdk",e),E=(e=>(e[e.FATAL=1]="FATAL",e[e.ERROR=2]="ERROR",e[e.WARN=3]="WARN",e[e.INFO=4]="INFO",e[e.DEBUG=5]="DEBUG",e))(E||{}),r=(e=>(e.SESSION_ID="HEIMDALLR_SDK_SESSION_ID",e.USER_ID="HEIMDALLR_SDK_USER_ID",e.APP="HEIMDALLR_SDK_APP_ID",e))(r||{}),O=(e=>(e.GET="GET",e.POST="POST",e.PUT="PUT",e.DELETE="DELETE",e))(O||{}),R=(e=>(e[e.LIFECYCLE=1]="LIFECYCLE",e[e.ERROR=2]="ERROR",e[e.PERFORMANCE=3]="PERFORMANCE",e[e.API=4]="API",e[e.DOM=5]="DOM",e[e.ROUTE=6]="ROUTE",e[e.CONSOLE=7]="CONSOLE",e[e.RECORD=8]="RECORD",e[e.VUE=9]="VUE",e[e.CUSTOMER=10]="CUSTOMER",e[e.EXTEND=11]="EXTEND",e))(R||{}),s=(e=>(e[e.BEACON=1]="BEACON",e[e.IMG=2]="IMG",e[e.GET=3]="GET",e[e.POST=4]="POST",e))(s||{}),n=(e=>(e.LOG="log",e.INFO="info",e.WARN="warn",e.ERROR="error",e.ASSERT="assert",e))(n||{}),o=(e=>(e[e.XHR=41]="XHR",e[e.FETCH=42]="FETCH",e))(o||{}),i=(e=>(e[e.HASH=61]="HASH",e[e.HISTORY=62]="HISTORY",e))(i||{}),a=(e=>(e.LOCAL="local",e.SESSION="session",e.COOKIE="cookie",e.GLOBAL="global",e))(a||{}),l=(e=>(e[e.LOAD=11]="LOAD",e[e.UNLOAD=12]="UNLOAD",e))(l||{}),L=(e=>(e[e.MOBILE=1]="MOBILE",e[e.PC=2]="PC",e))(L||{}),C=(e=>(e[e.BROWSER=1]="BROWSER",e[e.WECHAT=2]="WECHAT",e[e.NODE=3]="NODE",e))(C||{}),c=(e=>(e[e.ROUTE=11]="ROUTE",e[e.CLICK=12]="CLICK",e[e.CONSOLE=13]="CONSOLE",e[e.XHR=14]="XHR",e[e.FETCH=15]="FETCH",e[e.UNHANDLEDREJECTION=16]="UNHANDLEDREJECTION",e[e.RESOURCE=17]="RESOURCE",e[e.CODE_ERROR=18]="CODE_ERROR",e[e.CUSTOMER=19]="CUSTOMER",e[e.FRAMEWORK=20]="FRAMEWORK",e[e.LIFECYCLE=21]="LIFECYCLE",e[e.CRASH=22]="CRASH",e))(c||{}),T=(e=>(e[e.API=21]="API",e[e.ROUTE=22]="ROUTE",e[e.CLICK=23]="CLICK",e[e.ERROR=24]="ERROR",e[e.LIFECYCLE=25]="LIFECYCLE",e[e.CUSTOMER=26]="CUSTOMER",e))(T||{});const A=e=>{if(!e)return"";/^[a-zA-Z]+:\/\//.test(e)||(e=e.startsWith("/")?`http://dummyhost${e}`:`http://${e}`);const t=e.match(/^[a-zA-Z]+:\/\/(?:[^/?#]*@)?([^/?#]*)(\/[^?#]*)?/);let E=(null==t?void 0:t[2])||"";return E.endsWith("/")&&E.length>1&&(E=E.slice(0,-1)),E||"/"};var I=(e=>(e.REQUEST="request",e.DOWNLOAD_FILE="downloadFile",e.UPLOAD_FILE="uploadFile",e))(I||{}),u=Object.defineProperty,S=Object.defineProperties,D=Object.getOwnPropertyDescriptors,f=Object.getOwnPropertySymbols,d=Object.prototype.hasOwnProperty,x=Object.prototype.propertyIsEnumerable,N=(e,t,E)=>t in e?u(e,t,{enumerable:!0,configurable:!0,writable:!0,value:E}):e[t]=E,U=(e,t)=>{for(var E in t||(t={}))d.call(t,E)&&N(e,E,t[E]);if(f)for(var E of f(t))x.call(t,E)&&N(e,E,t[E]);return e};return function(e={}){const{ignoreUrls:t=[],reportResponds:r=!1}=e;return{name:"wxRequestPlugin",monitor(e){const{initUrl:E,uploadUrl:R}=this.getContext(),s=this,n=[...t,R,E].map((e=>A(e)));[I.REQUEST,I.DOWNLOAD_FILE,I.UPLOAD_FILE].forEach((t=>{const E=wx[t];Object.defineProperty(wx,t,{writable:!0,enumerable:!0,configurable:!0,value:function(...R){const o=R[0],{url:i}=o;if(n.includes(A(i)))return E.call(this,o);let a,l="";switch(l=o.method?o.method:t===I.DOWNLOAD_FILE?O.GET:O.POST,t){case I.REQUEST:a=o.data;break;case I.DOWNLOAD_FILE:a={filePath:o.filePath};break;case I.UPLOAD_FILE:a={filePath:o.filePath,name:o.name}}const L=s.getTime(),C={req:{url:i,m:l,dat:a},res:{},t:L},c=o.success,T=o.fail,u=(f=U({},o),S(f,D({success:function(t){const E=s.getTime();if(r&&(C.res.dat=t.data),C.et=E-L,C.res.sta=t.statusCode,C.res.msg=t.errMsg,e(C),"function"==typeof c)return c(t)},fail:function(t){const E=s.getTime();if(C.et=E-L,C.res.msg=t.errMsg,C.res.sta=0,e(C),"function"==typeof T)return T(t)}})));var f;return E.call(this,u)}})}))},transform(e){const t="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(e=>{const t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)})),{req:{m:r,url:O,dat:s},et:n,res:{sta:i}}=e;return this.breadcrumb.unshift({lid:t,bt:T.API,l:0===i?E.WARN:E.INFO,message:`${r} "${O}" width "${JSON.stringify(s)}" took ${n} ms`,t:this.getTime()}),{lid:t,t:this.getTime(),e:R.API,dat:U({st:o.XHR},e)}}}}}));