0.0.3 • Published 7 years ago

a4-http v0.0.3

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

a4-http

Angular 4 Enhanced Http Service

How-To

Install

npm install a4-http

app.module.ts

  1. Add HttpModule to imports of the app.module.ts.
...
import { HttpModule } from 'a4-http';
...

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    ...,
    HttpModule,
    ...
  ],
  ...
})

Inject the service

constructor(private http: HttpService) { }

Show/Hide the Spinner

this.http.setSpinner(
    () => {
        ... // show spinner code
    }, () => {
        ... // hide spinner code
    }
);

get, post, put, patch & delete

public get(url: string, showSpinner?: boolean): Observable<Response>;
public get(url: string, options?: any, showSpinner?: boolean): Observable<Response>;

public post(url: string, body: any, showSpinner?: boolean): Observable<Response>;
public post(url: string, body: any, options?: any, showSpinner?: boolean): Observable<Response>;

public put(url: string, body: any, showSpinner?: boolean): Observable<Response>;
public put(url: string, body: any, options?: any, showSpinner?: boolean): Observable<Response>;

public patch(url: string, body: any, showSpinner?: boolean): Observable<Response>;
public patch(url: string, body: any, options?: any, showSpinner?: boolean): Observable<Response>;

public delete(url: string, showSpinner?: boolean): Observable<Response>;
public delete(url: string, options?: any, showSpinner?: boolean): Observable<Response>;

redirect when error

this.http.setRedirect({
      '-1': '/error',
      0: '/signin',
      401: '/signin',
      403: '/signin'
    });
  1. Redirect to /signin if http status is 0, 401 or 403.
  2. '-1' is the default route if there is an error... '-1' is optional.