Skip to content

Instantly share code, notes, and snippets.

@ThimoDEV
Created June 1, 2025 11:59
Show Gist options
  • Save ThimoDEV/54c69087148398e5dc82d4f8fa70dcbc to your computer and use it in GitHub Desktop.
Save ThimoDEV/54c69087148398e5dc82d4f8fa70dcbc to your computer and use it in GitHub Desktop.
Implement orpc with nitro (better-t-stack)
- Create a file [...orpc].ts with the following:
const handler = new RPCHandler(appRouter);
export default defineEventHandler(
async (event: H3Event) => {
const context = await createContext(event);
const webRequest = toWebRequest(event);
const { matched, response } =
await handler.handle(webRequest, {
prefix: "/api/orpc",
context,
});
if (matched) {
return response;
}
}
);
Also add a middleware file to handle CORS:
const allowedOrigin = "http://localhost:3000";
export default defineEventHandler(
(event: H3Event) => {
setHeader(
event,
"Access-Control-Allow-Origin",
allowedOrigin
);
setHeader(
event,
"Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, OPTIONS"
);
setHeader(
event,
"Access-Control-Allow-Headers",
"Content-Type, Authorization, X-Requested-With"
);
setHeader(
event,
"Access-Control-Allow-Credentials",
"true"
);
// Handle preflight OPTIONS request
if (event.method === "OPTIONS") {
event.node.res.statusCode = 204;
event.node.res.end();
}
}
);
For the rest take whats already with hono, so the approuter, createContext function, orpc.ts file and you are good to go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment