h3 icon indicating copy to clipboard operation
h3 copied to clipboard

v2 migration

Open pi0 opened this issue 1 year ago • 4 comments

[!NOTE] This is a tracker issue for H3 v2 release. You can try with h3-nightly@2x

Migration guide:

  • https://github.com/unjs/h3/blob/main/MIGRATION.md
  • https://main.unjs-h3.pages.dev/migration

pi0 avatar Jun 24 '24 15:06 pi0

Why did you stop using destr? It's prevention of prototype pollution seemed quite useful.

AaronDewes avatar Jun 28 '24 05:06 AaronDewes

@AaronDewes mainly for bundle size. But i am up to add a replacement util for safe read (https://github.com/unjs/h3/issues/819, feel free to make a PR for it!)

pi0 avatar Jul 08 '24 14:07 pi0

What would you think of the following approach to handle Response manipulation, now that standard web Response is the de-facto representation?

defineEventHandler({
  handler(...),
  // Here response type would always be Response
  onBeforeResponse(event, response: Response) {
    // We can then edit the response headers
	response.headers.set('X', 'Y');
    // Or even return a new one?
    return new Response(...);
  }
});

// Another approach: Update EventHandler definition so that we can return a Response Handler
defineEventHandler(event => {
  // return a Response handler
  return (response: Response) => {
    response.headers.set('X', 'Y');
    return response;
  }
});

The benefits I see are:

  • Make most of Response utils deprecated, streamlining even more the v2 approach.
  • Allow tool authors to declare their middleware once in a universal manner, and have h3 (or any other server) ingest it without overhead.

magne4000 avatar Jul 15 '24 09:07 magne4000

@magne4000 With https://github.com/unjs/h3/pull/829 h3 will be web-first with event.response.headers to allow modification of headers. Creating multiple Response objects for middleware might have performance overhead and h3 (v2) by design expects explicit return for body, other partials can be set via event.response and Web standard APIs. Once landed, would love to hear your feedback.

pi0 avatar Jul 17 '24 15:07 pi0