Bun Integration
Bun comes with a built-in, high-performance HTTP server that follows the Fetch API. For additional context, refer to the Fetch Server Integration guide.
Basic
ts
import { RPCHandler } from '@orpc/server/fetch'
import { CORSPlugin } from '@orpc/server/plugins'
const handler = new RPCHandler(router, {
plugins: [
new CORSPlugin()
]
})
Bun.serve({
async fetch(request: Request) {
const url = new URL(request.url)
if (url.pathname.startsWith('/rpc')) {
const { response } = await handler.handle(request, {
prefix: '/rpc',
context: {} // Provide initial context if needed
})
if (response) {
return response
}
}
return new Response('Not found', { status: 404 })
}
})
INFO
The handler
can be any supported oRPC handler, such as RPCHandler, OpenAPIHandler, or another custom handler.