@vtex/api
Advanced tools
Comparing version 0.8.5 to 0.9.0
@@ -6,2 +6,3 @@ 'use strict'; | ||
}); | ||
exports.handleJson = undefined; | ||
exports.successful = successful; | ||
@@ -18,2 +19,14 @@ exports.StatusCodeError = StatusCodeError; | ||
var _response = require('requisition/lib/response'); | ||
var _response2 = _interopRequireDefault(_response); | ||
var _destroy = require('destroy'); | ||
var _destroy2 = _interopRequireDefault(_destroy); | ||
var _statuses = require('statuses'); | ||
var _statuses2 = _interopRequireDefault(_statuses); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -62,9 +75,11 @@ | ||
var handleJson = exports.handleJson = function handleJson(res) { | ||
if (res.is('json')) { | ||
return res.json(); | ||
} | ||
return res; | ||
}; | ||
_request2.default.prototype.thenJson = function () { | ||
return this.then(function (res) { | ||
if (res.is('json')) { | ||
return res.json(); | ||
} | ||
return res; | ||
}); | ||
return this.then(handleJson); | ||
}; | ||
@@ -110,2 +125,29 @@ | ||
return baseQuery.apply(this, [query]); | ||
}; | ||
// TODO fork requisition and implement properly there. | ||
_request2.default.prototype.sendStream = function (stream) { | ||
var self = this; | ||
return new Promise(function (resolve, reject) { | ||
var req = self._create(); | ||
stream.on('error', function (err) { | ||
req.abort(); | ||
reject(err); | ||
}); | ||
req.on('error', function (err) { | ||
(0, _destroy2.default)(stream); | ||
reject(err); | ||
}); | ||
req.on('response', function (res) { | ||
self.clearTimeout(); | ||
if (_statuses2.default.redirect[res.statusCode]) { | ||
return resolve(self.redirect(res)); | ||
} | ||
resolve(new _response2.default(req, res, self.options)); | ||
}); | ||
req.on('close', function () { | ||
(0, _destroy2.default)(stream); | ||
}); | ||
stream.pipe(req); | ||
}); | ||
}; |
@@ -21,2 +21,8 @@ 'use strict'; | ||
var _zlib = require('zlib'); | ||
var _path = require('path'); | ||
var _stream = require('stream'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -108,9 +114,24 @@ | ||
key: 'saveFile', | ||
value: function saveFile(account, workspace, bucket, path, filePath) { | ||
var unzip = arguments.length <= 5 || arguments[5] === undefined ? false : arguments[5]; | ||
value: function saveFile(account, workspace, bucket, path, streamOrPath) { | ||
var _ref2 = arguments.length <= 5 || arguments[5] === undefined ? {} : arguments[5]; | ||
(0, _required2.default)({ account: account, workspace: workspace, bucket: bucket, path: path, filePath: filePath }); | ||
var unzip = _ref2.unzip; | ||
var gzip = _ref2.gzip; | ||
var gzipOptions = _ref2.gzipOptions; | ||
(0, _required2.default)({ account: account, workspace: workspace, bucket: bucket, path: path, streamOrPath: streamOrPath }); | ||
var url = '' + this.endpointUrl + this.routes.Files(account, workspace, bucket, path); | ||
return this.http.put(url).query({ unzip: unzip }).sendFile(filePath).thenJson(); | ||
var put = this.http.put(url).type((0, _path.basename)(path)); | ||
if (streamOrPath.pipe && streamOrPath.on) { | ||
if (gzip) { | ||
var gz = (0, _zlib.createGzip)(gzipOptions); | ||
var gzPut = put.set('Content-Encoding', 'gzip'); | ||
return gzPut.sendStream(streamOrPath.pipe(gz)).then(_http.handleJson); | ||
} | ||
return put.sendStream(streamOrPath.pipe((0, _stream.PassThrough)())).then(_http.handleJson); | ||
} | ||
if (typeof streamOrPath === 'string' || streamOrPath instanceof String) { | ||
return put.query({ unzip: unzip }).sendFile(streamOrPath).thenJson(); | ||
} | ||
throw new Error('Argument streamOrPath must be a readable stream or the path to a file.'); | ||
} | ||
@@ -117,0 +138,0 @@ }, { |
{ | ||
"name": "@vtex/api", | ||
"version": "0.8.5", | ||
"version": "0.9.0", | ||
"description": "VTEX I/O API client", | ||
@@ -23,4 +23,6 @@ "main": "./lib/index.js", | ||
"any-promise": "^1.3.0", | ||
"destroy": "^1.0.4", | ||
"form-data": "^1.0.0-rc4", | ||
"requisition": "^1.7.0" | ||
"requisition": "^1.7.0", | ||
"statuses": "^1.3.0" | ||
}, | ||
@@ -27,0 +29,0 @@ "devDependencies": { |
@@ -14,2 +14,3 @@ # VTEX I/O API Client for Node | ||
- VBaseClient | ||
- VTEXIDClient | ||
@@ -30,1 +31,49 @@ Usage: | ||
Install the dependencies (`npm install`) and run `npm run build`. | ||
### Using VBaseClient.sendFile | ||
An example usage of the three supported method of sending a file to VBase: | ||
``` | ||
import {VBaseClient} from '@vtex/api' | ||
import {createReadStream} from 'fs' | ||
const client = new VBaseClient({ | ||
authToken: 'test', | ||
userAgent: 'test send', | ||
endpointUrl: 'BETA', | ||
}) | ||
client.saveFile( | ||
'account', | ||
'workspace', | ||
'bucket', | ||
'test-send-stream-gzip.txt', | ||
createReadStream('./test-send.txt'), | ||
{gzip: true, gzipOptions: {level: 9}} | ||
).then((res) => { | ||
console.log('gz:', res) | ||
}) | ||
client.saveFile( | ||
'account', | ||
'workspace', | ||
'bucket', | ||
'test-send-stream.txt', | ||
createReadStream('./test-send.txt'), | ||
{gzip: false} | ||
).then((res) => { | ||
console.log('stream:', res) | ||
}) | ||
client.saveFile( | ||
'account', | ||
'workspace', | ||
'bucket', | ||
'test-send-file.txt', | ||
'./test-send.txt' | ||
).then((res) => { | ||
console.log('file:', res) | ||
}) | ||
``` |
35102
719
78
5
+ Addeddestroy@^1.0.4
+ Addedstatuses@^1.3.0