fix(types): Change Bun.serve types to allow void when using websockets (#7160)

* `Response` -> `any`

* Revert previous commit & allow void in ws fetch

* Added type test to check if you can upgrade connection without returning

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
DidaS
2023-11-17 13:34:05 +00:00
committed by GitHub
parent 6e7014c91b
commit fbff18a723
2 changed files with 21 additions and 2 deletions

View File

@@ -2014,7 +2014,7 @@ declare module "bun" {
this: Server,
request: Request,
server: Server,
): Response | undefined | Promise<Response | undefined>;
): Response | undefined | void | Promise<Response | undefined | void>;
}
export interface UnixWebSocketServeOptions<WebSocketDataType = undefined>
@@ -2075,7 +2075,7 @@ declare module "bun" {
this: Server,
request: Request,
server: Server,
): Response | undefined | Promise<Response | undefined>;
): Response | undefined | void | Promise<Response | undefined | void>;
}
export interface TLSWebSocketServeOptions<WebSocketDataType = undefined>

View File

@@ -79,6 +79,25 @@ Bun.serve<User>({
},
});
Bun.serve({
fetch(req, server) {
server.upgrade(req);
},
websocket: {
open(ws) {
console.log("WebSocket opened");
ws.subscribe("test-channel");
},
message(ws, message) {
ws.publish("test-channel", `${message}`);
},
perMessageDeflate: true,
},
});
Bun.serve({
fetch(req) {
throw new Error("woops!");