| .. | .. |
|---|
| 1 | 1 | import { LocaleService } from '../common/i18n'; |
|---|
| 2 | 2 | import { BasicService } from '../common/utils'; |
|---|
| 3 | 3 | import { Observable } from 'rxjs/Observable'; |
|---|
| 4 | | -import { Http, RequestOptionsArgs, URLSearchParams } from '@angular/http'; |
|---|
| 4 | +import { Http, RequestOptionsArgs, URLSearchParams, Headers } from '@angular/http'; |
|---|
| 5 | +import { Locker } from 'angular-safeguard'; |
|---|
| 6 | + |
|---|
| 5 | 7 | |
|---|
| 6 | 8 | export class MySearchParams extends URLSearchParams { |
|---|
| 7 | | - constructor(obj : any = {}) { |
|---|
| 8 | | - var searchQuery = Object.keys(obj).map(key => `${key}=${encodeURIComponent(obj[key])}`).join('&'); |
|---|
| 9 | + constructor(obj: any = {}) { |
|---|
| 10 | + const searchQuery = Object.keys(obj).map(key => `${key}=${encodeURIComponent(obj[key])}`).join('&'); |
|---|
| 9 | 11 | super(searchQuery); |
|---|
| 10 | 12 | } |
|---|
| 11 | 13 | } |
|---|
| 12 | 14 | |
|---|
| 13 | 15 | export class SeCurisResourceServices extends BasicService { |
|---|
| 14 | | - constructor($L: LocaleService, |
|---|
| 15 | | - protected http: Http, |
|---|
| 16 | | - protected resource: string) { |
|---|
| 17 | | - super($L); |
|---|
| 18 | | - } |
|---|
| 16 | + constructor($L: LocaleService, |
|---|
| 17 | + protected http: Http, |
|---|
| 18 | + protected store: Locker, |
|---|
| 19 | + protected resource: string) { |
|---|
| 20 | + super($L); |
|---|
| 21 | + this.resource = `api/${resource}`; |
|---|
| 22 | + } |
|---|
| 19 | 23 | |
|---|
| 20 | | - public get(id?: any) : Observable<any> { |
|---|
| 21 | | - let url = `${this.resource}/${id || ''}`; |
|---|
| 22 | | - return this.http.get(url).map(response => response.json()).catch(err => super.processErrorResponse(err)); |
|---|
| 24 | + protected getAuthHeaders(): RequestOptionsArgs { |
|---|
| 25 | + const token = this.store.get('token'); |
|---|
| 26 | + const headers = new Headers(); |
|---|
| 27 | + if (token) { |
|---|
| 28 | + headers.append('X-SECURIS-TOKEN', token); |
|---|
| 23 | 29 | } |
|---|
| 30 | + headers.append('Content-Type', 'application/json'); |
|---|
| 31 | + return { headers: headers }; |
|---|
| 32 | + } |
|---|
| 24 | 33 | |
|---|
| 25 | | - public create(data: any) : Observable<any> { |
|---|
| 26 | | - let url = `${this.resource}`; |
|---|
| 27 | | - return this.http.post(url, JSON.stringify(data)).map(response => response.json()).catch(err => super.processErrorResponse(err)); |
|---|
| 28 | | - } |
|---|
| 34 | + public get(id?: any): Observable<any> { |
|---|
| 35 | + const url = `${this.resource}/${id || ''}`; |
|---|
| 36 | + return this.http.get(url, this.getAuthHeaders()) |
|---|
| 37 | + .map(response => response.json()) |
|---|
| 38 | + .catch(err => super.processErrorResponse(err)); |
|---|
| 39 | + } |
|---|
| 29 | 40 | |
|---|
| 30 | | - public modify(id: any, data: any) : Observable<any> { |
|---|
| 31 | | - let url = `${this.resource}/${id}`; |
|---|
| 32 | | - return this.http.post(url, JSON.stringify(data)).map(response => response.json()).catch(err => super.processErrorResponse(err)); |
|---|
| 33 | | - } |
|---|
| 41 | + public create(data: any): Observable<any> { |
|---|
| 42 | + const url = `${this.resource}`; |
|---|
| 43 | + return this.http.post(url, JSON.stringify(data), this.getAuthHeaders()) |
|---|
| 44 | + .map(response => response.json()) |
|---|
| 45 | + .catch(err => super.processErrorResponse(err)); |
|---|
| 46 | + } |
|---|
| 34 | 47 | |
|---|
| 35 | | - public remove(id: any) : Observable<any> { |
|---|
| 36 | | - let url = `${this.resource}/${id}`; |
|---|
| 37 | | - return this.http.delete(url).map(response => response.json()).catch(err => super.processErrorResponse(err)); |
|---|
| 38 | | - } |
|---|
| 39 | | - |
|---|
| 40 | | - public action(id: any, action: string, method = 'POST', params : any = {}) : Observable<any> { |
|---|
| 41 | | - let url = `${this.resource}/${id}/${action}`; |
|---|
| 42 | | - params.action = action; |
|---|
| 43 | | - var options:RequestOptionsArgs = { |
|---|
| 44 | | - url: url, |
|---|
| 45 | | - method: method, |
|---|
| 46 | | - search: (method == 'GET') && new MySearchParams(params) || undefined, |
|---|
| 47 | | - body: (method == 'POST') && JSON.stringify(params) || undefined |
|---|
| 48 | | - }; |
|---|
| 49 | | - return this.http.request(url, options).map(response => response.json()).catch(err => super.processErrorResponse(err)); |
|---|
| 50 | | - } |
|---|
| 48 | + public modify(id: any, data: any): Observable<any> { |
|---|
| 49 | + const url = `${this.resource}/${id}`; |
|---|
| 50 | + // Se usa POST para modificar (según tu diseño original) |
|---|
| 51 | + return this.http.post(url, JSON.stringify(data), this.getAuthHeaders()) |
|---|
| 52 | + .map(response => response.json()) |
|---|
| 53 | + .catch(err => super.processErrorResponse(err)); |
|---|
| 54 | + } |
|---|
| 51 | 55 | |
|---|
| 56 | + public remove(id: any): Observable<any> { |
|---|
| 57 | + const url = `${this.resource}/${id}`; |
|---|
| 58 | + return this.http.delete(url, this.getAuthHeaders()) |
|---|
| 59 | + .map(response => response.json()) |
|---|
| 60 | + .catch(err => super.processErrorResponse(err)); |
|---|
| 61 | + } |
|---|
| 52 | 62 | |
|---|
| 63 | + public action(id: any, action: string, method: string = 'POST', params: any = {}): Observable<any> { |
|---|
| 64 | + const url = `${this.resource}/${id}/${action}`; |
|---|
| 65 | + params.action = action; |
|---|
| 66 | + const options: RequestOptionsArgs = { |
|---|
| 67 | + url: url, |
|---|
| 68 | + method: method, |
|---|
| 69 | + search: (method === 'GET') ? new MySearchParams(params) : undefined, |
|---|
| 70 | + headers: this.getAuthHeaders().headers, |
|---|
| 71 | + body: (method === 'POST') ? JSON.stringify(params) : undefined |
|---|
| 72 | + }; |
|---|
| 73 | + return this.http.request(url, options) |
|---|
| 74 | + .map(response => response.json()) |
|---|
| 75 | + .catch(err => super.processErrorResponse(err)); |
|---|
| 76 | + } |
|---|
| 53 | 77 | } |
|---|