# All files / src/core options.ts

100% Statements 21/21
78.95% Branches 15/19
100% Functions 4/4
100% Lines 19/19

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

    5x

    5x

    5x

    5x
    10x
    2x
    2x
    2x
    2x
    2x

    5x

    5x

    5x
    4x
    2x
    1x
    1x

    5x
     import { InitOptions } from '../types/options'
    import { generateUUID, toStringValidateOption, validateOption, _support } from '../utils/index'

    export class Options {
      beforeAppAjaxSend: Function
      afterAppAjaxClose: Function
      enableTraceId: Boolean
      filterXhrUrlRegExp: RegExp
      includeHttpUrlTraceIdRegExp: RegExp
      traceIdFieldName = 'Trace-Id'
      constructor() {
        this.enableTraceId = false
      }
      bindOptions(Ioptions: InitOptions = {}): void {
        const { beforeAppAjaxSend, enableTraceId, filterXhrUrlRegExp, traceIdFieldName, includeHttpUrlTraceIdRegExp } = options
        validateOption(beforeAppAjaxSend, 'beforeAppAjaxSend', 'function') && (this.beforeAppAjaxSend = beforeAppAjaxSend)
        validateOption(enableTraceId, 'enableTraceId', 'boolean') && (this.enableTraceId = enableTraceId)
        validateOption(traceIdFieldName, 'traceIdFieldName', 'string') && (this.traceIdFieldName = traceIdFieldName)
        toStringValidateOption(filterXhrUrlRegExp, 'filterXhrUrlRegExp', '[object RegExp]') && (this.filterXhrUrlRegExp = filterXhrUrlRegExp)
        toStringValidateOption(includeHttpUrlTraceIdRegExp, 'includeHttpUrlTraceIdRegExp', '[object RegExp]') &&
          (this.includeHttpUrlTraceIdRegExp = includeHttpUrlTraceIdRegExp)
      }
    }

    const options = _support.options || (_support.options = new Options())

    export function setTraceId(httpUrl: string, callback: (headerFieldName: string, traceId: string) => void) {
      const { includeHttpUrlTraceIdRegExp, enableTraceId } = options
      if (enableTraceId && includeHttpUrlTraceIdRegExp && includeHttpUrlTraceIdRegExp.test(httpUrl)) {
        const traceId = generateUUID()
        callback(options.traceIdFieldName, traceId)
      }
    }

    export { options }