Marksmith
Marksmith
TtRPC
Created by Marksmith on 7/26/2023 in #❓-help
Does "enabled" in useInfiniteQuery work?
I am trying to prevent the hook to fetch data using "enabled" parameter. It works in useQuery, but useInfiniteQuery tried to load the data anyway.
const {data} = trpc.testRouter.testInfiniteQuery.useInfiniteQuery({
test: true,
}, {
enabled: false
})
const {data} = trpc.testRouter.testInfiniteQuery.useInfiniteQuery({
test: true,
}, {
enabled: false
})
What am I doing wrong?
2 replies
TtRPC
Created by Marksmith on 7/10/2023 in #❓-help
React tsc starts checking server types
My tsconfig files are different on the server and client. When I run "tsc" on react client, It is trying to check AppRouter types from the server. Is it possible to make him stop checking server types on compile?
3 replies
TtRPC
Created by Marksmith on 6/13/2023 in #❓-help
applyWSSHandler yells at context error in express
I used the example code from docs. My applyWSSHandler code is:
const handler = applyWSSHandler({wss, router: appRouter, createContext});
const handler = applyWSSHandler({wss, router: appRouter, createContext});
I see the next error on createContext:
TS2322: Type '({ req, res, }: CreateExpressContextOptions) => Promise<{ req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>; res: Response<...>; }>' is not assignable to type 'NodeHTTPCreateContextFn<CreateRouterInner<RootConfig<{ ctx: { req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>; res: Response<any, Record<string, any>>; }; meta: object; errorShape: { ...; }; transformer
TS2322: Type '({ req, res, }: CreateExpressContextOptions) => Promise<{ req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>; res: Response<...>; }>' is not assignable to type 'NodeHTTPCreateContextFn<CreateRouterInner<RootConfig<{ ctx: { req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>; res: Response<any, Record<string, any>>; }; meta: object; errorShape: { ...; }; transformer
The createContext function was copied from examples as well. I just made it to return req and res instead of the empty object:
export async function createContext({req, res}: trpcExpress.CreateExpressContextOptions) {return {req, res}}
export async function createContext({req, res}: trpcExpress.CreateExpressContextOptions) {return {req, res}}
How should I change the createContext function to make it work? I have a problem while trying to write tests probably because of this too.
1 replies
TtRPC
Created by Marksmith on 6/10/2023 in #❓-help
Creating trpc context (express server) for testing without req and res objects.
I have seen a few examples of tests with next auth or t3 stack that is using next-auth under the hood. My problem is that I don't understand what should my inner context object contain, because I'm using express server 😦 For example, this is my context and t object. Req and res are mandatory:
export async function createContext({req,res}: trpcExpress.CreateExpressContextOptions) {
return {req, res};
}

export type Context = inferAsyncReturnType<typeof createContext>;

export const t = initTRPC.context<Context>().create({
transformer: superjson,
});
export async function createContext({req,res}: trpcExpress.CreateExpressContextOptions) {
return {req, res};
}

export type Context = inferAsyncReturnType<typeof createContext>;

export const t = initTRPC.context<Context>().create({
transformer: superjson,
});
Then, my require auth middleware function is:
export const isAuthedMiddleware = t.middleware(async ({ctx, next}) => {
const {req, res,} = ctx;

const token: string | undefined = req.headers.authorization?.split(" ")[1];
if (!token) {
throw new TRPCError({code: "UNAUTHORIZED"});
}
// my internal check function
if (await authCheckFunction(token) === null) {
throw new TRPCError({
code: "UNAUTHORIZED",
});
}
const {
userId,
sessionId, role
} = await anotherAuthCheckFunction(token, email);
return next({
ctx: {
userEmail: email,
userId,
role
},
});

});
export const isAuthedMiddleware = t.middleware(async ({ctx, next}) => {
const {req, res,} = ctx;

const token: string | undefined = req.headers.authorization?.split(" ")[1];
if (!token) {
throw new TRPCError({code: "UNAUTHORIZED"});
}
// my internal check function
if (await authCheckFunction(token) === null) {
throw new TRPCError({
code: "UNAUTHORIZED",
});
}
const {
userId,
sessionId, role
} = await anotherAuthCheckFunction(token, email);
return next({
ctx: {
userEmail: email,
userId,
role
},
});

});
Of course, I cannot create called with this setup, because, I will need to have req and res object. It's not possible to use next-auth as well. If you have any example express trpc testing code, it would be awesome if you showed me how this can be achieved. If not, the question is: How do I need to rewrite the context creation and what should the inner context accept and return? Should I get the headers and other data before the inner context?
2 replies