UNPKG

go-meow

Version:

A modular microservice template built with TypeScript, Express, and Prisma (MongoDB). It includes service scaffolding tools, consistent query utilities with data grouping, Zod validation, structured logging, comprehensive seeding system, and Swagger/OpenA

146 lines (145 loc) 5.14 kB
import { getLogger } from "../helper/logger"; const logger = getLogger(); const formDataLogger = logger.child({ module: "formDataHelper" }); const cleanStringValue = (value) => { let cleaned = value.trim(); if ((cleaned.startsWith('"') && cleaned.endsWith('"')) || (cleaned.startsWith("'") && cleaned.endsWith("'"))) { cleaned = cleaned.slice(1, -1); } return cleaned; }; const convertToNumber = (value, fieldName, debug = false) => { const cleaned = cleanStringValue(value); const numValue = parseFloat(cleaned); if (!isNaN(numValue)) { if (debug) { formDataLogger.info(`Converted ${fieldName} from "${value}" to number ${numValue}`); } return numValue; } if (debug) { formDataLogger.warn(`Failed to convert ${fieldName}: "${cleaned}" is not a valid number`); } return value; }; const convertToInteger = (value, fieldName, debug = false) => { const cleaned = cleanStringValue(value); const intValue = parseInt(cleaned, 10); if (!isNaN(intValue)) { if (debug) { formDataLogger.info(`Converted ${fieldName} from "${value}" to integer ${intValue}`); } return intValue; } if (debug) { formDataLogger.warn(`Failed to convert ${fieldName}: "${cleaned}" is not a valid integer`); } return value; }; const convertToBoolean = (value, fieldName, debug = false) => { const cleaned = cleanStringValue(value).toLowerCase(); if (cleaned === "true" || cleaned === "1" || cleaned === "yes" || cleaned === "on") { if (debug) { formDataLogger.info(`Converted ${fieldName} from "${value}" to boolean true`); } return true; } if (cleaned === "false" || cleaned === "0" || cleaned === "no" || cleaned === "off" || cleaned === "") { if (debug) { formDataLogger.info(`Converted ${fieldName} from "${value}" to boolean false`); } return false; } if (debug) { formDataLogger.warn(`Failed to convert ${fieldName}: "${cleaned}" is not a valid boolean`); } return value; }; const parseJsonField = (value, fieldName, debug = false) => { const cleaned = cleanStringValue(value); try { const parsed = JSON.parse(cleaned); if (debug) { formDataLogger.info(`Parsed ${fieldName} JSON successfully`); } return parsed; } catch (error) { if (debug) { formDataLogger.warn(`Failed to parse ${fieldName} as JSON: ${error}`); } return value; } }; export const convertFormDataTypes = (data, options = {}) => { const { numericFields = [], integerFields = [], jsonFields = [], booleanFields = [], debug = false, } = options; const converted = { ...data }; for (const fieldName of numericFields) { if (converted[fieldName] !== undefined && converted[fieldName] !== null && converted[fieldName] !== "" && typeof converted[fieldName] === "string") { converted[fieldName] = convertToNumber(converted[fieldName], fieldName, debug); } } for (const fieldName of integerFields) { if (converted[fieldName] !== undefined && converted[fieldName] !== null && converted[fieldName] !== "" && typeof converted[fieldName] === "string") { converted[fieldName] = convertToInteger(converted[fieldName], fieldName, debug); } } for (const fieldName of booleanFields) { if (converted[fieldName] !== undefined && converted[fieldName] !== null && typeof converted[fieldName] === "string") { converted[fieldName] = convertToBoolean(converted[fieldName], fieldName, debug); } } for (const fieldName of jsonFields) { if (converted[fieldName] && typeof converted[fieldName] === "string") { converted[fieldName] = parseJsonField(converted[fieldName], fieldName, debug); } } return converted; }; export const ConversionPresets = { facility: { stringFields: ["name", "type", "description", "organizationId", "facilityTypeId"], booleanFields: [], numericFields: [], jsonFields: ["location", "metadata"], }, organization: { numericFields: [], integerFields: [], jsonFields: ["branding"], booleanFields: [], }, user: { numericFields: ["age"], integerFields: [], jsonFields: ["preferences", "metadata"], booleanFields: ["isActive", "emailVerified"], }, }; export const convertWithPreset = (data, presetName, debug = false) => { const preset = ConversionPresets[presetName]; if (!preset) { throw new Error(`Unknown preset: ${presetName}`); } return convertFormDataTypes(data, { ...preset, debug }); }; export const filterDatabaseFields = (data, fieldsToRemove) => { const filtered = { ...data }; for (const field of fieldsToRemove) { delete filtered[field]; } return filtered; };