I am working on implementing a reusable service to handle requests to my API. Currently, it is functioning as expected, but only for GET
requests.
This is the current function in use:
makeAPIRequest = ({ ...opts }) => {
return this.http.get(opts.url, opts.params)
.toPromise()
.then(response => response)
.catch(err => this.handleError(err));
};
Below is an example of how this function is utilized:
getCustomer(id): Promise<Customer> {
return this.APIService.makeAPIRequest({
url: this.customerEditUrl(id)
}) as Promise<Customer>;
}
I am looking to enhance the functionality by allowing the passing of an HTTP method into the opts
. However, I am uncertain about the most efficient way to achieve this without using extensive conditionals or repetition. Ideally, I would like to approach this in a concise manner. For instance, if I provide opts
with this structure:
{ url: this.customerEditUrl(id), params, httpMethod: 'POST' }
How can I modify my makeAPIRequest
function to accommodate this change?
makeAPIRequest = ({ ...opts }) => {
return this.http.post(opts.url, opts.params)
.toPromise()
.then(response => response)
.catch(err => this.handleError(err));
};
Appreciate any insights!