My understanding of Typescript is limited, so I have a basic question related to my web application's frontend. In most http get-requests, I need to include two parameters. To simplify this process, I created a simple wrapper for HttpClient
(from "angular/common/http") that automatically adds these parameters based on an object passed to it. Here is the current code snippet:
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,
});
}
}
Although this function works well in most cases, I also need to pass additional parameters in certain scenarios. However, I am unsure how to merge these additional parameters with the existing ones without modifying the functions that call my function. How can I achieve this without impacting the existing code?