{"id":14156242,"url":"https://github.com/cah4a/trpc-bun-adapter","last_synced_at":"2025-06-13T02:42:46.219Z","repository":{"id":214741024,"uuid":"737166343","full_name":"cah4a/trpc-bun-adapter","owner":"cah4a","description":"Use tRPC with bun and fun","archived":false,"fork":false,"pushed_at":"2025-03-29T10:12:40.000Z","size":90,"stargazers_count":62,"open_issues_count":3,"forks_count":6,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-03T15:37:34.689Z","etag":null,"topics":["adapter","bun","trpc"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/trpc-bun-adapter","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cah4a.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-30T03:29:50.000Z","updated_at":"2025-05-31T23:53:06.000Z","dependencies_parsed_at":"2023-12-30T12:20:42.867Z","dependency_job_id":"a48a6e9b-cfdd-46cf-bda6-2ab6e5c95ea0","html_url":"https://github.com/cah4a/trpc-bun-adapter","commit_stats":null,"previous_names":["cah4a/trpc-bun-adapter"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/cah4a/trpc-bun-adapter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cah4a%2Ftrpc-bun-adapter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cah4a%2Ftrpc-bun-adapter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cah4a%2Ftrpc-bun-adapter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cah4a%2Ftrpc-bun-adapter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cah4a","download_url":"https://codeload.github.com/cah4a/trpc-bun-adapter/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cah4a%2Ftrpc-bun-adapter/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259568271,"owners_count":22877885,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["adapter","bun","trpc"],"created_at":"2024-08-17T08:05:19.151Z","updated_at":"2025-06-13T02:42:46.187Z","avatar_url":"https://github.com/cah4a.png","language":"TypeScript","funding_links":[],"categories":["others"],"sub_categories":[],"readme":"# tRPC Bun Adapter\n\n[![npm version](https://badge.fury.io/js/trpc-bun-adapter.svg)](https://badge.fury.io/js/trpc-bun-adapter)\n[![License](https://img.shields.io/github/license/cah4a/trpc-bun-adapter)](https://opensource.org/licenses/MIT)\n\n## Description\n\n\n`trpc-bun-adapter` is a [tRPC](https://trpc.io/) adapter for [Bun](https://github.com/OptimalBits/bun).\n\nStart both HTTP and WebSockets transports with ease.\n\n## Quick Start\n\nInstall packages:\n```bash\nbun install @trpc/server trpc-bun-adapter\n```\n\nCreate a server.ts file with the following content:\n```ts\nimport {initTRPC} from '@trpc/server';\nimport {createBunServeHandler} from 'trpc-bun-adapter';\n\nconst t = initTRPC.create();\n\nexport const router = t.router({\n    ping: t.procedure.query(() =\u003e \"pong\"),\n});\n\nBun.serve(createBunServeHandler({ router }));\n```\n\nTo start the server, run:\n```bash\nbun run server.ts\nbun run --watch server.ts # to restart on file changes\n```\n\nCheck that it works:\n```bash\ncurl http://localhost:3000/ping\n```\n\n## Example\n\nfor a full example, see the [example](./example/) directory.\n\n## API Reference\n\nEnsure you have created a `router.ts` file as outlined in the tRPC documentation: [Define Routers](https://trpc.io/docs/server/routers).\n\n### createBunServeHandler\n\nCreates a Bun serve handler:\n\n```ts\nimport {createBunServeHandler, CreateBunContextOptions} from 'trpc-bun-adapter';\nimport {router} from './router';\n\nconst createContext = (opts: CreateBunContextOptions) =\u003e ({\n    user: 1,\n});\n\nBun.serve(\n    createBunServeHandler(\n        {\n            router,\n            // optional arguments:\n            endpoint: '/trpc', // Default to \"\"\n            createContext,\n            onError: console.error,\n            responseMeta(opts) {\n                return {\n                    status: 202,\n                    headers: {},\n                }\n            },\n            batching: {\n                enabled: true,\n            },\n        },\n        {\n            // Bun serve options\n            port: 3001,\n            fetch(request, server) {\n                // will be executed if it's not a TRPC request\n                return new Response(\"Hello world\");\n            },\n        },\n    ),\n);\n```\n\nTo add response headers like Cross-origin resource sharing (CORS) use `responseMeta` option:\n```ts\nBun.serve(\n   createBunServeHandler({\n         router: appRouter,\n         responseMeta(opts) {\n            return {\n               status: 200,\n               headers: {\n                  \"Access-Control-Allow-Origin\": \"*\",\n                  \"Access-Control-Allow-Methods\": \"GET, POST, OPTIONS\",\n                  \"Access-Control-Allow-Headers\": \"Content-Type, Authorization\"\n               }\n            };\n         }\n      }\n   )\n);\n```\n\n### createBunHttpHandler\n\nCreates a Bun HTTP handler for tRPC HTTP requests:\n\n```ts\nimport {createBunHttpHandler, CreateBunContextOptions} from 'trpc-bun-adapter';\nimport {router} from './router';\n\nconst createContext = (opts: CreateBunContextOptions) =\u003e ({\n    user: 1,\n});\n\nconst bunHandler = createBunHttpHandler({\n    router,\n    // optional arguments:\n    endpoint: '/trpc', // Default to \"\"\n    createContext,\n    onError: console.error,\n    responseMeta(opts) {\n        return {\n            status: 202,\n            headers: {},\n        }\n    },\n    batching: {\n        enabled: true,\n    },\n    emitWsUpgrades: false, // pass true to upgrade to WebSocket\n});\n\nBun.serve({\n    fetch(request, response) {\n        return bunHandler(request, response) ?? new Response(\"Not found\", {status: 404});\n    }\n});\n```\n\n### createBunWsHandler\n\nCreates a Bun WebSocket handler for tRPC websocket requests:\n\n```ts\nimport { createBunWSHandler, CreateBunContextOptions } from './src';\nimport { router } from './router';\n\nconst createContext = (opts: CreateBunContextOptions) =\u003e ({\n    user: 1,\n});\n\nconst websocket = createBunWSHandler({\n    router,\n    // optional arguments:\n    createContext,\n    onError: console.error,\n    batching: {\n        enabled: true,\n    },\n});\n\nBun.serve({\n    fetch(request, server) {\n        if (server.upgrade(request, {data: {req: request}})) {\n            return;\n        }\n\n        return new Response(\"Please use websocket protocol\", {status: 404});\n    },\n    websocket,\n});\n```\n\n### CreateBunContextOptions\n\nTo ensure your router recognizes the context type, define a `createContext` function utilizing the `CreateBunContextOptions` type:\n\n```ts\nimport { initTRPC } from '@trpc/server';\nimport type { CreateBunContextOptions } from \"src/createBunHttpHandler\";\n\nexport const createContext = async (opts: CreateBunContextOptions) =\u003e {\n    return {\n        authorization: req.headers.get('Authorization')\n    };\n};\n```\n\nWith `createContext` defined, you can use it in your router to access the context, such as the authorization information:\n```ts\nconst t = initTRPC.context\u003ctypeof createContext\u003e().create();\n\nexport const router = t.router({\n    session: t.procedure.query(({ ctx }) =\u003e ctx.authorization),\n});\n```\n\nFinally, pass your `createContext` function besides `router` to `createBunHttpHandler`. \nThis integrates your custom context into the HTTP handler setup:\n```ts\ncreateBunHttpHandler({\n    router,\n    createContext,\n})\n```\n\nRead more documentation about tRPC contexts here: [Contexts](https://trpc.io/docs/server/context)\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](https://github.com/cah4a/trpc-bun-adapter/blob/main/LICENSE) file for details.\n\n## Contributing\n\nContributions are welcome! Feel free to open issues and pull requests.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcah4a%2Ftrpc-bun-adapter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcah4a%2Ftrpc-bun-adapter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcah4a%2Ftrpc-bun-adapter/lists"}