Angular Forkable HTTP Client

4.0.0 · active · verified Wed Apr 22

ngx-forkable-http-client is an Angular library that extends the framework's standard `HttpClient` to introduce the concept of 'forking'. This enables developers to create new, isolated instances of `HttpClient` that can have their own specific `HttpInterceptor`s, rather than all interceptors being applied globally. This is particularly useful for applications interacting with multiple external APIs, each requiring different authentication, logging, or error handling mechanisms. The library, currently at version 4.0.0, maintains a strict compatibility matrix with Angular major versions, releasing new major versions as Angular itself evolves. Its primary differentiator is solving the common Angular challenge of managing non-global HTTP interceptors and establishing a hierarchical structure for HTTP client configurations.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to define and configure multiple `ForkableHttpClient` instances using `InjectionToken` and the `httpClient` factory, each with its own specific `HttpInterceptor`s, making them root-provided. This sets up distinct HTTP clients for different API needs.

import { InjectionToken, Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
import { Observable } from 'rxjs';
import { ForkableHttpClient, httpClient } from 'ngx-forkable-http-client';

// Dummy Interceptors for demonstration
@Injectable()
export class MyAuthenticationHttpInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    console.log('Auth Interceptor: ', req.url);
    const authReq = req.clone({ setHeaders: { Authorization: `Bearer ${process.env.AUTH_TOKEN ?? 'my-secret-token'}` } });
    return next.handle(authReq);
  }
}

@Injectable()
export class LoggingHttpInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    console.log('Logging Interceptor: ', req.method, req.url);
    return next.handle(req);
  }
}

@Injectable()
export class ErrorHandlerHttpInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    console.log('Error Handler Interceptor: ', req.url);
    return next.handle(req);
  }
}

@Injectable()
export class AnotherHttpInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    console.log('Another Interceptor: ', req.url);
    return next.handle(req);
  }
}

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)
  });

// Example of how to use in a component or service:
// @Component({...})
// export class MyService {
//   constructor(@Inject(MY_REST_API_HTTP_CLIENT) private myApiClient: ForkableHttpClient) {
//     this.myApiClient.get('/data').subscribe(res => console.log(res));
//   }
// }

view raw JSON →