-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
bcf-api-request.service.ts
108 lines (98 loc) · 3.38 KB
/
bcf-api-request.service.ts
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
101
102
103
104
105
106
107
108
import { HttpClient, HttpErrorResponse, HttpParams } from "@angular/common/http";
import { Injector } from "@angular/core";
import { TypedJSON } from "typedjson";
import { Constructor } from "@angular/cdk/table";
import { Observable, throwError } from "rxjs";
import {
HTTPClientHeaders,
HTTPClientOptions,
HTTPClientParamMap,
HTTPSupportedMethods
} from "core-app/modules/hal/http/http.interfaces";
import { URLParamsEncoder } from "core-app/modules/hal/services/url-params-encoder";
import { catchError, map } from "rxjs/operators";
import { InjectField } from "core-app/helpers/angular/inject-field.decorator";
export class BcfApiRequestService<T> {
@InjectField() http:HttpClient;
/**
* Create a BCF api request service.
* Optionally pass a resource map to map the resulting data to with TypedJson.
*
* @param injector Injector
* @param resourceClass Optional mapped resource class with TypedJson annotations
*/
constructor(readonly injector:Injector,
readonly resourceClass?:Constructor<T>) {
}
/**
* Request GET from the given BCF API 2.1 resource and map it to +resourceClass+.
*
* @param path API path to request
* @param params Request query params
* @param headers optional headers map
*/
get(path:string, params:HTTPClientParamMap, headers:HTTPClientHeaders = {}):Observable<T> {
const config:HTTPClientOptions = {
headers: headers,
params: new HttpParams({ encoder: new URLParamsEncoder(), fromObject: params }),
withCredentials: true,
responseType: 'json'
};
return this._request('get', path, config);
}
/**
* Request the given BCF API 2.1 resource and map it to +resourceClass+.
*
* @param method request method
* @param path API path to request
* @param data Request payload (URL params for get, JSON payload otherwise)
* @param data Request payload (URL params for get, JSON payload otherwise)
*/
public request(method:HTTPSupportedMethods, path:string, data:HTTPClientParamMap = {}, headers:HTTPClientHeaders = {}):Observable<T> {
// HttpClient requires us to create HttpParams instead of passing data for get
// so forward to that method instead.
if (method === 'get') {
return this.get(path, data, headers);
}
const config:HTTPClientOptions = {
body: data || {},
headers: headers,
withCredentials: true,
responseType: 'json'
};
return this._request(method, path, config);
}
/**
* Perform the request with httpClient and deserialize the result
*
* @param method Request method
* @param path Request path
* @param config HTTP client configuration
*
* @private
*/
private _request(method:HTTPSupportedMethods, path:string, config:HTTPClientOptions):Observable<T> {
return this
.http
.request<T>(method, path, config)
.pipe(
map((response:any) => this.deserialize(response)),
catchError((error:HttpErrorResponse) => {
console.error(`Failed to ${method} ${path}: ${error.name}`);
return throwError(error);
})
);
}
/**
* Deserialize the JSON data into the mapped resource class, if given.
* @param data JSON API response.
*/
protected deserialize(data:any):T {
if (this.resourceClass) {
const serializer = new TypedJSON(this.resourceClass);
return serializer.parse(data)!;
} else {
return data;
}
}
}