@lighthouseapps/request
v1.1.3
Published
> Javascript http request library
Downloads
12
Keywords
Readme
request
Javascript http request library
Usage
import oauth2 from '@lighthouseapps/react-native-oauth2';
import request from '@lighthouseapps/request';
import io from 'socket.io-client';
const router = {
"createCode": {
"uri": "/auth/authorize/:id",
"method": "POST",
"headers": {
"Content-Type": "application/x-www-form-urlencoded"
}
},
"createCodeToken": {
"uri": "/auth/token",
"method": "POST",
"headers": {
"Content-Type": "application/x-www-form-urlencoded"
}
},
"getUsers": {
"uri": "/users",
"method": "GET",
"listenHeaders": [
"Authorization"
],
"listenSocket": "users"
},
"getUser": {
"uri": "/user",
"method": "GET",
"listenHeaders": [
"Authorization"
],
"listenSocket": "user/:id"
}
};
const HOST = 'http://localhost:8080';
const io = require('socket.io-client');
const socket = io(HOST);
async function test() {
await request.initialize(router, {
baseURL: HOST,
timeout: 10000,
onHeaders: {
Authorization: async () => {
const token = await oauth2.getToken();
return 'Bearer ' + token.access_token;
}
},
socket
});
request.on('getUser', (err, res) => {
if(err) console.log(err);
console.log(res);
});
request.getUser()
.then(res => console.log(res))
.catch(err => console.log(err));
}
test();