unomi-node
Version:
Node.js SDK for the Apache Unomi CDP
170 lines (169 loc) • 5.85 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var querystring_1 = require("querystring");
var index_1 = require("../utils/index");
var profileGetByProperty_1 = require("../queryBuilder/profileGetByProperty");
var defaultProperties = {
consents: {},
itemId: undefined,
itemType: "profile",
mergedWith: null,
properties: undefined,
scores: {},
segments: [],
systemProperties: {},
version: null
};
/**
* @function create
* @param {AxiosInterface} axios
* @param {CreateProperties} properties
* @returns {FilteredResponse}
*/
function create(axios, properties) {
var requiredProperties = ["itemId", "properties"];
var propsValidation = index_1.validateRequiredProps(requiredProperties, properties);
if (!propsValidation.valid) {
throw new Error("The following properties are missing, null or undefined: " + propsValidation.missing.join(','));
}
return index_1.callUnomi(function () { return axios.post("/cxs/profiles", __assign({}, defaultProperties, properties)); });
}
exports.create = create;
/**
* @function get
* @param {AxiosInterface} axios
* @param {string} profileId
* @returns {FilteredResponse}
*/
function get(axios, profileId) {
if (!profileId) {
throw new Error("profileId argument is missing, null or undefined.");
}
return index_1.callUnomi(function () { return axios.get("/cxs/profiles/" + profileId); });
}
exports.get = get;
/**
* @function delete
* @param {AxiosInterface} axios
* @param {string} profileId
* @returns {FilteredResponse}
*/
function deleteProfile(axios, profileId) {
if (!profileId) {
throw new Error("profileId argument is missing, null or undefined.");
}
return index_1.callUnomi(function () { return axios.delete("/cxs/profiles/" + profileId); }, "deleteProfile");
}
exports.deleteProfile = deleteProfile;
/**
* @function count
* @param {AxiosInterface} axios
* @returns {FilteredResponse}
*/
function count(axios) {
return index_1.callUnomi(function () { return axios.get("/cxs/profiles/count"); });
}
exports.count = count;
/**
* @function existingProperties
* @param {AxiosInterface} axios
* @param {ExistingProperties} params
* @returns {FilteredResponse}
*/
function existingProperties(axios, params) {
var requiredProperties = ["tag", "itemType"];
var propsValidation = index_1.validateRequiredProps(requiredProperties, params);
if (!propsValidation.valid) {
throw new Error("The following properties are missing, null or undefined: " + propsValidation.missing.join(','));
}
return index_1.callUnomi(function () { return axios.get("/cxs/profiles/existingProperties?" + querystring_1.stringify(__assign({}, params))); });
}
exports.existingProperties = existingProperties;
/**
* @function allProperties
* @param {AxiosInterface} axios
* @returns {FilteredResponse}
*/
function allProperties(axios) {
return index_1.callUnomi(function () { return axios.get("/cxs/profiles/properties"); });
}
exports.allProperties = allProperties;
/**
* @function sessions
* @param {AxiosInstance} axios
* @param {FilteredResponse} profileId
* @returns {FilteredResponse}
*/
function sessions(axios, profileId) {
if (!profileId) {
throw new Error("Profile ID is not valid. Received: " + profileId);
}
return index_1.callUnomi(function () { return axios.get("/cxs/profiles/" + profileId + "/sessions"); });
}
exports.sessions = sessions;
/**
* @function getByProperty
* @param {AxiosInstance} axios
* @param {GetByProperty} params
* @returns {FilteredResponse}
*/
function getBySingleProperty(axios, params) {
var requiredProperties = ["query", "limit"];
var propsValidation = index_1.validateRequiredProps(requiredProperties, params);
if (!propsValidation.valid) {
throw new Error("The following properties are missing, null or undefined: " + propsValidation.missing.join(','));
}
var query = profileGetByProperty_1.queryBuilder(params.query);
var queryparam = {
offset: params.offset || 0,
limit: params.limit || 100,
condition: {
type: "profilePropertyCondition",
parameterValues: {
propertyName: "properties." + query.key,
comparisonOperator: query.operator,
propertyValue: query.value
}
},
forceRefresh: params.forceRefresh || false
};
return index_1.callUnomi(function () { return axios.post("cxs/profiles/search", queryparam); });
}
exports.getBySingleProperty = getBySingleProperty;
function query(axios, params, query) {
var subConditions = query.map(function (queryCond) {
return {
type: "profilePropertyCondition",
parameterValues: {
propertyName: queryCond.prop,
comparisonOperator: queryCond.operator,
propertyValue: queryCond.value
}
};
});
var fullQuery = {
offset: params.offset || 0,
limit: params.limit || 100,
condition: {
type: "booleanCondition",
parameterValues: {
operator: params.operator || "and",
subConditions: subConditions
}
},
forceRefresh: params.forceRefresh || true
};
return index_1.callUnomi(function () { return axios.post("cxs/profiles/search", fullQuery); });
}
exports.query = query;