All files / scripts/instrumentor/patcher/fetch requestInit.ts

100% Statements 7/7
100% Branches 2/2
100% Functions 3/3
100% Lines 7/7

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              16x                     8x 8x 8x                   8x 8x 8x    
/**
 * Resolves the HTTP method from a RequestInit object, defaulting to 'GET' if not specified.
 *
 * @param requestInit - `RequestInit` object that may contain a method property
 * @returns The HTTP method string, defaults to 'GET' if not provided
 */
export function resolveRequestInitMethod(requestInit: RequestInit) {
  return requestInit?.method ?? 'GET'
}
 
/**
 * Sets a header for a fetch request by copying the `Headers`, if set and updating the new `Headers` object
 *
 * @param name - The header name to set
 * @param value - The header value to set
 * @param requestInit - The `RequestInit` to update.
 */
export function setHeaderForRequestInit(name: string, value: string, requestInit: RequestInit) {
  const newHeaders = new Headers(requestInit.headers)
  newHeaders.set(name, value)
  requestInit.headers = newHeaders
}
 
/**
 * Sets the `RequestInit.credentials` property to `include`, returning the original value
 *
 * @param requestInit the `RequestInit`
 * @returns the updated `RequestInit` or if the requestInit parameter was not defined, a new `RequestInit` object
 */
export function setIncludeCredentialsForRequestInit(requestInit: RequestInit): boolean {
  const appIncludedCredentials = requestInit.credentials === 'include'
  requestInit.credentials = 'include'
  return appIncludedCredentials
}