mutiple
Version:
A SDK for monitoring browser and miniPrograme errors
125 lines (116 loc) • 3.6 kB
text/typescript
import { MitoVue } from '@/Vue/index'
import { breadcrumb, transportData, log as newLog } from '@/core/index'
import { logger, setSilentFlag, _global } from '@/utils/index'
import { SDK_VERSION, SDK_NAME } from '@/common/config'
import { InitOptions, ReportDataType } from '@/types/index'
import { options as initOptions } from '@/core/options'
import { Severity } from '@/utils/Severity'
import { BREADCRUMBCATEGORYS } from '@/common/constant'
import { errorBoundaryReport } from '@/React/index'
import { setupReplace } from '@/browser/load'
function init(options: InitOptions = {}): void {
if (!('addEventListener' in document) || options.disabled) return
bindOptions(options)
setupReplace()
}
function bindOptions(options: InitOptions = {}) {
setSilentFlag(options)
breadcrumb.bindOptions(options)
logger.bindOptions(options.debug)
transportData.bindOptions(options)
initOptions.bindOptions(options)
}
// 兼容老版log
function oldLog(tag = '', message = 'emptyMsg', ex = '', level = Severity.Critical) {
newLog({
message,
tag,
ex,
level
})
}
function log(...args) {
if (args.length > 0) {
if (typeof args[0] === 'string') {
oldLog(...args)
} else {
newLog(args[0])
}
}
}
interface IManualInitOption {
customTag: string
apikey: string
debug: boolean
}
type TCallBack = (param: IManualInitOption) => void
function manualInit(callback: TCallBack, qa = false) {
const scriptDom = document.querySelector('script[apikey]')
let apikey = null,
debug = 'false'
if (scriptDom) {
apikey = scriptDom.getAttribute('apikey')
debug = scriptDom.getAttribute('debug')
}
const isQa = location.href.indexOf('.qa.') !== -1 || location.href.indexOf('localhost') !== -1 || location.href.indexOf('127.0.0.1') !== -1
const envInit = qa ? isQa : !isQa
if (envInit && apikey) {
_global['Vue'] && MitoVue.install(_global['Vue'])
document.addEventListener('DOMContentLoaded', () => {
MitoVue.install(_global['Vue'])
})
_global['tryCatch'] = {
log
}
const customTag = scriptDom.getAttribute('customTag')
callback({ customTag, apikey, debug: debug === 'true' })
}
}
// 如果是接口正常情况下不取响应体 因为sls中单字段最多只能16k
function beforePushBreadcrumb(breadcrumb, cruBreadcrumbData) {
if (cruBreadcrumbData.category === BREADCRUMBCATEGORYS.HTTP) {
const data = cruBreadcrumbData.data as ReportDataType
if (data.response.status >= 200 && data.response.status < 300) {
data.response.data = ''
}
}
return cruBreadcrumbData
}
function beforeAppAjaxSend({ method, url }, config) {
if (/apigw/.test(url)) {
const ua = window.navigator.userAgent
let appInfo = window['APP_INFO'] || null
const appInfoMatch = ua.match(/AppInfo\((.*?);(.*?);/)
if (appInfoMatch && appInfoMatch.length >= 3) {
appInfo = `${appInfoMatch[1]};version=${appInfoMatch[2]};platform=H5`
}
let deviceId = null
const deviceIdMatch = ua.match(/DeviceId\((.*?)\)/)
if (deviceIdMatch && deviceIdMatch.length >= 2) {
deviceId = deviceIdMatch[1]
}
if (appInfo) {
config.setRequestHeader('App-Info', appInfo)
}
if (deviceId) {
config.setRequestHeader('Device-Id', deviceId)
}
}
}
const includeHttpUrlTraceIdRegExp = /apigw/
const maxBreadcrumbs = 20
const enableTraceId = true
export {
MitoVue,
SDK_VERSION,
SDK_NAME,
init,
log,
beforeAppAjaxSend,
errorBoundaryReport,
beforePushBreadcrumb,
manualInit,
includeHttpUrlTraceIdRegExp,
maxBreadcrumbs,
enableTraceId
}