Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { _support, validateOption, logger, isArray, isBrowserEnv, isWxMiniEnv } from '../utils/index'
import { Queue } from '../utils/index'
import createErrorId from './errorId'
import { SDK_NAME, SDK_VERSION, SERVER_URL } from '../common/config'
import { breadcrumb } from './breadcrumb'
import { EMethods, InitOptions } from '../types/options'
import { AuthInfo, TransportDataType, ReportDataType } from '../types/transportData'
/**
* 用来传输数据类,包含img标签、xhr请求
* 功能:支持img请求和xhr请求、可以断点续存(保存在localstorage),
* 待开发:目前不需要断点续存,因为接口不是很多,只有错误时才触发,如果接口太多可以考虑合并接口、
*
* ../class Transport
*/
export class TransportData {
// static img = new Image()
private queue: Queue
private beforeDataReport: unknown = null
private backTrackerId: InitOptions | unknown = null
private configReportXhr: unknown = null
private apikey = ''
constructor(public url: string) {
this.queue = new Queue()
}
// imgRequest(data: Record<string, unknown>): void {
// TransportData.img.src = `${this.url}?${splitObjToQuery(data)}`
// }
getRecord(): any[] {
const recordData = _support.record
Iif (recordData && isArray(recordData) && recordData.length > 2) {
return recordData
}
return []
}
beforePost(data: ReportDataType) {
Iif (typeof this.beforeDataReport === 'function') {
data = this.beforeDataReport(data)
// todo ? 是否需要加个判断 如果格式符合标准就不上传
if (!data) return false
}
const errorId = createErrorId(data)
Iif (!errorId) return false
data.errorId = errorId
return JSON.stringify(this.getTransportData(data))
}
xhrPost(data: ReportDataType): void {
const result = this.beforePost(data)
Iif (!result) return
const requestFun = (): void => {
const xhr = new XMLHttpRequest()
xhr.open(EMethods.Post, this.url)
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8')
xhr.withCredentials = true
Iif (typeof this.configReportXhr === 'function') {
this.configReportXhr(xhr)
}
xhr.send(result)
}
this.queue.addFn(requestFun)
}
// 需要抽离函数
wxPost(data: ReportDataType) {
const result = this.beforePost(data)
if (!result) return
const requestFun = (): void => {
wx.request({
method: 'POST',
header: {
'Content-Type': 'application/json;charset=UTF-8'
},
url: this.url,
data: result
})
}
this.queue.addFn(requestFun)
}
getAuthInfo(): AuthInfo {
const trackerId = this.getTrackerId()
return {
trackerId: String(trackerId),
sdkVersion: SDK_VERSION,
sdkName: SDK_NAME,
apikey: this.apikey
}
}
getTrackerId(): string | number {
Iif (typeof this.backTrackerId === 'function') {
const trackerId = this.backTrackerId()
if (typeof trackerId === 'string' || typeof trackerId === 'number') {
return trackerId
} else {
logger.error(`trackerId:${trackerId} 期望 string 或 number 类型,但是传入 ${typeof trackerId}`)
}
}
return ''
}
getTransportData(data: ReportDataType): TransportDataType {
return {
authInfo: this.getAuthInfo(),
breadcrumb: breadcrumb.getStack(),
data,
record: this.getRecord()
}
}
isSdkTransportUrl(targetUrl: string): boolean {
return targetUrl.indexOf(this.url) !== -1
}
bindOptions(options: InitOptions = {}): void {
const { dsn, beforeDataReport, apikey, configReportXhr, backTrackerId } = options
validateOption(apikey, 'apikey', 'string') && (this.apikey = apikey)
validateOption(dsn, 'dsn', 'string') && (this.url = dsn)
validateOption(beforeDataReport, 'beforeDataReport', 'function') && (this.beforeDataReport = beforeDataReport)
validateOption(configReportXhr, 'configReportXhr', 'function') && (this.configReportXhr = configReportXhr)
validateOption(backTrackerId, 'backTrackerId', 'function') && (this.backTrackerId = backTrackerId)
}
send(data: ReportDataType): void {
Eif (isBrowserEnv) {
return this.xhrPost(data)
}
if (isWxMiniEnv) {
return this.wxPost(data)
}
}
}
const transportData = _support.transportData || (_support.transportData = new TransportData(SERVER_URL))
export { transportData }
|