Joaquín Reñé
13 hours ago 78b085815b9873acdf178b2e9c9598d065fd40c0
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
77
import { LocaleService } from '../common/i18n';
import { BasicService } from '../common/utils';
import { Observable } from 'rxjs/Observable';
import { Http, RequestOptionsArgs, URLSearchParams, Headers } from '@angular/http';
import { Locker } from 'angular-safeguard';
export class MySearchParams extends URLSearchParams {
  constructor(obj: any = {}) {
    const searchQuery = Object.keys(obj).map(key => `${key}=${encodeURIComponent(obj[key])}`).join('&');
    super(searchQuery);
  }
}
export class SeCurisResourceServices extends BasicService {
  constructor($L: LocaleService,
              protected http: Http,
              protected store: Locker,
              protected resource: string) {
    super($L);
    this.resource = `api/${resource}`;
  }
  protected getAuthHeaders(): RequestOptionsArgs {
    const token = this.store.get('token');
    const headers = new Headers();
    if (token) {
      headers.append('X-SECURIS-TOKEN', token);
    }
    headers.append('Content-Type', 'application/json');
    return { headers: headers };
  }
  public get(id?: any): Observable<any> {
    const url = `${this.resource}/${id || ''}`;
    return this.http.get(url, this.getAuthHeaders())
      .map(response => response.json())
      .catch(err => super.processErrorResponse(err));
  }
  public create(data: any): Observable<any> {
    const url = `${this.resource}`;
    return this.http.post(url, JSON.stringify(data), this.getAuthHeaders())
      .map(response => response.json())
      .catch(err => super.processErrorResponse(err));
  }
  public modify(id: any, data: any): Observable<any> {
    const url = `${this.resource}/${id}`;
    // Se usa POST para modificar (según tu diseño original)
    return this.http.post(url, JSON.stringify(data), this.getAuthHeaders())
      .map(response => response.json())
      .catch(err => super.processErrorResponse(err));
  }
  public remove(id: any): Observable<any> {
    const url = `${this.resource}/${id}`;
    return this.http.delete(url, this.getAuthHeaders())
      .map(response => response.json())
      .catch(err => super.processErrorResponse(err));
  }
  public action(id: any, action: string, method: string = 'POST', params: any = {}): Observable<any> {
    const url = `${this.resource}/${id}/${action}`;
    params.action = action;
    const options: RequestOptionsArgs = {
      url: url,
      method: method,
      search: (method === 'GET') ? new MySearchParams(params) : undefined,
      headers: this.getAuthHeaders().headers,
      body: (method === 'POST') ? JSON.stringify(params) : undefined
    };
    return this.http.request(url, options)
      .map(response => response.json())
      .catch(err => super.processErrorResponse(err));
  }
}