"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { cleanDynamoData: () => cleanDynamoData, end: () => end, endWithError: () => endWithError, extractDynamoValue: () => extractDynamoValue, getAuthContext: () => getAuthContext, getBearerToken: () => getBearerToken, getCleanedDynamoItems: () => getCleanedDynamoItems, getCorrelationId: () => getCorrelationId, getInputBody: () => getInputBody, getMetadata: () => getMetadata, getPathParameters: () => getPathParameters, getQueryStringParameters: () => getQueryStringParameters, handleCors: () => handleCors, isJsonString: () => isJsonString, mapToDynamoAttribute: () => mapToDynamoAttribute, startLogger: () => startLogger }); module.exports = __toCommonJS(src_exports); // src/functions/end.ts function end(statusCode = 201, message, headers) { const contentType = !message || typeof message === "number" || typeof message === "string" ? "text/plain" : "application/json"; const body = (() => { if (typeof message === "string") return message; return JSON.stringify(message); })(); if (!headers) headers = { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS", "Access-Control-Allow-Headers": "Content-Type, Authorization", "Access-Control-Allow-Credentials": true, "Content-Type": contentType }; process.env.CORRELATION_ID = ""; return { statusCode, body, headers }; } // src/functions/endWithError.ts var import_mikrolog = require("mikrolog"); function endWithError(error, defaultErrorCode = 400, headers) { const statusCode = error?.cause?.statusCode || defaultErrorCode; const message = error.message || ""; const logger = import_mikrolog.MikroLog.start(); logger.error(error.message); return end(statusCode, message, headers); } // src/functions/getAuthContext.ts function getAuthContext(event) { return event?.requestContext?.authorizer?.lambda || {}; } // src/functions/getBearerToken.ts function getBearerToken(headers) { const authorization = headers.Authorization || headers.authorization; if (!authorization || !authorization.startsWith("Bearer ")) return null; return authorization.replace("Bearer ", ""); } // src/functions/getCleanedDynamoItems.ts function getCleanedDynamoItems(items, includePkSk = false) { if (!items) return []; const normalizedItems = Array.isArray(items) ? items : [items]; return normalizedItems.map( (item) => cleanDynamoData(item, includePkSk) ); } function cleanDynamoData(data, includePkSk = false) { const cleanData = {}; for (const key in data) { if (includePkSk || key !== "pk" && key !== "sk") cleanData[key] = extractDynamoValue(data[key]); } for (const key in cleanData) { if (typeof cleanData[key] === "string" && (cleanData[key].startsWith("{") || cleanData[key].startsWith("["))) { try { cleanData[key] = JSON.parse(cleanData[key]); } catch { } } } return cleanData; } function extractDynamoValue(value) { if (!value) return null; if (value.S) return value.S; if (value.N) return Number(value.N); if (value.B) return value.B; if (value.SS) return value.SS; if (value.NS) return value.NS.map((n) => Number(n)); if (value.BS) return value.BS; if (value.L) return value.L.map((item) => extractDynamoValue(item)); if (value.M) { return Object.entries(value.M).reduce( (acc, [k, v]) => { acc[k] = extractDynamoValue(v); return acc; }, {} ); } if (value.NULL) return null; if (value.BOOL) return value.BOOL; return null; } // src/functions/getCorrelationId.ts function getCorrelationId(event, context) { return process.env.CORRELATION_ID || event?.headers?.["x-correlation-id"] || event?.headers?.["X-Correlation-Id"] || event?.detail?.metadata?.correlationId || event?.detail?.metadata?.correlation_id || context?.awsRequestId || ""; } // src/functions/getInputBody.ts function getInputBody(event) { if (event?.detail) return event.detail?.data ? event.detail.data : event.detail; if (event.isBase64Encoded) return JSON.parse(Buffer.from(event.body, "base64").toString("utf-8")); return event?.body && typeof event?.body === "string" ? JSON.parse(event.body) : event.body; } // src/functions/getMetadata.ts function getMetadata(event) { return event?.detail?.metadata; } // src/functions/getPathParameters.ts function getPathParameters(event) { return event?.pathParameters || {}; } // src/functions/getQueryStringParameters.ts function getQueryStringParameters(event) { return event?.queryStringParameters || {}; } // src/functions/handleCors.ts function handleCors() { return { statusCode: 200, headers: { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS", "Access-Control-Allow-Headers": "Content-Type, Authorization", "Access-Control-Allow-Credentials": true }, body: JSON.stringify("OK") }; } // src/functions/isJsonString.ts var isJsonString = (str) => { try { JSON.parse(str); } catch (_e) { return false; } return true; }; // src/functions/mapToDynamoAttribute.ts function mapToDynamoAttribute(value) { if (value === null) return { NULL: true }; if (typeof value === "boolean") return { BOOL: value }; else if (typeof value === "string") return { S: value }; else if (typeof value === "number") return { N: value.toString() }; else if (Array.isArray(value)) return { L: value.map(mapToDynamoAttribute) }; else if (typeof value === "object") { return { M: Object.entries(value).reduce( (acc, [key, val]) => { acc[key] = mapToDynamoAttribute(val); return acc; }, {} ) }; } return { NULL: true }; } // src/functions/startLogger.ts var import_mikrolog2 = require("mikrolog"); function startLogger(event, context, metadataConfig) { const correlationId = getCorrelationId(event, context); process.env.CORRELATION_ID = correlationId; const logger = import_mikrolog2.MikroLog.start({ metadataConfig, event, context }); return logger; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { cleanDynamoData, end, endWithError, extractDynamoValue, getAuthContext, getBearerToken, getCleanedDynamoItems, getCorrelationId, getInputBody, getMetadata, getPathParameters, getQueryStringParameters, handleCors, isJsonString, mapToDynamoAttribute, startLogger });