UNPKG

@cobuildlab/8base-utils

Version:

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

147 lines (146 loc) 5.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.report = exports.isEnvironment = exports.responseBuilder = exports.getMessageFromGraphQLError = exports._validateReferences = exports._validateReference = exports._validateFiles = exports._validateFile = exports._validateNullOrUndefinedOrBlank = void 0; var validation_utils_1 = require("@cobuildlab/validation-utils"); var ValidationError_1 = require("./error/ValidationError"); /* eslint-disable @typescript-eslint/no-explicit-any */ /** * Validates a value for null, undefined or blank. * * @param {any}value - The Value. * @param {string} errorPrefix - An error prefix. * @private */ exports._validateNullOrUndefinedOrBlank = function (value, errorPrefix) { if (value === '' || validation_utils_1.isNullOrUndefined(value)) { throw new ValidationError_1.ValidationError(errorPrefix + ": value: can't be blank, null or undefined."); } }; /** * Validates that an object has a valid fileId property. * * @param {object}file - The object to be validated. * @param errorPrefix - An error prefix for the Error. * @private */ exports._validateFile = function (file, errorPrefix) { if (typeof file !== 'object') throw new ValidationError_1.ValidationError(errorPrefix + ": file is not a valid object."); if (validation_utils_1.isNullOrUndefined(file.fileId)) throw new ValidationError_1.ValidationError(errorPrefix + ": object is not a valid file as it doesn't contain a valid fileId property."); if (validation_utils_1.isNullOrUndefined(file.filename)) throw new ValidationError_1.ValidationError(errorPrefix + ": object is not a valid file as it doesn't contain a valid filename property."); }; /** * Validates that an object is a valid list of objects with fileId properties. * * @param {Array}files - The objects to be validated. * @param errorPrefix - An error prefix for the Error. * @private */ exports._validateFiles = function (files, errorPrefix) { if (!Array.isArray(files)) throw new ValidationError_1.ValidationError(errorPrefix + ": object is not a List."); for (var _i = 0, files_1 = files; _i < files_1.length; _i++) { var file = files_1[_i]; exports._validateFile(file, errorPrefix); } }; /** * Validates that an object has a valid id property. * * @param {object}obj - The object to be validated. * @param errorPrefix - An error prefix for the Error. * @private */ exports._validateReference = function (obj, errorPrefix) { if (typeof obj !== 'object' || validation_utils_1.isNullOrUndefined(obj.id)) throw new ValidationError_1.ValidationError(errorPrefix + ": object is not a valid reference as it doesn't contain a valid id property."); }; /** * Validates that an object is a valid list of objects with id properties. * * @param {Array}objs - The objects to be validated. * @param errorPrefix - An error prefix for the Error. * @private */ exports._validateReferences = function (objs, errorPrefix) { if (!Array.isArray(objs)) throw new ValidationError_1.ValidationError(errorPrefix + ": object is not a List."); for (var _i = 0, objs_1 = objs; _i < objs_1.length; _i++) { var obj = objs_1[_i]; exports._validateReference(obj, errorPrefix); } }; /** * Parses the GraphQL error to JSON to get its message. * If a GraphQL message is not found, it will return the error's message instead. * * @param {Error} error - The graphql error. * @returns {string} The error with the parsed message. */ function getMessageFromGraphQLError(error) { var message = error.message; var jsonStartIndex = message.indexOf(' {'); var jsonEndIndex = message.lastIndexOf('}') + 1; var jsonBody = message.substring(jsonStartIndex, jsonEndIndex); var jsonError; try { jsonError = JSON.parse(jsonBody); } catch (e) { console.error('getMessageFromGraphQLError Error: ', e); return message; } var jsonMessage = jsonError.message, raw = jsonError.raw; if (jsonMessage) { return jsonMessage; } if (raw && raw.message) { return raw.message; } // Return original message. return message; } exports.getMessageFromGraphQLError = getMessageFromGraphQLError; /** * Http response builder. * * @param code - Http status code. * @param message - Message to response. * @param headers - Http headers. * @returns Response object. */ exports.responseBuilder = function (code, message, headers) { if (code === void 0) { code = 200; } var bodyData = typeof message === 'string' ? { message: message } : message; return { body: JSON.stringify(bodyData), statusCode: code, headers: headers !== null && headers !== void 0 ? headers : {}, }; }; /** * Checks that the current environment is the one given. * * @param env - The environment in which code is running, production|development|test. * * @returns Result of comparing the equality between the given env and the current NODE_ENV. */ exports.isEnvironment = function (env) { return process.env.NODE_ENV === env; }; /** * Logs the error in Cloud Function. * * @param title - Title of the error to. * @param err - The error object thrown. * @param data - Data to identify the function state in the moment of the error. */ exports.report = function (title, err, data) { if (data === void 0) { data = {}; } console.log(title); console.log(JSON.stringify(data, null, 2)); console.log(JSON.stringify(err)); console.log(err.message); };