Migrate from v2 to v3 (Standard Parser)
mainIn v3, createParser no longer accepts a single callback function. Instead, it requires an object of callbacks. This change eliminates the need to manually check event.type to distinguish between data events and retry interval changes.
Key Changes:
- Callback Structure: Replace the single callback with an object containing
onEventandonRetry. - Type Renaming:
ParsedEventis nowEventSourceMessage. Note that thetypeproperty has been removed fromEventSourceMessage. - New Callbacks: You can now use
onErrorto handle parsing errors andonCommentto handle comments.
Removed/Renamed Types:
ParsedEvent$\rightarrow$EventSourceMessageEventSourceParseCallback$\rightarrow$ replaced by theParserCallbacksinterface (via theonEventproperty).ReconnectInterval$\rightarrow$ removed (theonRetrycallback now provides thenumberinterval directly).ParseEvent$\rightarrow$ removed (event types are now separated by callback type).
import {createParser, type EventSourceMessage} from 'eventsource-parser'
const parser = createParser({
onEvent: (event: EventSourceMessage) => {
// …handle event…
},
onRetry: (interval: number) => {
// …handle retry interval change…
},
onError: (error: Error) => {
// …handle parse error…
},
onComment: (comment: string) => {
// …handle comment…
},
})