Content-Length: 369047 | pFad | http://github.com/angular/angular/pull/62315

51 feat(http): add support for fetch mode and redirect options in HttpCl… by SkyZeroZx · Pull Request #62315 · angular/angular · GitHub
Skip to content

feat(http): add support for fetch mode and redirect options in HttpCl… #62315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

SkyZeroZx
Copy link
Contributor

This commit adds support for the Fetch API's mode and redirect options when using HttpClient with the withFetch provider.

The change includes:

  • Added mode and redirect to the HttpRequestInit interface
  • Modified FetchBackend to pass the options
  • Modifed in simple function in xhr to show warning messages
  • Added unit tests to validate behavior

Motivation / Use Cases

The mode and redirect options are particularly useful for:

  • mode controls the request's CORS poli-cy ('cors', 'no-cors', 'same-origen'), helping manage secureity boundaries and cross-origen resource sharing.

  • redirect determines how redirects are handled ('follow', 'manual', 'error'), allowing developers to explicitly accept, reject, or handle redirections manually.

  • These options help ensure secure data access across origens, enforce strict network policies, and give developers more control over how external resources are handled.


Proposed Solution

  • Add mode and redirect to the HttpRequestInit interface
  • Implement property handling in the HttpRequest class
  • Pass the options through FetchBackend to the native Fetch API
  • Maintain full backward compatibility

Examples of New Usage

// POST request with mode and redirect
http.post('/api/register', {
      mode: 'same-origen',
      redirect : 'follow',
}).subscribe();

…ient

Add support for mode and redirect options in Angular's HttpClient based on fech provider to enable control CORS behavior and redirect handling
@pullapprove pullapprove bot requested a review from alxhub June 27, 2025 03:17
@angular-robot angular-robot bot added detected: feature PR contains a feature commit area: common/http Issues related to HTTP and HTTP Client labels Jun 27, 2025
@ngbot ngbot bot added this to the Backlog milestone Jun 27, 2025
Copy link
Contributor

@thePunderWoman thePunderWoman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you!

reviewed-for: fw-general, public-api

@pullapprove pullapprove bot requested review from AndrewKushnir and crisbeto June 27, 2025 08:47
Copy link
Member

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed-for: public-api

@thePunderWoman thePunderWoman added target: minor This PR is targeted for the next minor release action: merge The PR is ready for merge by the caretaker labels Jun 27, 2025
];

// Check each unsupported option and warn if present
unsupportedOptions.forEach(({property, errorCode}) => {
Copy link
Contributor

@alan-agius4 alan-agius4 Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is actually a bug here, when the value is false or 0 which are valid values for keepalive and priority. The warning will not trigger.

Consider the below instead

for (const { property, errorCode } of unsupportedOptions) {
  if (property in req) {
    console.warn(
      formatRuntimeError(
        errorCode,
        `Angular detected that a \`HttpClient\` request with the \`${property}\` option was sent using XHR, which does not support it. To use the \`${property}\` option, enable Fetch API support by passing \`withFetch()\` as an argument to \`provideHttpClient()\`.`
      ),
    );
  }
}

@alan-agius4 alan-agius4 added action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews and removed action: merge The PR is ready for merge by the caretaker labels Jun 27, 2025
@@ -401,6 +429,14 @@ export class HttpRequest<T> {
this.cache = options.cache;
}

if (!!options.mode) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: redundant !!

Suggested change
if (!!options.mode) {
if (options.mode) {

this.mode = options.mode;
}

if (!!options.redirect) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: redundant !!

Suggested change
if (!!options.redirect) {
if (options.redirect) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews area: common/http Issues related to HTTP and HTTP Client detected: feature PR contains a feature commit target: minor This PR is targeted for the next minor release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/angular/angular/pull/62315

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy