F0 is a tiny library to make http request, get response and hold the response object in a instance. It uses fetch API for any requests and written in TypeScript.
You can generate a F0 instance and hold any of Premises, results and result json inside it.
//make a connection
let con = new F0({
hostname: "https://api.example.com",
})
const response = con.get("/category/list").json()
setTimeout(()=>{
console.log(response)
// { some:json, like:this}
console.log(con.jsonBody)
// { some:json, like:this}
},5000)
This package is released under experimental state. If you want to try it, install the package with npm:
npm install f0.js
make a new connection and get a json response
const {F0} = require("f0.js/dist/node-f0")
let con = new F0()
con.setHost("https://api.github.com")
con.setHeader("Accept","application/vnd.github.v3+json")
const repo = con.get("/users/g1eng/repos").json().then((e)=>{
console.log(e, "this json body is assigning into the const repo!")
return e
})
send a post request and get a raw response object
con.setHost("https://httpbin.org")
con.setBody("thisisok")
let res = con.post("/post").res().then((e)=>{
console.log(e)
})
make connection pool with pre-defined config and send request for each
let configList = [];
const config0 = {
hostname: "awesome.api.example.com",
options: {
mode: "cors",
headers: {
cache: "no-cache",
'Content-Type': "application/javascript",
'X-CSRF-TOKEN': "hoge"
}
}
}
for (let i in awesomeResourceList) {
configList.push(config0)
configList[i].resource = awesomeResourceList[i]
}
function sendRequestRecursive(seq=0){
let f = new F0(configList[seq++])
f.get()
setTimeout(()=>{
if (seq < configList.length)
sendRequest(seq)
},1000)
}
sendRequestRecursive()
Nomura Suzume (@g1eng)