Skip to content

Commit 5bbb1f8

Browse files
committed
pass headers through
1 parent 437c599 commit 5bbb1f8

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

example/convex/_generated/api.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @module
99
*/
1010

11+
import type * as auth from "../auth.js";
1112
import type * as chat from "../chat.js";
1213
import type * as http from "../http.js";
1314
import type * as messages from "../messages.js";
@@ -28,6 +29,7 @@ import type {
2829
* ```
2930
*/
3031
declare const fullApi: ApiFromModules<{
32+
auth: typeof auth;
3133
chat: typeof chat;
3234
http: typeof http;
3335
messages: typeof messages;

example/convex/http.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { httpRouter } from "convex/server";
22
import { streamChat } from "./chat";
33
import { httpAction } from "./_generated/server";
4+
import { auth } from "./auth";
45

56
const http = httpRouter();
7+
auth.addHttpRoutes(http);
68

79
http.route({
810
path: "/chat-stream",

src/react/index.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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(
119135
async 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

Comments
 (0)