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

@runopencode/ngx-http-client

v17.0.6

Published

This library provides Angular module for easy integration of `@runopencode/http` into Angular project. You can read more about `@runopencode/http` in its [README.md](https://github.com/RunOpenCode/http/blob/master/packages/http/README.md) file.

Downloads

185

Readme

@runopencode/ngx-http-client

This library provides Angular module for easy integration of @runopencode/http into Angular project. You can read more about @runopencode/http in its README.md file.

Installation

Library is available on NPM, so you can install it via NPM or Yarn.

npm i --save @runopencode/ngx-http-client

Within your Angular project, you can import HttpClientModule from @runopencode/ngx-http-client module and register it.

import {NgxHttpClientModule} from '@runopencode/ngx-http-client';

@NgModule({
    imports: [
        NgxHttpClientModule.forRoot({
            // optional configuration
        }),
    ],
})
export class AppModule {
}

Note that NgxHttpClientModule should be imported only once in your application with forRoot method (usually in your top application module).

Configuration

NgxHttpClientModule.forRoot method accepts optional configuration object. Configuration object has the following properties:

  • adapter, default value is fetch - by default, fetch API is used as HTTP adapter. However, you may choose to use angular adapter instead, or any other adapter which implements HttpAdapterInterface from @runopencode/http package. In that case, you must register your adapter within Angular's DI container and pass its name/injection token as a value.
  • interceptors, default value is empty array - you may create your own interceptors and register them here. Interceptors must be decorated with @Injectable decorator and must implement HttpInterceptorInterface from @runopencode/http package. For each provided interceptor, module will register it within Angular's DI container. You may also use functions as interceptors. In that case, function will be registered as interceptor within Angular's DI. Do note that functional interceptors are useful only for simple tasks, and if you need to inject some dependencies into your interceptor, you should use class-based interceptor instead (function with the state, or using inject() does not conform with functional programing paradigm).
  • guessContentType, default value is true - when set to true, ContentTypeInterceptor from @runopencode/http package will be registered as interceptor automatically. This interceptor will set Content-Type header based on request body.

Available injection tokens

  • NGX_HTTP_CLIENT - injection token for HTTP client. You may use this token to inject HTTP client into your classes if you want to typehint client using interface instead of concrete implementation.
  • NGX_HTTP_CLIENT_ADAPTER - injection token for HTTP adapter. Instead of configuring adapter via adapter configuration parameter, you may register your adapter within Angular's DI container with this injection token to be used by HTTP client. However, configuration is preferred way of doing this.
  • NGX_HTTP_CLIENT_INTERCEPTOR - injection token for HTTP interceptors. Instead of configuring interceptors via interceptors configuration parameter, you may register your interceptors within Angular's DI container with this injection token to be used by HTTP client. Make sure that you register your interceptors as multi-injection tokens (multi: true).

Usage

Once you have imported NgxHttpClientModule into your application, you can inject HttpClientInterface into your classes and use it to execute HTTP requests.

import {
    HttpClientInterface,
    HttpResponseInterface
} from '@runopencode/http';
import {
    Inject,
    Injectable,
} from '@angular/core';

import { NGX_HTTP_CLIENT } from '@runopencode/ngx-http-client';
import { Observable }      from 'rxjs';
import { map }             from 'rxjs/operators';

@Injectable()
export class PostRepository {

    private readonly _client: HttpClientInterface;

    public constructor(@Inject(NGX_HTTP_CLIENT) client: HttpClientInterface) {
        this._client = client;
    }

    public async getPosts(): Observable<Post[]> {
        return this._client.get<PostPayload[]>('https://jsonplaceholder.typicode.com/posts').pipe(map(/* ... */));
    }

}

TODO

  • [ ] Write tests.