Skip to content
This repository has been archived by the owner on Jul 24, 2021. It is now read-only.
/ f0 Public archive

a tiny and simple http client for browsers

License

Notifications You must be signed in to change notification settings

g1eng/f0

Repository files navigation

Tiny and Simple HTTP client using fetch

Coverage Status


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)

Install

This package is released under experimental state. If you want to try it, install the package with npm:

npm install f0.js

Basic usage

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()

Author

Nomura Suzume (@g1eng)

About

a tiny and simple http client for browsers

Resources

License

Stars

Watchers

Forks

Packages

No packages published