Registry / http-networking / ngx-forkable-http-client

ngx-forkable-http-client

JSON →
library4.0.0jsnpmunverified

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.

npm install ngx-forkable-http-client
INSTALL
IMPORT
SIG · NGX-FORKABLE-HTTP-
N
ngx-forkable-http-client
http-networkingjavascriptv4.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ForkableHttpClient
import { ForkableHttpClient } from 'ngx-forkable-http-client';
import { HttpClient as ForkableHttpClient } from '@angular/common/http';
This is the extended HttpClient class you should inject and use for forking. Do not confuse it with Angular's native HttpClient.
httpClient
import { httpClient } from 'ngx-forkable-http-client';
import { httpClient } from '@angular/common/http';
This is a utility factory function used to define ForkableHttpClient instances with specific interceptors within InjectionToken factories. It is not a class.
InjectionToken
import { InjectionToken } from '@angular/core';
While part of Angular core, it's crucial for defining and providing named instances of ForkableHttpClient.

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)); // } // }
Debug
Known issues
breakingMajor versions of `ngx-forkable-http-client` are tightly coupled to specific Angular versions. Version 4.x.x requires Angular >=13.0.0. Using an incompatible library version will lead to build errors or runtime failures.
fix
Consult the compatibility matrix in the package documentation and install the `ngx-forkable-http-client` version matching your Angular project's major version. For Angular 13+, use `npm install ngx-forkable-http-client@^4.0.0`.
affects: >=1.0.0
gotchaWhen defining a forked HTTP client, ensure you use the `httpClient().with(...)` factory pattern. Simply injecting `ForkableHttpClient` without this factory setup will not apply the intended non-global interceptors to that specific instance.
fix
Always use the `httpClient()` factory within your `InjectionToken`'s `factory` function to specify interceptors, for example: `factory: () => httpClient().with(MyInterceptor)`.
affects: >=1.0.0
gotchaMisunderstanding the 'fork' concept can lead to confusion with `rxjs.forkJoin`. This library's 'fork' refers to creating a new `HttpClient` instance that inherits from a parent but can have additional interceptors. It does not relate to concurrent Observable execution.
fix
Remember that `ForkableHttpClient.fork()` is for creating a new, customized `HttpClient` instance with its own interceptor chain, while `rxjs.forkJoin` is for combining multiple Observables to emit values when all complete.
affects: >=1.0.0
Errors
Common errors & fixes
Error: NG0201: No provider for InjectionToken MY_REST_API_HTTP_CLIENT!
The `InjectionToken` for your forked `HttpClient` is not correctly provided in the Angular dependency injection system.
fix
Ensure your `InjectionToken` specifies `providedIn: 'root'` or that it is explicitly added to the `providers` array of an `@NgModule` where it needs to be available. Example: `new InjectionToken<ForkableHttpClient>(..., { providedIn: 'root', factory: ... });`
TypeError: httpClient.fork is not a function
You are attempting to call `fork()` on Angular's native `HttpClient` instead of the extended `ForkableHttpClient` provided by this library.
fix
Ensure you are injecting `ForkableHttpClient` from `ngx-forkable-http-client` and not `HttpClient` from `@angular/common/http` when you intend to use the forking functionality. Update your import and injection site.
Argument of type 'typeof MyInterceptor' is not assignable to parameter of type 'HttpInterceptor'.
The class provided to `httpClient().with()` or `ForkableHttpClient.fork()` does not correctly implement the `HttpInterceptor` interface, or it's not marked as `@Injectable()`.
fix
Verify that your interceptor class has an `intercept` method with the correct signature (`intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>`) and is decorated with `@Injectable()`.
Upgrade
Version history
4.0.0latest on npm
Audit
Dependencies
@angular/corerequiredCore Angular functionalities, including dependency injection and InjectionToken.
@angular/commonrequiredProvides the base HttpClient and HttpInterceptor interfaces that this library extends.
rxjsrequiredAngular's HttpClient relies heavily on RxJS Observables.
Agent activity
4 hits · last 30 days
node
4
Resources
ngx-forkable-http-client — npm install ngx-forkable-http-client · libregistry