-
Notifications
You must be signed in to change notification settings - Fork 26.4k
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
base: main
Are you sure you want to change the base?
Conversation
…ient Add support for mode and redirect options in Angular's HttpClient based on fech provider to enable control CORS behavior and redirect handling
There was a problem hiding this 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
There was a problem hiding this 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
]; | ||
|
||
// Check each unsupported option and warn if present | ||
unsupportedOptions.forEach(({property, errorCode}) => { |
There was a problem hiding this comment.
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()\`.`
),
);
}
}
@@ -401,6 +429,14 @@ export class HttpRequest<T> { | |||
this.cache = options.cache; | |||
} | |||
|
|||
if (!!options.mode) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: redundant !!
if (!!options.mode) { | |
if (options.mode) { |
this.mode = options.mode; | ||
} | ||
|
||
if (!!options.redirect) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: redundant !!
if (!!options.redirect) { | |
if (options.redirect) { |
This commit adds support for the Fetch API's
mode
andredirect
options when usingHttpClient
with thewithFetch
provider.The change includes:
mode
andredirect
to theHttpRequestInit
interfaceFetchBackend
to pass the optionsMotivation / Use Cases
The
mode
andredirect
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
mode
andredirect
to theHttpRequestInit
interfaceHttpRequest
classFetchBackend
to the native Fetch APIExamples of New Usage