bc-http
v0.2.1
Published
starting point for npm moudles
Downloads
3
Readme
http
install
npm install --save bc-http babel-polyfill
Note:
babel-polyfill
is required on all github.com/DrawboardLtd/bc-...` modules
Example Usage
import { get, post, put, del } from "bc-http"
get(`api/v1/users/9`)
.then(body => console.log(body))
.catch(error => console.error(error))
// post with some data
post(`api/v1/users`, { email:"[email protected]" })
.then(body => console.log(body))
.catch(error => console.error(error))
// transform the raw response and send to the .then callback
get(`api/v1/users/9`, {}, {}, res => res.body.username)
.then(username => console.log("username:", username))
.catch(error => console.error(error))
// add a header
get(`api/v1/users/9`, {}, { "api-key":"lololol" })
.then(body => console.log(body))
.catch(error => console.error(error))
// async/await ready
async function getUser (id) {
try {
const user = await get(`api/v1/users/${ id }`)
console.log(user)
return user
} catch (err) {
console.error(err)
}
}
// add and remove global headers
import {
addGlobalHeader,
removeGlobalHeader,
} from "bc-http"
async function authenticate (username, passwords) {
try {
const { authToken } = await post(`api/v1/authenticate`, { username, password })
addGlobalHeader("authToken", authToken)
} catch (err) {
// what to do if something goes wrong
}
}
// requires an authToken header
async function createPost (uuidOfUser, postData) {
try {
post(`api/v1/users/${ uuidOfUser }/posts`, postData)
} catch (err) {
// what to do if something goes wrong
}
}