all files / src/ path.ts

94.59% Statements 35/37
100% Branches 0/0
83.33% Functions 10/12
94.59% Lines 35/37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76    49× 49×   21×   10× 10×   11× 11×                 13× 13× 13× 18× 18×   13× 13×                                                      
import { RestangularHttp } from './http';
import { Response, RequestOptionsArgs } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { RestangularConfig } from './config';
import { RestangularOnePath } from './one-path';
import { RestangularAllPath } from './all-path';
import { PathInterface } from './path.interface';
 
import 'rxjs/add/operator/map';
 
export class RestangularPath {
  config: RestangularConfig;
  parent: RestangularPath;
  route: PathInterface;
 
  constructor(config: RestangularConfig, parent?: RestangularPath) {
    this.config = config;
    this.parent = parent;
  }
 
  private __normalizeRoute(route: string): string {
    return route.replace(/^\/+|\/+$/g, '');
  }
 
  all(route: string): RestangularPath {
    this.route = new RestangularAllPath(this.__normalizeRoute(route));
 
    return new RestangularPath(this.config, this);
  }
 
  one(route: string, id: string | number): RestangularPath {
    this.route = new RestangularOnePath(this.__normalizeRoute(route), id);
 
    return new RestangularPath(this.config, this);
  }
 
  get(options?: RequestOptionsArgs): Observable<Response> {
    return RestangularHttp.get(this, options);
  }
 
  getList(options?: RequestOptionsArgs): Observable<Response> {
    return RestangularHttp.getList(this, options);
  }
 
  post(body: any, options?: RequestOptionsArgs): Observable<Response> {
    return RestangularHttp.post(this, body, options);
  }
 
  put(body: any, options?: RequestOptionsArgs): Observable<Response> {
    return RestangularHttp.put(this, body, options);
  }
 
  remove(options?: RequestOptionsArgs): Observable<Response> {
    return RestangularHttp.remove(this, options);
  }
 
  toArray(): Array<string> {
    let path: Array<string> = [];
    let current: RestangularPath = this;
 
    while (current.parent) {
      path.push(...current.parent.route.path().reverse());
 
      current = current.parent;
    }
 
    path.push(this.config.baseUrl);
 
    return path.reverse();
  }
 
  toString(): string {
    return this.toArray().join('/');
  }
}