EdgeFunctionRecentInvocations.utils.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. const parts = eventMessage.split(' | ')
  22. if (parts.length < 3) return eventMessage
  23. const messageMethod = parts[0].trim()
  24. const messageStatus = parts[1].trim()
  25. const methodMatches = method !== undefined && messageMethod === method
  26. const statusMatches = statusCode !== undefined && messageStatus === statusCode
  27. if (methodMatches && statusMatches) {
  28. return parts.slice(2).join(' | ').trim()
  29. }
  30. return eventMessage
  31. }