(event: LambdaEvent)
| 133 | const MAX_ENVELOPE_DEPTH = 4; |
| 134 | |
| 135 | export function unwrapEvent(event: LambdaEvent): PlanEvent | RenderChunkEvent | AssembleEvent { |
| 136 | let cursor: LambdaEvent = event; |
| 137 | for (let i = 0; i < MAX_ENVELOPE_DEPTH; i++) { |
| 138 | if (cursor && typeof cursor === "object") { |
| 139 | const obj = cursor as Record<string, unknown>; |
| 140 | if (typeof obj.Action === "string" && isLambdaAction(obj.Action)) { |
| 141 | return cursor as PlanEvent | RenderChunkEvent | AssembleEvent; |
| 142 | } |
| 143 | if ("Payload" in obj) { |
| 144 | cursor = obj.Payload as LambdaEvent; |
| 145 | continue; |
| 146 | } |
| 147 | if ("Input" in obj) { |
| 148 | cursor = obj.Input as LambdaEvent; |
| 149 | continue; |
| 150 | } |
| 151 | } |
| 152 | break; |
| 153 | } |
| 154 | throw new Error( |
| 155 | `[handler] event has no recognised Action; unwrapped ${MAX_ENVELOPE_DEPTH} levels of Payload/Input without finding one.`, |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | function isLambdaAction(value: string): value is LambdaAction { |
| 160 | return value === "plan" || value === "renderChunk" || value === "assemble"; |
no test coverage detected