react-eventsource-hook
Version:
⚠️ DEPRECATED: Use 'react-eventsource' instead. React hook for Server-Sent Events with custom headers support, built on @microsoft/fetch-event-source
1 lines • 3.44 kB
Source Map (JSON)
{"version":3,"sources":["../src/index.ts","../src/use-eventsource.ts"],"sourcesContent":["// Export all types and interfaces\nexport * from \"./use-eventsource\";\n\n// Import the hook function to create aliases\nimport { useEventSource } from \"./use-eventsource\";\n\n// Primary export: useSSE (modern, concise name)\nexport { useEventSource as useSSE };\n\n// Secondary export: useEventSource (for clarity and searchability)\nexport { useEventSource };","import { useState, useEffect, useRef } from \"react\";\nimport { fetchEventSource, type EventSourceMessage } from \"@microsoft/fetch-event-source\";\n\nexport interface UseEventSourceOptions {\n url: string;\n headers?: Record<string, string>;\n method?: string;\n body?: string | FormData;\n onMessage?: (message: EventSourceMessage) => void;\n onOpen?: (response: Response) => void;\n onError?: (error: unknown) => void;\n fetch?: typeof window.fetch;\n}\n\nexport interface UseEventSourceResult {\n readyState: number;\n close: () => void;\n reconnect: () => void;\n}\n\nexport function useEventSource(options: UseEventSourceOptions): UseEventSourceResult {\n const { url, headers, method, body, onMessage, onOpen, onError, fetch: customFetch } = options;\n const [readyState, setReadyState] = useState(0);\n const controllerRef = useRef<AbortController | null>(null);\n\n const connect = () => {\n if (controllerRef.current) {\n controllerRef.current.abort();\n }\n\n const controller = new AbortController();\n controllerRef.current = controller;\n\n setReadyState(0);\n\n fetchEventSource(url, {\n method: method || 'GET',\n headers: headers,\n body: body,\n signal: controller.signal,\n fetch: customFetch,\n \n async onopen(response) {\n setReadyState(1);\n onOpen?.(response);\n },\n \n onmessage(message) {\n onMessage?.(message);\n },\n \n onerror(err) {\n setReadyState(2);\n onError?.(err);\n },\n });\n };\n\n const close = () => {\n if (controllerRef.current) {\n controllerRef.current.abort();\n setReadyState(2);\n }\n };\n\n const reconnect = () => {\n connect();\n };\n\n useEffect(() => {\n connect();\n return () => {\n close();\n };\n }, [url, headers, method, body, onMessage, onOpen, onError]);\n\n return {\n readyState,\n close,\n reconnect,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAA4C;AAC5C,gCAA0D;AAmBnD,SAAS,eAAe,SAAsD;AACnF,QAAM,EAAE,KAAK,SAAS,QAAQ,MAAM,WAAW,QAAQ,SAAS,OAAO,YAAY,IAAI;AACvF,QAAM,CAAC,YAAY,aAAa,QAAI,uBAAS,CAAC;AAC9C,QAAM,oBAAgB,qBAA+B,IAAI;AAEzD,QAAM,UAAU,MAAM;AACpB,QAAI,cAAc,SAAS;AACzB,oBAAc,QAAQ,MAAM;AAAA,IAC9B;AAEA,UAAM,aAAa,IAAI,gBAAgB;AACvC,kBAAc,UAAU;AAExB,kBAAc,CAAC;AAEf,oDAAiB,KAAK;AAAA,MACpB,QAAQ,UAAU;AAAA,MAClB;AAAA,MACA;AAAA,MACA,QAAQ,WAAW;AAAA,MACnB,OAAO;AAAA,MAEP,MAAM,OAAO,UAAU;AACrB,sBAAc,CAAC;AACf,yCAAS;AAAA,MACX;AAAA,MAEA,UAAU,SAAS;AACjB,+CAAY;AAAA,MACd;AAAA,MAEA,QAAQ,KAAK;AACX,sBAAc,CAAC;AACf,2CAAU;AAAA,MACZ;AAAA,IACF,CAAC;AAAA,EACH;AAEA,QAAM,QAAQ,MAAM;AAClB,QAAI,cAAc,SAAS;AACzB,oBAAc,QAAQ,MAAM;AAC5B,oBAAc,CAAC;AAAA,IACjB;AAAA,EACF;AAEA,QAAM,YAAY,MAAM;AACtB,YAAQ;AAAA,EACV;AAEA,8BAAU,MAAM;AACd,YAAQ;AACR,WAAO,MAAM;AACX,YAAM;AAAA,IACR;AAAA,EACF,GAAG,CAAC,KAAK,SAAS,QAAQ,MAAM,WAAW,QAAQ,OAAO,CAAC;AAE3D,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;","names":[]}