diegomvh / angular-odata

Client side OData typescript library for Angular
https://www.npmjs.com/package/angular-odata
MIT License
50 stars 14 forks source link

Feature request: Simplify ODataSingeltonResource get options responseType to default to 'entity' #8

Closed Ali-ModernBeauty closed 4 years ago

Ali-ModernBeauty commented 4 years ago

Hey! very minor issue. I hope I'm using the library correctly. It would be nice to have the responseType in options parameter of get in ODataSingletonResource to default to 'entity'

Here is some reference code:

  private _user : AppUser;
  private _userOdata      : ODataSingletonResource<AppUser>;

  constructor(private _CONST  : ConstantsService,
                      odata   : ODataClient) {
    this._userOdata = odata.singleton<AppUser>(this._CONST.ODATA_URLS.USER);
  }

  public get user() : Promise<AppUser> {
    return new Promise(async (resolve)=>{
      if(!this._user)
        await this.initUser();
      resolve(this._user);
    });
  }

  async initUser(){
    // is this necessary? Singelton is implied that it returns one entity
    let options = {
      responseType: 'entity' as 'entity'
    }
    this._user = await this._userOdata.get(options).toPromise().then(([user]) => user);
  }
diegomvh commented 4 years ago

I think you are right, the main intention of the OData<Thing>Resource is to map the specifications of the standard. For other uses the client can be used directly

this.odata.get(this._userOdata).subscribe(body => { r.toEntities(body) });

Although for this the methods toEntity, toEntities and toValue would have to be public.

On the other hand, similar to what happens with the functions, it is not bad that the responseTypes are optional, what I do not know if it corresponds is to assume the value entity by default.

I'm going to make some changes and take a closer look at the standard's specifications.

Thank you regards

Ali-ModernBeauty commented 4 years ago

I'll be more than happy to help any way I can.

diegomvh commented 4 years ago

Singlets work by default on a single entity

Addressing Entities