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. Lets look at a couple of examples! Im going to define a few requirements that my API clients typically have: The API client code we write applies to any HTTP client you might build, but in this case, well use Axios. 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.

Read Full Article at

Tags: , , , , , , , , ,