Skip to content
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = function (config) {
var makeRequest = setupMakeRequest(config);

return {
makeRequest,
readChannel: function (opts, cb) {
assert(Object(opts) === opts, 'opts required');
assert(typeof cb === 'function', 'cb required');
Expand Down
3 changes: 1 addition & 2 deletions lib/encode-form-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const http = require('http');
const https = require('https');
const seriesEach = require('eachy');
const FileType = require('file-type');
const objectAssign = require('object-assign');

const CRLF = '\r\n';

Expand All @@ -29,7 +28,7 @@ function append (form, item, data, contentType) {
'; size=' + len + CRLF + CRLF;

form.append(item.name || item.filename, data,
objectAssign({ header: header, knownLength: len }, options));
Object.assign({ header: header, knownLength: len }, options));
}

module.exports = function encodeFormData (formData, cb) {
Expand Down
43 changes: 29 additions & 14 deletions lib/make-request.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const crypto = require('crypto');
const objectAssign = require('object-assign');
const https = require('https');
const encodeFormData = require('./encode-form-data');

Expand All @@ -11,11 +10,14 @@ module.exports = function (config) {
function sendRequest (method, endpoint, post, callback) {
const host = config.host || defaultHost;
const date = new Date().toISOString().replace(/\.\d{3}Z$/, 'Z'); // remove milliseconds
let canonicalRequest = Buffer.from(method + 'https://' + host + endpoint + date +
(post ? post.headers['content-type'] : ''));
if (post) {
canonicalRequest = Buffer.concat([canonicalRequest, post.buffer]);
}

const headers = (post || {}).headers || {};
const buffer = (post || {}).buffer || Buffer.alloc(0);

const canonicalRequest = Buffer.concat([
Buffer.from(method + 'https://' + host + endpoint + date + (headers['content-type'] || '')),
buffer
]);

const key = Buffer.from(config.apiSecret, 'base64');
const signature = crypto.createHmac('sha256', key)
Expand All @@ -38,10 +40,10 @@ module.exports = function (config) {
port: config.port || void 0,
rejectUnauthorized: process.env.NODE_ENV !== 'test',
path: endpoint,
headers: objectAssign({
headers: Object.assign({
Accept: 'application/json',
Authorization: auth
}, post ? post.headers : {})
}, headers)
}, function (res) {
let result = '';

Expand All @@ -68,7 +70,11 @@ module.exports = function (config) {

if (parsed.data) {
parsed.data.meta = parsed.meta;
return cb(null, res, parsed.data);
const data = parsed.data;
const links = parsed.links;
const returnBody = {data, links};

return cb(null, res, returnBody);
}

if (parsed.errors && Array.isArray(parsed.errors) &&
Expand All @@ -84,11 +90,7 @@ module.exports = function (config) {

req.on('error', cb);

if (post) {
req.write(post.buffer);
}

req.end();
req.end(buffer);
}

return function makeRequest (method, endpoint, requestOpts, cb) {
Expand All @@ -113,6 +115,19 @@ module.exports = function (config) {

sendRequest(method, endpoint, encoded, done);
});
} else if (requestOpts.body != null) {
let buffer = requestOpts.body;
if (typeof buffer === 'string') {
buffer = Buffer.from(buffer);
} else if (!Buffer.isBuffer(buffer)) {
buffer = Buffer.from(JSON.stringify(buffer));
}
const headers = Object.assign({
'content-type': 'application/json',
'content-length': String(buffer.length)
}, requestOpts.headers || {});

return sendRequest(method, endpoint, { headers: headers, buffer: buffer }, done);
}

sendRequest(method, endpoint, null, done);
Expand Down