/* Enums */ // Enumeration of sort direction // Values: asc, desc var advDirection = Object.freeze({ Asc: 'asc', Desc: 'desc' }); // Enumeration of datatypes available // Values: string,date,boolean,decimal,integer,uniqueidentifier var advDataType = Object.freeze({ String: 'string', Date: 'date', Boolean: 'boolean', Decimal: 'decimal', Integer: 'integer', UniqueIdentifier: 'uniqueidentifier' }); // Enumeration of comparison types // Values: equals, notequals, like, notlike, greaterthan, greaterorequals,lessthan,lessorequals, all var advCompare = Object.freeze({ Equals: 'equals', NotEquals: 'notequals', Like: 'like', NotLike: 'notlike', GreaterThan: 'greaterthan', GreaterOrEquals: 'greaterorequals', LessThan: 'lessthan', LessOrEquals: 'lessorequals', All: 'all' }); /*Types */ function AdvKeyValueCompare(key, value, dataType, comparison) { /// /// Object that holds the Index/Filter criteria when performing a data request. /// /// The name of the field/index. /// The value to use in comparison. /// the datatype of the 'value' passed (advDataType). /// The comparison to perform (advCompare). this.Key = key; this.Value = value; this.DataType = dataType; this.Comparison = comparison; } function AdvSort(fieldName, direction) { /// /// Object that holds the sort when performing a data request /// /// Name of the field. /// The direction to sort (advDirection). this.Direction = direction; this.FieldName = fieldName; } function isUUID(uuid) { /// /// Determines whether the specified value is a unique identifier. /// /// The value passed. /// true if the specified is a unique identifier, otherwise false. let s = "" + uuid; s = s.match('^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$'); if (s === null) { return false; } return true; } function AdvContentRequest(apiName, language) { /// /// Object to fill when performing a data request for multiple records. When populated use as parameter with advGetContent function. /// You can optionally specify the following: /// Index: the index to use when retrieving data (AdvKeyValueCompare) /// Filter: Multiple filters can be specified using AddFilter (AdvKeyValueCompare). Works as iterative 'where clause' with 'and' /// SortList: Multiple sort orders can be specified with AddSort (AdvSort). /// FieldList: Reduce payload by specifying what fields to return populated with data by using AddField. /// /// Name of the API. /// The language abbreviation code used in Advantage CSP associated with the data. this.name = 'AdvContentRequest'; this.ApiName = apiName; this.MaxRecords = 0; this.SkipRecords = 0; this.Header = { 'Authorization': 'Basic' }; this.Host = '' this.CrossDomain = true; this.Authenticate = false; this.Action = 'POST'; var _index = new AdvKeyValueCompare("", "", "string", advCompare.All); // private member var _filter = []; var _sort = []; var _fieldList = []; var _language = "default"; if (language !== undefined && language !== null) _language = language; Object.defineProperty(this, "Language", { get: function () { if (language === undefined && language === null) return 'default'; return _language; }, set: function (value) { _language = value; } }); var checkDirection = function (val) { if (['asc', 'desc'].indexOf(val.toLowerCase()) >= 0) return ''; return 'Direction type must be of the following values: Asc, Desc'; } var checkCompare = function (val) { if (['equals', 'notequals', 'like', 'notlike', 'greaterthan', 'greaterorequals', 'lessthan', 'lessorequals', 'all'].indexOf(val.toLowerCase()) >= 0) return ''; return 'Compare type must be of the following values: Equals, NotEquals, Like, NotLike, GreaterThan, GreaterOrEquals, LessThanLessOrEquals, All'; } var checkDataType = function (val) { if (['string', 'date', 'boolean', 'decimal', 'integer', 'uniqueidentifier'].indexOf(val.toLowerCase()) >= 0) return ''; return 'Data type must be of the following values: String, Date, Boolean, Decimal, Integer, UniqueIdentifier'; } Object.defineProperty(this, "Index", { get: function () { return _index; }, set: function (value) { if (value.constructor.name === "AdvKeyValueCompare") { var result = checkCompare(value.Comparison); if (result) throw result; result = checkDataType(value.DataType); if (result) throw result; _index = value; } else throw "Index property type error"; } }); this.set_Index = function (key, value, dataType, comparison) { var result = checkCompare(comparison); if (result) throw result; result = checkDataType(dataType); if (result) throw result; _index = new AdvKeyValueCompare(key, value, dataType, comparison); } Object.defineProperty(this, "Filter", { get: function () { return _filter; }, set: function (value) { var result = ''; if (Array.isArray(value)) { var arrayLength = value.length; for (var i = 0; i < arrayLength; i++) { if (value[i].constructor.name !== "AdvKeyValueCompare") throw "Filter property type error"; result = checkCompare(value.Comparison); if (result) throw result; result = checkDataType(value.DataType); if (result) throw result; } _filter = value; } else { throw "Filter property type error"; } } }); this.AddFilter = function (key, value, dataType, comparison) { var result = ''; result = checkCompare(comparison); if (result) throw result; result = checkDataType(dataType); if (result) throw result; if (result === '') { var tmp = new AdvKeyValueCompare(key, value, dataType, comparison); _filter.push(tmp); } }; Object.defineProperty(this, "SortList", { get: function () { return _sort; }, set: function (value) { var result = ''; if (Array.isArray(value)) { var arrayLength = value.length; for (var i = 0; i < arrayLength; i++) { if (value[i].constructor.name !== "AdvSort") throw "SortList property type error"; result = checkDirection(value.Direction); if (result) throw result; } _sort = value; } else { throw "SortList property type error"; } } }); this.AddSort = function (fieldName, direction) { var result = ''; result = checkDirection(direction); if (result) throw result; if (result === '') { var tmp = new AdvSort(fieldName, direction); _sort.push(tmp); } else throw "Sort property type error"; }; Object.defineProperty(this, "FieldList", { get: function () { return _fieldList; }, set: function (value) { var result = ''; if (Array.isArray(value)) { var arrayLength = value.length; for (var i = 0; i < arrayLength; i++) { if (value[i].constructor.name !== "String") throw "FieldList property type error"; } _fieldList = value; } else { throw "FieldList property type error"; } } }); this.AddField = function (fieldName) { if (fieldName.constructor.name !== "String") throw "FieldList property type error"; if (fieldName !== '') { _fieldList.push(fieldName); } else throw "Sort property type error"; }; } function AdvContentRequestById(apiName, language, id) { /// /// Object to fill when performing a data request for a single record. When populated use as parameter with advGetContent function. /// You can optionally specify the following: /// Id: The masterId or seoName for the object requested /// /// Name of the API. /// The language abbreviation code used in Advantage CSP associated with the data. /// The masterId or seoName for the object requested this.name = 'AdvContentRequestById'; this.ApiName = apiName; this.Header = { 'Authorization': 'Basic' }; this.Host = '' this.CrossDomain = true; this.Authenticate = false; this.Action = 'POST'; var _id = ''; var _language = "default"; if (language !== undefined && language !== null) _language = language; if (id !== undefined && id !== null) _id = id; Object.defineProperty(this, "Language", { get: function () { if (language === undefined && language === null) return 'default'; return _language; }, set: function (value) { _language = value; } }); Object.defineProperty(this, "Id", { get: function () { return _id; }, set: function (value) { _id = value; } }); } /*Functions */ async function advGetContent(requestContent) { /// /// Makes a synchronous (using await) the rest service to retrieve the data based on the requestContent object. /// Usage: var result= await advGetContent(requestContent); /// /// Content of the request. advGetContent(requestContent, null, null); } async function advGetContent(requestContent, successFunction, failureFunction) { /// /// Calls the rest service to retrieve the data based on the requestContent object. /// /// Content of the request. /// The success function. /// The failure function. if (failureFunction === undefined || failureFunction === null) failureFunction = successFunction; var myHeaders = new Headers(); var requestUrl = '/advantageapi/content/get/'; if (requestContent.Authenticate) requestUrl = '/advantageapi/secure/content/get/'; requestUrl = requestUrl + requestContent.ApiName; if (requestContent.Language === null && requestContent.Language === '') requestContent.Language = 'default'; requestUrl += '/' + requestContent.Language; for (var key in requestContent.Header) myHeaders.append(key, requestContent.Header[key]); myHeaders.append('Content-Type', 'application/json'); if (requestContent.name === "AdvContentRequest") { if (requestContent.Host !== '') requestUrl = requestContent.Host + requestUrl; const theRequest = fetch(requestUrl, { method: requestContent.Action, body: JSON.stringify({ "Index": requestContent.Index, "Filter": requestContent.Filter, "SortList": requestContent.SortList, "FieldList": requestContent.FieldList, "MaxRecords": requestContent.MaxRecords, "SkipRecords": requestContent.SkipRecords }), ContentType: 'application/json', headers: myHeaders }); if (successFunction === undefined || successFunction === null) return await theRequest.then(response => response.text()) .then(data => advParseRequest(data, false, false)) .catch(err => advParseRequest(err, false, true)); else theRequest.then(response => response.text()) .then(function (data) { var result = advParseRequest(data, false, false); if (result.Error !== null) return failureFunction(result); else return successFunction(result); }) .catch(err => failureFunction(advParseRequest(err, false, true))); return void (0); } if (requestContent.name === "AdvContentRequestById") { requestUrl += '/' + requestContent.Id; if (requestContent.Host !== '') requestUrl = requestContent.Host + requestUrl; const theSingleRequest = fetch(requestUrl, { method: requestContent.Action, headers: myHeaders, ContentType: 'application/json' }); if (successFunction === undefined || successFunction === null) return await theSingleRequest.then(response => response.text()) .then(data => advParseRequest(data, true, false)) .catch(err => advParseRequest(err, true, true)); else theSingleRequest.then(response => response.text()) .then(function (data) { var result = advParseRequest(data, true, false); if (result.Error !== null) return failureFunction(result); else return successFunction(result); }) .catch(err => failureFunction(advParseRequest(err, true, true))); return void (0); } throw "Parameter must be of type AdvContentRequest or AdvContentRequestById"; } function advParseRequest(data, singleResult, forceError) { try { if (forceError) throw "forced"; var json = {}; if (singleResult) json.Data = JSON.parse(data); else json = JSON.parse(data); json.ResponseCode = 200; json.Error = null; return json; } catch (e) { var respCode = 500; if (data === "API action is not available on the non-secure channel") respCode = 403; if (data === "API object is unavailable") respCode = 404; var err = { "Error": data }; err.ResponseCode = respCode; err.Data = null; return err; } }