Created
August 24, 2021 23:01
-
-
Save Pompeu/95cee151ed9bbc6167e5a74ff5ba150a to your computer and use it in GitHub Desktop.
axios.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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