Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap results from API into a "schema" #2

Open
carlobeltrame opened this issue Nov 13, 2020 · 1 comment
Open

Wrap results from API into a "schema" #2

carlobeltrame opened this issue Nov 13, 2020 · 1 comment

Comments

@carlobeltrame
Copy link
Member

carlobeltrame commented Nov 13, 2020

Goal: To be able to use computed getters and maybe more on the data returned from the API. E.g.:

const book = this.api.get().books().items[0]
book.releaseDate // automatically cast to whatever date format you want
book.author().fullName // automatically compiled from `firstName` and `lastName`

The main problem is: How should hal-json-vuex know what getters to expose? How should it know what type of entity the current piece of data represents?

Option 1: We define the entity when retrieving from the API / Vuex, i.e. a decorator pattern. Possible implementations:

this.api.get().lastReadBook(Book) // ok
this.api.get().lastReadBook().as(Book) // hmm...
Book(this.api.get().lastReadBook()) // akward

Option 2: Detect automatically according to the URI. I.e. the developer can define that URIs like '/books/*' always yield a Book. Goes against the idea of HATEOAS.

Option 3: The API sends a special property that defines the entity type. Or, we parse an OpenAPI specification. Not an option in case the developer doesn't control the API implementation.

Option 4: Expose all getters globally on all entities, and if one tries to call author.releaseDate, this fails. Not nice in case of naming conflicts.

Option 5: Allow the developer to define custom rules to determine the entity class that a given piece of data represents, based on self link, relation name that was used to get the entity, and the properties. It could also be done before the data hits the Vuex store, similar to dataTransformers in Vuex ORM. Will be complex to use for developers.

Originally posted by @usu in ecamp/ecamp3#507 (comment)

Personally I lean towards option 1 or 5, and both of those could be implemented mostly externally to this library.

@carlobeltrame carlobeltrame transferred this issue from ecamp/ecamp3 Nov 13, 2020
@pmattmann
Copy link
Member

Global Hook (ähnlich wie DataTransformer) on Hal-Json-Vuex Config.
Hook läuft wenn Daten aus VuexStore geladen werden.
Hook-Name Vorschlag: ModelMapping, StoreValueMapper, ...

Hook bekommt Daten aus VuexStore, muss ein Objekt "StoreValueProxy" oder eine Ableitung davon zurückgehen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants