Skip to content

Replace RestClient with higher-level API abstraction

Axel Bocciarelli requested to merge rest-client into master

RestClient has a few downsides:

  • It's a low-level API on top of axios, which is already a low-level API.
  • It's difficult to mock because the methods are so low-level. This is the main reason for the refactor: we need to mock the data layer in our Jest feature tests, and mocking axios or RestClient would be too tedious.
  • Its initialization and set-up are convoluted because of the object-oriented approach.

I decided to write a very simple functional API to replace RestClient. An axios instance is initialized and configured (with a baseUrl, interceptors and so on) on initial load. I then use this instance in a few async functions designed to be easily mocked and to better fit the needs of the app. We get two main benefits:

  • A bunch of the complexity that was previously in the thunks is now located in those API functions (like picking the properties we need from the server responses).
  • We'll be able to mock the API functions in the feature tests by simply calling jest.mock('path/to/api.ts') and by mocking return values as needed, for instance: fetchDirectory.mockReturnValueOnce({ currentPath: '/home/...', ... }).

Note that I've kept the RestClient file for now, in case I've missed something in my refactoring.

Edited by Axel Bocciarelli

Merge request reports