bun-types 1.2.3-canary.20250216T140609 → 1.2.3-canary.20250218T140705

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bun.d.ts CHANGED
@@ -465,192 +465,6 @@ declare module "bun" {
465
465
  }
466
466
  const TOML: TOML;
467
467
 
468
- type Serve<WebSocketDataType = undefined> =
469
- | ServeOptions
470
- | TLSServeOptions
471
- | UnixServeOptions
472
- | UnixTLSServeOptions
473
- | WebSocketServeOptions<WebSocketDataType>
474
- | TLSWebSocketServeOptions<WebSocketDataType>
475
- | UnixWebSocketServeOptions<WebSocketDataType>
476
- | UnixTLSWebSocketServeOptions<WebSocketDataType>;
477
-
478
- /**
479
- Bun.serve provides a high-performance HTTP server with built-in routing support.
480
- It enables both function-based and object-based route handlers with type-safe
481
- parameters and method-specific handling.
482
-
483
- @example Basic Usage
484
- ```ts
485
- Bun.serve({
486
- port: 3000,
487
- fetch(req) {
488
- return new Response("Hello World");
489
- }
490
- });
491
- ```
492
-
493
- @example Route-based Handlers
494
- ```ts
495
- Bun.serve({
496
- routes: {
497
- // Static responses
498
- "/": new Response("Home page"),
499
-
500
- // Function handlers with type-safe parameters
501
- "/users/:id": (req) => {
502
- // req.params.id is typed as string
503
- return new Response(`User ${req.params.id}`);
504
- },
505
-
506
- // Method-specific handlers
507
- "/api/posts": {
508
- GET: () => new Response("Get posts"),
509
- POST: async (req) => {
510
- const body = await req.json();
511
- return new Response("Created post");
512
- },
513
- DELETE: (req) => new Response("Deleted post")
514
- },
515
-
516
- // Wildcard routes
517
- "/static/*": (req) => {
518
- // Handle any path under /static/
519
- return new Response("Static file");
520
- },
521
-
522
- // Disable route (fall through to fetch handler)
523
- "/api/legacy": false
524
- },
525
-
526
- // Fallback handler for unmatched routes
527
- fetch(req) {
528
- return new Response("Not Found", { status: 404 });
529
- }
530
- });
531
- ```
532
-
533
- @example Path Parameters
534
- ```ts
535
- Bun.serve({
536
- routes: {
537
- // Single parameter
538
- "/users/:id": (req: BunRequest<"/users/:id">) => {
539
- return new Response(`User ID: ${req.params.id}`);
540
- },
541
-
542
- // Multiple parameters
543
- "/posts/:postId/comments/:commentId": (
544
- req: BunRequest<"/posts/:postId/comments/:commentId">
545
- ) => {
546
- return new Response(JSON.stringify(req.params));
547
- // Output: {"postId": "123", "commentId": "456"}
548
- }
549
- }
550
- });
551
- ```
552
-
553
- @example Route Precedence
554
- ```ts
555
- // Routes are matched in the following order:
556
- // 1. Exact static routes ("/about")
557
- // 2. Parameter routes ("/users/:id")
558
- // 3. Wildcard routes ("/api/*")
559
-
560
- Bun.serve({
561
- routes: {
562
- "/api/users": () => new Response("Users list"),
563
- "/api/users/:id": (req) => new Response(`User ${req.params.id}`),
564
- "/api/*": () => new Response("API catchall"),
565
- "/*": () => new Response("Root catchall")
566
- }
567
- });
568
- ```
569
-
570
- @example Error Handling
571
- ```ts
572
- Bun.serve({
573
- routes: {
574
- "/error": () => {
575
- throw new Error("Something went wrong");
576
- }
577
- },
578
- error(error) {
579
- // Custom error handler
580
- console.error(error);
581
- return new Response(`Error: ${error.message}`, {
582
- status: 500
583
- });
584
- }
585
- });
586
- ```
587
-
588
- @example Server Lifecycle
589
- ```ts
590
- const server = Bun.serve({
591
- // Server config...
592
- });
593
-
594
- // Update routes at runtime
595
- server.reload({
596
- routes: {
597
- "/": () => new Response("Updated route")
598
- }
599
- });
600
-
601
- // Stop the server
602
- server.stop();
603
- ```
604
-
605
- @example Development Mode
606
- ```ts
607
- Bun.serve({
608
- development: true, // Enable hot reloading
609
- routes: {
610
- // Routes will auto-reload on changes
611
- }
612
- });
613
- ```
614
-
615
- @example Type-Safe Request Handling
616
- ```ts
617
- type Post = {
618
- id: string;
619
- title: string;
620
- };
621
-
622
- Bun.serve({
623
- routes: {
624
- "/api/posts/:id": async (
625
- req: BunRequest<"/api/posts/:id">
626
- ) => {
627
- if (req.method === "POST") {
628
- const body: Post = await req.json();
629
- return Response.json(body);
630
- }
631
- return new Response("Method not allowed", {
632
- status: 405
633
- });
634
- }
635
- }
636
- });
637
- ```
638
- @param options - Server configuration options
639
- @param options.routes - Route definitions mapping paths to handlers
640
- */
641
- function serve<
642
- T,
643
- R extends { [K in keyof R]: RouterTypes.RouteValue<K & string> },
644
- >(
645
- options: Serve<T> & {
646
- routes?: R;
647
- /**
648
- * @deprecated Use {@link routes} instead in new code
649
- */
650
- static?: R;
651
- },
652
- ): Server;
653
-
654
468
  /**
655
469
  * Synchronously resolve a `moduleId` as though it were imported from `parent`
656
470
  *
@@ -4450,7 +4264,32 @@ declare module "bun" {
4450
4264
  *
4451
4265
  * Passing other options such as `port` or `hostname` won't do anything.
4452
4266
  */
