@@ -37,7 +37,13 @@ export function useStream(
3737 > ,
3838 streamUrl : URL ,
3939 driven : boolean ,
40- streamId ?: StreamId
40+ streamId : StreamId | undefined ,
41+ opts ?: {
42+ // If provided, this will be passed as the Authorization header.
43+ authToken ?: string | null ;
44+ // If provided, these will be passed as additional headers.
45+ headers ?: Record < string , string > ;
46+ }
4147) {
4248 const [ streamEnded , setStreamEnded ] = useState ( null as boolean | null ) ;
4349
@@ -56,20 +62,30 @@ export function useStream(
5662 // Otherwise, we'll try to drive the stream and use the HTTP response.
5763 return false ;
5864 } , [ driven , streamId , streamEnded ] ) ;
59- // console.log("usePersistence", usePersistence);
65+ // console.log("usePersistence", usePersistence);
6066 const persistentBody = useQuery (
6167 getPersistentBody ,
62- usePersistence && streamId ? { streamId : streamId ! } : "skip"
68+ usePersistence && streamId ? { streamId } : "skip"
6369 ) ;
6470 const [ streamBody , setStreamBody ] = useState < string > ( "" ) ;
6571
6672 useEffect ( ( ) => {
6773 if ( driven && streamId && ! streamStarted . current ) {
6874 // Kick off HTTP action.
6975 void ( async ( ) => {
70- const success = await startStreaming ( streamUrl , streamId , ( text ) => {
71- setStreamBody ( ( prev ) => prev + text ) ;
72- } ) ;
76+ const success = await startStreaming (
77+ streamUrl ,
78+ streamId ,
79+ ( text ) => {
80+ setStreamBody ( ( prev ) => prev + text ) ;
81+ } ,
82+ {
83+ ...opts ?. headers ,
84+ ...( opts ?. authToken
85+ ? { Authorization : `Bearer ${ opts . authToken } ` }
86+ : { } ) ,
87+ }
88+ ) ;
7389 setStreamEnded ( success ) ;
7490 } ) ( ) ;
7591 // If we get remounted, we don't want to start a new stream.
@@ -119,14 +135,15 @@ export function useStream(
119135async function startStreaming (
120136 url : URL ,
121137 streamId : StreamId ,
122- onUpdate : ( text : string ) => void
138+ onUpdate : ( text : string ) => void ,
139+ headers : Record < string , string >
123140) {
124141 const response = await fetch ( url , {
125142 method : "POST" ,
126143 body : JSON . stringify ( {
127144 streamId : streamId ,
128145 } ) ,
129- headers : { "Content-Type" : "application/json" } ,
146+ headers : { "Content-Type" : "application/json" , ... headers } ,
130147 } ) ;
131148 // Adapted from https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_streams
132149 if ( response . status === 205 ) {
0 commit comments