Comparing version 1.2.0 to 1.3.0
10
index.js
@@ -5,10 +5,10 @@ const http = require('http'); | ||
function httpRequest (options, callback) { | ||
function callarest (options, callback) { | ||
const uri = new URL(options.url); | ||
const headers = options.headers || {}; | ||
if (options.data) { | ||
if (typeof options.data === 'object') { | ||
if (options.data != null) { | ||
if (typeof options.data !== 'string') { | ||
return callback(new ErrorWithObject({ | ||
message: 'You set the data property to an Object. Did you mean to JSON.stringify it?', | ||
message: 'You did not set the data property to an String. Did you mean to JSON.stringify an object it or use the callarest.json shortcut?', | ||
code: 'SENT_OBJECT_AS_DATA' | ||
@@ -60,2 +60,2 @@ })); | ||
module.exports = httpRequest; | ||
module.exports = callarest; |
{ | ||
"name": "callarest", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"keywords": [ | ||
@@ -9,3 +9,4 @@ "rest", | ||
"https", | ||
"request" | ||
"request", | ||
"json" | ||
], | ||
@@ -12,0 +13,0 @@ "description": "An http request library for node", |
@@ -11,8 +11,40 @@ # Callarest | ||
## Example Usage | ||
### Verbose | ||
```javascript | ||
const callarestJson = require('callarest/json') | ||
callarestJson({ | ||
method: 'post', | ||
data: JSON.stringify({ hello: 'world' }), | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
url: 'https://www.example.com' | ||
}, function (error, rest) { | ||
if (error) { | ||
console.log('There was an error:', error); | ||
} | ||
console.log('The request was:', rest.request); | ||
console.log('The response was:', rest.response); | ||
console.log('The body was:', rest.body); | ||
// body will be a string | ||
}) | ||
``` | ||
### JSON | ||
```javascript | ||
const callarest = require('callarest') | ||
callarest({ | ||
callarest.json({ | ||
method: 'post', | ||
data: { hello: 'world' }, | ||
url: 'https://www.example.com' | ||
}, function (error, result) { | ||
console.log({error, result}) | ||
}, function (error, rest) { | ||
if (error) { | ||
console.log('There was an error:', error); | ||
} | ||
console.log('The request was:', rest.request); | ||
console.log('The response was:', rest.response); | ||
console.log('The body was:', rest.body); | ||
// body will be a javascript Object | ||
}) | ||
@@ -19,0 +51,0 @@ ``` |
@@ -27,2 +27,27 @@ const http = require('http'); | ||
function createJsonServer (callback) { | ||
server = http.createServer((request, response) => { | ||
if (request.url === '/echo' && request.method === 'POST') { | ||
parseBody(request, (error, body) => { | ||
if (error) { | ||
throw error; | ||
} | ||
response.writeHead(200, { | ||
'Content-Type': 'application/json' | ||
}); | ||
response.end(JSON.stringify({ | ||
a: 'you said', | ||
b: body | ||
})); | ||
}); | ||
} else { | ||
response.writeHead(200, { | ||
'Content-Type': 'application/json' | ||
}); | ||
response.end(JSON.stringify({success: true})); | ||
} | ||
}).listen(8000); | ||
callback(null, server); | ||
} | ||
function destroyServer () { | ||
@@ -34,3 +59,4 @@ server.close(); | ||
createServer, | ||
createJsonServer, | ||
destroyServer | ||
}; |
@@ -33,3 +33,3 @@ const test = require('tape'); | ||
request(function (error, result) { | ||
t.equal(error.errno, 'ENOTFOUND'); | ||
t.equal(error.code, 'ENOTFOUND'); | ||
t.ok(error.request); | ||
@@ -36,0 +36,0 @@ t.notOk(result); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11112
10
282
54