aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/rest/rest-extractor.service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/shared/rest/rest-extractor.service.ts')
-rw-r--r--client/src/app/shared/rest/rest-extractor.service.ts66
1 files changed, 36 insertions, 30 deletions
diff --git a/client/src/app/shared/rest/rest-extractor.service.ts b/client/src/app/shared/rest/rest-extractor.service.ts
index f6a818ec8..32dad5c73 100644
--- a/client/src/app/shared/rest/rest-extractor.service.ts
+++ b/client/src/app/shared/rest/rest-extractor.service.ts
@@ -1,52 +1,58 @@
1import { Injectable } from '@angular/core' 1import { Injectable } from '@angular/core'
2import { Response } from '@angular/http'
3import { Observable } from 'rxjs/Observable' 2import { Observable } from 'rxjs/Observable'
3import { HttpErrorResponse } from '@angular/common/http'
4 4
5export interface ResultList { 5import { Utils } from '../utils'
6 data: any[] 6import { ResultList } from '../../../../../shared'
7 total: number
8}
9 7
10@Injectable() 8@Injectable()
11export class RestExtractor { 9export class RestExtractor {
12 10
13 extractDataBool (res: Response) { 11 extractDataBool () {
14 return true 12 return true
15 } 13 }
16 14
17 extractDataList (res: Response) { 15 applyToResultListData <T> (result: ResultList<T>, fun: Function, additionalArgs?: any[]): ResultList<T> {
18 const body = res.json() 16 const data: T[] = result.data
17 const newData: T[] = []
19 18
20 const ret: ResultList = { 19 data.forEach(d => newData.push(fun.call(this, d, additionalArgs)))
21 data: body.data,
22 total: body.total
23 }
24 20
25 return ret 21 return {
22 total: result.total,
23 data: newData
24 }
26 } 25 }
27 26
28 extractDataGet (res: Response) { 27 convertResultListDateToHuman <T> (result: ResultList<T>, fieldsToConvert: string[] = [ 'createdAt' ]): ResultList<T> {
29 return res.json() 28 return this.applyToResultListData(result, this.convertDateToHuman, [ fieldsToConvert ])
30 } 29 }
31 30
32 handleError (res: Response) { 31 convertDateToHuman (target: object, fieldsToConvert: string[]) {
33 let text = 'Server error: ' 32 const source = {}
34 text += res.text() 33 fieldsToConvert.forEach(field => {
35 let json = '' 34 source[field] = Utils.dateToHuman(target[field])
35 })
36 36
37 try { 37 return Object.assign(target, source)
38 json = res.json() 38 }
39 } catch (err) {
40 console.error('Cannot get JSON from response.')
41 }
42 39
43 const error = { 40 handleError (err: HttpErrorResponse) {
44 json, 41 let errorMessage
45 text 42
43 if (err.error instanceof Error) {
44 // A client-side or network error occurred. Handle it accordingly.
45 errorMessage = err.error.message
46 console.error('An error occurred:', errorMessage)
47 } else if (err.status !== undefined) {
48 // The backend returned an unsuccessful response code.
49 // The response body may contain clues as to what went wrong,
50 errorMessage = err.error
51 console.error(`Backend returned code ${err.status}, body was: ${errorMessage}`)
52 } else {
53 errorMessage = err
46 } 54 }
47 55
48 console.error(error) 56 return Observable.throw(errorMessage)
49
50 return Observable.throw(error)
51 } 57 }
52} 58}