c8osdkjscore
Version:
convertigo's sdk js core
526 lines • 23.1 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var c8oBase_1 = require("./c8oBase");
var c8oLogger_1 = require("./c8oLogger");
var c8oUtilsCore_1 = require("./c8oUtilsCore");
var c8oFullSync_1 = require("./c8oFullSync");
var c8oResponse_1 = require("./c8oResponse");
var c8oExceptionMessage_1 = require("./Exception/c8oExceptionMessage");
var c8oCallTask_1 = require("./c8oCallTask");
var c8oFullSyncChangeListener_1 = require("./c8oFullSyncChangeListener");
var c8oPromise_1 = require("./c8oPromise");
var c8oCouchBaseLiteException_1 = require("./Exception/c8oCouchBaseLiteException");
var c8oException_1 = require("./Exception/c8oException");
var c8oExceptionListener_1 = require("./Exception/c8oExceptionListener");
var C8oCore = (function (_super) {
__extends(C8oCore, _super);
function C8oCore() {
var _this = _super.call(this) || this;
_this._automaticRemoveSplashsCreen = true;
_this.lives = [];
_this.livesDb = [];
_this._couchUrl = null;
_this.handleFullSyncLive = new c8oFullSyncChangeListener_1.C8oFullSyncChangeListener(function (changes) {
for (var task in _this.lives) {
_this.lives[task].executeFromLive();
}
});
_this.data = null;
_this.c8oLogger = new c8oLogger_1.C8oLogger(_this, true);
return _this;
}
Object.defineProperty(C8oCore.prototype, "couchUrl", {
get: function () {
return this._couchUrl;
},
set: function (value) {
this._couchUrl = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(C8oCore.prototype, "logC8o", {
get: function () {
return this._logC8o;
},
set: function (value) {
this._logC8o = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(C8oCore.prototype, "logRemote", {
get: function () {
return this._logRemote;
},
set: function (value) {
this._logRemote = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(C8oCore.prototype, "logLevelLocal", {
get: function () {
return this._logLevelLocal;
},
set: function (value) {
this._logLevelLocal = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(C8oCore.prototype, "log", {
get: function () {
return this.c8oLogger;
},
enumerable: true,
configurable: true
});
C8oCore.prototype.toString = function () {
return "C8o[" + this._endpoint + "]";
};
Object.defineProperty(C8oCore.prototype, "endpoint", {
get: function () {
return this._endpoint;
},
set: function (value) {
this._endpoint = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(C8oCore.prototype, "endpointConvertigo", {
get: function () {
return this._endpointConvertigo;
},
set: function (value) {
this._endpointConvertigo = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(C8oCore.prototype, "endpointIsSecure", {
get: function () {
return this._endpointIsSecure;
},
set: function (value) {
this._endpointIsSecure = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(C8oCore.prototype, "endpointHost", {
get: function () {
return this._endpointHost;
},
set: function (value) {
this._endpointHost = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(C8oCore.prototype, "endpointPort", {
get: function () {
return this._endpointPort;
},
set: function (value) {
this._endpointPort = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(C8oCore.prototype, "endpointProject", {
get: function () {
return this._endpointProject;
},
set: function (value) {
this._endpointProject = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(C8oCore.prototype, "deviceUUID", {
get: function () {
return C8oCore.deviceUUID;
},
enumerable: true,
configurable: true
});
Object.defineProperty(C8oCore.prototype, "httpPublic", {
get: function () {
return this._http;
},
enumerable: true,
configurable: true
});
Object.defineProperty(C8oCore.prototype, "coreVersion", {
get: function () {
return require("../../package.json").version;
},
enumerable: true,
configurable: true
});
C8oCore.prototype.finalizeInit = function () {
var _this = this;
this.promiseFinInit = new Promise(function (resolve) {
Promise.all([_this.promiseInit]).then(function () {
if (_this._automaticRemoveSplashsCreen) {
if (navigator["splashscreen"] !== undefined) {
navigator["splashscreen"].hide();
}
}
if (window["cblite"] != undefined) {
window["cblite"].getURL(function (err, url) {
if (err) {
resolve();
}
else {
url = url.replace(new RegExp("/$"), "");
_this.couchUrl = url;
resolve();
}
});
}
else {
resolve();
}
});
});
return this.promiseFinInit;
};
C8oCore.prototype.extractendpoint = function () {
if (!c8oUtilsCore_1.C8oUtilsCore.isValidUrl(this.endpoint)) {
throw new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.illegalArgumentInvalidURL(this.endpoint).toString());
}
var matches = C8oCore.RE_ENDPOINT.exec(this.endpoint.toString());
if (matches === null) {
throw new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.illegalArgumentInvalidEndpoint(this.endpoint.toString()));
}
this.endpointConvertigo = matches[0].substring(0, (matches[0].indexOf("/projects")));
this.endpointIsSecure = matches[1] != null;
this.endpointHost = matches[2];
this.endpointPort = matches[3];
this.endpointProject = matches[4];
};
C8oCore.prototype.call = function (requestable, parameters, c8oResponseListener, c8oExceptionListener) {
if (parameters === void 0) { parameters = null; }
if (c8oResponseListener === void 0) { c8oResponseListener = null; }
if (c8oExceptionListener === void 0) { c8oExceptionListener = null; }
try {
if (requestable === null || requestable === undefined) {
throw new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.illegalArgumentNullParameter("resquestable"));
}
if (parameters === null || parameters === undefined) {
parameters = {};
}
else {
}
var regex = C8oCore.RE_REQUESTABLE.exec(requestable);
if (regex === null || regex === undefined) {
throw new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.InvalidArgumentInvalidEndpoint(this._endpoint));
}
if (regex[1] !== "") {
parameters[C8oCore.ENGINE_PARAMETER_PROJECT.toString()] = regex[1];
}
if (regex[2] != null) {
parameters[C8oCore.ENGINE_PARAMETER_SEQUENCE.toString()] = regex[2];
}
else {
parameters[C8oCore.ENGINE_PARAMETER_CONNECTOR.toString()] = regex[3];
parameters[C8oCore.ENGINE_PARAMETER_TRANSACTION.toString()] = regex[4];
}
return this._call(parameters, c8oResponseListener, c8oExceptionListener);
}
catch (error) {
this.handleCallException(c8oExceptionListener, parameters, error);
}
finally {
}
};
C8oCore.prototype._call = function (parameters, c8oResponseListener, c8oExceptionListener) {
var _this = this;
if (parameters === void 0) { parameters = null; }
if (c8oResponseListener === void 0) { c8oResponseListener = null; }
if (c8oExceptionListener === void 0) { c8oExceptionListener = null; }
Promise.all([this.promiseFinInit]).then(function () {
try {
_this.c8oLogger.logMethodCall("call", parameters, c8oResponseListener, c8oExceptionListener);
if (parameters == null) {
parameters = {};
}
else {
}
var task = new c8oCallTask_1.C8oCallTask(_this, parameters, c8oResponseListener, c8oExceptionListener);
task.run();
}
catch (error) {
_this.handleCallException(c8oExceptionListener, parameters, error);
}
finally {
}
});
};
C8oCore.prototype.callJsonObject = function (requestable, parameters) {
var promise = new c8oPromise_1.C8oPromise(this);
this.call(requestable, parameters, new c8oResponse_1.C8oResponseJsonListener(function (response, requestParameters) {
if (requestParameters == null) {
requestParameters = {};
}
if (response == null && requestParameters[C8oCore.ENGINE_PARAMETER_PROGRESS]) {
promise.onProgress(requestParameters[C8oCore.ENGINE_PARAMETER_PROGRESS]);
}
else {
promise.onResponse(response, requestParameters);
}
}), new c8oExceptionListener_1.C8oExceptionListener(function (exception, data) {
promise.onFailure(exception, data);
}));
return promise;
};
C8oCore.prototype.callJson = function (requestable) {
var parameters = [];
for (var _i = 1; _i < arguments.length; _i++) {
parameters[_i - 1] = arguments[_i];
}
return this.callJsonObject(requestable, C8oCore.toParameters(parameters));
};
C8oCore.toParameters = function (parameters) {
var newParameters = {};
if (0 !== parameters.length % 2) {
throw new c8oException_1.C8oException("Incorrect number of parameters");
}
for (var i = 0; i < parameters.length; i += 2) {
newParameters[parameters[i]] = parameters[i + 1];
}
return newParameters;
};
C8oCore.prototype.handleCallException = function (c8oExceptionListener, requestParameters, exception) {
this.c8oLogger.warn("Handle a call exception", exception);
if (c8oExceptionListener != null) {
c8oExceptionListener.onException(exception, requestParameters);
}
};
C8oCore.prototype.get_attachment = function (id, attachment_name, database_name) {
var _this = this;
return new Promise(function (resolve, reject) {
if (database_name == null) {
database_name = _this.defaultDatabaseName;
}
if (_this.c8oFullSync != undefined) {
var fullsyncdb = _this.c8oFullSync.getOrCreateFullSyncDatabase(database_name);
fullsyncdb.getdatabase.getAttachment(id, attachment_name).then(function (buffer) {
resolve(buffer);
}).catch(function (err) {
reject(err);
});
}
});
};
C8oCore.prototype.addFullSyncChangeListener = function (db, listener) {
this.c8oFullSync.addFullSyncChangeListener(db, listener);
};
C8oCore.prototype.removeFullSyncChangeListener = function (db, listener) {
this.c8oFullSync.removeFullSyncChangeListener(db, listener);
};
C8oCore.prototype.addLive = function (liveid, db, task) {
this.cancelLive(liveid);
this.lives[liveid] = task;
this.livesDb[liveid] = db;
this.addFullSyncChangeListener(db, this.handleFullSyncLive);
};
C8oCore.prototype.cancelLive = function (liveid) {
if (this.livesDb[liveid] !== undefined) {
var db = this.livesDb[liveid];
delete this.livesDb[liveid];
if (this.livesDb[db] !== undefined) {
db = null;
}
if (db !== null) {
this.removeFullSyncChangeListener(db, this.handleFullSyncLive);
}
}
delete this.lives[liveid];
};
C8oCore.RE_REQUESTABLE = /^([^.]*)\.(?:([^.]+)|(?:([^.]+)\.([^.]+)))$/;
C8oCore.RE_ENDPOINT = /^(https?:\/\/([^:/]+)(:[0-9]+)?\/?.*?)\/projects\/([^\/]+)$/;
C8oCore.ENGINE_PARAMETER_PROJECT = "__project";
C8oCore.ENGINE_PARAMETER_SEQUENCE = "__sequence";
C8oCore.ENGINE_PARAMETER_CONNECTOR = "__connector";
C8oCore.ENGINE_PARAMETER_TRANSACTION = "__transaction";
C8oCore.ENGINE_PARAMETER_ENCODED = "__encoded";
C8oCore.ENGINE_PARAMETER_DEVICE_UUID = "__uuid";
C8oCore.ENGINE_PARAMETER_PROGRESS = "__progress";
C8oCore.ENGINE_PARAMETER_FROM_LIVE = "__fromLive";
C8oCore.FS_POLICY = "_use_policy";
C8oCore.FS_POLICY_NONE = "none";
C8oCore.FS_POLICY_CREATE = "create";
C8oCore.FS_POLICY_OVERRIDE = "override";
C8oCore.FS_POLICY_MERGE = "merge";
C8oCore.FS_SUBKEY_SEPARATOR = "_use_subkey_separator";
C8oCore.FS_LIVE = "__live";
C8oCore.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE = "response";
C8oCore.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE_TYPE = "responseType";
C8oCore.LOCAL_CACHE_DOCUMENT_KEY_EXPIRATION_DATE = "expirationDate";
C8oCore.LOCAL_CACHE_DATABASE_NAME = "c8olocalcache";
C8oCore.RESPONSE_TYPE_XML = "pxml";
C8oCore.RESPONSE_TYPE_JSON = "json";
C8oCore.deviceUUID = c8oUtilsCore_1.C8oUtilsCore.getNewGUIDString();
return C8oCore;
}(c8oBase_1.C8oBase));
exports.C8oCore = C8oCore;
var FullSyncPolicy = (function () {
function FullSyncPolicy(value, action) {
this.value = value;
this.action = action;
}
FullSyncPolicy.values = function () {
return [this.NONE, this.CREATE, this.OVERRIDE, this.MERGE];
};
FullSyncPolicy.getFullSyncPolicy = function (value) {
if (value != null) {
var fullSyncPolicyValues = FullSyncPolicy.values();
for (var _i = 0, fullSyncPolicyValues_1 = fullSyncPolicyValues; _i < fullSyncPolicyValues_1.length; _i++) {
var fullSyncPolicy = fullSyncPolicyValues_1[_i];
if (fullSyncPolicy.value === value) {
return fullSyncPolicy;
}
}
}
return this.NONE;
};
FullSyncPolicy.NONE = new FullSyncPolicy(C8oCore.FS_POLICY_NONE, function (database, newProperties) {
var documentId = c8oUtilsCore_1.C8oUtilsCore.getParameterStringValue(newProperties, c8oFullSync_1.C8oFullSync.FULL_SYNC__ID, false);
if (documentId === "") {
documentId = null;
}
return new Promise(function (resolve, reject) {
database.post(newProperties).then(function (createdDocument) {
resolve(createdDocument);
}).catch(function (error) {
reject(new c8oCouchBaseLiteException_1.C8oCouchBaseLiteException(c8oExceptionMessage_1.C8oExceptionMessage.fullSyncPutProperties(newProperties), error));
});
});
});
FullSyncPolicy.CREATE = new FullSyncPolicy(C8oCore.FS_POLICY_CREATE, function (database, newProperties) {
try {
delete newProperties[c8oFullSync_1.C8oFullSync.FULL_SYNC__ID];
delete newProperties[c8oFullSync_1.C8oFullSync.FULL_SYNC__REV];
return new Promise(function (resolve) {
database.post(newProperties).then(function (createdDocument) {
resolve(createdDocument);
});
});
}
catch (error) {
throw new c8oCouchBaseLiteException_1.C8oCouchBaseLiteException(c8oExceptionMessage_1.C8oExceptionMessage.fullSyncPutProperties(newProperties), error);
}
});
FullSyncPolicy.OVERRIDE = new FullSyncPolicy(C8oCore.FS_POLICY_OVERRIDE, function (database, newProperties) {
try {
var documentId_1 = c8oUtilsCore_1.C8oUtilsCore.getParameterStringValue(newProperties, c8oFullSync_1.C8oFullSync.FULL_SYNC__ID, false);
delete newProperties[c8oFullSync_1.C8oFullSync.FULL_SYNC__ID];
delete newProperties[c8oFullSync_1.C8oFullSync.FULL_SYNC__REV];
if (documentId_1 == null) {
return new Promise(function (resolve) {
database.post(newProperties).then(function (createdDocument) {
resolve(createdDocument);
});
});
}
else {
return new Promise(function (resolve, reject) {
database.get(documentId_1).then(function (doc) {
newProperties["_id"] = documentId_1;
newProperties["_rev"] = doc._rev;
return database.put(newProperties);
}).then(function (createdDocument) {
resolve(createdDocument);
}).catch(function (error) {
if (error.status === "404" || error.status === 404) {
newProperties["_id"] = documentId_1;
return database.post(newProperties);
}
else {
reject(error);
}
}).then(function (createdDocument) {
resolve(createdDocument);
});
});
}
}
catch (error) {
throw new c8oCouchBaseLiteException_1.C8oCouchBaseLiteException(c8oExceptionMessage_1.C8oExceptionMessage.fullSyncPutProperties(newProperties), error);
}
});
FullSyncPolicy.MERGE = new FullSyncPolicy(C8oCore.FS_POLICY_MERGE, function (database, newProperties) {
try {
var documentId_2 = c8oUtilsCore_1.C8oUtilsCore.getParameterStringValue(newProperties, c8oFullSync_1.C8oFullSync.FULL_SYNC__ID, false);
delete newProperties[c8oFullSync_1.C8oFullSync.FULL_SYNC__REV];
if (documentId_2 == null) {
return new Promise(function (resolve, reject) {
database.put(newProperties).then(function (createdDocument) {
resolve(createdDocument);
}).catch(function (error) {
reject(new c8oCouchBaseLiteException_1.C8oCouchBaseLiteException(c8oExceptionMessage_1.C8oExceptionMessage.fullSyncPutProperties(newProperties), error));
});
});
}
else {
return new Promise(function (resolve, reject) {
database.get(documentId_2).then(function (doc) {
c8oFullSync_1.C8oFullSyncCbl.mergeProperties(newProperties, doc);
database.put(newProperties).then(function (createdDocument) {
resolve(createdDocument);
})
.catch(function (error) {
reject(new c8oCouchBaseLiteException_1.C8oCouchBaseLiteException(c8oExceptionMessage_1.C8oExceptionMessage.fullSyncPutProperties(newProperties), error));
});
}).catch(function (error) {
if (error.status === 404) {
database.put(newProperties).then(function (createdDocument) {
resolve(createdDocument);
})
.catch(function (error) {
reject(new c8oCouchBaseLiteException_1.C8oCouchBaseLiteException(c8oExceptionMessage_1.C8oExceptionMessage.fullSyncPutProperties(newProperties), error));
});
}
else {
reject(new c8oCouchBaseLiteException_1.C8oCouchBaseLiteException(c8oExceptionMessage_1.C8oExceptionMessage.fullSyncPutProperties(newProperties), error));
}
});
});
}
}
catch (error) {
throw new c8oCouchBaseLiteException_1.C8oCouchBaseLiteException(c8oExceptionMessage_1.C8oExceptionMessage.fullSyncPutProperties(newProperties), error);
}
});
return FullSyncPolicy;
}());
exports.FullSyncPolicy = FullSyncPolicy;
var FullSyncPostDocumentParameter = (function () {
function FullSyncPostDocumentParameter(name) {
this.name = name;
}
FullSyncPostDocumentParameter.values = function () {
var array = [];
array.push(this.POLICY, this.SUBKEY_SEPARATOR);
return array;
};
FullSyncPostDocumentParameter.POLICY = new FullSyncPostDocumentParameter(C8oCore.FS_POLICY);
FullSyncPostDocumentParameter.SUBKEY_SEPARATOR = new FullSyncPostDocumentParameter(C8oCore.FS_SUBKEY_SEPARATOR);
return FullSyncPostDocumentParameter;
}());
exports.FullSyncPostDocumentParameter = FullSyncPostDocumentParameter;
//# sourceMappingURL=c8oCore.js.map