UNPKG

@cobuildlab/8base-utils

Version:

This is package to deal with common scenarios working with 8base platform

40 lines (39 loc) 1.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.normalize8baseReferenceConnect = void 0; var utils_1 = require("./utils"); var ValidationError_1 = require("./error/ValidationError"); /* eslint-disable @typescript-eslint/no-explicit-any */ /** * Helper to change non null keys to 8base 'connect' reference. * WARNING: This function mutates the data. * WARNING: This functions assumes that all 8base keys are strings. * * @param {object} data - The Object to be Mutated. * @param {string} key - The key in the Object. */ exports.normalize8baseReferenceConnect = function (data, key) { utils_1._validateNullOrUndefinedOrBlank(key, 'normalize8baseReferenceConnect:key'); var currentValue = data[key]; if (currentValue === null || currentValue === '') { delete data[key]; return; } // If currentValue is an string we assume that this is the ID for the connect if (typeof currentValue === 'string') { data[key] = { connect: { id: currentValue } }; return; } if (typeof currentValue === 'object') { var currentId = currentValue.id; if (currentId === null || currentId === undefined || typeof currentId !== 'string' || currentId === '') { throw new ValidationError_1.ValidationError("normalize8baseReferenceConnect: the value on data of the key: '" + key + "' is not a string, instead is: '" + currentId + "'"); } data[key] = { connect: { id: currentId } }; return; } throw new ValidationError_1.ValidationError("normalize8baseReferenceConnect: '" + key + "' in data is not a 'string' or a 'object'."); };