Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hono env context in @hono/trpc-server createContext does not have type bindings #694

Open
tsatsujnr139 opened this issue Aug 16, 2024 · 1 comment

Comments

@tsatsujnr139
Copy link

tsatsujnr139 commented Aug 16, 2024

Using the example from the trpc server middleware for hono docs, in the createContext example the env is typed as Context<any, any, {}> is there a way to get the type safe bindings same as when using the hono routes directly so that var1 in the example below is typesafe?

image

import { trpcServer } from "@hono/trpc-server";
import { initTRPC } from "@trpc/server";
import { Hono } from "hono";
import { z } from "zod";

type Bindings = {
	DB: D1Database;
};
type HonoContext = {
	env: Bindings;
};

const t = initTRPC.context<HonoContext>().create();

const publicProcedure = t.procedure;
const router = t.router;

const app = new Hono<{ Bindings: Bindings }>();

export const appRouter = router({
	usersCount: publicProcedure.query(async ({ input, ctx }) => {
		const result = await ctx.env.DB.prepare("SELECT count(*) from user;").all();
		return result?.results[0]?.count;
	}),
});

app.use(
	"/trpc/*",
	trpcServer({
		router: appRouter,
		createContext: (_opts, c) => ({
			// c is the hono context
			var1: c.env.DB, // This is not typesafe
			var2: c.req.header("X-VAR2"),
		}),
	}),
);

export type AppRouter = typeof appRouter;
@limzykenneth
Copy link

May be related but I'm seeing similar issue with @hono/zod-validator where the bindings defined on the app is not present in the hook callback's context parameter passed to the zValidator() function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants