-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathhttp.ts
42 lines (37 loc) · 1.04 KB
/
http.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { httpRouter } from "convex/server";
import { httpAction, query } from "./_generated/server";
const http = httpRouter();
http.route({
path: "/basic",
method: "POST",
/* eslint-disable-next-line require-await */
handler: httpAction(async (_ctx, _request) => {
return new Response(JSON.stringify({ hello: "world" }), {
headers: new Headers({ "content-type": "application/json" }),
status: 200,
});
}),
});
http.route({
path: "/streaming",
method: "POST",
/* eslint-disable-next-line require-await */
handler: httpAction(async (_ctx, _request) => {
const encoder = new TextEncoder();
const stream = new ReadableStream({
type: "bytes",
start(controller) {
controller.enqueue(encoder.encode("<html>"));
setTimeout(() => {
controller.enqueue(encoder.encode("</html>"));
controller.close();
}, 20);
},
});
return new Response(stream);
}),
});
export const siteUrl = query(() => {
return process.env.CONVEX_SITE_URL!;
});
export default http;