All files / src/worker/utils request.ts

100% Statements 1/1
100% Branches 2/2
100% Functions 1/1
100% Lines 1/1

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                                                                              64x          
interface CopyRequestParams {
  // The original request to be copied
  request: Request
  // Object containing custom settings to apply to the new Request
  init: RequestInit
  // Optional parameter to specify a new URL for the copied Request. If empty, the original request URL is used.
  url?: string | URL
}
 
/**
 * Creates a new Request object by copying an existing one and optionally modifying its URL.
 * Using `Request.clone()` in Cloudflare runtime is not recommended: https://developers.cloudflare.com/workers/examples/modify-request-property/
 *
 * @return {Request} A new Request object based on the original, with any modifications applied.
 *
 * @example Modifing headers in a request.
 * ```typescript
 * const request = new Request('https://example.com/', { headers: { 'Original-Header': 'value' }, method: 'GET' })
 *
 * // In case if you'd like to completly overwrite headers, use 'new Headers()' instead.
 * const updatedHeaders = new Headers(request.headers)
 * updatedHeaders.set('X-New-Header', 'new-value')
 *
 * const modifiedRequest = copyRequest({
 *   request,
 *   init: {
 *     // Updates request headers with new values
 *     headers: updatedHeaders,
 *     // Modify request method
 *     method: 'POST',
 *   },
 * })
 *
 * console.log(modifiedRequest.headers.get('X-New-Header')) // 'new-value'
 * console.log(modifiedRequest.headers.get('Original-Header')) // 'value'
 * ```
 */
export function copyRequest({ request, init, url }: CopyRequestParams): Request<unknown, IncomingRequestCfProperties> {
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
  return new Request(url ?? request.url, new Request(request, init)) as unknown as Request<
    unknown,
    IncomingRequestCfProperties
  >
}