While navigating through this new environment, I encountered the need to make a Web API call. The abundance of versions and options can be overwhelming. Nevertheless, I managed to find a solution that works well. Although I understand that defining "best practice" is somewhat subjective, I am curious if there is a more straightforward approach to accomplish this task.
My desired response format looks like this:
export interface AuditDetail {
updatedAt: Date;
updatedBy: string;
}
This is the server-side model:
[DataContract]
public class PlanHistory
{
[Key]
public int Id { get; set; }
[DataMember]
[MaxLength(50)]
public string UpdatedBy { get; set; }
[DataMember]
public DateTime UpdatedAt { get; set; }
}
And here is the Angular service I wrote:
getAuditDetails(planId: number, fieldName: string, fieldValue: string): Observable<AuditDetail> {
//return of({ updatedAt: new Date(), updatedBy: "Hawchorn" }); //Tooltip displays data
interface PlanHistory //Created because couldn't figure out how to directly map from Json to an Audit Detail.
{
UpdatedAt: Date;
UpdatedBy: string;
}
this.log("Retrieving Audit Details");
return this.http
.get<PlanHistory>(this.webApiUrl +
"PlanHistories?planId=" +
planId +
"&fieldName=" +
fieldName +
"%20&fieldValue=" +
fieldValue).pipe(map((response: PlanHistory) => {
return <AuditDetail>{ updatedAt: response.UpdatedAt, updatedBy: response.UpdatedBy };
}));
}
The current method is effective, but I find creating that intermediate interface bothersome.
How can I achieve the same result without needing the additional interface?