Skip to content

Instantly share code, notes, and snippets.

@Pompeu
Created August 24, 2021 23:01
Show Gist options
  • Save Pompeu/95cee151ed9bbc6167e5a74ff5ba150a to your computer and use it in GitHub Desktop.
Save Pompeu/95cee151ed9bbc6167e5a74ff5ba150a to your computer and use it in GitHub Desktop.
axios.js
// npm init
// npm install axios --save
const axios = require("axios");
const id = 1;
const getUrl = "https://jsonplaceholder.typicode.com/posts/";
const log = (res) => console.log(res.data);
const getAll = () => axios.get(getUrl).then(log).catch(console.error);
const getById = (id) =>
axios.get(`${getUrl}${id}`).then(log).catch(console.error);
const getComentsById = (id) =>
axios.get(`${getUrl}${id}/comments`).then(log).catch(console.error);
const createBody = {
title: "foo",
body: "bar",
userId: 888,
};
const createPost = (url, body) => axios.post(url, body).then(log).catch(log);
const updatePost = (url, id, newBody) =>
axios.put(`${url}${id}`, newBody).then(log).catch(log);
const deletePost = (url, id) =>
axios.delete(`${url}${id}`).then(log).catch(log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment