API Calls


Introduction

A common use case for code actions is to make API calls to external services. Mira includes Axios for making XMLHttpRequests from the browser. Axios Mock Adapter is also included to mock those requests. Learn more.

Axios

Performing a GET request:
axios.get('/api/user?id=12345')
.then(function (response) {
  // handle success
  console.log(response);
})
.catch(function (error) {
  // handle error
  console.log(error);
});
Performing a POST request:
axios.post('/api/user', {
  firstName: 'Fred',
  lastName: 'Flintstone'
})
.then(function (response) {
  // handle success
  console.log(response);
})
.catch(function (error) {
  // handle error
  console.log(error);
});

Axios Mock Adapter

Axios adapter that allows to easily mock requests. Learn more.
Mocking a GET request:
mock.onGet("/api/user").reply((config) => {
  return [
    200,
    {
      users: [{ id: 12345, firstName: "Fred", lastName: "Flintstone" }],
    },
  ];
});
Mocking a POST request:
mock.onPost("/api/user").reply((config) => {
  const { firstName, lastName } = JSON.parse(config.data);

  if (firstName && lastName) {
    return [200, {
      id: "12345",
      firstName: "Fred",
      lastName: "Flintstone",
    };];
  }

  return [400, { message: "Looks like you didn't provide the required data." }];
});