All files / src/scripts/instrumentor/patcher/fetch patcherRequest.ts

95% Statements 19/20
91.66% Branches 11/12
100% Functions 4/4
95% Lines 19/20

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 68 69 70 71 72 73 74 75 76 77 78 79 80                                  27x 15x     15x 1x     14x           7x           12x 2x     2x       2x           1x           10x 9x     9x 1x     8x         6x         1x   1x    
import { logger } from '../../../shared/logger'
import { PatcherRequest } from '../types'
import { FetchParamsWithRequestInit, resolveRequestInitMethod, setHeaderForRequestInit } from './requestInit'
 
/**
 * Resolves fetch parameters into a standardized PatcherRequest object.
 *
 * Supports three types of fetch calls:
 * - fetch(url, requestInit) - URL as string
 * - fetch(urlObject, requestInit) - URL as URL object
 * - fetch(request) - Request object
 *
 * @param params - The parameters passed to the fetch function. Can be modified via the `setHeader` method of the returned PatcherRequest object.
 * @returns A PatcherRequest object with URL, method, and setHeader function, or undefined if unsupported
 */
export function resolvePatcherRequest(params: Parameters<typeof fetch>): PatcherRequest | undefined {
  // fetch("https://example.com", {...})
  if (typeof params[0] === 'string') {
    const requestInit = params[1]
 
    // no-cors mode requests disallow custom headers: https://developer.mozilla.org/en-US/docs/Web/API/RequestInit#mode
    if (requestInit?.mode === 'no-cors') {
      return undefined
    }
 
    return {
      url: params[0],
      method: resolveRequestInitMethod(requestInit),
 
      setHeader: (name, value) => {
        // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
        setHeaderForRequestInit(name, value, params as FetchParamsWithRequestInit)
      },
    }
  }
 
  // fetch(new URL("https://example.com", window.location.href), {...})
  if (params[0] instanceof URL) {
    const requestInit = params[1]
 
    // no-cors mode requests disallow custom headers: https://developer.mozilla.org/en-US/docs/Web/API/RequestInit#mode
    Iif (requestInit?.mode === 'no-cors') {
      return undefined
    }
 
    return {
      url: params[0].toString(),
      method: resolveRequestInitMethod(requestInit),
 
      setHeader: (name, value) => {
        // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
        setHeaderForRequestInit(name, value, params as FetchParamsWithRequestInit)
      },
    }
  }
 
  // fetch({url: "https://example.com", method: "POST"})
  if (params[0] instanceof Request) {
    const request = params[0]
 
    // no-cors mode requests disallow custom headers: https://developer.mozilla.org/en-US/docs/Web/API/RequestInit#mode
    if (request.mode === 'no-cors') {
      return undefined
    }
 
    return {
      url: request.url.toString(),
      method: request.method,
 
      setHeader: (name, value) => {
        request.headers.set(name, value)
      },
    }
  }
 
  logger.warn('Unsupported fetch request', params)
 
  return undefined
}