Skip to content

v0.1.14

Latest
Compare
Choose a tag to compare
@egoist egoist released this 05 May 15:01
· 2 commits to main since this release

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);
  }
}