Generics for Input #1331
Unanswered
masterbater
asked this question in
Q&A
Replies: 1 comment
-
I ran into a similar thing, and thought I would share what I ended up with at one point. controller.ts import { Hono } from "hono";
import { prismaMiddleware } from "../config/db";
import { getTeachers, getTeachersValidator } from "./services";
export const teacherController = new Hono();
teacherController.get("/", prismaMiddleware, getTeachersValidator, getTeachers); service.ts import { zValidator } from "@hono/zod-validator";
import { env } from "hono/adapter";
import extension from "prisma-paginate";
import { z } from "zod";
const teachersSchema = z.object({
page: z.string(),
});
export const getTeachersValidator = zValidator("query", teachersSchema)
type ValidatorContext = Parameters<typeof getTeachersValidator>[0]
export const getTeachers = async (c: ValidatorContext) => {
const { NODE_ENV } = env(c);
const prisma = c.get("prisma");
const { page } = c.req.valid("query");
const paginatePrisma = prisma.$extends(extension);
const user = await paginatePrisma.user.paginate({
page: parseInt(page),
limit: 2,
});
return c.jsonT({
...user,
hasNextPage: user.hasNextPage,
hasPrevPage: user.hasPrevPage,
});
}; Basically I made sure the validator was defined in the same place as my actual request handler. I then forward the Context generated by |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Ive been struggling to properly type an extracted function, Im a noob in typescript and just keep learning of it, im still in a phase where I keep pulling my hair when it doesnt work.
controller.ts
service.ts
This types everything except the inputs, I extracted the type of zValidator its not compatible to whatToPutHere
export const getTeachers = async (c: Context<Env, any, whatToPutHere>)
I also infer zod schema it returns this
But input expected like this
Is there a builtin helper in typescript or in hono that makes types above using like
then it generates a type like above?
Beta Was this translation helpful? Give feedback.
All reactions