GithubHelp home page GithubHelp logo

sdolier / angular-api-handler Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 1.0 63 KB

An angular api handler for intercepting http requests. Ideal for mocking responses during development or transforming/caching responses in production

License: MIT License

TypeScript 79.46% HTML 1.42% JavaScript 19.13%

angular-api-handler's Introduction

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 { }

angular-api-handler's People

Contributors

sdolier avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

samartomar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.