Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kody/sentry

src/summarize-issue-event.ts

51 lines · 1.8 KB · TypeScript
import {
	type IssueInput,
	type JsonRecord,
	type SentryEvent,
	type SummarizeIssueEventOptions,
	getLatestIssueEvent,
	requireRecord,
	requireStringOrNumber,
	summarizeIssueEvent,
} from './client.ts'

type WrappedInput = {
	event: SentryEvent
	options?: SummarizeIssueEventOptions
}

type SummarizeLatestInput = IssueInput & {
	latest?: boolean
	options?: SummarizeIssueEventOptions
}

/**
 * Summarize a Sentry event object, or fetch and summarize an issue's latest event.
 *
 * @param params.event - Raw Sentry event, or `{ event, options }` wrapper.
 * @param params.issueId - With `latest: true` (or alone), fetch the issue's latest event first.
 * @param params.host - Optional API host when fetching by `issueId`. Default `sentry.io`.
 * @returns Truncated summary with exceptions, breadcrumbs, and tags.
 * @example
 * import summarizeEvent from 'kody:@kody/sentry/summarize-issue-event'
 * const summary = summarizeEvent({ event: { title: 'TypeError', entries: [] } })
 * // => { title: 'TypeError', exceptions: [...], breadcrumbs: [...] }
 */
export default async function summarizeIssueEventEntrypoint(params: unknown) {
	const input = requireRecord(params, 'summarize-issue-event')

	if (input.latest === true || (input.issueId !== undefined && !('event' in input))) {
		requireStringOrNumber(input, 'issueId', 'summarize-issue-event')
		const latestInput = input as SummarizeLatestInput
		const event = await getLatestIssueEvent(latestInput)
		return summarizeIssueEvent(event, latestInput.options)
	}

	const typedInput = input as WrappedInput | SentryEvent
	if ('event' in typedInput) {
		const wrappedInput = typedInput as WrappedInput
		return summarizeIssueEvent(wrappedInput.event, wrappedInput.options)
	}

	return summarizeIssueEvent(typedInput as JsonRecord as SentryEvent)
}