{"id":17302,"library":"ngx-forkable-http-client","title":"Angular Forkable HTTP Client","description":"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.","status":"active","version":"4.0.0","language":"javascript","source_language":"en","source_url":"git://github.com/dscheerens/ngx-forkable-http-client","tags":["javascript","angular","ng","ngx","http","client","fork","forkable","interceptor","typescript"],"install":[{"cmd":"npm install ngx-forkable-http-client","lang":"bash","label":"npm"},{"cmd":"yarn add ngx-forkable-http-client","lang":"bash","label":"yarn"},{"cmd":"pnpm add ngx-forkable-http-client","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core Angular functionalities, including dependency injection and InjectionToken.","package":"@angular/core","optional":false},{"reason":"Provides the base HttpClient and HttpInterceptor interfaces that this library extends.","package":"@angular/common","optional":false},{"reason":"Angular's HttpClient relies heavily on RxJS Observables.","package":"rxjs","optional":false}],"imports":[{"note":"This is the extended HttpClient class you should inject and use for forking. Do not confuse it with Angular's native HttpClient.","wrong":"import { HttpClient as ForkableHttpClient } from '@angular/common/http';","symbol":"ForkableHttpClient","correct":"import { ForkableHttpClient } from 'ngx-forkable-http-client';"},{"note":"This is a utility factory function used to define ForkableHttpClient instances with specific interceptors within InjectionToken factories. It is not a class.","wrong":"import { httpClient } from '@angular/common/http';","symbol":"httpClient","correct":"import { httpClient } from 'ngx-forkable-http-client';"},{"note":"While part of Angular core, it's crucial for defining and providing named instances of ForkableHttpClient.","symbol":"InjectionToken","correct":"import { InjectionToken } from '@angular/core';"}],"quickstart":{"code":"import { InjectionToken, Injectable } from '@angular/core';\nimport { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';\nimport { Observable } from 'rxjs';\nimport { ForkableHttpClient, httpClient } from 'ngx-forkable-http-client';\n\n// Dummy Interceptors for demonstration\n@Injectable()\nexport class MyAuthenticationHttpInterceptor implements HttpInterceptor {\n  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {\n    console.log('Auth Interceptor: ', req.url);\n    const authReq = req.clone({ setHeaders: { Authorization: `Bearer ${process.env.AUTH_TOKEN ?? 'my-secret-token'}` } });\n    return next.handle(authReq);\n  }\n}\n\n@Injectable()\nexport class LoggingHttpInterceptor implements HttpInterceptor {\n  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {\n    console.log('Logging Interceptor: ', req.method, req.url);\n    return next.handle(req);\n  }\n}\n\n@Injectable()\nexport class ErrorHandlerHttpInterceptor implements HttpInterceptor {\n  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {\n    console.log('Error Handler Interceptor: ', req.url);\n    return next.handle(req);\n  }\n}\n\n@Injectable()\nexport class AnotherHttpInterceptor implements HttpInterceptor {\n  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {\n    console.log('Another Interceptor: ', req.url);\n    return next.handle(req);\n  }\n}\n\nexport const MY_REST_API_HTTP_CLIENT =\n  new InjectionToken<ForkableHttpClient>('MY_REST_API_HTTP_CLIENT', {\n    providedIn: 'root',\n    factory: () => httpClient().with(MyAuthenticationHttpInterceptor, LoggingHttpInterceptor)\n  });\n\nexport const EXTERNAL_API_X_HTTP_CLIENT =\n  new InjectionToken<ForkableHttpClient>('EXTERNAL_API_X_HTTP_CLIENT', {\n    providedIn: 'root',\n    factory: () => httpClient().with(ErrorHandlerHttpInterceptor)\n  });\n\nexport const EXTERNAL_API_Y_HTTP_CLIENT =\n  new InjectionToken<ForkableHttpClient>('EXTERNAL_API_Y_HTTP_CLIENT', {\n    providedIn: 'root',\n    factory: () => httpClient().with(AnotherHttpInterceptor)\n  });\n\n// Example of how to use in a component or service:\n// @Component({...})\n// export class MyService {\n//   constructor(@Inject(MY_REST_API_HTTP_CLIENT) private myApiClient: ForkableHttpClient) {\n//     this.myApiClient.get('/data').subscribe(res => console.log(res));\n//   }\n// }","lang":"typescript","description":"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."},"warnings":[{"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`.","message":"Major 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.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Always use the `httpClient()` factory within your `InjectionToken`'s `factory` function to specify interceptors, for example: `factory: () => httpClient().with(MyInterceptor)`.","message":"When 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"Misunderstanding 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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"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: ... });`","cause":"The `InjectionToken` for your forked `HttpClient` is not correctly provided in the Angular dependency injection system.","error":"Error: NG0201: No provider for InjectionToken MY_REST_API_HTTP_CLIENT!"},{"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.","cause":"You are attempting to call `fork()` on Angular's native `HttpClient` instead of the extended `ForkableHttpClient` provided by this library.","error":"TypeError: httpClient.fork is not a function"},{"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()`.","cause":"The class provided to `httpClient().with()` or `ForkableHttpClient.fork()` does not correctly implement the `HttpInterceptor` interface, or it's not marked as `@Injectable()`.","error":"Argument of type 'typeof MyInterceptor' is not assignable to parameter of type 'HttpInterceptor'."}],"ecosystem":"npm","meta_description":null}