LF Node.js wrapper
What for wraper? All you need is http get request:
https://nodejs.org/api/http.html#http_http_get_options_callback
http.get('http://www.google.com/index.html',{
headers: {
Authorization: "Bearer " + key
}
}, (res) => {
console.log(`Got response: ${res.statusCode}`);
// update your models with received data res.data
}).on('error', (e) => {
console.log(`Got error: ${e.message}`);
});
It’s not so simple as it seems without library (you need to consume the res stream and listen to errors … and of course clean up the mess) but with request or fetch is pretty simple as in:
request (callback pattern):
request.get('url',
{
headers: {
Authorization:
`Bearer ${new Buffer('my_long_key').toString('base64') }`
}
}, (err, res) => { /* consume the request */ });
fetch (promise):
function check(res) {
if (res.status > 300) {
throw new Error(`My fancy error for ${status}`)
}
return res;
}
function json(res) {
return res.json()
}
fetch('url',
{
headers: {
Authorization:
`Bearer ${new Buffer('my_long_key').toString('base64') }`
}
})
.then(check)
.then(json)
.then((body) => doSomething(body))
.catch((err) => handleError(err))
...;
“You’re dumb. You’ll die, and you’ll leave a dumb corpse.”