]>
Commit | Line | Data |
---|---|---|
df98563e C |
1 | import { Injectable } from '@angular/core' |
2 | import { Response } from '@angular/http' | |
3 | import { Observable } from 'rxjs/Observable' | |
de59c48f C |
4 | |
5 | export interface ResultList { | |
df98563e C |
6 | data: any[] |
7 | total: number | |
de59c48f C |
8 | } |
9 | ||
10 | @Injectable() | |
11 | export class RestExtractor { | |
12 | ||
df98563e C |
13 | extractDataBool (res: Response) { |
14 | return true | |
de59c48f C |
15 | } |
16 | ||
df98563e C |
17 | extractDataList (res: Response) { |
18 | const body = res.json() | |
de59c48f C |
19 | |
20 | const ret: ResultList = { | |
21 | data: body.data, | |
22 | total: body.total | |
df98563e | 23 | } |
de59c48f | 24 | |
df98563e | 25 | return ret |
de59c48f C |
26 | } |
27 | ||
df98563e C |
28 | extractDataGet (res: Response) { |
29 | return res.json() | |
de59c48f C |
30 | } |
31 | ||
df98563e C |
32 | handleError (res: Response) { |
33 | let text = 'Server error: ' | |
34 | text += res.text() | |
35 | let json = '' | |
bf68dd75 C |
36 | |
37 | try { | |
df98563e C |
38 | json = res.json() |
39 | } catch (err) { | |
40 | console.error('Cannot get JSON from response.') | |
41 | } | |
de59c48f C |
42 | |
43 | const error = { | |
44 | json, | |
45 | text | |
df98563e | 46 | } |
de59c48f | 47 | |
df98563e | 48 | console.error(error) |
bf68dd75 | 49 | |
df98563e | 50 | return Observable.throw(error) |
de59c48f C |
51 | } |
52 | } |