-
-
Notifications
You must be signed in to change notification settings - Fork 116
/
imagine.ts
37 lines (37 loc) · 1.16 KB
/
imagine.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
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import { Midjourney } from "midjourney";
import { ResponseError } from "../../interfaces";
export const config = {
runtime: "edge",
};
const client = new Midjourney(
<string>process.env.SERVER_ID,
<string>process.env.CHANNEL_ID,
<string>process.env.SALAI_TOKEN
);
const handler = async (req: Request) => {
const { prompt } = await req.json();
console.log("imagine.handler", prompt);
const encoder = new TextEncoder();
const readable = new ReadableStream({
start(controller) {
console.log("imagine.start", prompt);
client
.Imagine(prompt, (uri: string) => {
console.log("imagine.loading", uri);
controller.enqueue(encoder.encode(JSON.stringify({ uri })));
})
.then((msg) => {
console.log("imagine.done", msg);
controller.enqueue(encoder.encode(JSON.stringify(msg)));
controller.close();
})
.catch((err: ResponseError) => {
console.log("imagine.error", err);
controller.close();
});
},
});
return new Response(readable, {});
};
export default handler;