sbx-parse-api
Version:
JavaScript API for Securibox Parse
204 lines (182 loc) • 7.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.AuthMethods = exports.Parse = undefined;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _base = require('base-64');
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var AuthMethods = {
BASIC: 'Basic',
JWT: 'Bearer'
};
var Parse = function () {
function Parse(url, authMethod) {
_classCallCheck(this, Parse);
var auth = void 0;
switch (authMethod) {
case AuthMethods.BASIC:
if ((arguments.length <= 2 ? 0 : arguments.length - 2) === 2) {
auth = (0, _base.encode)((arguments.length <= 2 ? undefined : arguments[2]) + ':' + (arguments.length <= 3 ? undefined : arguments[3]));
} else {
throw new Error('Username and password must not be empty!');
}
break;
case AuthMethods.JWT:
if ((arguments.length <= 2 ? 0 : arguments.length - 2) === 1) {
auth = arguments.length <= 2 ? undefined : arguments[2];
} else {
throw new Error('JWT must not be empty');
}
break;
default:
throw new Error('Invalid auth type "' + authMethod + '"');
//
}
this.url = url;
this.headers = new Headers();
this.headers.append('Authorization', authMethod + ' ' + auth);
this.headers.append('Content-Type', 'application/json');
this.headers.append('Accept', 'application/json');
}
_createClass(Parse, [{
key: '_create_payload',
value: function _create_payload(docs) {
var result = [];
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = docs[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var doc = _step.value;
var obj = {};
if (doc.id === undefined) {
throw Error('Document ID must not be empty!');
}
obj['id'] = doc.id;
if (doc.buffer !== undefined) {
obj['content'] = (0, _base.encode)(_arrayBufferToBytes(doc.buffer));
} else if (doc.bytes !== undefined) {
obj['content'] = (0, _base.encode)(obj.bytes);
} else if (doc.content !== undefined) {
obj['content'] = obj.content;
} else {
throw Error('Document content must not be empty!');
}
if (doc.labelId !== undefined) {
obj['labelId'] = doc.labelId;
}
if (doc.detailedLabelId !== undefined) {
obj['detailedLabelId'] = doc.detailedLabelId;
}
if (doc.extractedData !== undefined) {
obj['extractedData'] = doc.extractedData;
}
result.push(obj);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return JSON.stringify(result);
}
}, {
key: '_doc_post',
value: function _doc_post(docs, url) {
return this._request(this._create_payload(docs), url, 'POST');
}
}, {
key: '_request',
value: function _request(payload, url, method) {
return fetch('' + this.url + url, {
method: method,
headers: this.headers,
body: payload,
credentials: 'include'
}).then(function (response) {
if (!response.ok) {
throw { 'error': response };
}
return response.json();
}).catch(function (error) {
throw { 'error': error };
});
}
}, {
key: 'classify',
value: function classify(docs, take) {
var url = '/docs/classify';
if (take !== undefined) {
url += '?take=' + take;
}
return this._doc_post(docs, url);
}
}, {
key: 'parse',
value: function parse(docs, mode) {
var url = '/docs/parse';
if (mode !== undefined) {
url += '?mode=' + mode;
}
return this._doc_post(docs, url);
}
}, {
key: 'guess',
value: function guess(docs) {
var url = '/docs/guess';
return this._doc_post(docs, url);
}
}, {
key: 'feed',
value: function feed(docs) {
var url = '/docs/feed';
return this._doc_post(docs, url);
}
}, {
key: 'train',
value: function train(docs) {
var url = '/docs/train';
return this._doc_post(docs, url);
}
}, {
key: 'pending',
value: function pending(docIds) {
var url = '/docs/pending';
return this._request(docsIds, url, 'POST');
}
}, {
key: 'getExtractedDataKeys',
value: function getExtractedDataKeys() {
var url = '/xdata';
return this._request(null, url, 'GET');
}
}, {
key: 'getLabels',
value: function getLabels() {
var url = '/labels';
return this._request(null, url, 'GET');
}
}]);
return Parse;
}();
function _arrayBufferToBytes(buffer) {
var binary = '';
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return binary;
}
exports.Parse = Parse;
exports.AuthMethods = AuthMethods;