UNPKG

@cognigy/rest-api-client

Version:

Cognigy REST-Client

117 lines 3.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseMapper = void 0; class BaseMapper { constructor(endpointType) { this.isValidUrl = (url) => { const regex = new RegExp("^(http|https)://"); return regex.test(url); }; this.isValidPhoneNumber = (number) => { const regex = /^\+{0,2}([\-\. ])?(\(?\d{0,3}\))?([\-\. ])?\(?\d{0,3}\)?([\-\. ])?\d{3}([\-\. ])?\d{4}/; return regex.test(number); }; this.endpointType = endpointType; } handleInput(input, api, isGenericNode) { } handleVGInput(input, api) { } handleAudioCodesInput(input, api) { } stripNulls(data) { Object.entries(data).forEach(([key, value]) => { if (typeof value === "undefined" || typeof value === null || (typeof value === "string" && value.length == 0)) { delete data[key]; } if (typeof value === "object" && (typeof value !== "number" || typeof value !== "string")) { this.stripNulls(value); } if (typeof value === "object" && (typeof value !== "number" || typeof value !== "string" || typeof value !== "boolean") && Object.keys(!value).length === 0) { delete data[key]; } }); return data; } buildPayload(payload) { switch (this.endpointType) { case "bandwidth": { if (!Array.isArray(payload)) { payload = [payload]; } return { _bandwidth: { json: { activities: payload } } }; } ; case "audioCodes": if (!Array.isArray(payload)) { payload = [payload]; } return { _voiceGateway: { json: { activities: payload } } }; case "voiceGateway2": default: return { _voiceGateway2: { json: payload } }; } } isValidDTMF(dtmf) { if (dtmf.length !== 1) return false; const regex = new RegExp("^[0-9#*abcdABCD]+$"); return regex.test(dtmf); } isValidJSON(json) { try { if (json && typeof json === "object" && Object.entries(json).length && !Array.isArray(json)) { for (const value of Object.values(json)) { if (!["string", "number", "boolean"].includes(typeof value)) { return false; } } } return true; } catch (error) { return false; } } has(data) { if (typeof data === "string") { return data && data.length > 0; } else if (Array.isArray(data)) { return data && data.length > 0; } else if (typeof data === "object") { return data && Object.keys(data).length > 0; } else if (typeof data === "number") { return data !== null; } else if (typeof data === "boolean") { return data !== null; } else { return false; } } } exports.BaseMapper = BaseMapper; //# sourceMappingURL=base.mapper.js.map