ecamp / ecamp/hal-json-vuex

Wrap results from API into a "schema"

Open
#2 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
8
Forks
3
PR merge metrics
No merged PRs in 30d

Description

Goal: To be able to use computed getters and maybe more on the data returned from the API. E.g.:
```js
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](https://vuex-orm.github.io/plugin-axios/guide/configurations.html#datatransformer) in Vuex ORM. Will be complex to use for developers.

_Originally posted by @usu in https://github.com/ecamp/ecamp3/issues/507#issuecomment-723531995_

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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.