Home > front end >  The best way to process data between a client application and a web api?
The best way to process data between a client application and a web api?

Time:08-08

I want to create a SPA web application (ASP.NET Core Web API Angular). I have a controller that returns a list of objects, and I have an angular component that is a list of objects, but this list doesn't need all the information that comes from the API. What is the best way to do: create another object in the controller and return certain information or just edit the data in the component?

CodePudding user response:

Good approach would be to return only the required properties in typed object from the Angular Service, so the component knows exactly the type and has access only to properties it needs:

interface DataDTO {
   importantProp1: string;
   importantProp2: string;
   otherProp3: string;
}

export interface Data {
   importantProp1: string;
   importantProp2: string;
}

export class ApiService {

  getData(): Observable<Data[]> {

    return this.http.get<DataDTO[]>('url').pipe(
       map(response => response.map(item => ({
           importantProp1: item.importantProp1;
           importantProp2: item.importantProp2;
         }))
       ),
    );
  }

}



  • Related