restangular_wrapper_service.ts
3.64 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
export abstract class RestangularWrapperService<T> {
private lastResponse: restangular.IResponse;
constructor(protected Restangular: restangular.IService, protected $q: ng.IQService, protected $log: ng.ILogService) {
}
protected abstract getPath(): string;
protected abstract getDataKeys(): { singular: string, plural: string };
protected get(id: number): restangular.IElement {
return this.Restangular.one(this.getPath(), id);
}
protected post<T>(elementRoot: restangular.IElement, element?: any, path?: string, params?: any, headers?: any): ng.IPromise<T> {
let deferred = this.$q.defer<T>();
let postData = <any>{};
postData[this.getDataKeys().singular] = element;
this.customPOST(
elementRoot,
postData,
this.getPath(),
{}
)
.then(this.getPostSuccessHandleFunction(deferred))
.catch(this.getHandleErrorFunction(deferred));
return deferred.promise;
}
protected customPOST(elementRoot: restangular.IElement, elem?: any, path?: string, params?: any, headers?: any) {
if (headers) {
headers['Content-Type'] = 'application/json';
} else {
headers = { 'Content-Type': 'application/json' };
}
return elementRoot.customPOST(elem, path, params, headers);
}
// TODO create a handle ErrorFactory too and move handleSuccessFactory and handleErrorFactory
// to a base class (of course we will have to creates a base class too)
getHandleSuccessFunction<C>(deferred: ng.IDeferred<C>, responseKey?: string): (response: restangular.IResponse) => void {
let self = this;
let successFunction = (response: restangular.IResponse): void => {
if (self.$log) {
self.$log.debug("Request successfull executed", self, response);
}
let data = response.data;
let dataKey: string;
if (data && self.getDataKeys()) {
if ((<Object>data).hasOwnProperty(self.getDataKeys().singular)) {
data = data[self.getDataKeys().singular];
dataKey = self.getDataKeys().singular;
} else if ((<Object>data).hasOwnProperty(self.getDataKeys().plural)) {
data = data[self.getDataKeys().plural];
dataKey = self.getDataKeys().plural;
}
}
let result: any = {};
result[dataKey] = data;
result.headers = response.headers;
deferred.resolve(result);
};
return successFunction;
}
getPostSuccessHandleFunction<T>(deferred: ng.IDeferred<T>): (response: restangular.IResponse) => void {
let self = this;
let successFunction = (response: restangular.IResponse): void => {
if (self.$log) {
self.$log.debug("Post successfully executed", self, response);
}
let data = response.data;
if ((<Object>data).hasOwnProperty(self.getDataKeys().singular)) {
deferred.resolve(data[self.getDataKeys().singular]);
} else {
deferred.resolve(data);
}
};
return successFunction;
}
getHandleErrorFunction<T>(deferred: ng.IDeferred<T>): (response: restangular.IResponse) => void {
let self = this;
let successFunction = (response: restangular.IResponse): void => {
if (self.$log) {
self.$log.error("Error executing request", self, response);
}
deferred.reject(response);
};
return successFunction;
}
}