Skip to content

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

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

fix: accept async/sync middleware on onOriginRequest / onOriginResponse#78
stefanmaric wants to merge 4 commits 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.

stefanmaric and others added 2 commits May 11, 2026 12:28
…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.
Comment thread .changeset/fix-onoriginrequest-async-signature.md Outdated

@jamie-at-bunny jamie-at-bunny left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stefanmaric solid PRk, this is great. One comment

};

// Plain function returning `Promise.resolve(...)` — the classic workaround.
const _promiseResolve: OnRequest = (ctx) => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nitpick, but we could probably drop this. The inferred type ends up being the same.

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.

2 participants