msgraph-typescript-typings icon indicating copy to clipboard operation
msgraph-typescript-typings copied to clipboard

Suggestion: Generic OData response type

Open Jonas-Seiler-Wave opened this issue 3 years ago • 1 comments

I think this library should include a type to match the OData response signature, as these properties are very important to consider in certain scenarios and I don't think the library consumer should have to define this type themselves or have to rely on another library.

Concretely, I suggest an implementation that ideally enables the following Angular code to compile

(this.http as HttpClient)
      .get<ExampleODataType<User>>('https://graph.microsoft.com/v1.0/me')
      .subscribe((response) => {
        console.log(response['@odata.context']);
        console.log(response.displayName);
      });
      
(this.http as HttpClient)
      .get<ExampleODataType<User[]>>('https://graph.microsoft.com/v1.0/users?$count')
      .subscribe((response) => {
        console.log(response['@odata.count']);
        console.log(response['@odata.nextLink']);
        console.log(response.value[0].displayName);
      });  
      

A simpler alternative to this could be a Type containing just the OData properties which one can make intersections with

(this.http as HttpClient)
      .get<User & ExampleODataType>('https://graph.microsoft.com/v1.0/me')
      .subscribe((response) => {
        console.log(response['@odata.context']);
        console.log(response.displayName);
      });
      
(this.http as HttpClient)
      .get<{ value: User[] } & ExampleODataType>('https://graph.microsoft.com/v1.0/users?$count')
      .subscribe((response) => {
        console.log(response['@odata.count']);
        console.log(response['@odata.nextLink']);
        console.log(response.value[0].displayName);
      });  
      

Jonas-Seiler-Wave avatar Mar 01 '22 14:03 Jonas-Seiler-Wave

@Jonas-Seiler-Wave Thank for your feedback!

nikithauc avatar Apr 05 '22 03:04 nikithauc