npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-forkable-http-client

v4.0.0

Published

Angular HTTP client that can be forked

Downloads

1,243

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.