README
Description
Simple and opinionated module to make http/https-requests with node
.
Wraps got
and tunnel
with convenience methods for easy of use and fluent API
for best development experience.
It uses your system-proxy
if it is configured by default.
Also, it assumes that all your data that you send and receive will have json
format.
It also provides an adapter for fetch()
-requests to be used as client.fetch()
.
Install
npm install tunneled-got
Basic Usage
// import
const client = require('tunneled-got');
//use
try {
const responseBody = await client.get('https://my-server.com/api/users');
} catch (error) {
console.log(error);
}
Usage with configuration
const options = {
timeout: 60000,
headers: {
'x-foo': 'bar'
}
}
const url = 'https://my-server.com/api/users';
try {
const responseBody = await client.options(options).get(url);
} catch (error) {
console.log(error);
}
Example POST
const data = {
foo: 'bar',
nested: {
'foo': 'baz'
}
}
try {
const responseBody = await client.post(url, data);
} catch (error) {
console.log(error);
}
Example with specific types POST
defaults are: contentType='application/json'
, responseType='json'
try {
const responseBody = await client.post(url, data, contentType='application/xml', responseType='application/xml');
} catch (error) {
console.log(error);
}
example fetch()
Following two requests yield same result:
Using node-fetch
:
const fetch = require('node-fetch');
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
body: "{\"title\":\"Kenneth Simon\",\"body\":\"Duis aute esse eiusmod aute do fugiat id.\",\"userId\":\"d3621c6b-2bfc-5dea-b317-a39060e48531\"}",
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})
.then((response) => response.json())
.then((json) => console.log(json));
Using tunneled-got
:
const client = require('tunneled-got');
const json = await client.fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
body: "{\"title\":\"Kenneth Simon\",\"body\":\"Duis aute esse eiusmod aute do fugiat id.\",\"userId\":\"d3621c6b-2bfc-5dea-b317-a39060e48531\"}",
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
});
console.log(json);
Backlog
- Add tests for SOAP-POST service1, service2
- Add jsdoc to exported functions
- add automatic token refresh
- describe default options
- add .reset(), .options(), .headers(), etc. descriptions
- add info to new-node-module 'how to pubslish new version' and what's the difference of pushing to git -> push to git != npm publish