Building Flexible Axios Clients

Recently I set out to improve how I work with APIs in my Vue applications by building a flexible Axios client that I can use in my Vuex actions and one-off components.

I prefer to build specific API JS modules that I can import into my components and Vuex modules instead of a baked-in Axios call from a component. Building API resource modules allows me to abstract working with HTTP resources, and provide convenience methods for common patterns. Let’s look at a couple of examples!

Getting Started

I’m going to define a few requirements that my API clients typically have:

Hook into Error reporting (i.e., Sentry)

Ability to pass HTTP Headers like the Authorization header

Ability to tap into my Vuex store if needed (i.e., to get a user’s JWT)

Resource-specific methods that simplify making common API calls (i.e., users.find(userId))

The API client code we write applies to any HTTP client you might build, but in this case, we’ll use Axios.

The Client

The client.js file is going to be the foundation module that our other HTTP clients use to create a new Axios instance. We will also set up what Axios calls “interceptors” for requests and responses.

The request interceptor takes a request configuration and an error callback. You could customize the configuration at this point, but for starters, we’ll return the same configuration. The error callback will capture the exception through Sentry’s Raven:

Here’s a full example of how you might build out your own base Axios client which taps into Sentry, checks a Vuex store for a user access token, and provides an ApiClient class that you can use to build your clients.

The above example is simple, but you could do further processing by chaining a promise call after getting the response back. For example, you could only sending along the relevant properties needed from the response, for example. Or perhaps you don’t have access to the backend, and you could use the response to build a custom object or add additional properties, for example, a fullName property if you only get back firstName and lastName. Vue also has computed properties, but I hope you get the idea that you can format the response data before sending it along to consumer code.

Vuex Usage

One reason I suggest that you build HTTP client modules is that we can pull them into components, and we can also use them in Vuex stores as well. The same benefits apply, including sending along the Authorization tokens and avoiding axios calls directly in Vuex modules.

Here’s an example of getting a user and setting their profile data in a Vuex store:

Improvements

One noticeable improvement in my mind is allowing more flexible client configuration through callbacks when creating a new client. The way that I’ve shown using Vuex to send along a bearer token is baked into the module in a way that isn’t very flexible.

I hope you can at least catch the ideas that I am conveying. It’s helpful to build a base client JS module for low-level details that you probably want on any HTTP client. For example, we can focus on creating useful API clients to manage users and other resources, instead of having to worry about hooking in Sentry to our request and responses.

Do you have any more Axios or JavaScript API client tips? Sound off on Twitter @laravelnews.