UNPKG

@kaltura-ng/kaltura-client

Version:
315 lines 15.2 kB
import { KalturaUtils } from "./utils/kaltura-utils"; import { KalturaTypesFactory } from './kaltura-types-factory'; import { KalturaLogger } from './kaltura-logger'; ; var logger = new KalturaLogger('KalturaObjectBase'); var KalturaObjectBase = (function () { function KalturaObjectBase(data) { this._dependentProperties = {}; if (data) { Object.assign(this, data); } } KalturaObjectBase.prototype.setData = function (handler) { if (handler) { handler(this); } return this; }; KalturaObjectBase.prototype.getTypeName = function () { return this._getMetadata().properties['objectType'].default; }; KalturaObjectBase.prototype._getMetadata = function () { return { properties: {} }; }; KalturaObjectBase.prototype.hasMetadataProperty = function (propertyName) { return !!this._getMetadata().properties[propertyName]; }; KalturaObjectBase.prototype.toRequestObject = function () { var _this = this; var metadata = this._getMetadata(); var result = {}; try { Object.keys(metadata.properties).forEach(function (propertyName) { var propertyData = metadata.properties[propertyName]; var propertyValue = _this._createRequestPropertyValue(propertyName, propertyData); switch (propertyValue.status) { case "exists": result[propertyName] = propertyValue.value; break; case "removed": result[propertyName + "__null"] = ''; // mark property for deletion break; case "missing": default: break; } }); } catch (err) { // TODO [kaltura] should use logHandler logger.warn(err.message); throw err; } return result; }; KalturaObjectBase.prototype.fromResponseObject = function (data) { var _this = this; var metadata = this._getMetadata(); var result = {}; try { Object.keys(metadata.properties).forEach(function (propertyName) { var propertyData = metadata.properties[propertyName]; var propertyValue = _this._parseResponseProperty(propertyName, propertyData, data); if (propertyValue != null && typeof propertyValue !== 'undefined') { _this[propertyName] = propertyValue; } }); } catch (err) { // TODO [kaltura] should use logHandler logger.warn(err.message); throw err; } return result; }; KalturaObjectBase.prototype._parseResponseProperty = function (propertyName, property, source) { var _this = this; var result; var sourceValue = propertyName ? source[propertyName] : source; if (typeof sourceValue !== 'undefined') { if (sourceValue === null) { result = null; } else { switch (property.type) { case 'b':// boolean if (typeof sourceValue === 'boolean') { result = sourceValue; } else if (sourceValue + '' === '0') { result = false; } else if (sourceValue + '' === '1') { result = true; } break; case 's':// string result = sourceValue + ''; break; case 'n': // number case 'en':// enum of type number result = sourceValue * 1; break; case 'o':// object var propertyObjectType = sourceValue['objectType']; if (propertyObjectType) { result = this._createKalturaObject(propertyObjectType, property.subType); if (result) { result.fromResponseObject(sourceValue); } else { throw new Error("Failed to create kaltura object of type '" + source['objectType'] + "' (fallback type '" + property.subType + "')"); } } else { throw new Error("Failed to create kaltura object for property '" + propertyName + "' (type '" + property.subType + "'). provided response object is missing property 'objectType'."); } break; case 'm':// map var parsedMap_1 = {}; if (sourceValue instanceof Object) { Object.keys(sourceValue).forEach(function (itemKey) { var itemValue = sourceValue[itemKey]; var newItem = _this._createKalturaObject(property.subType); if (itemValue && newItem) { newItem.fromResponseObject(itemValue); parsedMap_1[itemKey] = newItem; } else { throw new Error("Failed to create kaltura object for type '" + property.subType + "'"); } }); } else { throw new Error("failed to parse property '" + propertyName + ". Expected type object, got type '" + typeof sourceValue); } break; case 'a':// array if (sourceValue instanceof Array) { var parsedArray_1 = []; sourceValue.forEach(function (responseItem) { var newItem = _this._createKalturaObject(responseItem['objectType'], property.subType); if (newItem) { newItem.fromResponseObject(responseItem); parsedArray_1.push(newItem); } else { throw new Error("Failed to create kaltura object for type '" + responseItem['objectType'] + "' and for fallback type '" + property.subType + "'"); } }); result = parsedArray_1; } else { throw new Error("failed to parse property '" + propertyName + ". Expected type array, got type '" + typeof sourceValue); } break; case 'd':// date if (this._isNumeric(sourceValue)) { result = KalturaUtils.fromServerDate(sourceValue * 1); } else { throw new Error("failed to parse property '" + propertyName + ". Expected type date, got type '" + typeof sourceValue); } break; case "es": result = this._createKalturaObject(property.subType); if (result && typeof result !== 'undefined') { result['_value'] = sourceValue + ''; } else { throw new Error("Failed to create kaltura enum for type '" + property.subType + "'"); } break; default: break; } } } return result; }; KalturaObjectBase.prototype._isNumeric = function (n) { return !isNaN(parseFloat(n)) && isFinite(n); }; KalturaObjectBase.prototype._createKalturaObject = function (objectType, fallbackObjectType) { var result = null; var usedFallbackType = false; if (objectType) { result = KalturaTypesFactory.createObject(objectType); } if (!result && fallbackObjectType) { usedFallbackType = true; result = KalturaTypesFactory.createObject(fallbackObjectType); } if (usedFallbackType && result) { logger.warn("[kaltura-client]: Could not find object type '" + objectType + "', Falling back to '" + fallbackObjectType + "' object type. (Did you remember to set your accepted object types in the request \u201CacceptedTypes\u201D attribute?)"); } else if (!result) { logger.warn("[kaltura-client]: Could not find object type '" + objectType + "'. (Did you remember to set your accepted object types in the request \u201CacceptedTypes\u201D attribute?)"); } return result; }; KalturaObjectBase.prototype._createRequestPropertyValue = function (propertyName, property) { var result = { status: 'missing' }; if (property.type === 'c') { // constant string if (property.default) { result = { status: 'exists', value: property.default }; } } else if (this._dependentProperties[propertyName]) { var dependentProperty = this._dependentProperties[propertyName]; var resultValue = "{" + dependentProperty.request + ":result" + (dependentProperty.targetPath ? ':' + dependentProperty.targetPath : '') + "}"; result = { status: 'exists', value: resultValue }; } else if (!property.readOnly) { var value = this[propertyName]; if (typeof value !== 'undefined') { if (value === null) { result = { status: 'removed' }; } else { switch (property.type) { case 'b':// boolean result = { status: 'exists', value: value }; break; case 's':// string result = { status: 'exists', value: value + '' }; break; case 'n': // number case 'en':// enum of type number result = { status: 'exists', value: value * 1 }; break; case 'o':// object if (value instanceof KalturaObjectBase) { result = { status: 'exists', value: value.toRequestObject() }; } else { throw new Error("failed to parse property. Expected '" + propertyName + " to be kaltura object"); } break; case 'a':// array if (value instanceof Array) { var parsedArray_2 = []; value.forEach(function (item) { if (item instanceof KalturaObjectBase) { parsedArray_2.push(item.toRequestObject()); } }); if (parsedArray_2.length !== 0) { if (parsedArray_2.length === value.length) { result = { status: 'exists', value: parsedArray_2 }; } else { throw new Error("failed to parse array. Expected all '" + propertyName + " items to be kaltura object"); } } } else { throw new Error("failed to parse property. Expected '" + propertyName + " to be kaltura object"); } break; case 'd':// date if (value instanceof Date) { result = { status: 'exists', value: KalturaUtils.toServerDate(value) }; } else { throw new Error("failed to parse property. Expected '" + propertyName + " to be date"); } break; case 'es':// enum of type string if (typeof value._value !== 'undefined') { result = { status: 'exists', value: value._value }; } else { throw new Error("failed to parse property. Expected '" + propertyName + " to be of type string enum"); } break; case 'f': if (value instanceof FormData) { result = { status: 'exists', value: value }; } break; default: // do nothing break; } } } } return result; }; KalturaObjectBase.prototype.setDependency = function () { var dependency = []; for (var _i = 0; _i < arguments.length; _i++) { dependency[_i] = arguments[_i]; } for (var i = 0, len = dependency.length; i < len; i++) { var item = dependency[i]; var _a = item, property = _a.property, request = _a.request, targetPath = _a.targetPath; if (item instanceof Array) { property = item[0]; // The server expect one based index (meaning the first item has index 1) // since Javascript array are zero based index we expose the api as zero based // and transform the index value in the actual request by adding 1 request = item[1] + 1; targetPath = item.length == 3 ? item[2] : null; } this._dependentProperties[property] = { property: property, request: request, targetPath: targetPath }; } return this; }; return KalturaObjectBase; }()); export { KalturaObjectBase }; //# sourceMappingURL=kaltura-object-base.js.map