{"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.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install ngx-forkable-http-client"],"cli":null},"imports":["import { ForkableHttpClient } from 'ngx-forkable-http-client';","import { httpClient } from 'ngx-forkable-http-client';","import { InjectionToken } from '@angular/core';"],"auth":{"required":false,"env_vars":[]},"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}