feat(web): inspect upstream responses from route handlers

Merged
RU
18d ago
+122-13
  • Adds route.fetch() to WebRoute: reads the upstream response inside a route handler without deciding the route, then the handler fulfills, continues, or aborts as usual.
  • Sends the request's own headers plus the configured site headers, maxRedirects: 0 so a redirect can't take the fetch past the navigation rule (packages/web/src/browser.ts).
  • Returns a WebResponse (url, status, headers, text(), json()); the body is read through the shared readResponseBody, so an unreadable body keeps the metadata and rejects text()/json() with ACTION_FAILED.
await browser.route('**/api/user', async (route) => {
  const upstream = await route.fetch();
  const user = await upstream.json<User>();
  await route.fulfill({ json: { ...user, plan: 'pro' } });
});

Verified

Ran it locally: yes

  • packages/web/tests/integration/routes.test.ts against real Chromium: fetch + amend + fulfill with site headers (server hit once), unreadable body keeps metadata, 302 returned without following. All 3 fail with maxRedirects: 0 removed and the body failure swallowed.
  • Full packages/web suite: 599 passed.