evidence.ts 737 Bytes
function trimTrailingSlash(value: string) {
  return value.replace(/\/+$/, '')
}

function normalizeEvidencePath(evidencePath: string) {
  const normalized = evidencePath.replace(/^\/+/, '')
  return normalized.split('/').map(segment => encodeURIComponent(segment)).join('/')
}

function isAbsoluteUrl(value: string) {
  return /^https?:\/\//i.test(value)
}

export function buildEvidenceUrl(evidencePath?: string, apiBase = '') {
  if (!evidencePath) {
    return ''
  }

  const relativeUrl = `/uploads/${normalizeEvidencePath(evidencePath)}`
  const normalizedBase = trimTrailingSlash(apiBase.trim())

  if (!normalizedBase || !isAbsoluteUrl(normalizedBase)) {
    return relativeUrl
  }

  return `${normalizedBase}${relativeUrl}`
}