Skip to content

fix: accept async/sync middleware on onOriginRequest / onOriginResponse#78

Open
stefanmaric wants to merge 1 commit into
BunnyWay:mainfrom
stefanmaric:fix/onoriginrequest-async-middleware-signature
Open

fix: accept async/sync middleware on onOriginRequest / onOriginResponse#78
stefanmaric wants to merge 1 commit into
BunnyWay:mainfrom
stefanmaric:fix/onoriginrequest-async-middleware-signature

Conversation

@stefanmaric

Copy link
Copy Markdown

onOriginRequest's old Promise<Request> | Promise<Response> signature
doesn't accept async callbacks returning a mix of the two: TS infers
Promise<Request | Response> for them, which is a supertype the narrower
union won't accept. Flagged in #63 and partially fixed in #64 (which only
touched the ambient declaration).

Widen both hooks, with sync returns too:

  • onOriginRequest: (ctx) => Request | Response | Promise<Request | Response>
  • onOriginResponse: (ctx) => Response | Promise<Response>

Same shape as ServerHandler, strictly wider, nothing breaks. Internal
middleware arrays and the ambient Bunny.v1.registerMiddlewares get the
same widening; runtime is unchanged since both call sites already
await mid(...).

src/net/serve.test.ts covers sync, async, and mixed at the type level.

…ponse`

`onOriginRequest`'s old `Promise<Request> | Promise<Response>` signature
doesn't accept `async` callbacks returning a mix of the two: TS infers
`Promise<Request | Response>` for them, which is a supertype the narrower
union won't accept. Flagged in BunnyWay#63 and partially fixed in BunnyWay#64 (which only
touched the ambient declaration).

Widen both hooks, with sync returns too:

* `onOriginRequest`: `(ctx) => Request | Response | Promise<Request | Response>`
* `onOriginResponse`: `(ctx) => Response | Promise<Response>`

Same shape as `ServerHandler`, strictly wider, nothing breaks. Internal
middleware arrays and the ambient `Bunny.v1.registerMiddlewares` get the
same widening; runtime is unchanged since both call sites already
`await mid(...)`.

`src/net/serve.test.ts` covers sync, async, and mixed at the type level.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant