All files / src/scripts/instrumentor protectedApis.ts

0% Statements 0/17
0% Branches 0/13
0% Functions 0/3
0% Lines 0/16

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                                                                       
import { ProtectedApi } from '../../shared/types'
import { logger } from '../shared/logger'
 
// This template will be replaced during injection by the worker.
const PROTECTED_APIS_STR: unknown = '<PROTECTED_APIS>'
 
function isProtectedApis(value: unknown): value is ProtectedApi[] {
  return Array.isArray(value) && value.every((item) => typeof item === 'object' && 'method' in item && 'url' in item)
}
 
export function getProtectedApis(): ProtectedApi[] {
  try {
    if (typeof PROTECTED_APIS_STR === 'string') {
      logger.warn('Protected APIs are not set, instrumentation will not run.')
      return []
    }
 
    const data = PROTECTED_APIS_STR
    if (!isProtectedApis(data)) {
      logger.warn('Protected APIs are not in the correct format, instrumentation will not run.')
      return []
    }
 
    if (!data?.length) {
      logger.warn('No protected APIs found, instrumentation will not run.')
    } else {
      logger.debug('Found protected APIs', data)
    }
 
    return data ?? []
  } catch (error) {
    logger.error('Error parsing protected APIs:', error)
    return []
  }
}