0

I need to be able to run a node script to delete an object from an external API. So I should be able to run this command:

node server.js Customer55555

And it should delete the object.

I have called to the API by using Axios.

const axios = require("axios");

const API = "http://dummy.restapiexample.com/api/v1/employees";
function getAllEmployees() {
  axios
    .get("http://dummy.restapiexample.com/api/v1/employees")
    .then(response => {
      //   console.log(response.data);
      console.log(response.status);
      function filterEmployee() {
        const employeeData = response.data;
        employeeData.filter(employee => {
          console.log(employee);
        });
        // console.log(employeeData);
      }
      filterEmployee();
    })
    .catch(error => {
      console.log(error);
    });
}

function deleteEmployee() {
  axios({
    method: "DELETE",
    url: "http://dummy.restapiexample.com/api/v1/delete/36720",
    headers: { "Content-Type": "application/json" }
  })
    .then(
      // Observe the data keyword this time. Very important
      // payload is the request body
      // Do something
      console.log("user deleted")
    )
    .catch(function(error) {
      // handle error
      console.log(error);
    });
}

// getAllEmployees();
deleteEmployee();

I am able to get an individual object, but I need to figure out how to delete it by running the command above.

  • Are you just asking how to get `Customer55555` from the command line arguments? Otherwise, I can't really tell what exactly you're asking for help with. We don't know the specific APIs you're trying to call so we can't help you with proper syntax for them. – jfriend00 Jul 23 '19 at 03:13
  • Customer5555 was just an example. The two apis that I need to call are http://dummy.restapiexample.com/api/v1/employees and for the delete http://dummy.restapiexample.com/api/v1/delete/{id} – springer937 Jul 23 '19 at 03:24
  • And, what exactly is the problem you want us to help you with? You show a bunch of code, but don't actually ask a question anywhere. – jfriend00 Jul 23 '19 at 03:37

1 Answers1

0

You can do something like this:

const axios = require("axios")

const API = "http://dummy.restapiexample.com/api/v1/employees"

async function getAllEmployees(filter = null) {
  try {
    const response = await axios.get("http://dummy.restapiexample.com/api/v1/employees")
    console.log(response.status)
    let employeeData = response.data

    if (filter) {
      // return only employees whose name contains filter.name
      employeeData = employeeData.filter(({ employee_name }) => {
        return employee_name.toLowerCase().indexOf(filter.name.toLowerCase()) >= 0
      })
    }

    return employeeData
   } catch(error) {
     console.error(error)
     return []
   }
}

async function deleteEmployee({ id }) {
  if (!id) {
    throw new Error('You should pass a parameter')
  }
  try {
    const response = await axios({
        method: "DELETE",
        url: `http://dummy.restapiexample.com/api/v1/delete/${id}`,
        headers: { "Content-Type": "application/json" }
    })

    console.log("user deleted " + id)
  } catch(error) {
    // handle error
    console.error(error)
  }
}

async function main(params) {
   const employees = await getAllEmployees({ name: params[0] || '' })
   // Returns a promise to wait all delete promises
   return Promise.all(employess.map(employee => deleteEmployee(employee)))
}

// process.argv contains console parameters. (https://stackoverflow.com/questions/4351521/how-do-i-pass-command-line-arguments-to-a-node-js-program)
main(process.argv.slice(2)).then(() => {
  // returns 0 (Success) (https://stackoverflow.com/questions/5266152/how-to-exit-in-node-js)
  process.exit(0)
}).catch(() => {
  // returns 1 (error)
  process.exit(1)
})

You should adapt this sample to get proper filtering and error reporting.