Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aligajani's avatar

Any Vue library that could help me normalize API service calls?

Basically, I know I can make one myself, but I am surverying if there's any you know. I am imagining a common service for calling APIs which then specific resources feed into for get, post,patch,delete et. al, but normalized so if I have to change something in the core API calling process, I don't change dozens of files.

0 likes
2 replies
wafto's avatar

In my opinion i will go by creating a file called api.js and there exporting functions that call axios for the api calls. something like:

import axios from 'axios'

const API = axios.create({
  baseURL: API_BASE_URL
})

export async function me () {
  return await API.get('/me')
}

export async function usersList () {
  return await API.get(`/users`)
}

export async function usersShow (id) {
  return await API.get(`/users/${id}`)
}

export async function usersCreate (data) {
  return await API.post('/users', data)
}

export async function usersUpdate (id, data) {
  return await API.put(`/users/${id}`, data)
}

export async function usersDelete (id) {
  return await API.delete(`/users/${id}`)
}

Then wherever you want to use it only import it:

...
<script>
import { usersList } as api from '@/api'

export default {
  ...
  methods: {
    logUsers () {
      usersList().then(res => {
        console.log(res)
      })
    }
  }
  ...
}
</script>
...

So if sometime in the future I want to change axios for any other thing or add axios calls extra parameters like header and so on, all the api call I have it in one place.

aligajani's avatar

Firstly, thank you. This makes a lot of sense. Still wondering if there is a library to do all this? What happened to return new Promise resolve() reject() stuff? Am I living in the old world :)

Please or to participate in this conversation.