Replies: 2 comments 3 replies
-
Hi @mharis! The If, for whatever reason, you don't want to use API Routes, the // app/api/my-queue.ts
import { QuirrelClient } from "quirrel/client"
export const myQueue = new QuirrelClient({
route: "/api/my-queue",
async handler(payload) {
// ... your queue logic ...
console.log(payload)
}
})
export async function POST(request: Request) {
const { body, headers, status } = await quirrel.respondTo(
await request.text(),
Object.fromEntries([...request.headers.entries()])
);
return new Response(body, { headers, status });
}
// then later in a different, server-side execution:
import { myQueue } from "app/api/my-queue"
await myQueue.enqueue(...) Again, I haven't tested this - it's mostly an adaptation of https://github.com/quirrel-dev/quirrel/blob/main/src/sveltekit.ts. If you end up implementing this in your project, I'd be happy to review a PR that adds this like a |
Beta Was this translation helpful? Give feedback.
-
A working example if anyone stumbles on this open discussion in the feature. import { QuirrelClient } from "quirrel/client";
import { NextApiRequest } from "next";
export const companiesQueue = new QuirrelClient({
route: "/api/queues/companies",
handler: async (payload) => {
console.log({ payload });
},
});
async function streamToString(
stream: ReadableStream<Uint8Array>
): Promise<string> {
const reader = stream.getReader();
const decoder = new TextDecoder("utf-8");
const readChunk = async (chunks: string[] = []): Promise<string[]> => {
const result = await reader.read();
if (result.done) {
return chunks;
}
chunks.push(decoder.decode(result.value));
return readChunk(chunks);
};
const chunks = await readChunk();
return chunks.join("");
}
export async function POST(req: NextApiRequest) {
const payload = await streamToString(req.body);
const { body, headers, status } = await companiesQueue.respondTo(
payload,
req.headers
);
return new Response(body, { headers, status });
} |
Beta Was this translation helpful? Give feedback.
-
I am having a hard time integrating with the app directory.
If anyone can show an example of the implementation, it'd be great.
Beta Was this translation helpful? Give feedback.
All reactions