Skip to main content

0005 - Refactor Api Service

ID:ADR-0005
Status:ACCEPTED
Published:2022-07-08

Context and Problem Statement

The ApiService currently handles all API requests. This has resulted in the class evolving into a Bloater, and as of right now consists of 2021 lines of code, and has 268 methods. Additionally, since it knows everything related to the servers it also needs to import every request and response which necessitates that the ApiService and request/responses are put in the same npm package.

Considered Options

  • Extract Class - We should break up the class using the Extract Class refactor, where each domain context should have its own API service. The ApiService should be converted into a generic service that doesn't care what the request or response is and should only be used within other API services.
  • Do nothing - Leave it as is.

Decision Outcome

Chosen option: Extract Class

The naming of these new classes should be {Domain}ApiService, the folder domain for example should be called FolderApiService.

Example of the refactor:

  • folder-api.service.ts: Create a new service, move the methods from ApiService to the new service. During this refactor we also moved the server knowledge from the FolderService, as it should only be responsible for maintaining it's state.
  • api.service.ts: Remove the old methods from the ApiService.