4.0.0 • Published 7 years ago

angular2-http-interceptor v4.0.0

Weekly downloads
81
License
MIT
Repository
github
Last release
7 years ago

angular2-http-interceptor

Http interceptor for angular2. Built with angular-library-starter

Features

  • Registering interceptors globally
  • Separate interceptors for requests and responses
  • Modify requests (even url) from request interceptors
  • Cancel requests from request interceptors
  • Modify responses from response interceptors
  • Interceptor Service is not coupled with Http Service
  • Choose between overriding original Http Service or keep it and still use interceptors
  • Support AOT.

Table of Contents

Installation

To install this library, run:

$ npm install angular2-http-interceptor --save

Usage

First, write your interceptor.

import {Request, Response} from '@angular/http';
import {Inject} from '@angular/core';
import {Platform} from 'ionic-angular';

import {IHttpInterceptor} from 'angular2-http-interceptor';
export class AuthInterceptor implements IHttpInterceptor {
  constructor(@Inject(Platform) private platform: Platform) {
  }

  before(request: Request): Request {
    if (this.platform.is('cordova') && request.url.match(/^\/api-hk\//)) {
      request.url = `http://yulonh.com${request.url}`;
    }
    return request;
  }
}

Then add code HttpInterceptorModule.withInterceptors([AuthInterceptor]) in app.module.ts file.

...
import {HttpInterceptorModule} from 'angular2-http-interceptor';

@NgModule({
  ...
  imports: [
    ...
    HttpInterceptorModule.withInterceptors(      [{
                                                   deps: [Platform],
                                                   provide: HttpInterceptor,
                                                   useClass: AuthInterceptor,
                                                   multi: true
                                                 }])
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    ...
  ],
  providers: [...]
})
export class AppModule {

}

License

MIT © yulonh

4.0.0

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.8.0

7 years ago

1.6.7

7 years ago

1.6.6

7 years ago

1.6.5

7 years ago

1.6.4

7 years ago

1.6.3

7 years ago

1.6.2

7 years ago

1.6.1

7 years ago

1.6.0

7 years ago

1.5.9

7 years ago

1.5.8

7 years ago

1.5.7

7 years ago

1.5.6

7 years ago

1.5.5

7 years ago

1.5.4

7 years ago

1.5.3

7 years ago

1.5.2

7 years ago

1.5.1

7 years ago

1.5.0

7 years ago

1.4.9

7 years ago

1.4.8

7 years ago

1.4.7

7 years ago

1.3.5

7 years ago

1.3.3

7 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.9

7 years ago

1.2.8

7 years ago

1.2.7

7 years ago

1.2.6

7 years ago

1.2.5

7 years ago

1.2.4

7 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.8

7 years ago

1.1.7

7 years ago

1.1.6

7 years ago

1.1.5

7 years ago

1.1.4

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago

0.0.0

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago