UNPKG

@energica-city/shared-amplify-utils

Version:

Shared utilities for AWS Amplify projects

114 lines 3.99 kB
import { setupStructuredLoggingWith, getErrorMessage as sharedGetErrorMessage, getErrorStack as sharedGetErrorStack, parseJsonBody as sharedParseJsonBody, buildErrorContextWith, getModelsFromInput as sharedGetModelsFromInput, } from '../utils/common'; /** * Build standardized log context for REST operations */ export function buildRestContext(input, additionalContext = {}) { const { event, context } = input; return { ...additionalContext, method: event.httpMethod || 'UNKNOWN', path: event.path || 'UNKNOWN', resource: event.resource || 'UNKNOWN', // Add null checks to prevent undefined values from being logged requestId: context?.awsRequestId || undefined, functionName: context?.functionName || undefined, functionVersion: context?.functionVersion || undefined, stage: event.requestContext?.stage, sourceIp: event.requestContext?.identity?.sourceIp, }; } /** * Extract basic event information for logging */ export function extractEventInfo(event) { return { method: event.httpMethod || 'UNKNOWN', path: event.path || 'UNKNOWN', resource: event.resource || 'UNKNOWN', stage: event.requestContext?.stage, hasBody: !!event.body, bodyLength: event.body?.length || 0, queryStringParameters: event.queryStringParameters, pathParameters: event.pathParameters, }; } /** * Setup structured logging for REST middleware */ export function setupStructuredLogging(input, forceStructuredLogging = true, defaultContext = {}) { const typedInput = input; const buildContext = (i, extra) => buildRestContext(i, extra); setupStructuredLoggingWith(typedInput, buildContext, forceStructuredLogging, defaultContext); } /** * Extract error message safely */ export function getErrorMessage(error) { return sharedGetErrorMessage(error); } /** * Get error stack trace if available */ export function getErrorStack(error) { return sharedGetErrorStack(error); } /** * Safely parses JSON body with error handling and logging */ export function parseJsonBody(body, context) { return sharedParseJsonBody(body, context, 'REST'); } /** * Extracts request ID from multiple possible sources */ export function getRequestId(event, context) { return (context?.awsRequestId || event.requestContext?.requestId || `unknown-${Date.now()}`); } /** * Build error context with REST information */ export function buildErrorContext(input, error, additionalContext = {}) { const typedInput = input; const buildContext = (i, extra) => buildRestContext(i, extra); return buildErrorContextWith(typedInput, buildContext, error, additionalContext); } export function getModelsFromInput(input) { const typedInput = input; return sharedGetModelsFromInput(typedInput, 'Models not available. Ensure RestModelInitializer middleware is used before this handler.'); } export function createSuccessResponse(payload, options = {}) { const { statusCode = 200, headers = {}, isBase64Encoded = false } = options; const baseHeaders = { 'Content-Type': 'application/json', }; const body = payload === undefined ? '' : JSON.stringify(payload); return { statusCode, headers: { ...baseHeaders, ...headers }, body, isBase64Encoded, }; } /** * Create a standard API Gateway error response */ export function createErrorResponse(message, options = {}) { const { statusCode = 400, code, details, headers = {}, isBase64Encoded = false, } = options; const baseHeaders = { 'Content-Type': 'application/json', }; const body = JSON.stringify({ message, ...(code ? { code } : {}), ...(details !== undefined ? { details } : {}), }); return { statusCode, headers: { ...baseHeaders, ...headers }, body, isBase64Encoded, }; } //# sourceMappingURL=utils.js.map