First of all, my Typescript knowledge is very low, so I assume that this is a somewhat basic question. In the frontend of my web application, there are two parameters which I need to provide in 99% of all http get-requests. To make my life a little bit easier, I decided to write a simple wrapper around HttpClient
(from "@angular/common/http") which provides these params for me. All I need to pass to it is an object from which it then reads those two parameters and adds them to the parameters of the get-request. Here is how this code currently looks like:
public get<T>(url: string, additionalObj?: AdditionalObject) {
if (additionalObj == null) {
return this.http.get<T>(url, {
headers: {
"Content-Type": "application/json",
},
});
} else {
const params: Record<string, string> = {};
const param1: string | undefined = additionalObj?.param1;
const param2: string | undefined = additionalObj?.param2;
if (param1) {
params["param1"] = param1;
}
if (param2) {
params["param2"] = param2;
}
return this.http.get<T>(url, {
headers: {
"Content-Type": "application/json",
},
params,
});
}
}
While this works just fine in most cases, I also have some functions which need to provide even more params to the get-request, so I need some way to merge the params provided to my function with the two params which I provide inside the function, but I don't know how to do that. I do not want to change anything in the functions which already call my function but I also want to provide a way to specify additional params if necessary. How can I do that?