Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | 4x 3x 3x 1x 2x 2x 1x 1x 4x 2x 2x 2x | import type {
APIGatewayProxyEventV2,
APIGatewayProxyStructuredResultV2,
Context
} from 'aws-lambda'
/**
* @interface HelloRequestInput
*
* Normalized input resolved from an API Gateway HTTP API v2 event.
* Aggregates name resolution logic across path parameters, query string,
* and default fallback; tracks the source of the name for observability.
*
* @property {string} name - The resolved display name (from path, query, or default 'world')
* @property {('path' | 'query' | 'default')} source - Origin of the name value
*
* @author Bayu Dwiyan Satria
* @since 1.0.0
* @see {@link resolveHelloInput}
* @see {@link helloHandler}
*/
export interface HelloRequestInput {
name: string
source: 'path' | 'query' | 'default'
}
/**
* @interface HelloResponseBody
*
* Response body returned by {@link helloHandler} for API Gateway HTTP API events.
* Includes a greeting message, request metadata, and input resolution details
* for debugging and logging purposes.
*
* @property {string} message - The greeting message (e.g., "Hello, Bayu!")
* @property {string} requestId - AWS request ID from Lambda context
* @property {string} timestamp - ISO 8601 timestamp of the invocation
* @property {HelloRequestInput} input - Normalized input including name source
*
* @author Bayu Dwiyan Satria
* @since 1.0.0
* @see {@link helloHandler}
*/
export interface HelloResponseBody {
message: string
requestId: string
timestamp: string
input: HelloRequestInput
}
/**
* @function resolveHelloInput
*
* Resolves a display name from an API Gateway HTTP API v2 event.
* Priority order: path parameters > query string parameters > default ("world").
* Performs trimming and validation to normalize input.
*
* @param {Pick<APIGatewayProxyEventV2, 'pathParameters' | 'queryStringParameters'>} event - Event object containing path and query parameters
* @returns {HelloRequestInput} Normalized input with resolved name and source metadata
*
* @example
* // Path parameter takes precedence
* const input = resolveHelloInput({
* pathParameters: { name: 'Bayu' },
* queryStringParameters: { name: 'Ignored' }
* });
* // { name: 'Bayu', source: 'path' }
*
* @example
* // Query string used if no path parameter
* const input = resolveHelloInput({
* pathParameters: undefined,
* queryStringParameters: { name: 'Alice' }
* });
* // { name: 'Alice', source: 'query' }
*
* @example
* // Default fallback
* const input = resolveHelloInput({
* pathParameters: undefined,
* queryStringParameters: undefined
* });
* // { name: 'world', source: 'default' }
*
* @author Bayu Dwiyan Satria
* @since 1.0.0
* @see {@link helloHandler}
*/
export const resolveHelloInput = (
event: Pick<APIGatewayProxyEventV2, 'pathParameters' | 'queryStringParameters'>
): HelloRequestInput => {
const pathName = event.pathParameters?.name?.trim()
if (pathName) {
return {
name: pathName,
source: 'path'
}
}
const queryName = event.queryStringParameters?.name?.trim()
if (queryName) {
return {
name: queryName,
source: 'query'
}
}
return {
name: 'world',
source: 'default'
}
}
/**
* @function helloHandler
*
* Sample AWS Lambda handler for API Gateway HTTP API (v2) events.
* Accepts HTTP GET requests with optional path or query parameters to customize
* the greeting recipient. Returns a JSON response with greeting, request metadata,
* and input resolution details.
*
* Supported invocation patterns:
* - `GET /hello` — greeting with default name "world"
* - `GET /hello/{name}` — greeting with path parameter
* - `GET /hello?name=value` — greeting with query parameter
*
* @param {APIGatewayProxyEventV2} event - API Gateway HTTP API v2 event payload
* @param {Context} context - Lambda invocation context
* @returns {Promise<APIGatewayProxyStructuredResultV2>} HTTP response with 200 status and JSON body
*
* @example
* // Invoked via API Gateway
* const response = await helloHandler(
* {
* pathParameters: { name: 'Bayu' },
* queryStringParameters: {}
* } as APIGatewayProxyEventV2,
* context
* );
* // Returns: { statusCode: 200, headers: {...}, body: '{"message":"Hello, Bayu!","..." }' }
*
* @author Bayu Dwiyan Satria
* @since 1.0.0
* @see {@link resolveHelloInput}
* @see {@link HelloResponseBody}
*/
export const helloHandler = async (
event: APIGatewayProxyEventV2,
context: Context
): Promise<APIGatewayProxyStructuredResultV2> => {
const input = resolveHelloInput(event)
const body: HelloResponseBody = {
message: `Hello, ${input.name}!`,
requestId: context.awsRequestId,
timestamp: new Date().toISOString(),
input
}
return {
statusCode: 200,
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(body)
}
}
|