Ayush Goyal
Ayush Goyal
Explore posts from servers
TtRPC
Created by Ayush Goyal on 8/29/2023 in #❓-help
TRPC retries with new app dir
let { data: name, isLoading, isFetching, isError, error } = trpc.home.home.useQuery();
let { data: name, isLoading, isFetching, isError, error } = trpc.home.home.useQuery();
I am using React query with App directory in next js , can somebody tell me how can i aplly retry:1 , because by default it is set to 3
2 replies
TtRPC
Created by Ayush Goyal on 8/29/2023 in #❓-help
opts.input is of type undifined
register: publicProcedure.input(
RegisterSchema
).mutation(async (opts) => {
//checking for existing user
const existingUser = await db.select().from(user).where(eq(user.email, opts.input.email));
register: publicProcedure.input(
RegisterSchema
).mutation(async (opts) => {
//checking for existing user
const existingUser = await db.select().from(user).where(eq(user.email, opts.input.email));
RegisterSchema is a zod schema comming from an another file it is giving me the error opts.input is of type undefined
2 replies
TtRPC
Created by Ayush Goyal on 7/27/2023 in #❓-help
what is "use" keyword
const baseProcedure = t.procedure
.input(z.object({ townName: z.string() }))
.use((opts) => {
const input = opts.input;

const input: {
townName: string;
}

console.log(`Handling request with user from: ${input.townName}`);

return opts.next();
});
const baseProcedure = t.procedure
.input(z.object({ townName: z.string() }))
.use((opts) => {
const input = opts.input;

const input: {
townName: string;
}

console.log(`Handling request with user from: ${input.townName}`);

return opts.next();
});
what is this usekeyword any place i can read about it
5 replies
TtRPC
Created by Ayush Goyal on 6/10/2023 in #❓-help
What is error formating
I understand error handeling but what is error formating , found it in trcp docs have'nt seen that thing before
3 replies
TtRPC
Created by Ayush Goyal on 6/10/2023 in #❓-help
where does opts.path and opts.type came from ? i guess by default it only stores ctx ,next and input
const loggerMiddleware = middleware(async (opts) => {
const start = Date.now();
 
const result = await opts.next();
 
const durationMs = Date.now() - start;
const meta = { path: opts.path, type: opts.type, durationMs };
 
result.ok
? console.log('OK request timing:', meta)
: console.error('Non-OK request timing', meta);
 
return result;
});
 
export const loggedProcedure = publicProcedure.use(loggerMiddleware);
const loggerMiddleware = middleware(async (opts) => {
const start = Date.now();
 
const result = await opts.next();
 
const durationMs = Date.now() - start;
const meta = { path: opts.path, type: opts.type, durationMs };
 
result.ok
? console.log('OK request timing:', meta)
: console.error('Non-OK request timing', meta);
 
return result;
});
 
export const loggedProcedure = publicProcedure.use(loggerMiddleware);
the code is from middleware documentation
4 replies
TtRPC
Created by Ayush Goyal on 6/10/2023 in #❓-help
Passing objects to the next() function of the middle ware
// -------------------------------------------------
// @filename: context.ts
// -------------------------------------------------
import type { inferAsyncReturnType } from '@trpc/server';
import type { CreateNextContextOptions } from '@trpc/server/adapters/next';
import { getSession } from 'next-auth/react';
 
/**
* Creates context for an incoming request
* @link https://trpc.io/docs/context
*/
export async function createContext(opts: CreateNextContextOptions) {
const session = await getSession({ req: opts.req });
 
return {
session,
};
}
 
export type Context = inferAsyncReturnType<typeof createContext>;
 
// -------------------------------------------------
// @filename: trpc.ts
// -------------------------------------------------
import { initTRPC, TRPCError } from '@trpc/server';
import { Context } from './context';
 
const t = initTRPC.context<Context>().create();
 
const isAuthed = t.middleware(({ next, ctx }) => {
if (!ctx.session?.user?.email) {
throw new TRPCError({
code: 'UNAUTHORIZED',
});
}
return next({
ctx: {
// Infers the `session` as non-nullable
session: ctx.session,
},
});
});
 
export const middleware = t.middleware;
export const router = t.router;
 
/**
* Unprotected procedure
*/
export const publicProcedure = t.procedure;
 
/**
* Protected procedure
*/
export const protectedProcedure = t.procedure.use(isAuthed);
// -------------------------------------------------
// @filename: context.ts
// -------------------------------------------------
import type { inferAsyncReturnType } from '@trpc/server';
import type { CreateNextContextOptions } from '@trpc/server/adapters/next';
import { getSession } from 'next-auth/react';
 
/**
* Creates context for an incoming request
* @link https://trpc.io/docs/context
*/
export async function createContext(opts: CreateNextContextOptions) {
const session = await getSession({ req: opts.req });
 
return {
session,
};
}
 
export type Context = inferAsyncReturnType<typeof createContext>;
 
// -------------------------------------------------
// @filename: trpc.ts
// -------------------------------------------------
import { initTRPC, TRPCError } from '@trpc/server';
import { Context } from './context';
 
const t = initTRPC.context<Context>().create();
 
const isAuthed = t.middleware(({ next, ctx }) => {
if (!ctx.session?.user?.email) {
throw new TRPCError({
code: 'UNAUTHORIZED',
});
}
return next({
ctx: {
// Infers the `session` as non-nullable
session: ctx.session,
},
});
});
 
export const middleware = t.middleware;
export const router = t.router;
 
/**
* Unprotected procedure
*/
export const publicProcedure = t.procedure;
 
/**
* Protected procedure
*/
export const protectedProcedure = t.procedure.use(isAuthed);
i never knew if one can pass arguments to the next() function , my doubt is why are we passing them and how do we use them
4 replies
TtRPC
Created by Ayush Goyal on 6/10/2023 in #❓-help
opts object passed to mutation , i can not understand where it came from
export const authorizedProcedure = publicProcedure
.input(z.object({ townName: z.string() }))
.use((opts) => {
if (opts.input.townName !== 'Pucklechurch') {
throw new TRPCError({
code: 'FORBIDDEN',
message: "We don't take kindly to out-of-town folk",
});
}
 
return opts.next();
});
 
export const appRouter = t.router({
hello: authorizedProcedure.query(() => {
return {
message: 'hello world',
};
}),
goodbye: authorizedProcedure.mutation(async (opts) => {
await opts.ctx.signGuestBook();
 
return {
message: 'goodbye!',
};
}),
});
export const authorizedProcedure = publicProcedure
.input(z.object({ townName: z.string() }))
.use((opts) => {
if (opts.input.townName !== 'Pucklechurch') {
throw new TRPCError({
code: 'FORBIDDEN',
message: "We don't take kindly to out-of-town folk",
});
}
 
return opts.next();
});
 
export const appRouter = t.router({
hello: authorizedProcedure.query(() => {
return {
message: 'hello world',
};
}),
goodbye: authorizedProcedure.mutation(async (opts) => {
await opts.ctx.signGuestBook();
 
return {
message: 'goodbye!',
};
}),
});
here in goodbye route , where does the opts object come from , in authorized proccesor middleware , i get it otps come from the input but in goodbye i am not able to understand where opts come from , maybe
return opts.next();
return opts.next();
passes it to the goodbye route
5 replies