UNPKG

unomi-sdk-node

Version:

Node module to interact with unomi.

294 lines (293 loc) 12.8 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.callResponse = exports.callElasticsearch = exports.callUnomi = void 0; const helpers_1 = require("./helpers"); /** * @function callUnomi * @param {string} method * @param {string} url * @param {object} body * @param {Record<string, string>} headers * @param {string} successStatus * @param {Array<object>} errors * @returns {FilteredResponse} */ function callUnomi(method, url, body, headers, successStatus, errors) { return __awaiter(this, void 0, void 0, function* () { try { if (errors) { // check if there is an error code return { statusCode: 500, errorMessage: "Validation error.", errors: errors, exception: null, response: { method: method, url: url, body: body, successStatus: successStatus } }; } let response; // API call response if (body === null) { // check if API call has no body response = yield fetch(url, { method: method, headers: headers, credentials: "omit", mode: 'cors', }); } else { // API call has a body response = yield fetch(url, { method: method, headers: headers, body: JSON.stringify(body), credentials: "omit", mode: 'cors', }); } let data = new Object; // object for response data if (response.status != 204) { // check if status code is not "No Content success status" data = yield response.json(); // get response object from unomi } if (response.status === successStatus) { // check if status code from response is the expected status code of unomi return { statusCode: response.status, responseData: data }; } if (response.status === 204 && successStatus === 200 && url.includes("/cxs/segments/")) { // check if we are trying to get a segment that doesn't exist return { statusCode: response.status, errorMessage: "This segment does not exist.", errors: [], exception: null, response: response }; } return { statusCode: response.status, errorMessage: "Something went wrong processing this request. Please try again later or contact support if this error persists.", errors: [], exception: null, response: response }; } catch (err) { // error from unomi-sdk side return { statusCode: 500, errorMessage: "Something went wrong processing this request. Please try again later or contact support if this error persists.", errors: [], exception: err.stack, response: { method: method, url: url, body: body, successStatus: successStatus } }; } }); } exports.callUnomi = callUnomi; /** * @function callElasticsearch * @param {string} method * @param {string} url * @param {object} body * @param {Record<string, string>} headers * @param {number} successStatus * @param {Array<object>} errors * @returns {FilteredResponse} */ function callElasticsearch(method, url, body, headers, successStatus, errors) { return __awaiter(this, void 0, void 0, function* () { try { if (errors) { // check if there is an error code return { statusCode: 500, errorMessage: "Validation error.", errors: errors, exception: null, response: { method: method, url: url, body: body, successStatus: successStatus } }; } let response; // API call response if (body === null) { // check if API call has no body response = yield fetch(url, { method: method, headers: headers, credentials: "omit", mode: 'cors', }); } else { // API call has a body response = yield fetch(url, { method: method, headers: headers, body: JSON.stringify(body), credentials: "omit", mode: 'cors', }); } if (response.status === successStatus) { // check if status code from response is the expected status code of elasticsearch let data = yield response.json(); // get response object from elasticsearch if (url.endsWith("/context-profile")) { // if we are getting the profile properties let responseData = data["context-profile"]["mappings"]["properties"]["properties"]["properties"]; // get profile properties let flattenedResponseData = helpers_1.flattenObject(responseData); // flatten profile properties let temporary_filter = ["jsonld", "mediaCapabilities", "metatags", "microdata", "rdfa"]; // filter for properties which might return in later release let newFlattenedResponseData = {}; // create empty object for (var property in flattenedResponseData) { // iterate over object if (!property.endsWith(".analyzer")) { // if key does not end with .analyzer if (!helpers_1.isItemInList(temporary_filter, property)) { // if key does not contain a word from the temporary filter list | second if-structure so .analyzer won't get checked (improve performance) newFlattenedResponseData[property] = flattenedResponseData[property]; // add key and value to new object } } } return { statusCode: response.status, responseData: Object.keys(newFlattenedResponseData) }; } else if (url.endsWith("/context-profile/_count")) { // if we are getting the total amount of profiles return { statusCode: response.status, responseData: data.count }; } else { return { statusCode: response.status, responseData: { "message": "We don't support this endpoint yet." } }; } } return { statusCode: response.status, errorMessage: "Something went wrong processing this request. Please try again later or contact support if this error persists.", errors: [], exception: null, response: response }; } catch (err) { // error from unomi-sdk side return { statusCode: 500, errorMessage: "Something went wrong processing this request. Please try again later or contact support if this error persists.", errors: [], exception: err.stack, response: { method: method, url: url, body: null, successStatus: successStatus } }; } }); } exports.callElasticsearch = callElasticsearch; /** * @function callResponse * @param {string} method * @param {string} url * @param {any} body * @param {string} successStatus * @returns {FilteredResponse} */ function callResponse(method, url, body, successStatus) { return __awaiter(this, void 0, void 0, function* () { try { return { statusCode: successStatus, responseData: body // list of the keys of the data object }; } catch (err) { // error from unomi-sdk side return { statusCode: 500, errorMessage: "Something went wrong processing this request. Please try again later or contact support if this error persists.", errors: [], exception: err.stack, response: { method: method, url: url, body: null, successStatus: successStatus } }; } }); } exports.callResponse = callResponse; // var flattenObject = function (ob: any) { // flatten given object // var toReturn: { [key: string]: string } = {} // result object // for (var i in ob) { // go through object // if (!ob.hasOwnProperty(i)) continue; // // if ((typeof ob[i]) == 'object') { // check if property is an object // if (i != 'fields') { // check if object key is not "fields" // var a = ob[i]; // // if (ob[i].hasOwnProperty('properties')) { // // a = ob[i]['properties'] // assign value to a // } // var flatObject = flattenObject(a); // // for (var x in flatObject) { // go through object // if (!flatObject.hasOwnProperty(x)) continue; // // if (x != 'type') { // check if object key is not "type" // toReturn[i + '.' + x] = flatObject[x]; // // } // else { // object key is "type" // toReturn[i] = flatObject[x]; // // } // } // } // } else { // property is not an object // toReturn[i] = ob[i]; // add property to result object // } // } // return toReturn; // return result object // }; // export function flattenObject(ob: any): any { // flatten given object // var toReturn: { [key: string]: string } = {} // result object // for (var i in ob) { // go through object // if (!ob.hasOwnProperty(i)) continue; // // if ((typeof ob[i]) == 'object') { // check if property is an object // if (i != 'fields') { // check if object key is not "fields" // var a = ob[i]; // // if (ob[i].hasOwnProperty('properties')) { // // a = ob[i]['properties'] // assign value to a // } // var flatObject = flattenObject(a); // // for (var x in flatObject) { // go through object // if (!flatObject.hasOwnProperty(x)) continue; // // if (x != 'type') { // check if object key is not "type" // toReturn[i + '.' + x] = flatObject[x]; // // } // else { // object key is "type" // toReturn[i] = flatObject[x]; // // } // } // } // } else { // property is not an object // toReturn[i] = ob[i]; // add property to result object // } // } // return toReturn; // return result object // };