EdgeFunctionRecentInvocations.utils.ts37 lines · main
| 1 | /** |
| 2 | * Parses an edge function invocation event_message to extract the meaningful |
| 3 | * content, stripping out the method and status code that are already displayed |
| 4 | * as structured fields. |
| 5 | * |
| 6 | * The event_message typically follows the format: |
| 7 | * "{METHOD} | {STATUS_CODE} | {URL_OR_DETAIL}" |
| 8 | * |
| 9 | * e.g. "POST | 200 | https://example.briven.red/functions/v1/hello-world" |
| 10 | * |
| 11 | * When the structured method and status_code fields match what's in the message, |
| 12 | * we strip them out to avoid duplication. If parsing fails or the message doesn't |
| 13 | * match the expected format, we return the original message as-is. |
| 14 | */ |
| 15 | export function parseEdgeFunctionEventMessage( |
| 16 | eventMessage: string, |
| 17 | method?: string, |
| 18 | statusCode?: string |
| 19 | ): string { |
| 20 | if (!eventMessage) return eventMessage |
| 21 | |
| 22 | const parts = eventMessage.split(' | ') |
| 23 | |
| 24 | if (parts.length < 3) return eventMessage |
| 25 | |
| 26 | const messageMethod = parts[0].trim() |
| 27 | const messageStatus = parts[1].trim() |
| 28 | |
| 29 | const methodMatches = method !== undefined && messageMethod === method |
| 30 | const statusMatches = statusCode !== undefined && messageStatus === statusCode |
| 31 | |
| 32 | if (methodMatches && statusMatches) { |
| 33 | return parts.slice(2).join(' | ').trim() |
| 34 | } |
| 35 | |
| 36 | return eventMessage |
| 37 | } |