#Bun serve throws weird error

1 messages · Page 1 of 1 (latest)

small jungle
#

I have an websocket

const port = 3333;
import { isValidApiKey } from "./functions/apikey";
type WebSocketData = {
    apikey: string;
    matchId: string;
  };

const clients = new Set()

Bun.serve<WebSocketData>({
  port: port,
  async fetch(req: any, server: any) {
    const url = new URL(req.url);
    const apikey = url.searchParams.get("apikey");
    const matchId = url.searchParams.get("matchId");
    if (!apikey || !matchId) {
      return new Response("No API key or matchId given", { status: 402 });
    }
[...]

but for some reason the serve<WebSocketData> throws an error because it expected 2 type arguments. But when I add a second it just throws a different error and in the docs isn't anything about it

tardy sleet
#

what's the error? types or runtime? if it's type error, it's probably because you're not returning a response in the fetch handler (although the docs and unit tests show this is possible but the types don't allow it)

small jungle
#

Types it just says it expects 2 arguments and when I add a second it says the second Argument does not match. But the funny thing is in a different project I have the exact same Code which is working completely fine

tardy sleet
#

yeah.. the types have been changing between releases especially for the server, so it depends on whatever bun-types is in your project (this is the package that @types/bun references for actual type definitions).

Run this in the root of your projects to see which currently installed in your project:

bun -e 'console.log((await import("./node_modules/bun-types/package.json")).version)'
small jungle
#

ok thank you

runic walrus