Skip to content

Add httpClient option to httpResource #62616

@MillerSvt

Description

@MillerSvt

Which @angular/* package(s) are relevant/related to the feature request?

common

Description

In our project we use modular HttpClient with different setup of interceptors for accessing different API's.

We create createHttpClient helper for that:

export function createHttpClient(...features: HttpFeature<HttpFeatureKind>[]): HttpClient {
    assertInInjectionContext(createHttpClient);

    const injector = createEnvironmentInjector(
        [provideHttpClient(...features)],
        inject(EnvironmentInjector),
    );

    return injector.get(HttpClient);
}

And we use this helper inside {provide: CUSTOM_HTTP_CLIENT_TOKEN, useFactory: () => createHttpClient()}.

It works until we migrate to httpResource, because there is no way to specify what instance of HttpClient should be used.

Proposed solution

Add option httpClient?: HttpClient to HttpResourceOptions.

Alternatives considered

Create custom injection context, and run httpResource inside it:

function bindHttpResource<T>(resourceFactory: () => HttpResourceRef<T>, httpClientToken: InjectionToken<HttpClient>): HttpResourceRef<T> {
    assertInInjectionContext(bindHttpResource);
    const injector = Injector.create({
        providers: [{
            provide: HttpClient,
            useExisting: httpClientToken,
        }],
        parent: inject(Injector),
    });

    return runInInjectionContext(injector, resourceFactory);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: common/httpIssues related to HTTP and HTTP Clientcross-cutting: resourceIssues related to the newly introduced resource / httpResource

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy