Easy to use API client based on the Command pattern for Angular framework.
This module works with Angular 2.4 and higher.
npm install --save ng2-api-clientImport 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]})exportclassAppModule{}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',})exportclassAppComponentimplementsOnInit{constructor(privatereadonlyapiClient: ApiClient){}ngOnInit(){this.apiClient.executeRequest<User>(newFetchUserCommand('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';exportclassFetchUserCommandimplementsApiBaseCommand{publicheaders: Headers={'X-Forwarded-For': 'proxy1'};publicmethod: RequestMethod=RequestMethod.Get;publicurl: string='/api/user/:uuid';publicurlPathParameters: UrlPathParameters;constructor(userUuid: string){this.urlPathParameters={uuid: userUuid};}}
ApiClient.executeRequest has a second parameter where you can pass an amount
of required retries and the ApiClient will take care of it.
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/:idor/api/user/:name/:lastname...If the url includes the wildcard it will be validated with an input defined in the property
urlPathParametersand replaced by the provided value.
urlPathParameters
This value is required when the wildcard is included in the url.
Example:
{role: 'admin'}with the url:/user/:rolewill generate:/user/adminqueryParameters
An object defining query parameters.
Examples:
{search: 'neymar', team: 'psg'}will generate:?search=neymar&team=psgheaders
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.
Run npm test to execute tests.
The MIT License (see the LICENSE file for the full text)
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:
npm pack ./dist/lib- In your project
npm i ../PATH_TO_TAR/ng2-api-client-X.X.X.tgzwhere X.X.X is the current version of a library.