Building a generic HTTP service containing a model adapter
To build your generic HTTP service with a model adapter, start by creating a new library named generic-http of type data-access
in the domain shared. In this library, create a file named generic-http.service.ts
and a file named model-adapter.interface.ts
.
Inside the model-adapter
interface file, add this interface:
export interface ModelAdapter<T, S> { fromDto(dto: T): S; toDto(model: S): T; }
We use generic types so we can make our model adapter type-safe. The T
and S
are placeholders for the data transfer object (DTO) and frontend model we will provide to the adapter. After creating the interface, start with the generic HTTP service.
The generic HTTP service will need a property for the API URL and the default HTTP headers, and the service needs to inject the HTTP client.
@Injectable({ providedIn: 'root' }) export abstract class GenericHttpService<T, S>...