@zohocrm/typescript-sdk-6.0
Version:
TypeScript SDK for Zoho CRM
332 lines (331 loc) • 17.5 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Converter = void 0;
const sdk_exception_1 = require("../../core/com/zoho/crm/api/exception/sdk_exception");
const initializer_1 = require("../../routes/initializer");
const choice_1 = require("./choice");
const constants_1 = require("./constants");
const path_1 = __importDefault(require("path"));
const oauth_token_1 = require("../../models/authenticator/oauth_token");
/**
* This class is to construct API request and response.
*/
class Converter {
/**
* Creates a Converter class instance with the CommonAPIHandler class instance.
* @param {CommonAPIHandler} commonAPIHandler - A CommonAPIHandler class instance.
*/
constructor(commonAPIHandler) {
this.commonAPIHandler = commonAPIHandler;
}
/**
* This method is to validate if the input values satisfy the constraints for the respective fields.
* @param {string} className - A String containing the class name.
* @param {string} memberName - A String containing the member name.
* @param {object} keyDetails - A JSONObject containing the key JSON details.
* @param {object} value - A Object containing the key value.
* @param {Map} uniqueValuesMap - A Map containing the value of constructed object's unique fields.
* @param {int} instanceNumber - An Integer containing the class instance list number.
* @returns A Boolean representing the key value is expected pattern, unique, length, and values.
* @throws {SDKException}
*/
async valueChecker(className, memberName, keyDetails, value, uniqueValuesMap, instanceNumber) {
let detailsJO = {};
var name = keyDetails[constants_1.Constants.NAME];
var type = keyDetails[constants_1.Constants.TYPE];
var valueType = Object.prototype.toString.call(value);
let check = true;
let givenType = null;
if (keyDetails[constants_1.Constants.INTERFACE]) {
let interfaceDetail = initializer_1.Initializer.jsonDetails[keyDetails[constants_1.Constants.STRUCTURE_NAME]];
let classes = interfaceDetail[constants_1.Constants.CLASSES];
check = false;
if (classes != null) {
for (let i = 0; i < classes.length; i++) {
let className = (await Promise.resolve(`${"../../" + classes[i]}`).then(s => __importStar(require(s)))).MasterModel;
if (value instanceof className) {
check = true;
}
}
}
}
if (constants_1.Constants.TYPE_VS_DATATYPE.has(type.toLowerCase())) {
if (Array.isArray(value) && keyDetails.hasOwnProperty(constants_1.Constants.STRUCTURE_NAME)) {
let structureName = keyDetails[constants_1.Constants.STRUCTURE_NAME];
let index = 0;
let className = (await Promise.resolve(`${"../../" + structureName}`).then(s => __importStar(require(s)))).MasterModel;
for (let data of value) {
if (!(data instanceof className)) {
check = false;
instanceNumber = index;
let baseName = structureName.split("/").pop();
let classNameSplit = baseName.split("_");
let expectedClassName = "";
for (var nameIndex = 0; nameIndex < classNameSplit.length; nameIndex++) {
var fieldName = classNameSplit[nameIndex];
var firstLetterUppercase = fieldName[0].toUpperCase() + fieldName.slice(1);
expectedClassName = expectedClassName.concat(firstLetterUppercase);
}
type = constants_1.Constants.ARRAY_KEY + "(" + expectedClassName + ")";
givenType = constants_1.Constants.ARRAY_KEY + "(" + data.constructor.name + ")";
break;
}
index = index + 1;
}
}
else if (Array.isArray(value) && keyDetails.hasOwnProperty(constants_1.Constants.SUB_TYPE)) {
let index = 0;
for (let data of value) {
let subType = keyDetails[constants_1.Constants.SUB_TYPE];
let sub_type = subType[constants_1.Constants.TYPE];
let dataType;
if (sub_type.toLowerCase() == constants_1.Constants.OBJECT_KEY) {
check = true;
}
else {
dataType = constants_1.Constants.SPECIAL_TYPES.has(sub_type.toLowerCase()) ? constants_1.Constants.SPECIAL_TYPES.get(sub_type.toLowerCase()) : sub_type;
if (constants_1.Constants.TYPE_VS_DATATYPE.has(dataType.toLowerCase())) {
if (Object.prototype.toString.call(data) != constants_1.Constants.TYPE_VS_DATATYPE.get(sub_type.toLowerCase())) {
check = false;
}
}
}
if (!check) {
instanceNumber = index;
type = constants_1.Constants.ARRAY_KEY + "(" + dataType + ")";
givenType = constants_1.Constants.ARRAY_KEY + "(" + Object.getPrototypeOf(data).constructor.name + ")";
break;
}
index = index + 1;
}
}
else if (value != null) { //TypeScript don't have int type
check = (valueType != constants_1.Constants.TYPE_VS_DATATYPE.get(type.toLowerCase()) ? false : true);
givenType = Object.getPrototypeOf(value).constructor.name;
}
}
else if (value != null && type.toLowerCase() !== constants_1.Constants.OBJECT_KEY) {
let expectedStructure = keyDetails[constants_1.Constants.TYPE];
if (expectedStructure == "TimeZone") {
check = value.hasOwnProperty("name");
}
else {
if (!keyDetails[constants_1.Constants.INTERFACE]) {
let className = (await Promise.resolve(`${"../../" + expectedStructure}`).then(s => __importStar(require(s)))).MasterModel;
if (!(value instanceof className)) {
check = false;
type = expectedStructure;
givenType = value.constructor.name;
}
if (check == false) {
if (givenType.toLowerCase() == constants_1.Constants.INVENTORYTEMPLATE && type.toLowerCase() == constants_1.Constants.TEMPLATE) {
check = true;
}
}
}
}
}
if (!check) {
detailsJO[constants_1.Constants.ERROR_HASH_FIELD] = name;
detailsJO[constants_1.Constants.ERROR_HASH_CLASS] = className;
detailsJO[constants_1.Constants.ACCEPTED_TYPE] = constants_1.Constants.SPECIAL_TYPES.has(type) ? constants_1.Constants.SPECIAL_TYPES.get(type) : type;
detailsJO[constants_1.Constants.GIVEN_TYPE] = givenType;
if (instanceNumber != null) {
detailsJO[constants_1.Constants.INDEX] = instanceNumber;
}
throw new sdk_exception_1.SDKException(constants_1.Constants.TYPE_ERROR, null, detailsJO);
}
let initializer = await initializer_1.Initializer.getInitializer().catch((err) => { throw err; });
if (initializer == null || initializer == undefined) {
throw new sdk_exception_1.SDKException(constants_1.Constants.SDK_UNINITIALIZATION_ERROR, constants_1.Constants.SDK_UNINITIALIZATION_MESSAGE);
}
if (keyDetails.hasOwnProperty(constants_1.Constants.VALUES) && (!keyDetails.hasOwnProperty(constants_1.Constants.PICKLIST) || (keyDetails[constants_1.Constants.PICKLIST] && initializer.getSDKConfig().getPickListValidation() == true))) {
let valuesJA = keyDetails[constants_1.Constants.VALUES];
if (Array.isArray(value)) {
let value_1 = value;
for (let value_2 of value_1) {
if (value_2 instanceof choice_1.Choice) {
let choice = value_2;
value_2 = choice.getValue();
}
if (!valuesJA.includes(value_2)) {
detailsJO[constants_1.Constants.ERROR_HASH_FIELD] = memberName;
detailsJO[constants_1.Constants.ERROR_HASH_CLASS] = className;
if (instanceNumber != null) {
detailsJO[constants_1.Constants.INDEX] = instanceNumber;
}
detailsJO[constants_1.Constants.GIVEN_VALUE] = value;
detailsJO[constants_1.Constants.ACCEPTED_VALUES] = valuesJA;
throw new sdk_exception_1.SDKException(constants_1.Constants.UNACCEPTED_VALUES_ERROR, null, detailsJO);
}
}
}
else {
if (value instanceof choice_1.Choice) {
value = value.getValue();
}
if (!valuesJA.includes(value)) {
detailsJO[constants_1.Constants.ERROR_HASH_FIELD] = memberName;
detailsJO[constants_1.Constants.ERROR_HASH_CLASS] = className;
if (instanceNumber != null) {
detailsJO[constants_1.Constants.INDEX] = instanceNumber;
}
detailsJO[constants_1.Constants.GIVEN_VALUE] = value;
detailsJO[constants_1.Constants.ACCEPTED_VALUES] = valuesJA;
throw new sdk_exception_1.SDKException(constants_1.Constants.UNACCEPTED_VALUES_ERROR, null, detailsJO);
}
}
}
if (keyDetails.hasOwnProperty(constants_1.Constants.UNIQUE)) {
let valuesArray = uniqueValuesMap.get(name);
if (valuesArray !== undefined && valuesArray.includes(value)) {
detailsJO[constants_1.Constants.ERROR_HASH_FIELD] = memberName;
detailsJO[constants_1.Constants.ERROR_HASH_CLASS] = className;
detailsJO[constants_1.Constants.FIRST_INDEX] = valuesArray.indexOf(value);
detailsJO[constants_1.Constants.NEXT_INDEX] = instanceNumber;
throw new sdk_exception_1.SDKException(constants_1.Constants.UNIQUE_KEY_ERROR, null, detailsJO);
}
else {
if (valuesArray === undefined) {
valuesArray = [];
}
valuesArray.push(value);
uniqueValuesMap.set(name, valuesArray);
}
}
if (keyDetails.hasOwnProperty(constants_1.Constants.MIN_LENGTH) || keyDetails.hasOwnProperty(constants_1.Constants.MAX_LENGTH)) {
let count = value.toString().length;
if (Array.isArray(value)) {
count = value.length;
}
if (keyDetails.hasOwnProperty(constants_1.Constants.MAX_LENGTH) && (count > keyDetails[constants_1.Constants.MAX_LENGTH])) {
detailsJO[constants_1.Constants.ERROR_HASH_FIELD] = memberName;
detailsJO[constants_1.Constants.ERROR_HASH_CLASS] = className;
detailsJO[constants_1.Constants.GIVEN_LENGTH] = count;
detailsJO[constants_1.Constants.ERROR_HASH_MAXIMUM_LENGTH] = keyDetails[constants_1.Constants.MAX_LENGTH];
throw new sdk_exception_1.SDKException(constants_1.Constants.MAXIMUM_LENGTH_ERROR, null, detailsJO);
}
if (keyDetails.hasOwnProperty(constants_1.Constants.MIN_LENGTH) && count < keyDetails[constants_1.Constants.MIN_LENGTH]) {
detailsJO[constants_1.Constants.ERROR_HASH_FIELD] = memberName;
detailsJO[constants_1.Constants.ERROR_HASH_CLASS] = className;
detailsJO[constants_1.Constants.GIVEN_LENGTH] = count;
detailsJO[constants_1.Constants.ERROR_HASH_MINIMUM_LENGTH] = keyDetails[constants_1.Constants.MIN_LENGTH];
throw new sdk_exception_1.SDKException(constants_1.Constants.MINIMUM_LENGTH_ERROR, null, detailsJO);
}
}
return true;
}
/**
* getEncodedFileName
*/
static async getEncodedFileName() {
let initializer = await initializer_1.Initializer.getInitializer().catch((err) => { throw err; });
if (initializer == null || initializer == undefined) {
throw new sdk_exception_1.SDKException(constants_1.Constants.SDK_UNINITIALIZATION_ERROR, constants_1.Constants.SDK_UNINITIALIZATION_MESSAGE);
}
let token = initializer.getToken();
let accessToken = "";
let refreshToken = "";
let tokenKey = "";
if (token instanceof oauth_token_1.OAuthToken) {
let user = token.getUserSignature();
if (user != null) {
tokenKey = user.getName();
}
else {
refreshToken = token.getRefreshToken();
if (refreshToken != null && refreshToken.length > 0) {
tokenKey = refreshToken.substring(refreshToken.length - 32);
}
else {
accessToken = token.getAccessToken();
if (accessToken != null && accessToken.length > 0) {
tokenKey = accessToken.substring(accessToken.length - 32);
}
}
}
}
let fileName = initializer.getEnvironment().getUrl();
if (tokenKey != null && tokenKey.length > 0) {
fileName = fileName + tokenKey;
}
var input = this.toUTF8Array(fileName);
var str = Buffer.from(input).toString('base64');
let filepath = path_1.default.join(initializer.getResourcePath(), constants_1.Constants.FIELD_DETAILS_DIRECTORY);
return path_1.default.join(filepath, str + ".json");
}
static toUTF8Array(str) {
var utf8 = [];
for (var i = 0; i < str.length; i++) {
var charcode = str.charCodeAt(i);
if (charcode < 0x80)
utf8.push(charcode);
else if (charcode < 0x800) {
utf8.push(0xc0 | (charcode >> 6), 0x80 | (charcode & 0x3f));
}
else if (charcode < 0xd800 || charcode >= 0xe000) {
utf8.push(0xe0 | (charcode >> 12), 0x80 | ((charcode >> 6) & 0x3f), 0x80 | (charcode & 0x3f));
}
else {
i++;
// UTF-16 encodes 0x10000-0x10FFFF by
// subtracting 0x10000 and splitting the
// 20 bits of 0x0-0xFFFFF into two halves
charcode = 0x10000 + (((charcode & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff));
utf8.push(0xf0 | (charcode >> 18), 0x80 | ((charcode >> 12) & 0x3f), 0x80 | ((charcode >> 6) & 0x3f), 0x80 | (charcode & 0x3f));
}
}
return utf8;
}
async moduleToClass(moduleName) {
let className = moduleName;
if (moduleName.includes("_")) {
className = '';
let moduleSplit = moduleName.split("_");
for (let eachName in moduleSplit) {
eachName = eachName.charAt(0).toUpperCase() + eachName.slice(1);
className = className + eachName;
}
}
else {
className = moduleName.charAt(0).toUpperCase() + className.slice(1);
}
return className;
}
async getFileName(name) {
let fileName = [];
let nameParts = name.split(/([A-Z][a-z]+)/).filter(function (e) { return e; });
fileName.push(nameParts[0].toLowerCase());
for (let i = 1; i < nameParts.length; i++) {
fileName.push(nameParts[i].toLowerCase());
}
return fileName.join("_");
}
}
exports.Converter = Converter;
;