### File

`src/app/core/helix.service.ts`

### Index

###### Methods
Public can Protected delete Protected errorHandler Protected getHeaders Protected getHelixKey Protected post Protected put Protected request

### Constructor

`constructor(router: Router, http: HttpClient)`
Defined in src/app/core/helix.service.ts:11
Parameters :
Name | Type | Optional
router | `Router` | No
http | `HttpClient` | No

### Methods

Public can
`can()`
Defined in src/app/core/helix.service.ts:14
Returns : `Observable<any>`

Protected delete
`delete(path: string)`
Defined in src/app/core/helix.service.ts:48
Parameters :
Name | Type | Optional
path | `string` | No
Returns : `Observable<any>`

Protected errorHandler
`errorHandler(error: any)`
Defined in src/app/core/helix.service.ts:68
Parameters :
Name | Type | Optional
error | `any` | No
Returns : `any`

Protected getHeaders
`getHeaders()`
Defined in src/app/core/helix.service.ts:61
Returns : `any`

Protected getHelixKey
`getHelixKey()`
Defined in src/app/core/helix.service.ts:56
Returns : `string`

Protected post
`post(path: string, data: any)`
Defined in src/app/core/helix.service.ts:32
Parameters :
Name | Type | Optional
path | `string` | No
data | `any` | No
Returns : `Observable<any>`

Protected put
`put(path: string, data: string)`
Defined in src/app/core/helix.service.ts:40
Parameters :
Name | Type | Optional
path | `string` | No
data | `string` | No
Returns : `Observable<any>`

Protected request
`request(path: string, helix?: string)`
Defined in src/app/core/helix.service.ts:20
Parameters :
Name | Type | Optional
path | `string` | No
helix | `string` | Yes
Returns : `Observable<any>`

    import { throwError as observableThrowError, Observable } from 'rxjs';

    import { catchError } from 'rxjs/operators';
    import { Injectable } from '@angular/core';
    import { HttpHeaders, HttpClient, HttpResponse } from '@angular/common/http';
    import { Router } from '@angular/router';

    import { Settings } from './settings';

    @Injectable()
    export class HelixService {
      constructor(protected router: Router, private http: HttpClient) {}

      public can(): Observable<any> {
        return this.http
          .get(`${Settings.userAPI}/can`, { headers: this.getHeaders() })
          .pipe(catchError(this.errorHandler));
      }

      protected request(path: string, helix?: string): Observable<any> {
        if (helix == null) {
          helix = this.getHelixKey();
        }

        return this.http
          .get(`${Settings.helixAPI}${helix}${path}`, {
            headers: this.getHeaders(),
          })
          .pipe(catchError(this.errorHandler));
      }

      protected post(path: string, data: any): Observable<any> {
        return this.http
          .post(`${Settings.helixAPI}${this.getHelixKey()}${path}`, data, {
            headers: this.getHeaders(),
          })
          .pipe(catchError(this.errorHandler));
      }

      protected put(path: string, data: string): Observable<any> {
        return this.http
          .put(`${Settings.helixAPI}${this.getHelixKey()}${path}`, data, {
            headers: this.getHeaders(),
          })
          .pipe(catchError(this.errorHandler));
      }

      protected delete(path: string): Observable<any> {
        return this.http
          .delete(`${Settings.helixAPI}${this.getHelixKey()}${path}`, {
            headers: this.getHeaders(),
          })
          .pipe(catchError(this.errorHandler));
      }

      protected getHelixKey(): string {
        // fetch helix key from url
        return `/${this.router.url.split('/')[1]}`;
      }

      protected getHeaders() {
        const headers = new HttpHeaders();
        headers.append('Accept', 'application/json');
        headers.append('Content-Type', 'application/json');
        return headers;
      }

      protected errorHandler(error: any) {
        console.error(error);

        let message = error.message || 'Cannot reach Helix restful service.';

        if (error instanceof HttpResponse) {
          if (error.status == 404) {
            // rest api throws 404 directly to app without any wrapper
            message = 'Not Found';
          } else {
            message = error;
            try {
              message = JSON.parse(message).error;
            } catch (e) {}
          }
        }

        return observableThrowError(message);
      }
    }

# results matching ""

# No results matching ""