Refactoring Wooster's API Layer: A Simple Approach with Axios
Why Move to Axios?
After building Wooster's initial MVP with the fetch API, it was time for an upgrade. While fetch worked well, Axios offers some helpful features out of the box that would make my code cleaner and more maintainable.
Following the Docs
Like any good developer, I first spent three hours looking for "the perfect way" to implement Axios, only to end up exactly where I should have started: the official documentation. Sometimes the simplest approach is the best one, even if it does make you feel a bit daft for not trying it first.
The Implementation
1. Setting Up the Instance
First, I created a configured Axios instance following the official pattern (after trying three "clever" approaches that all ended in tears):
This gave me everything fetch did, plus some features I didn't know I needed until I had them:
- A base URL for all requests
- Automatic timeout handling
- Default headers
Rather like getting a dishwasher - you don't realize how much time you've been wasting until you stop doing it the hard way.
2. Authentication
I added a simple interceptor for authentication:
3. API Endpoints
Then I created simple functions for each endpoint:
Refactoring the Application
The migration wasn't just about the API service - I needed to update several parts of my application.
Benefits I Got
- Automatic Transforms: Axios automatically transforms JSON data
- Request/Response Interceptors: Makes it easy to add auth headers
- Better Error Handling: Axios provides consistent error objects
- Request Timeouts: Built-in timeout handling
- Easy Configuration: One place to set common options
Using the API
Here's how I use it in components, now with 100% less promise chaining and 50% more error catching:
Key Lessons
- Keep It Simple: Following the official docs often leads to cleaner code
- Don't Overengineer: I resisted the urge to add complex abstraction layers
- Consistent Patterns: Using Axios's built-in features rather than creating my own
What's Next?
With this foundation in place, I'm ready to add TanStack Query for state management. But that's a story for another post. First, I had to fix all those tests I broke... which led me down quite the rabbit hole with Mock Service Worker.