Commit graph

143 commits

Author SHA1 Message Date
Matthieu Riegler
85e31465c9 refactor(http): create an InjectionToken for a global HttpBackend (#51999)
`withHttp` provides the new `PRIMARY_HTTP_BACKEND` token with `FetchBackend` to use it globally.

PR Close #51999
2023-10-04 07:26:18 -07:00
Alan Agius
8413b64a6b refactor(core): add whenStable private API (#51807)
Prior to this change `this.isStable.pipe(first((isStable) => isStable)).toPromise()` had to be done in multiple places across the framework and the Angular CLI see https://github.com/angular/angular-cli/pull/25856#discussion_r1328158846. In the majority of cases an Observable based `isStable` API is not needed. This also removes the need for RXJS operator imports.

PR Close #51807
2023-09-27 10:31:56 -07:00
Jeremy Mowery
635318fd61 refactor: add readonly to public InjectionToken types (#51407)
This fixes warnings caused by an internal lint rule

refactor: add readonly to public InjectionToken types

PR Close #51407
2023-09-12 12:55:14 -07:00
Matthieu Riegler
17c6ad7775 docs: Add usage information to HttpInterceptorFn (#51236)
Simple doc addendum

PR Close #51236
2023-08-15 10:02:43 -07:00
arturovt
57e8412e53 fix(http): check whether Zone is defined (#51119)
Accessing the `Zone` variable without checking if it's defined or not
leads to an error "Zone is not defined" if zone.js is not imported (nooped).
This commit adds an additional check before getting the current zone where
the `doRequest` is being called.

PR Close #51119
2023-07-21 16:45:26 +00:00
Matthieu Riegler
c5608e5ca9 fix(http): Run fetch request out the angular zone (#50981)
Having the request run in the angular zone has the consequence of triggering the CD for every read of the response stream.

This commit wraps the whole `doRequest` to run outside angular with every callback on the observer being called inside the zone.

Fixes #50979.

PR Close #50981
2023-07-17 14:53:52 +00:00
mgechev
9f2fb7c6dc docs: update invalid links to the new http guides (#49456)
We don't have `guide/http` anymore, so this PR updates the invalid links

PR Close #49456
2023-06-22 17:06:59 -07:00
Heo Sangmin
a126cbcf22 fix(http): use serializeBody to support JSON payload in FetchBackend (#50776)
`HttpRequest.serializeBody` was used in HttpXhrBackend. `fetch` also needs to serialize request body.

Close #50775

PR Close #50776
2023-06-20 13:31:25 -07:00
Matthieu Riegler
135167fe8e fix(http): Send query params on fetch request (#50740)
QueryParams were missing when using the `FetchBackend`.

Fixes #50728

PR Close #50740
2023-06-20 12:59:26 +02:00
Matthieu Riegler
85c5427582 feat(http): Introduction of the fetch Backend for the HttpClient (#50247)
This commit introduces a new `HttpBackend` implentation which makes requests using the fetch API

This feature is a developer preview and is opt-in.
It is enabled by setting the providers with `provideHttpClient(withFetch())`.

NB: The fetch API is experimental on Node but available without flags from Node 18 onwards.

PR Close #50247
2023-06-08 09:48:12 -07:00
Alan Agius
0c80349cf6 refactor(http): replace zone.js macrotask creation with InitialRenderPendingTasks (#50425)
This commits refactors the HTTP client to use `InitialRenderPendingTasks` instead of Zone.js macrotask. This is another approach to https://github.com/angular/angular/pull/50406 which was revert due to a failure in G3.

PR Close #50425
2023-05-30 12:58:22 -07:00
Alan Agius
28c68f709c fix(core): update ApplicationRef.isStable to account for rendering pending tasks (#50425)
This commit updates the `ApplicationRef.isStable` API to account for
pending rendering task. This is needed as once a pending rendering task
is done, new macrotask and microtask could be created which previously caused these not
to be intercepted and thus ignored when doing SSR.

PR Close #50425
2023-05-30 12:58:22 -07:00
Dylan Hunn
6f5dabe0d2 Revert "fix(http): create macrotask during request handling instead of load start (#50406)" (#50475)
This reverts commit 2cdb4c5911.

PR Close #50475
2023-05-25 16:24:39 -04:00
Alan Agius
2cdb4c5911 fix(http): create macrotask during request handling instead of load start (#50406)
This commit schedules the macrotask creation to happen before the XHR `loadStart` event. This is needed as in some cases, Zone.js becomes stable too early.

With this commit, we also update the internal `createBackgroundMacroTask` method to use Zone.js `scheduleMacroTask` as otherwise the `setTimeout` would cause `fakeAsync` tests to fail due to pending timers.

Closes #50405

PR Close #50406
2023-05-24 15:33:43 +00:00
Matthieu Riegler
5fd4bf6651 refactor(http): tree-shakable error on JSONP request (#50376)
This commit provides a tree shakable error message (+doc) when JSONP request is made without loading the `HttpClientJsonpModule`.

PR Close #50376
2023-05-24 13:58:49 +00:00
Kristiyan Kostadinov
69dadd2502 feat(core): support TypeScript 5.1 (#50156)
Updates the project to support building with TypeScript 5.1.

PR Close #50156
2023-05-09 14:44:30 -07:00
Andrew Kushnir
59837f4749 refactor(platform-server): import xhr2 dynamically in the ServerXhr class (#50095)
This commit updates the `@angular/common/http` and `@angular/platform-server` packages to allow dynamic import of the `xhr2` dependency. The `xhr2` dependency has side-effects that rely on a global scope and as a result in some environments those side-effectful calls fail. With the changes from this PR, the import is delayed until it's actually needed, which gives a chance for the underlying platform to setup global scope (via shims) as needed.

Co-authored-by: alan-agius4 <17563226+alan-agius4@users.noreply.github.com>

PR Close #50095
2023-05-08 14:32:43 -07:00
Matthieu Riegler
8741670a2d docs: Make links out of @see tags (#50098)
This commit is part of the work for #50097 to improve the linking on the online documentation.

PR Close #50098
2023-05-02 13:14:25 +02:00
Alan Agius
ddf0d4eabe fix(http): HTTP cache was being disabled prematurely (#49826)
This commit fixes an issue were on the server the HTTP cache was being disabled prematurely which caused HTTP calls performed in `ngOnInit` life cycle hooks not to be cached.

PR Close #49826
2023-04-13 14:04:00 +00:00
Danilo Bassi
ab5e2d9387 fix(http): prevent headers from throwing an error when initializing numerical values (#49379)
Some libraries could use numbers in headers. this fix prevents Angular from
throwing an error by casting those numerical values into strings.

Fixes #49353

PR Close #49379
2023-04-13 14:00:16 +00:00
Alan Agius
f9b821f07d fix(http): delay accessing pendingTasks.whenAllTasksComplete (#49784)
Accessing `pendingTasks.whenAllTasksComplete` too early causes the `InitialRenderPendingTasks` to return a resolved promise too early. This commit changes the way we access `whenAllTasksComplete` to only happen when the application is stabilized.

PR Close #49784
2023-04-11 14:12:38 -07:00
Alan Agius
079f4bc1ef fix(http): wait for all XHR requests to finish before stabilizing application (#49776)
Previously, since the `HttpXhrBackend` is a singleton, the macrotask was created and completed only for the initial request since it was stored as in property in the class instance. This commit replaces this logic to create a macro task for every XHR request.

Closes #49730

PR Close #49776
2023-04-11 13:44:30 -07:00
Andrew Kushnir
7ee542d263 refactor(platform-server): include info about enabled features into ng-server-context (#49773)
This commit updates the logic that adds the "ng-server-context" attribute to the root elements to also include information about SSR feature enabled got an application.

PR Close #49773
2023-04-11 12:46:09 -07:00
Brandon Roberts
0b3677e149 fix(http): ensure new cache state is returned on each request (#49749)
The cache state is only used only the first server request. Restarting the server uses the cache on first request.

Subsequent requests skip the cache because the value is mutated.

This change ensures a new cache state is returned on every request.

PR Close #49749
2023-04-11 10:40:04 -07:00
Alan Agius
aff1512950 feat(http): allow HttpClient to cache requests (#49509)
This commit adds a new option for `provideHttpClient` called
`withHttpTransferCache()`. When this option is passed, requests done on the server are cached and reused during the  bootstrapping of the application in the browser thus avoiding duplicate requests and reducing load time.

This is the same as `TransferHttpCacheModule` in https://github.com/angular/universal/blob/main/modules/common/src/transfer_http.ts

PR Close #49509
2023-04-03 19:13:45 -07:00
Alan Agius
45a6ac09fd fix(http): force macro task creation during HTTP request (#49546)
This commit adds a background macrotask when an XHR request is performed. The macrotask is started during `loadstart` and ended during `loadend` event.

The macrotask is needed so that the application is not stabilized during HTTP calls. This is important for server rendering, as the application is rendering when the application is stabilized.
The application is stabilized when there are no longer pending Macro and Micro tasks intercepted by Zone.js, Since an XHR request is none of these, we create a background macrotask so that Zone.js is
made aware that there is something pending.

Prior to this change, we patched the `HttpHandler` in `@angular/platform-server` but this is not enough, as there can be multiple `HttpHandler` in an application, example when importing `HttpClient` in a lazy loaded component/module.
Which causes a new unpatched instance of `HttpHandler` to be created in the child injector which is not intercepted by Zone.js and thus the application is stabalized and rendered before the XHR request is finalized.

NB: Zone.js is fundamental for SSR and currently, it's not possible to do SSR without it.

Closes: #49425

PR Close #49546
2023-03-29 09:13:41 -07:00
cexbrayat
8fb63dff94 refactor(http): simplify HttpClientModule XSRF configuration (#48957)
The current configuration of `HttpClientModule` explicitely enables the XSRF configuration,
but this XSRF configuration is the one used by default by `provideHttpClient`.

See https://github.com/angular/angular/blob/main/packages/common/http/src/provider.ts#L50-L98

PR Close #48957
2023-02-17 11:17:53 -08:00
Bob Watson
a6d953e145 docs: move tour of heroes tutorial to a subdirectory (#48162)
This commit prepares the documentation directories for future tutorials organized by directory.

Also, it moves the Angular Libraries topic from the Tutorials section to Developer Guides in TOC

PR Close #48162
2022-12-12 12:07:46 -08:00
Alex Rickabaugh
febf29dd51 fix(http): rename withLegacyInterceptors to withInterceptorsFromDi (#47901)
This rename reflects what the function actually does. Although the intention
is still not to have two different interceptor mechanisms, that is now
communicated in the docs for the function instead of in its name.

Fixes #47764

PR Close #47901
2022-11-03 17:54:03 -07:00
Alex Rickabaugh
cf6c819489 docs(http): add public API tags and jsdoc for provideHttpClient (#47901)
This commit adds documentation and API tagging for `provideHttpClient` and
the `with*` feature functions.

PR Close #47901
2022-11-03 17:54:03 -07:00
Andrew Kushnir
ddbc4931e6 refactor(common): make provideHttpClient return EnvironmentProviders (#47758)
This commit updates the `provideHttpClient` function to return the `EnvironmentProviders` instead of a regular `Provider[]`, to make sure that the `provideHttpClient` can only be used where an environment is being setup.

PR Close #47758
2022-10-13 16:44:59 +00:00
Alex Rickabaugh
ea16a98dfe fix(http): better handle unexpected undefined XSRF tokens (#47683)
`HttpXsrfTokenExtractor` allows returning `string|null` for an XSRF token,
and the interceptor checked if the returned token is `null`. However, some
implementations return `undefined` instead (behind an `any`) type, which
caused the interceptor to crash when trying to set an `undefined` value for
the header.

This commit makes the XSRF interceptor a little more resilient against such
broken implementations of the `HttpXsrfTokenExtractor` interface.

PR Close #47683
2022-10-06 16:41:06 -07:00
Alex Rickabaugh
c09c1bb033 docs(http): fix copy-paste error in HttpClientJsonpModule docs (#47502)
This commit deletes a sentence from the `HttpClientJsonpModule` docs which
was accidentally copy-pasted from the docs for another symbol.

PR Close #47502
2022-10-06 19:02:46 +00:00
Alex Rickabaugh
3ba99e286a feat(http): allow for child HttpClients to request via parents (#47502)
Ordinarily, providing `HttpClient` (either via `provideHttpClient` or the
`HttpClientModule`) creates an entirely separate HTTP context. Requests made
via that client are not passed through the interceptor chains that are
configured in a parent injector, for this example.

This commit introduces a new option for `provideHttpClient` called
`withRequestsMadeViaParent()`. When this option is passed, requests made in
the child context flow through any injectors, etc. and are then handed off
to the parent context.

This addresses a longstanding issue with interceptors where it's not
possible to extend the set of interceptors in a child context without
repeating all of the interceptors from the parent.

PR Close #47502
2022-10-06 19:02:46 +00:00
Alex Rickabaugh
62c7a7a16e feat(http): introduce functional interceptors (#47502)
This commit introduces a new feature for `provideHttpClient` called
`withInterceptors`. This feature exposes and configures the new concept of
functional interceptors.

Functional interceptors use functions instead of classes to implement an
HTTP interceptor. Such interceptor functions have access to the DI context
from the `EnvironmentInjector` in which they're configured via the
`inject()` function. Otherwise, functional interceptors are identical in
capability to the existing interceptor system.

PR Close #47502
2022-10-06 19:02:46 +00:00
Alex Rickabaugh
fc69c8021c refactor(http): convert NgModules to use provideHttpClient internally (#47502)
This commit converts `HttpClientModule` to use `provideHttpClient()`
internally, with a particular configuration of features. Other NgModules
related to configuring `HttpClient` are also converted to use the providers
directly from various features, to ensure consistency of behavior.

PR Close #47502
2022-10-06 19:02:45 +00:00
Alex Rickabaugh
e47b129070 feat(http): introduce the provideHttpClient() API (#47502)
This commit introduces the main components of the `provideHttpClient()`
provider API, designed in the style of `provideRouter()`. Initial features
are defined for including legacy class-based interceptors, JSONP support,
and configuring or disabling the builtin XSRF protection.

This API is an alternative to providing `HttpClient` via the
`HttpClientModule`, and is more tree-shakable and more capable than the
NgModule implementation.

Tests are included to validate the new configuration format as well as the
interoperability of the two styles of providing and configuring
`HttpClient`.

PR Close #47502
2022-10-06 19:02:45 +00:00
Alex Rickabaugh
67ef8b19b3 refactor(http): convert the XSRF interceptor to functional style (#47502)
This commit converts the XSRF interceptor into a functional interceptor
instead of a legacy class-based interceptor.

PR Close #47502
2022-10-06 19:02:45 +00:00
Alex Rickabaugh
ab6a3d7d03 refactor(http): provide XSRF configuration tokens in root (#47502)
This commit switches the XSRF configuration tokens (for header name and
cookie name) to be `providedIn: 'root'`. This is a no-op change now as they
are always provided along with any usage of them via `HttpClientModule`, but
will become load-bearing as the `provideHttpClient` API will not provide
these tokens, and will rely on injecting them from either the parent context
or from these root providers.

PR Close #47502
2022-10-06 19:02:45 +00:00
Alex Rickabaugh
d98d8d7645 refactor(http): use an InjectionToken to disable the XSRF interceptor (#47502)
@angular/common/http has XSRF protection which is enabled by default and is
implemented as an interceptor. Previously, this protection could be disabled
with an API which would internally provide a `NoopInterceptor` in place of
the standard XSRF interceptor.

To achieve the same capability of disabling the XSRF interceptor after it is
converted to the functional style, an InjectionToken is added in this commit
which disables the XSRF interceptor. This way, the interceptor can be
disabled in place without needing to override it via DI (which is difficult
for functional interceptors).

PR Close #47502
2022-10-06 19:02:45 +00:00
Alex Rickabaugh
c3ecd8f5c8 refactor(http): convert the JSONP interceptor to the functional style (#47502)
This commit rewrites the JSONP interceptor to use the functional interceptor
style internally, while still maintaining the same public API and behavior.

PR Close #47502
2022-10-06 19:02:45 +00:00
Alex Rickabaugh
546f9ca299 refactor(http): use a DI token for new interceptors (#47502)
This commit introduces a new DI token for the set of functional
interceptors. This is a no-op in terms of behavior currently, but will allow
for the deduplication of the bridge interceptor which connects legacy class-
based interceptors to the functional interceptor chain.

PR Close #47502
2022-10-06 19:02:45 +00:00
Alex Rickabaugh
d7683a611a refactor(http): introduce the functional interceptors API (#47502)
This commit introduces new types and symbols related to functional HTTP
interceptors - interceptors which are plain functions with access to DI via
the `inject()` operation.

This new form of interceptor is not exposed publically in this commit, but
the legacy class-based interceptors are refactored to be built on top of the
new API internally.

PR Close #47502
2022-10-06 19:02:45 +00:00
Daniel Ostrovsky
f8e78e3f9b docs(http): Update HttClient "JSON object" to "JavaScript object" (#47325)
updated all references of "JSON object" to "JavaScript object" in HttpClient `common/http/src/client.ts`

Fixes #43242

PR Close #47325
2022-09-03 11:00:43 -07:00
Giovanni Alberto Rivas
5dd72b7610 docs(http): replace http method return description (#47322)
PR Close #47322
2022-09-01 19:14:56 -07:00
John Vandenberg
c14c701775 docs: fix spelling (#46713)
PR Close #46713
2022-07-08 20:54:52 +00:00
markostanimirovic
82acbf919b feat(http): improve error message for nullish header (#46059)
This commit improves the error message for nullish headers.

Fixes #46048

PR Close #46059
2022-06-16 14:39:41 -07:00
cexbrayat
10691c626b fix(common): properly cast http param values to strings (#42643)
Before this commit, when initializing `HttpParams` with:

    const body = new HttpParams({fromObject: {b: 2}});

then `body.get('b')` returned `2` instead of `'2'` as expected.

This commit makes sure the values are converted to strings in such cases.

Fixes #42641

PR Close #42643
2022-05-04 12:43:37 -07:00
Adrian Kunz
76a9a24cdc fix(http): encode + signs in query params as %2B (angular#11058) (#45111)
Servers always decode + as a space, which is undesirable when one
actually wants to query for a plus.

BREAKING CHANGE:

Queries including + will now actually query for + instead of space.
Most workarounds involving custom codecs will be unaffected.
Possible server-side workarounds will need to be undone.

Fixes angular#11058

PR Close #45111
2022-04-20 09:00:19 -07:00
Maddie Klein
d43c0e973f fix(http): Throw error when headers are supplied in JSONP request (#45210)
JSONP does not support headers being set on requests. This
enables JSONP to throw an error when headers are supplied
in the request to prevent attempts to set them.

BREAKING CHANGE: JSONP will throw an error when headers are set on a reques

JSONP does not support headers being set on requests. Before when
a request was sent to a JSONP backend that had headers set the headers
were ignored. The JSONP backend will now throw an error if it
receives a request that has any headers set. Any uses of JSONP
on requests with headers set will need to remove the headers
to avoid the error.

Closes #9141

PR Close #45210
2022-03-28 09:38:19 -07:00