oRPC HTTP Adapter
How to use oRPC over HTTP with various server and client adapters.
Файлы2
Открыть в GitHubSKILL.mdskills/orpc-http/SKILL.md
GitHubHTTP
oRPC includes built-in HTTP support, making it easy to expose RPC endpoints.
Server Adapters
| Adapter | Target |
|---|---|
fetch |
MDN Fetch API (Browser, Bun, Deno, Cloudflare Workers) |
node |
Node.js built-in http/http2 |
fastify |
Fastify |
aws-lambda |
AWS Lambda |
Node.js Example
import { createServer } from 'node:http'
import { RPCHandler } from '@orpc/server/node'
import { CORSPlugin } from '@orpc/server/plugins'
import { onError } from '@orpc/server'
const handler = new RPCHandler(router, {
plugins: [new CORSPlugin()],
interceptors: [onError(e => console.error(e))],
})
const server = createServer(async (req, res) => {
const { matched } = await handler.handle(req, res, {
prefix: '/rpc',
context: {}
})
if (matched) return
res.statusCode = 404
res.end('Not found')
})
server.listen(3000)
Bun Example
Bun.serve({
async fetch(request: Request) {
const { matched, response } = await handler.handle(request, {
prefix: '/rpc',
context: {}
})
if (matched) return response
return new Response('Not found', { status: 404 })
}
})
Cloudflare Workers Example
export default {
async fetch(request: Request, env: any, ctx: ExecutionContext) {
const { matched, response } = await handler.handle(request, {
prefix: '/rpc',
context: {}
})
if (matched) return response
return new Response('Not found', { status: 404 })
}
}
AWS Lambda Example
import { APIGatewayProxyEventV2 } from 'aws-lambda'
import { RPCHandler } from '@orpc/server/aws-lambda'
export const handler = awslambda.streamifyResponse<APIGatewayProxyEventV2>(async (event, responseStream, context) => {
const { matched } = await rpcHandler.handle(event, responseStream, {
prefix: '/rpc',
context: {}
})
if (matched) return
awslambda.HttpResponseStream.from(responseStream, { statusCode: 404 })
responseStream.write('Not found')
responseStream.end()
})
Client Adapters
| Adapter | Target |
|---|---|
fetch |
MDN Fetch API |
import { RPCLink } from '@orpc/client/fetch'
const link = new RPCLink({
url: 'http://localhost:3000/rpc',
headers: () => ({ 'x-api-key': 'my-api-key' }),
})
Event Iterator Options
HTTP adapters provide reliability features for streaming Event Iterators:
const handler = new RPCHandler(router, {
eventIteratorInitialCommentEnabled: true,
eventIteratorInitialComment: 'start',
eventIteratorKeepAliveEnabled: true,
eventIteratorKeepAliveInterval: 5000,
eventIteratorKeepAliveComment: '',
})
Initial Comment
Sends comment when stream starts to flush response headers early.
eventIteratorInitialCommentEnabled(default:true)eventIteratorInitialComment(default:'')
Keep-Alive Comments
Sends periodic comments during inactivity to prevent timeouts.
eventIteratorKeepAliveEnabled(default:true)eventIteratorKeepAliveInterval(default:5000)eventIteratorKeepAliveComment(default:'')