lightning-request
Advanced tools
Comparing version
30
index.js
@@ -32,6 +32,2 @@ const http = require('http'); | ||
const protocol = parsedUrl.protocol; | ||
if (['http:', 'https:'].indexOf(protocol) === -1) { | ||
throw new Error(`protocol '${protocol}' is not supported.`); | ||
} | ||
const timeout = options.timeout || 15000; | ||
@@ -46,8 +42,9 @@ const headers = Object.assign( | ||
const contentType = headers['Content-Type']; | ||
if (Object.prototype.toString.call(data) === '[object Object]') { | ||
if (contentType.startsWith('application/json')) { | ||
data = JSON.stringify(data); | ||
} else if (contentType.startsWith('application/x-www-form-urlencoded')) { | ||
data = qs.stringify(data); | ||
} | ||
if ( | ||
Object.prototype.toString.call(data) === '[object Object]' && | ||
contentType.startsWith('application/x-www-form-urlencoded') | ||
) { | ||
data = qs.stringify(data); | ||
} else if (Object.prototype.toString.call(data) !== '[object String]') { | ||
data = JSON.stringify(data); | ||
} | ||
@@ -70,3 +67,2 @@ | ||
let req; | ||
const resHandler = res => { | ||
@@ -85,7 +81,8 @@ const statusCode = res.statusCode; | ||
if (responseType === 'json' && statusCode === 200) { | ||
response.body = response.json(); | ||
response.data = response.json(); | ||
} else if (responseType === 'buffer' && statusCode === 200) { | ||
response.data = response.buffer(); | ||
} else { | ||
response.body = response.text(); | ||
response.data = response.text(); | ||
} | ||
resolve(response); | ||
@@ -98,6 +95,9 @@ }); | ||
let req; | ||
if (protocol === 'http:') { | ||
req = http.request(requestOptions, resHandler); | ||
} else if (protocol === 'https:') { | ||
req = https.request(requestOptions, resHandler); | ||
} else { | ||
req = https.request(requestOptions, resHandler); | ||
throw new Error(`Protocol "${protocol}" not supported. Expected "http:" or "https:"`); | ||
} | ||
@@ -104,0 +104,0 @@ |
{ | ||
"name": "lightning-request", | ||
"version": "0.0.5", | ||
"version": "0.1.0", | ||
"description": "Lightweight Node.js HTTP client", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -26,4 +26,2 @@ ⚡ Lightweight Node.js HTTP client. | ||
``` | ||
const request = require('lightning-request'); | ||
(async function() { | ||
@@ -35,3 +33,3 @@ try { | ||
console.log(result.statusCode); // response status code | ||
console.log(result.body); // response body | ||
console.log(result.data); // response data | ||
} catch (error) { | ||
@@ -50,3 +48,3 @@ console.log(error); | ||
// `url` is the server URL that will be used for the request | ||
url: 'http://www.example/test', | ||
url: 'http://www.example.com/', | ||
@@ -57,7 +55,7 @@ // `method` is the request method to be used when making the request | ||
// `headers` are custom headers to be sent | ||
headers: {'X-Requested-With': 'XMLHttpRequest'}, | ||
headers: {'Content-Type': 'application/json'}, | ||
// `data` is the data to be sent as the request body | ||
data: { | ||
firstName: 'Fred' | ||
foo: 'bar' | ||
}, | ||
@@ -67,6 +65,6 @@ | ||
// If the request takes longer than `timeout`, the request will be aborted. | ||
timeout: 1000, // default is `15000` (no timeout) | ||
timeout: 1000, // default is `15000` milliseconds | ||
// `responseType` indicates the type of data that the server will respond with | ||
// options are: 'json', 'text' | ||
// options are: 'json', 'text', 'buffer' | ||
responseType: 'json', // default | ||
@@ -86,2 +84,5 @@ | ||
{ | ||
// `body` is the raw buffer data from the server response | ||
body: <Buffer > | ||
// `statusCode` is the HTTP status code from the server response | ||
@@ -96,4 +97,4 @@ statusCode: 200, | ||
// `body` is the response data that was provided by the server | ||
body: {} | ||
// `data` is the response data that was provided by the server | ||
data: {} | ||
} | ||
@@ -100,0 +101,0 @@ ``` |
class Response { | ||
constructor(options = {}) { | ||
this.data = Buffer.alloc(0); // raw data | ||
this.body = Buffer.alloc(0); // raw data | ||
this.headers = options.headers; | ||
this.statusCode = options.statusCode; | ||
this.statusMessage = options.statusMessage; | ||
this.body = this.data; | ||
this.data = this.body; | ||
} | ||
addChunk(chunk) { | ||
this.data = Buffer.concat([this.data, chunk]); | ||
this.body = Buffer.concat([this.body, chunk]); | ||
} | ||
@@ -23,7 +23,7 @@ | ||
text() { | ||
return this.data.toString('utf8'); | ||
return this.body.toString('utf8'); | ||
} | ||
buffer() { | ||
return this.data; | ||
return this.body; | ||
} | ||
@@ -30,0 +30,0 @@ } |
21283
0.78%132
1.54%109
0.93%