4453
- reload(options: Serve): void;
4267
+ reload<T, R extends { [K in keyof R]: RouterTypes.RouteValue<K & string> }>(
4268
+ options: (
4269
+ | (Omit<ServeOptions, "fetch"> & {
4270
+ routes: R;
4271
+ fetch?: (
4272
+ this: Server,
4273
+ request: Request,
4274
+ server: Server,
4275
+ ) => Response | Promise<Response>;
4276
+ })
4277
+ | (Omit<ServeOptions, "routes"> & {
4278
+ routes?: never;
4279
+ fetch: (
4280
+ this: Server,
4281
+ request: Request,
4282
+ server: Server,
4283
+ ) => Response | Promise<Response>;
4284
+ })
4285
+ | WebSocketServeOptions<T>
4286
+ ) & {
4287
+ /**
4288
+ * @deprecated Use `routes` instead in new code. This will continue to work for awhile though.
4289
+ */
4290
+ static?: R;
4291
+ },
4292
+ ): Server;
4454
4293
 
4455
4294
  /**
4456
4295
  * Mock the fetch handler for a running server.
@@ -4648,6 +4487,209 @@ declare module "bun" {
4648
4487
  readonly id: string;
4649
4488
  }
4650
4489
 
4490
+ type Serve<WebSocketDataType = undefined> =
4491
+ | ServeOptions
4492
+ | TLSServeOptions
4493
+ | UnixServeOptions
4494
+ | UnixTLSServeOptions
4495
+ | WebSocketServeOptions<WebSocketDataType>
4496
+ | TLSWebSocketServeOptions<WebSocketDataType>
4497
+ | UnixWebSocketServeOptions<WebSocketDataType>
4498
+ | UnixTLSWebSocketServeOptions<WebSocketDataType>;
4499
+
4500
+ /**
4501
+ Bun.serve provides a high-performance HTTP server with built-in routing support.
4502
+ It enables both function-based and object-based route handlers with type-safe
4503
+ parameters and method-specific handling.
4504
+
4505
+ @example Basic Usage
4506
+ ```ts
4507
+ Bun.serve({
4508
+ port: 3000,
4509
+ fetch(req) {
4510
+ return new Response("Hello World");
4511
+ }
4512
+ });
4513
+ ```
4514
+
4515
+ @example Route-based Handlers
4516
+ ```ts
4517
+ Bun.serve({
4518
+ routes: {
4519
+ // Static responses
4520
+ "/": new Response("Home page"),
4521
+
4522
+ // Function handlers with type-safe parameters
4523
+ "/users/:id": (req) => {
4524
+ // req.params.id is typed as string
4525
+ return new Response(`User ${req.params.id}`);
4526
+ },
4527
+
4528
+ // Method-specific handlers
4529
+ "/api/posts": {
4530
+ GET: () => new Response("Get posts"),
4531
+ POST: async (req) => {
4532
+ const body = await req.json();
4533
+ return new Response("Created post");
4534
+ },
4535
+ DELETE: (req) => new Response("Deleted post")
4536
+ },
4537
+
4538
+ // Wildcard routes
4539
+ "/static/*": (req) => {
4540
+ // Handle any path under /static/
4541
+ return new Response("Static file");
4542
+ },
4543
+
4544
+ // Disable route (fall through to fetch handler)
4545
+ "/api/legacy": false
4546
+ },
4547
+
4548
+ // Fallback handler for unmatched routes
4549
+ fetch(req) {
4550
+ return new Response("Not Found", { status: 404 });
4551
+ }
4552
+ });
4553
+ ```
4554
+
4555
+ @example Path Parameters
4556
+ ```ts
4557
+ Bun.serve({
4558
+ routes: {
4559
+ // Single parameter
4560
+ "/users/:id": (req: BunRequest<"/users/:id">) => {
4561
+ return new Response(`User ID: ${req.params.id}`);
4562
+ },
4563
+
4564
+ // Multiple parameters
4565
+ "/posts/:postId/comments/:commentId": (
4566
+ req: BunRequest<"/posts/:postId/comments/:commentId">
4567
+ ) => {
4568
+ return new Response(JSON.stringify(req.params));
4569
+ // Output: {"postId": "123", "commentId": "456"}
4570
+ }
4571
+ }
4572
+ });
4573
+ ```
4574
+
4575
+ @example Route Precedence
4576
+ ```ts
4577
+ // Routes are matched in the following order:
4578
+ // 1. Exact static routes ("/about")
4579
+ // 2. Parameter routes ("/users/:id")
4580
+ // 3. Wildcard routes ("/api/*")
4581
+
4582
+ Bun.serve({
4583
+ routes: {
4584
+ "/api/users": () => new Response("Users list"),
4585
+ "/api/users/:id": (req) => new Response(`User ${req.params.id}`),
4586
+ "/api/*": () => new Response("API catchall"),
4587
+ "/*": () => new Response("Root catchall")
4588
+ }
4589
+ });
4590
+ ```
4591
+
4592
+ @example Error Handling
4593
+ ```ts
4594
+ Bun.serve({
4595
+ routes: {
4596
+ "/error": () => {
4597
+ throw new Error("Something went wrong");
4598
+ }
4599
+ },
4600
+ error(error) {
4601
+ // Custom error handler
4602
+ console.error(error);
4603
+ return new Response(`Error: ${error.message}`, {
4604
+ status: 500
4605
+ });
4606
+ }
4607
+ });
4608
+ ```
4609
+
4610
+ @example Server Lifecycle
4611
+ ```ts
4612
+ const server = Bun.serve({
4613
+ // Server config...
4614
+ });
4615
+
4616
+ // Update routes at runtime
4617
+ server.reload({
4618
+ routes: {
4619
+ "/": () => new Response("Updated route")
4620
+ }
4621
+ });
4622
+
4623
+ // Stop the server
4624
+ server.stop();
4625
+ ```
4626
+
4627
+ @example Development Mode
4628
+ ```ts
4629
+ Bun.serve({
4630
+ development: true, // Enable hot reloading
4631
+ routes: {
4632
+ // Routes will auto-reload on changes
4633
+ }
4634
+ });
4635
+ ```
4636
+
4637
+ @example Type-Safe Request Handling
4638
+ ```ts
4639
+ type Post = {
4640
+ id: string;
4641
+ title: string;
4642
+ };
4643
+
4644
+ Bun.serve({
4645
+ routes: {
4646
+ "/api/posts/:id": async (
4647
+ req: BunRequest<"/api/posts/:id">
4648
+ ) => {
4649
+ if (req.method === "POST") {
4650
+ const body: Post = await req.json();
4651
+ return Response.json(body);
4652
+ }
4653
+ return new Response("Method not allowed", {
4654
+ status: 405
4655
+ });
4656
+ }
4657
+ }
4658
+ });
4659
+ ```
4660
+ @param options - Server configuration options
4661
+ @param options.routes - Route definitions mapping paths to handlers
4662
+ */
4663
+ function serve<
4664
+ T,
4665
+ R extends { [K in keyof R]: RouterTypes.RouteValue<K & string> },
4666
+ >(
4667
+ options: (
4668
+ | (Omit<ServeOptions, "fetch"> & {
4669
+ routes: R;
4670
+ fetch?: (
4671
+ this: Server,
4672
+ request: Request,
4673
+ server: Server,
4674
+ ) => Response | Promise<Response>;
4675
+ })
4676
+ | (Omit<ServeOptions, "routes"> & {
4677
+ routes?: never;
4678
+ fetch: (
4679
+ this: Server,
4680
+ request: Request,
4681
+ server: Server,
4682
+ ) => Response | Promise<Response>;
4683
+ })
4684
+ | WebSocketServeOptions<T>
4685
+ ) & {
4686
+ /**
4687
+ * @deprecated Use `routes` instead in new code. This will continue to work for awhile though.
4688
+ */
4689
+ static?: R;
4690
+ },
4691
+ ): Server;
4692
+
4651
4693
  /**
4652
4694
  * [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) powered by the fastest system calls available for operating on files.
4653
4695
  *
package/docs/api/fetch.md CHANGED
@@ -337,7 +337,7 @@ This will print the request and response headers to your terminal:
337
337
  ```sh
338
338
  [fetch] > HTTP/1.1 GET http://example.com/
339
339
  [fetch] > Connection: keep-alive
340
- [fetch] > User-Agent: Bun/1.2.3-canary.20250216T140609
340
+ [fetch] > User-Agent: Bun/1.2.3-canary.20250218T140705
341
341
  [fetch] > Accept: */*
342
342
  [fetch] > Host: example.com
343
343
  [fetch] > Accept-Encoding: gzip, deflate, br