Angular2 Simple API Client Service

simple-api-client-ng2

simple-api-client-ng2 is an Angular 2 api service, which work with simple-api-express, an expressjs api handler.

Index

Install

npm install simple-api-client-ng2  

Usage Flow

Import into Angular 2 application (typescript)

simple-api-client-ng2 is implemented as Angular 2 injectable service name SimpleApiClient.

For module using SimpleApiClient

Add SimpleApiClient into module providers:

import { SimpleApiClient } from 'simple-api-client-ng2';

@NgModule({
    providers: [SimpleApiClient]
})

For each child component using SimpleApiClient

import {SimpleApiClient, SimpleApiObj} from 'simple-api-client-ng2';

export class ChildComponent implement OnInit {

    apiObject: SimpleApiObj;

    constructor(private api: SimpleApiClient) { }

    ngOnInit() {
        this.apiObject = this.api.get('/demo');

        let reply = '';
        this.apiObject.call(
            'echo',
            'This is a test',
            r => this.reply = r);

        console.log(this.reply);
    }

}

API

SimpleApiClient.get

SimpleApiClient.get(baseUrl: string = '/'): SimpleApiObj will return a SimpleApiObj configure with baseUrl. Previous created SimpleApiObj will be returned if the same baseUrl is used.

this.apiObject = this.api.get('/demo');  

SimpleApiClient.list

SimpleApiClient.list(): string[] will return a string array containing the baseUrl of all SimpleApiObj created.

SimpleApiObj.call

SimpleApiObj.call(method, params, callback, errorHandler)

  • method: string Name of api
  • params: any Argument of api, can be basic type like string, number, or object
  • callback: (result: any) => void Callback function for handling api result
  • errorHandler: (error: any) => void = this.errorHandler Optional error handler to handle api call error

SimpleApiObj.setErrorHandler

SimpleApiObj.setErrorHandler(handler: (any) => void) replace SimpleApiObj default error handler with the specified one.

Error Handling

For detail example on error handling, please refer to error.component.ts contain in full example below.

Example

A detail example for both simple-api-express and simple-api-client-ng2.