Now FetchEvent
is also available in the middleware:
import type { MiddlewareFetchEvent, MiddlewareRequest } from 'vite-vercel/server';
export default (req: MiddlewareRequest, event: MiddlewareFetchEvent) => {
const url = new URL(req.url)
if (url.pathname === '/responses/send-response') {
const { readable, writable } = new TransformStream();
event.waitUntil(
(async () => {
const writer = writable.getWriter();
const encoder = new TextEncoder();
writer.write(encoder.encode('Hello, world! Streamed!'));
writer.write(encoder.encode('response'));
writer.close();
})(),
);
return new Response(readable);
}
}