angular-api-handler
TypeScript icon, indicating that this package has built-in type declarations

0.0.6 • Public • Published

Angular API Handler

npm version Build Status codecov peerDependency Status

Handle API requests through the implementation of request handlers.

Ideal for:

  • mocking API responses during development and testing
  • transforming api requests/responses
  • caching API responses

Installation instructions

Install angular-api-handler from npm

npm install angular-api-handler --save

Getting Started

Create request handlers that implement AngularApiHandlerRequestHandler for each api endpoint.

import { Request, ResponseOptions } from '@angular/http';
 
import {AngularApiHandlerRequestHandler} from "angular-api-handler";
 
export class HelloServiceHandler extends AngularApiHandlerRequestHandler {
 
    matchingUrlsRegex: [{ url: string, data?: any }] = [
        {
            url: 'api/hello.*'
        }
    ];
 
    get(req: Request, index: number): ResponseOptions {
        let responseBody = { message: "Hello from mock API Handler"};
 
        console.log('mocking response for ' + req.url);
        console.log(responseBody);
 
        return this.successResponse(responseBody);
    }
}

Implement AngularApiHandlerService that defines the list of request handlers.

import { AngularApiHandlerService, AngularApiHandlerRequestHandler } from "angular-api-handler";
 
import { HelloServiceHandler } from './service-handlers/hello-handler';
 
export class AngularApiHandlersService implements AngularApiHandlerService {
 
    getHandlers(): AngularApiHandlerRequestHandler[] {
        return [
            new HelloServiceHandler()
        ];
    }
}

Register the module in the @NgModule.imports with optional module configuration.

import { HttpModule }  from '@angular/http';
 
import { AngularApiHandlerModule, AngularApiHandlerConfig } from 'angular-api-handler';
import { AngularApiHandlersService } from './angular-api-handler-service';
 
@NgModule({
    imports: [
        BrowserModule,
        HttpModule,
        AngularApiHandlerModule.forRoot(AngularApiHandlersService, new AngularApiHandlerConfig({ enabled: true }))
    ],
    declarations: [
        AppComponent
    ],
    bootstrap: [
        AppComponent
    ]
})
 
export class AppModule { }

Package Sidebar

Install

npm i angular-api-handler

Weekly Downloads

0

Version

0.0.6

License

MIT

Last publish

Collaborators

  • sdolier