All files / src/handlers handleApiRequest.ts

100% Statements 27/27
100% Branches 6/6
100% Functions 5/5
100% Lines 26/26

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                        22x       14x 9x         14x   14x 14x   14x 14x 2x   12x   14x   14x 14x         8x 8x   8x 8x       22x 22x   22x 14x 14x   14x       8x 8x   8x      
import { IntegrationEnv, isProxySecretSet } from '../env'
import {
  addProxyIntegrationHeaders,
  addTrafficMonitoringSearchParamsForVisitorIdRequest,
  createErrorResponseForIngress,
  createFallbackErrorResponse,
} from '../utils'
import { getFilteredCookies } from '../utils/cookie'
import { getIngressBackendByRegion } from '../utils/getIngressBackendByRegion'
import { CacheOverride } from 'fastly:cache-override'
 
function isMethodAuthorized(method: string): boolean {
  return method === 'POST'
}
 
async function makeAuthorizedRequest(receivedRequest: Request, env: IntegrationEnv, url: URL): Promise<Response> {
  if (!isProxySecretSet(env)) {
    console.warn(
      "PROXY_SECRET is not set in the integration's Secret store, your integration is not working correctly."
    )
  }
 
  addTrafficMonitoringSearchParamsForVisitorIdRequest(url)
 
  const oldCookieValue = receivedRequest.headers.get('cookie')
  const newCookieValue = getFilteredCookies(oldCookieValue, (key) => key === '_iidt')
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Fastly Request/RequestInit type mismatch with exactOptionalPropertyTypes
  const request = new Request(url, receivedRequest as RequestInit)
  if (newCookieValue) {
    request.headers.set('cookie', newCookieValue)
  } else {
    request.headers.delete('cookie')
  }
  addProxyIntegrationHeaders(request.headers, receivedRequest.url, env)
 
  console.log(`sending ingress request to ${url.toString()}...`)
  return await fetch(request, { backend: getIngressBackendByRegion(url) })
}
 
function makeUnauthorizedRequest(receivedRequest: Request, url: URL): Promise<Response> {
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Fastly Request/RequestInit type mismatch with exactOptionalPropertyTypes
  const request = new Request(url, receivedRequest as RequestInit)
  request.headers.delete('Cookie')
 
  console.log(`sending cache request to ${url.toString()}...`)
  return fetch(request, { backend: getIngressBackendByRegion(url), cacheOverride: new CacheOverride('pass') })
}
 
export async function handleApiRequest(request: Request, env: IntegrationEnv, pathname: string): Promise<Response> {
  const url = new URL(request.url)
  url.pathname = pathname
 
  if (isMethodAuthorized(request.method)) {
    try {
      return await makeAuthorizedRequest(request, env, url)
    } catch (e) {
      return createErrorResponseForIngress(request, e)
    }
  }
 
  try {
    return await makeUnauthorizedRequest(request, url)
  } catch (e) {
    return createFallbackErrorResponse(request, e)
  }
}