Created
February 24, 2023 19:50
-
-
Save renatocfrancisco/22b02576928f16fe5be27cf4f52fbed9 to your computer and use it in GitHub Desktop.
explicando req. axios com funções array/object
This file contains 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
// executando com node, npm -> package.json -> axios 1.3.4 | |
import axios from "axios"; | |
import fs from "fs"; | |
// config e req. tirado do postman | |
var config = { | |
method: "get", | |
maxBodyLength: Infinity, | |
url: "https://brasilapi.com.br/api/banks/v1", | |
headers: {}, | |
}; | |
// requisição axios sem await | |
axios(config) | |
.then(function (response) { | |
response.data = response.data | |
// ordem crescente com campo "code" | |
.sort((a, b) => a.code - b.code) | |
.filter( | |
(banco) => | |
// deletar campo "ispb" | |
delete banco.ispb && | |
// filtrar "code" nulos | |
banco.code !== null && | |
// remover "S.A." de "fullName" e "name" com regex | |
banco.fullName.replace(/S\.A\./gm, "") && | |
banco.name.replace(/S\.A\./gm, "") | |
); | |
// response.data como string json | |
const data = JSON.stringify(response.data, null, 2); | |
console.log(data); | |
// output json com fs | |
// fs.writeFile("bancos.json", data, (err) => { | |
// if (err) throw err; | |
// console.log("saved"); | |
// }); | |
}) | |
.catch(function (error) { | |
// caso der erro na req. | |
console.log(error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment