I'm developing a service in Angular 4 which must offer a generic http request method (instead of separated get, put, post ... ). I'm trying to use the request method in HttpClient, but I can't manage to pass the required options parameters. My code looks roughly like this:
let options = {
headers: headers,
content: content,
responseType: ????,
observe: ??????
};
http.request(method, url, options)
.map(res => new SeamResponse(res.status, res.json()))
Now, I'm having two problems:
- I can't manage to pass the reponseType option, because if I specify a value like 'json', I get this eror:
error TS2345: Argument of type '{ headers: HttpHeaders; content: string; responseType: string; }' is not assignable to parameter of type '{ body?: any; headers?: HttpHeaders; params?: HttpParams; observe?: HttpObserve; reportProgress?:...'. Types of property 'responseType' are incompatible. Type 'string' is not assignable to type '"json" | "arraybuffer" | "blob" | "text"'.
- Neither can I pass the observe options, because it is in a type which is apparently not exported from the HttpClient class.
error TS2305: Module '"/Users/pchacin/Documents/workspace/seam-sdk-core-ts/node_modules/@angular/common/http"' has no exported member 'HttpObserve'.
- I can't manage to process the response because it doesn't recognise the map method, which I understand is standard for Observable:
error TS2339: Property 'map' does not exist on type 'Observable'.
Many thanks in advance
EDIT:
Question 2 is solved in the latest version of angular (4.4.1) as an experiment feature by exporting HttpObserve, however discussions about the topic in github suggest the HttpObserve type will be removed and this option will be defined as string, so I'm not inclined to follow this path. The question I have is: the request method has been around for some time, since 4.2, at least Hasn't anyone used it ever? if so, how?