From 49b0fa1fb60a45090e192c8d321480041e529159 Mon Sep 17 00:00:00 2001 From: Anthony Griffon Date: Thu, 29 Aug 2024 13:54:37 +0200 Subject: [PATCH] feat: change serve function --- .changeset/polite-trees-grab.md | 5 +++++ example/deno-simple-http-page/src/main.ts | 2 +- example/simple-http-page/src/main.ts | 2 +- libs/bunny-sdk/src/net/serve.ts | 6 +++--- 4 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 .changeset/polite-trees-grab.md diff --git a/.changeset/polite-trees-grab.md b/.changeset/polite-trees-grab.md new file mode 100644 index 0000000..94291f1 --- /dev/null +++ b/.changeset/polite-trees-grab.md @@ -0,0 +1,5 @@ +--- +"@bunny.net/edgescript-sdk": minor +--- + +serve function should have the handler as first arg diff --git a/example/deno-simple-http-page/src/main.ts b/example/deno-simple-http-page/src/main.ts index e5b3f24..bb26b24 100644 --- a/example/deno-simple-http-page/src/main.ts +++ b/example/deno-simple-http-page/src/main.ts @@ -5,7 +5,7 @@ function sleep(ms: number): Promise { } console.log("Starting server..."); -BunnySDK.net.http.serve({ port: 8080, hostname: '127.0.0.1' }, async (req) => { +BunnySDK.net.http.serve(async (req) => { console.log(`[INFO]: ${req.method} - ${req.url}`); await sleep(1); return new Response("blbl"); diff --git a/example/simple-http-page/src/main.ts b/example/simple-http-page/src/main.ts index 2de7a7d..1ae7c1e 100644 --- a/example/simple-http-page/src/main.ts +++ b/example/simple-http-page/src/main.ts @@ -6,7 +6,7 @@ function sleep(ms: number): Promise { console.log("Starting server..."); -BunnySDK.net.http.serve({ port: 8080, hostname: '127.0.0.1' }, async (req) => { +BunnySDK.net.http.serve(async (req) => { console.log(`[INFO]: ${req.method} - ${req.url}`); await sleep(1); return new Response("blbl"); diff --git a/libs/bunny-sdk/src/net/serve.ts b/libs/bunny-sdk/src/net/serve.ts index a182322..75a9f2d 100644 --- a/libs/bunny-sdk/src/net/serve.ts +++ b/libs/bunny-sdk/src/net/serve.ts @@ -16,9 +16,9 @@ type ServeHandler = {} & unknown; /** * Serves HTTP requests on the given [TcpListener] */ -function serve(listener: { port: number; hostname: string; }, handler: ServerHandler): ServeHandler; -function serve(listener: TcpListener, handler: ServerHandler): ServeHandler; -function serve(listener: TcpListener | { port: number; hostname: string; }, handler: ServerHandler): ServeHandler { +function serve(handler: ServerHandler, listener: { port: number; hostname: string; }): ServeHandler; +function serve(handler: ServerHandler, listener: TcpListener): ServeHandler; +function serve(handler: ServerHandler, listener?: TcpListener | { port: number; hostname: string; },): ServeHandler { const platform = internal_getPlatform(); const listenerUnion = Tcp.isTcpListener(listener) ? listener : Tcp.unstable_new();