Comparing version 1.1.0 to 1.2.0
48
index.js
@@ -1,13 +0,20 @@ | ||
const http = require('http') | ||
const https = require('https') | ||
const http = require('http'); | ||
const https = require('https'); | ||
const ErrorWithObject = require('error-with-object'); | ||
function httpRequest (options, callback) { | ||
const uri = new URL(options.url) | ||
const uri = new URL(options.url); | ||
const headers = options.headers || {} | ||
const headers = options.headers || {}; | ||
if (options.data) { | ||
headers['Content-Length'] = Buffer.byteLength(options.data) | ||
if (typeof options.data === 'object') { | ||
return callback(new ErrorWithObject({ | ||
message: 'You set the data property to an Object. Did you mean to JSON.stringify it?', | ||
code: 'SENT_OBJECT_AS_DATA' | ||
})); | ||
} | ||
headers['Content-Length'] = Buffer.byteLength(options.data); | ||
} | ||
const httpOrHttps = uri.protocol === 'https:' ? https : http | ||
const httpOrHttps = uri.protocol === 'https:' ? https : http; | ||
const opts = { | ||
@@ -21,10 +28,10 @@ headers, | ||
...options | ||
} | ||
}; | ||
const chunks = [] | ||
const chunks = []; | ||
const request = httpOrHttps.request(opts, (response) => { | ||
response.setEncoding(options.encoding || 'utf8') | ||
response.setEncoding(options.encoding || 'utf8'); | ||
response.on('data', (chunk) => { | ||
chunks.push(chunk) | ||
}) | ||
chunks.push(chunk); | ||
}); | ||
response.on('end', () => { | ||
@@ -35,19 +42,20 @@ callback(null, { | ||
body: chunks.join() | ||
}) | ||
}) | ||
}) | ||
}); | ||
}); | ||
}); | ||
request.on('error', (error) => { | ||
callback({ | ||
callback(new ErrorWithObject({ | ||
code: 'REQUEST_ERROR', | ||
...error, | ||
request | ||
}) | ||
}) | ||
})); | ||
}); | ||
if (options.data) { | ||
request.write(options.data) | ||
request.write(options.data); | ||
} | ||
request.end() | ||
request.end(); | ||
} | ||
module.exports = httpRequest | ||
module.exports = httpRequest; |
{ | ||
"name": "callarest", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"keywords": [ | ||
@@ -29,3 +29,6 @@ "rest", | ||
"righto": "^6.0.1" | ||
}, | ||
"dependencies": { | ||
"error-with-object": "^1.1.0" | ||
} | ||
} |
@@ -1,29 +0,29 @@ | ||
const { ErrorObject } = require('./error') | ||
const ErrorWithObject = require('error-with-object'); | ||
function parseBody (request, callback) { | ||
let body = [] | ||
let body = []; | ||
request | ||
.on('data', function (chunk) { | ||
body.push(chunk) | ||
body.push(chunk); | ||
}) | ||
.on('end', function () { | ||
body = Buffer.concat(body).toString() | ||
body = Buffer.concat(body).toString(); | ||
if (body) { | ||
try { | ||
return callback(null, body) | ||
return callback(null, body); | ||
} catch (error) { | ||
return callback(new ErrorObject({ code: 400, error, body })) | ||
return callback(new ErrorWithObject({ code: 400, error, body })); | ||
} | ||
} | ||
return callback(null, null) | ||
return callback(null, null); | ||
}) | ||
.on('error', function (error) { | ||
callback(error) | ||
}) | ||
callback(error); | ||
}); | ||
} | ||
module.exports = parseBody | ||
module.exports = parseBody; |
@@ -1,4 +0,4 @@ | ||
const http = require('http') | ||
const parseBody = require('./parseBody') | ||
let server | ||
const http = require('http'); | ||
const parseBody = require('./parseBody'); | ||
let server; | ||
@@ -9,19 +9,22 @@ function createServer (callback) { | ||
parseBody(request, (error, body) => { | ||
if (error) { | ||
throw error; | ||
} | ||
response.writeHead(200, { | ||
'Content-Type': 'text/html' | ||
}) | ||
response.end('You said: ' + body) | ||
}) | ||
}); | ||
response.end('You said: ' + body); | ||
}); | ||
} else { | ||
response.writeHead(200, { | ||
'Content-Type': 'text/html' | ||
}) | ||
response.end('Success') | ||
}); | ||
response.end('Success'); | ||
} | ||
}).listen(8000) | ||
callback(null, server) | ||
}).listen(8000); | ||
callback(null, server); | ||
} | ||
function destroyServer () { | ||
server.close() | ||
server.close(); | ||
} | ||
@@ -32,2 +35,2 @@ | ||
destroyServer | ||
} | ||
}; |
@@ -1,45 +0,45 @@ | ||
const test = require('tape') | ||
const righto = require('righto') | ||
const test = require('tape'); | ||
const righto = require('righto'); | ||
const {createServer, destroyServer} = require('./helpers/server') | ||
const callarest = require('../') | ||
const { createServer, destroyServer } = require('./helpers/server'); | ||
const callarest = require('../'); | ||
test('get -> success', t => { | ||
t.plan(3) | ||
t.plan(3); | ||
const server = righto(createServer) | ||
const server = righto(createServer); | ||
const request = righto(callarest, { | ||
url: 'http://localhost:8000' | ||
}, righto.after(server)) | ||
}, righto.after(server)); | ||
request(function (error, result) { | ||
t.notOk(error) | ||
t.ok(result) | ||
t.equal(result.body, 'Success') | ||
t.notOk(error); | ||
t.ok(result); | ||
t.equal(result.body, 'Success'); | ||
destroyServer() | ||
}) | ||
}) | ||
destroyServer(); | ||
}); | ||
}); | ||
test('get -> failure', t => { | ||
t.plan(3) | ||
t.plan(3); | ||
const server = righto(createServer) | ||
const server = righto(createServer); | ||
const request = righto(callarest, { | ||
url: 'http://localhostnope:8000' | ||
}, righto.after(server)) | ||
}, righto.after(server)); | ||
request(function (error, result) { | ||
t.equal(error.errno, 'ENOTFOUND') | ||
t.ok(error.request) | ||
t.notOk(result) | ||
t.equal(error.errno, 'ENOTFOUND'); | ||
t.ok(error.request); | ||
t.notOk(result); | ||
destroyServer() | ||
}) | ||
}) | ||
destroyServer(); | ||
}); | ||
}); | ||
test('post -> success', t => { | ||
t.plan(3) | ||
t.plan(3); | ||
const server = righto(createServer) | ||
const server = righto(createServer); | ||
const request = righto(callarest, { | ||
@@ -49,12 +49,30 @@ method: 'post', | ||
data: 'hello' | ||
}, righto.after(server)) | ||
}, righto.after(server)); | ||
request(function (error, result) { | ||
t.notOk(error) | ||
t.ok(result) | ||
t.equal(result.body, 'You said: hello') | ||
t.notOk(error); | ||
t.ok(result); | ||
t.equal(result.body, 'You said: hello'); | ||
destroyServer() | ||
}) | ||
}) | ||
destroyServer(); | ||
}); | ||
}); | ||
test('post -> send object as data', t => { | ||
t.plan(1); | ||
const server = righto(createServer); | ||
const request = righto(callarest, { | ||
method: 'post', | ||
url: 'http://localhost:8000/echo', | ||
data: { | ||
a: 1 | ||
} | ||
}, righto.after(server)); | ||
request(function (error, result) { | ||
t.equal(error.code, 'SENT_OBJECT_AS_DATA'); | ||
destroyServer(); | ||
}); | ||
}); |
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
7104
166
1
8
+ Addederror-with-object@^1.1.0
+ Addederror-with-object@1.1.0(transitive)