Skip to content
This repository has been archived by the owner on Aug 9, 2022. It is now read-only.

Service classes should define the in/out mapper methods themselves #733

Open
jurajpiar opened this issue Mar 1, 2021 · 0 comments
Open

Comments

@jurajpiar
Copy link
Member

jurajpiar commented Mar 1, 2021

Mappers should be defined in the service class and be called via the abstract api class. This would allow:

  1. less code duplication in _fetch methods
  2. easier and clearer testing
  3. better debugging
  4. de-coupling of fetch and mapping

Example:

class AbstractApiService {
   _fetch = (args) => {
     const options = this.mapForTransport(args)
     const result = this.fetch(options)
     const { data, ...metadata } = isResultPaginated(result) ? result : { data: result }
     this.meta = metadata
     const mapped = this.mapFromTransport(data)
     
     return data
  }
}

class ExampleService extends AbstractApiService {
    _fetch = () => this.service.find()
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant