lightning-request
Advanced tools
Comparing version
27
index.js
@@ -15,4 +15,5 @@ const http = require('http'); | ||
* @param {String} options.responseType `responseType` indicates the type of data that the server will respond with, default: json | ||
* @param {Object} options.agentConfig `agentConfig` http.Agent config. | ||
* @param {Object} options.agentConfig `agentConfig` http.Agent or https.Agent config. | ||
* @param {Object} options.httpAgent `httpAgent` define a custom agent to be used when performing http requests, respectively, in node.js. This allows options to be added like `keepAlive` that are not enabled by default. | ||
* @param {Object} options.httpsAgent `httpsAgent` define a custom agent to be used when performing https requests, respectively, in node.js. This allows options to be added like `keepAlive` that are not enabled by default. | ||
*/ | ||
@@ -28,5 +29,6 @@ async function request(options) { | ||
if (['GET', 'HEAD'].indexOf(method) > -1) { | ||
Object.keys(data).forEach(key => { | ||
parsedUrl.searchParams.append(key, data[key]); | ||
}); | ||
data && | ||
Object.keys(data).forEach(key => { | ||
parsedUrl.searchParams.append(key, data[key]); | ||
}); | ||
} | ||
@@ -49,3 +51,3 @@ | ||
'Content-Type': contentType, | ||
'User-Agent': 'Lightweight Node.js HTTP client/0.0.1', | ||
'User-Agent': 'Lightweight Node.js HTTP client', | ||
}, | ||
@@ -63,3 +65,16 @@ options.headers || {} | ||
}; | ||
const agent = options.httpAgent ? options.httpAgent : new http.Agent(agentConfig); | ||
let agent; | ||
if (protocol === 'http:') { | ||
if (options.httpAgent) { | ||
agent = options.httpAgent; | ||
} else { | ||
agent = new http.Agent(agentConfig); | ||
} | ||
} else if (protocol === 'https:') { | ||
if (options.httpsAgent) { | ||
agent = options.httpsAgent; | ||
} else { | ||
agent = new https.Agent(agentConfig); | ||
} | ||
} | ||
@@ -66,0 +81,0 @@ return new Promise((resolve, reject) => { |
{ | ||
"name": "lightning-request", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Lightweight Node.js HTTP client", | ||
@@ -11,11 +11,11 @@ "main": "index.js", | ||
"type": "git", | ||
"url": "git@github.com:Node-LabX/lightning-request.git" | ||
"url": "git@github.com:node-labx/lightning-request.git" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"author": "kk", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/Node-LabX/lightning-request/issues" | ||
"url": "https://github.com/node-labx/lightning-request/issues" | ||
}, | ||
"homepage": "https://github.com/Node-LabX/lightning-request" | ||
"homepage": "https://github.com/node-labx/lightning-request" | ||
} |
@@ -1,3 +0,49 @@ | ||
# light-request | ||
⚡ Lightweight Node.js HTTP client. | ||
Lightweight Node.js HTTP client | ||
 | ||
## Install | ||
``` | ||
npm i lightning-request | ||
``` | ||
## Why lightning-request? | ||
lightning-request is the most lightweight HTTP client for Node, it provides a number of extremely useful features. | ||
## Use lightning-request | ||
First, require the library. | ||
``` | ||
const request = require('lightning-request'); | ||
``` | ||
Then let's make a request in an async function. | ||
``` | ||
const request = require('lightning-request'); | ||
(async function() { | ||
try { | ||
const result = await request({ | ||
url: 'https://github.com/node-labx/lightning-request', | ||
}); | ||
console.log(result.statusCode); // response status code | ||
console.log(result.body); // response body | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
})(); | ||
``` | ||
## Contributing | ||
- Fork this repo | ||
- Clone your repo | ||
- Install dependencies | ||
- Checkout a feature branch | ||
- Feel free to add your features | ||
- Make sure your features are fully tested | ||
- Open a pull request, and enjoy <3 |
@@ -15,3 +15,7 @@ class Response { | ||
json() { | ||
return JSON.parse(this.text()); | ||
try { | ||
return JSON.parse(this.text()); | ||
} catch (error) { | ||
return this.text(); | ||
} | ||
} | ||
@@ -18,0 +22,0 @@ |
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
20188
202.62%10
42.86%151
14.39%0
-100%50
1150%