deblockt / hal-rest-client

Typescript HAL Rest client
Apache License 2.0
25 stars 11 forks source link

Models not always loading and / or populating #34

Closed davestewart closed 5 years ago

davestewart commented 5 years ago

I gotta say, I'm loving HalResource models and @HalProperty decorators! Amazing stuff, well done.

However, I've found model population and loading to be a big hit and miss, I assume, because of 1) my coding but possibly 2) the data.

I had a hierarchy loading well, but it now seems to have stopped loading at appData (I am aliasing this in the model decorator):

export default class Company extends HalResource {
  @HalProperty()
  public name: string

  @HalProperty('apps', AppsData)
  public appsData: AppsData
}
sites.sites[0].companies[0].links.appsData.isLoaded // false

image

The JSON / class mapping is:

- Sites
  - _link: sites[]
    - Site
      - _embed: companies[]
        - Company
          - _link: appsData (originally apps)
            - AppsData
              - _embed: apps[]
                - App

The site data is:

{
  "name": "DEV",
  "_embedded": {
    "companies": [
      {
        "name": "Space Invaders Parent - DEV",
        "_links": {
          "self": {
            "href": "https://spaceinvaders.bookingbug.com/api/v1/site/37003"
          },
          "install_app": {
            "href": "https://spaceinvaders.bookingbug.com/api/v1/37003/apps/install_from_portal{?name}",
            "templated": true
          },
          "apps": {
            "href": "https://spaceinvaders.bookingbug.com/api/v1/site/apps"
          }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "https://spaceinvaders.bookingbug.com/api/v1/site"
    }
  }
}

The link site > _embed:companies[] > _link:apps is supposed to load:

{
  "total_entries": 1,
  "_embedded": {
    "apps": [
      {
        "id": 2,
        "name": "vidyo",
        ...
      }
    ]
  },
  "_links": {
    "self": {
      "href": "https://spaceinvaders.bookingbug.com/api/v1/admin//apps"
    }
  }
}

I assume I need a model class for every link and every embed?

It seems that if I match the data hierarchy wrong, a model will be instantiated but with everything undefined.

I'm pretty sure at some point I had the Apps loading.

My hunch is that the lib is getting confused as there are two consecutive apps keys.

Is there any way to debug a model as it populates?

davestewart commented 5 years ago

I'm an idiot!

I thought models were populating themselves, but actually I was loading the first level of sites in the initial client call, but (because I had forgotten this) so I had assumed that nested resources were automatically loaded.

Just teething issues :)