function createXsrfRequestInterceptor(
  cookieName,
  headerName,
  xsrfTrustedOrigins,
  _document = document,
) {
  /**
   * @param {Request} request
   */
  async function xsrfRequestInterceptor(request) {
    const xsrfToken = getCookie(cookieName, _document);

    const isSameSite = isURLTrustedOrSameOrigin(request.url, xsrfTrustedOrigins);

    // Only add the XSRF token when it is needed and/or allowed.
    if (['POST', 'PUT', 'DELETE', 'PATCH'].includes(request.method) && isSameSite && xsrfToken) {
      request.headers.set(headerName, xsrfToken);
    }
    return request;
  }

  return xsrfRequestInterceptor;
}
Creates a request transformer that adds a XSRF header for protecting
against cross-site request forgery.
@param {string} cookieName the cookie name
@param {string} headerName the header name
@param {string[]} xsrfTrustedOrigins List of trusted origins
@param {Document | { cookie: string }} _document overwriteable for testing
@returns {RequestInterceptor}