# All files / src/logger report-v3.ts

100% Statements 12/12
62.96% Branches 17/27
100% Functions 2/2
100% Lines 11/11

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
    491x

    12x
    11x
    11x
    11x
    11x
    11x
    29x
    7x

    11x

    11x

     import { createXHR, getAuthHeaders } from '../utils'

    export interface V3LogInfo {
      code: number
      reqId: string
      host: string
      remoteIp: string
      port: string
      duration: number
      time: number
      bytesSent: number
      upType: 'jssdk-h5'
      size: number
    }

    /**
     * @param  {string} token 上传使用的 token
     * @param  {V3LogInfo} data 上报的统计数据
     * @param  {number} retry 重试的次数，默认值 3
     * @description v3 版本的日志上传接口，参考文档 https://github.com/qbox/product/blob/master/kodo/uplog.md#%E7%89%88%E6%9C%AC-3。
     */
    export function reportV3(token: string, data: V3LogInfo, retry = 3) {
      const xhr = createXHR()
      xhr.open('POST', 'https://uplog.qbox.me/log/3')
      xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
      xhr.setRequestHeader('Authorization', getAuthHeaders(token).Authorization)
      xhr.onreadystatechange = () => {
        if (xhr.readyState === 4 && xhr.status !== 200 && retry > 0) {
          reportV3(token, data, retry - 1)
        }
      }

      // 顺序参考：https://github.com/qbox/product/blob/master/kodo/uplog.md#%E7%89%88%E6%9C%AC-3
      const stringifyData = [
        data.code || '',
        data.reqId || '',
        data.host || '',
        data.remoteIp || '',
        data.port || '',
        data.duration || '',
        data.time || '',
        data.bytesSent || '',
        data.upType || '',
        data.size || ''
      ].join(',')

      xhr.send(stringifyData)
    }