ngx-forkable-http-client

Angular HTTP client that can be forked

Usage no npm install needed!

<script type="module">
  import ngxForkableHttpClient from 'https://cdn.skypack.dev/ngx-forkable-http-client';
</script>

README

Build Status NPM Version

Forkable HTTP client extension for Angular

This Angular module provides a ForkableHttpClient class which is an extension of the HttpClient that was introduced in Angular 4.3. With the extension it becomes possible to fork HTTP clients to create new ones. In the process of forking a ForkableHttpClient you can specify a number of additional HttpInterceptor instances that will be used by the new HTTP client. This enables you to easily support non-global HTTP interceptors. Furthermore it allows you employ a hierarchically structured approach in setting up the HTTP clients needed by the different services of your application. The latter is very useful when your application needs to access multiple external API's exposed through HTTP endpoints.

A detailed explanation of the concepts behind this module can be found in the following article: "Fork your HTTP client: supporting non-global HTTP interceptors in Angular"

Installation

Start by installing the ngx-forkable-http-client NPM package:

npm install --save ngx-forkable-http-client

After having installed the ngx-forkable-http-client package you might need to update your project configuration depending on the build tools you use, e.g. SystemJS or Karma. The ngx-forkable-http-client package is published in the Angular Package Format.

Angular version compatibility matrix

Use the compatibility matrix below to determine which version of this module works with your project's Angular version.

Library version Angular version
ngx-forkable-http-client - 1.x.x >= 4.0.0
ngx-forkable-http-client - 2.x.x >= 6.0.0
ngx-forkable-http-client - 3.x.x >= 12.0.0
ngx-forkable-http-client - 4.x.x >= 13.0.0

Usage

To make use of this package begin by defining an InjectionToken for each of the HttpClient forks.

Example:

import { InjectionToken } from '@angular/core';
import { ForkableHttpClient, httpClient } from 'ngx-forkable-http-client';
import {
    MyAuthenticationHttpInterceptor,
    LoggingHttpInterceptor,
    ErrorHandlerHttpInterceptor,
    AnotherHttpInterceptor
} from './my-http-interceptors';

export const MY_REST_API_HTTP_CLIENT =
  new InjectionToken<ForkableHttpClient>('MY_REST_API_HTTP_CLIENT', {
    providedIn: 'root',
    factory: httpClient().with(MyAuthenticationHttpInterceptor, LoggingHttpInterceptor)
  });

export const EXTERNAL_API_X_HTTP_CLIENT =
  new InjectionToken<ForkableHttpClient>('EXTERNAL_API_X_HTTP_CLIENT', {
    providedIn: 'root',
    factory: httpClient().with(ErrorHandlerHttpInterceptor)
  });

export const EXTERNAL_API_Y_HTTP_CLIENT =
  new InjectionToken<ForkableHttpClient>('EXTERNAL_API_Y_HTTP_CLIENT', {
    providedIn: 'root',
    factory: httpClient().with(AnotherHttpInterceptor)
  });

In the example above the injection tokens are 'self-providing' in the root injector. This means that they don't need to be added to any module and have a factory function that will be invoked to resolve the HttpClient once they need to be injected. The factories are defined using the httpClient() utility function from the ngx-forkable-http-client module. It can be chained with a call to the .with() function to specify additional (non-global) interceptors for the ForkableHttpClient that will be generated by the factory.

Once you have defined the injection tokens, they can be used as qualifier to inject the correct HttpClient in your services, e.g.:

import { Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';

export class MyRestApiService {
  constructor(
    @Inject(MY_REST_API_HTTP_CLIENT) private httpClient: HttpClient
  ) { /* ... */ }
}

export class ServiceThatUsesExternalApis {
  constructor(
    @Inject(EXTERNAL_API_X_HTTP_CLIENT) private httpClientForApiX: HttpClient,
    @Inject(EXTERNAL_API_Y_HTTP_CLIENT) private httpClientForApiY: HttpClient
  ) { /* ... */ }
}

You can use these injected HTTP clients in the exact same way as you would do with the default (non-qualified) HttpClient. The only difference is that the qualified versions may have additional HTTP interceptors.

The final step is to make sure the non-global interceptors are injectable. This can be done either by making them 'self-providing' (@Injectable({ providedIn: 'root' })) or by adding them to the providers list of module as is shown in the example below:

CAUTION: Be sure not to define them using the HTTP_INTERCEPTORS injection token, as that will result in them being used as global interceptors.

import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import {
    MyAuthenticationHttpInterceptor,
    LoggingHttpInterceptor,
    ErrorHandlerHttpInterceptor,
    AnotherHttpInterceptor
} from './my-http-interceptors';

@NgModule({
  imports: [ HttpClientModule ],
  providers: [
    // Define the providers for the non-global interceptors.
    // Don't use the `HTTP_INTERCEPTORS` injection token for this!
    MyAuthenticationHttpInterceptor,
    LoggingHttpInterceptor,
    ErrorHandlerHttpInterceptor,
    AnotherHttpInterceptor
  ]
})
export class AppModule { }

If you want to create a hierarchy of HTTP clients this can be achieved simply by providing the InjectionToken of the parent ForkableHttpClient as argument to the httpClient factory function:

import { InjectionToken } from '@angular/core';
import { ForkableHttpClient, httpClient } from 'ngx-forkable-http-client';
import { MY_REST_API_HTTP_CLIENT } from './my-http-clients';
import { CacheHttpInterceptor } from './my-http-interceptors';

export const MY_REST_API_HTTP_CLIENT_WITH_CACHING =
  new InjectionToken<ForkableHttpClient>('MY_REST_API_HTTP_CLIENT_WITH_CACHING', {
    providedIn: 'root',
    factory: httpClient(MY_REST_API_HTTP_CLIENT).with(CacheHttpInterceptor)
  });

As can be seen from the example above, a new client is defined that forks off from the MY_REST_API_HTTP_CLIENT, inheriting all of the parent's interceptors and obtaining the additional CacheHttpInterceptor.