Skip to content

API client for Angular

License

Notifications You must be signed in to change notification settings

erento/angular-api-client

Repository files navigation

API client for Angular

Build Status License

Easy to use API client based on the Command pattern for Angular framework.

This module works with Angular 2.4 and higher.

Installation

npm install --save ng2-api-client

Quick start

Import API client module to your app.module.ts

// ...
import {HttpClient, HttpClientModule} from '@angular/common/http';
import {ApiClientHttpClient, ApiClientModule} from 'ng2-api-client';
import {HttpClient} from 'ng2-api-client';
// ...

@NgModule({
  declarations: [AppComponent],
  imports: [
    // ...
    HttpClientModule,
    // ...
    ApiClientModule.forRoot({httpClient: {
        provide: ApiClientHttpClient,
        useClass: HttpClient,
    }}),
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

Inject it to your class (could be Component, Service, etc...)

  • app.component.ts:

    import {Component, OnInit} from '@angular/core';
    import {ApiClient} from 'ng2-api-client';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
    })
    export class AppComponent implements OnInit {
      constructor(private readonly apiClient: ApiClient) {}
    
      ngOnInit() {
        this.apiClient
          .executeRequest<User>(new FetchUserCommand('u-u-i-d'))
          .subscribe((user: User) => {
            console.log('My user data is: ', user);
          });
      }
    }
  • fetch-user.command.ts:

    import {ApiBaseCommand, RequestMethod, UrlPathParameters} from 'ng2-api-client';
    
    export class FetchUserCommand implements ApiBaseCommand {
      public headers: Headers = {'X-Forwarded-For': 'proxy1'};
      public method: RequestMethod = RequestMethod.Get;
      public url: string = '/api/user/:uuid';
      public urlPathParameters: UrlPathParameters;
    
      constructor (userUuid: string) {
        this.urlPathParameters = {uuid: userUuid};
      }
    }

How to retry an request?

ApiClient.executeRequest has a second parameter where you can pass an amount of required retries and the ApiClient will take care of it.

Documentation

Command

Required properties:

  • method

    The property method defines the HTTP method. It requires the string enum value of RequestMethod.

  • url

    The url path without a scheme. Url can include wildcards starting with :.

    Examples: /api/user/:id or /api/user/:name/:lastname ...

    If the url includes the wildcard it will be validated with an input defined in the property urlPathParameters and replaced by the provided value.

Optional properties:

  • urlPathParameters

    This value is required when the wildcard is included in the url.

    Example: {role: 'admin'} with the url: /user/:role will generate: /user/admin

  • queryParameters

    An object defining query parameters.

    Examples:

    {search: 'neymar', team: 'psg'} will generate: ?search=neymar&team=psg

  • headers

    Used to set the headers of your request.

  • body

    Used to set the body of your request.

  • withCredential

    Boolean value that indicates whether or not requests should be made using credentials such as cookies, authorization headers or TLS client certificates.

    Default value: false.

  • responseType

    Used to set the response type of your request. Can be one of: 'arraybuffer' | 'blob' | 'json' | 'text'.

    Default value: json.

  • reportProgress

    Boolean value that indicates whether or not requests should report about progress.

    Default value: false.

Testing

Run npm test to execute tests.

License

The MIT License (see the LICENSE file for the full text)

Publishing

Always run npm run build before.

To publish a package run: npm publish ./dist/lib

If you want only to run it locally use npm pack as follows:

  1. npm pack ./dist/lib
  2. In your project npm i ../PATH_TO_TAR/ng2-api-client-X.X.X.tgz where X.X.X is the current version of a library.