### Starting the Server with Bun Source: https://github.com/cah4a/trpc-bun-adapter/blob/main/example/README.md This command initiates the server application using Bun. It starts the main process, making the application accessible and ready to handle requests. ```bash bun start ``` -------------------------------- ### Starting the tRPC Bun Server - Bash Source: https://github.com/cah4a/trpc-bun-adapter/blob/main/README.md These commands show how to start the tRPC server created in `server.ts` using Bun. The first command starts it normally, while the second command uses `--watch` to automatically restart the server on file changes, which is useful for development. ```Bash bun run server.ts bun run --watch server.ts # to restart on file changes ``` -------------------------------- ### Installing Dependencies with Bun Source: https://github.com/cah4a/trpc-bun-adapter/blob/main/example/README.md This command installs all required project dependencies using the Bun package manager. It ensures that all necessary packages are available before running the application. ```bash bun install ``` -------------------------------- ### Installing tRPC Bun Adapter Packages - Bash Source: https://github.com/cah4a/trpc-bun-adapter/blob/main/README.md This command installs the necessary npm packages, `@trpc/server` and `trpc-bun-adapter`, using Bun's package manager. These packages are required to set up a tRPC server with the Bun adapter. ```Bash bun install @trpc/server trpc-bun-adapter ``` -------------------------------- ### Creating a Basic tRPC Server with Bun - TypeScript Source: https://github.com/cah4a/trpc-bun-adapter/blob/main/README.md This TypeScript code initializes a tRPC server, defines a simple `ping` query procedure, and then uses `createBunServeHandler` to serve the router with Bun. It demonstrates the minimal setup for a tRPC server running on Bun. ```TypeScript import {initTRPC} from '@trpc/server'; import {createBunServeHandler} from 'trpc-bun-adapter'; const t = initTRPC.create(); export const router = t.router({ ping: t.procedure.query(() => "pong") }); Bun.serve(createBunServeHandler({ router })); ``` -------------------------------- ### Configuring tRPC Bun Serve Handler with Options - TypeScript Source: https://github.com/cah4a/trpc-bun-adapter/blob/main/README.md This example demonstrates how to use `createBunServeHandler` with various optional arguments, including `endpoint`, `createContext`, `onError`, `responseMeta`, and `batching`. It also shows how to pass Bun's native `serve` options like `port` and a custom `fetch` handler for non-tRPC requests. ```TypeScript import {createBunServeHandler, CreateBunContextOptions} from 'trpc-bun-adapter'; import {router} from './router'; const createContext = (opts: CreateBunContextOptions) => ({ user: 1 }); Bun.serve( createBunServeHandler( { router, // optional arguments: endpoint: '/trpc', // Default to "" createContext, onError: console.error, responseMeta(opts) { return { status: 202, headers: {} }; }, batching: { enabled: true } }, { // Bun serve options port: 3001, fetch(request, server) { // will be executed if it's not a TRPC request return new Response("Hello world"); } } ) ); ``` -------------------------------- ### Testing tRPC Bun Server Endpoint - Bash Source: https://github.com/cah4a/trpc-bun-adapter/blob/main/README.md This `curl` command is used to test the `ping` endpoint of the tRPC server running on `localhost:3000`. It sends an HTTP GET request to verify that the server is responding correctly. ```Bash curl http://localhost:3000/ping ``` -------------------------------- ### Bundling Client Code with Bun Source: https://github.com/cah4a/trpc-bun-adapter/blob/main/example/README.md This command bundles the client-side code using Bun's built-in bundler. This step is typically required to optimize and prepare the client assets for deployment or execution. ```bash bun bundle ``` -------------------------------- ### Creating a tRPC Bun WebSocket Handler - TypeScript Source: https://github.com/cah4a/trpc-bun-adapter/blob/main/README.md This snippet shows how to use `createBunWSHandler` to set up a tRPC WebSocket server. It integrates with Bun's `server.upgrade` method to handle WebSocket connections and provides options for `createContext`, `onError`, and `batching` for WebSocket-specific tRPC procedures. ```TypeScript import { createBunWSHandler, CreateBunContextOptions } from './src'; import { router } from './router'; const createContext = (opts: CreateBunContextOptions) => ({ user: 1 }); const websocket = createBunWSHandler({ router, // optional arguments: createContext, onError: console.error, batching: { enabled: true } }); Bun.serve({ fetch(request, server) { if (server.upgrade(request, {data: {req: request}})) { return; } return new Response("Please use websocket protocol", {status: 404}); }, websocket }); ``` -------------------------------- ### Creating a tRPC Bun HTTP Handler - TypeScript Source: https://github.com/cah4a/trpc-bun-adapter/blob/main/README.md This code demonstrates `createBunHttpHandler` for handling tRPC HTTP requests separately from the main Bun `fetch` handler. It allows for custom logic for non-tRPC requests and includes options like `endpoint`, `createContext`, `onError`, `responseMeta`, `batching`, and `emitWsUpgrades`. ```TypeScript import {createBunHttpHandler, CreateBunContextOptions} from 'trpc-bun-adapter'; import {router} from './router'; const createContext = (opts: CreateBunContextOptions) => ({ user: 1 }); const bunHandler = createBunHttpHandler({ router, // optional arguments: endpoint: '/trpc', // Default to "" createContext, onError: console.error, responseMeta(opts) { return { status: 202, headers: {} }; }, batching: { enabled: true }, emitWsUpgrades: false // pass true to upgrade to WebSocket }); Bun.serve({ fetch(request, response) { return bunHandler(request, response) ?? new Response("Not found", {status: 404}); } }); ``` -------------------------------- ### Integrating tRPC Context with Bun HTTP Handler in TypeScript Source: https://github.com/cah4a/trpc-bun-adapter/blob/main/README.md This snippet demonstrates the final step of integrating the custom context. It shows how to pass both the tRPC router and the `createContext` function to `createBunHttpHandler`, ensuring that the custom context is properly set up and available for incoming HTTP requests. ```TypeScript createBunHttpHandler({ router, createContext, }) ``` -------------------------------- ### Accessing tRPC Context in Router Procedures in TypeScript Source: https://github.com/cah4a/trpc-bun-adapter/blob/main/README.md This code illustrates how to initialize tRPC with the previously defined `createContext` function. It then shows how to create a router and access the custom context properties, like `ctx.authorization`, within a tRPC procedure. ```TypeScript const t = initTRPC.context().create(); export const router = t.router({ session: t.procedure.query(({ ctx }) => ctx.authorization), }); ``` -------------------------------- ### Defining tRPC Context with CreateBunContextOptions in TypeScript Source: https://github.com/cah4a/trpc-bun-adapter/blob/main/README.md This snippet demonstrates how to define a `createContext` function using the `CreateBunContextOptions` type. This function is crucial for the tRPC router to recognize and utilize the custom context, such as extracting authorization information from request headers. ```TypeScript import { initTRPC } from '@trpc/server'; import type { CreateBunContextOptions } from "src/createBunHttpHandler"; export const createContext = async (opts: CreateBunContextOptions) => { return { authorization: req.headers.get('Authorization') }; }; ``` -------------------------------- ### Adding CORS Headers with tRPC Bun `responseMeta` - TypeScript Source: https://github.com/cah4a/trpc-bun-adapter/blob/main/README.md This snippet illustrates how to configure Cross-Origin Resource Sharing (CORS) headers using the `responseMeta` option within `createBunServeHandler`. It sets `Access-Control-Allow-Origin`, `Access-Control-Allow-Methods`, and `Access-Control-Allow-Headers` to enable cross-origin requests. ```TypeScript Bun.serve( createBunServeHandler({ router: appRouter, responseMeta(opts) { return { status: 200, headers: { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET, POST, OPTIONS", "Access-Control-Allow-Headers": "Content-Type, Authorization" } }; } } ) ); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.