DangerZone
DangerZone
TtRPC
Created by DangerZone on 7/3/2024 in #❓-help
Property 'error' does not exist on type 'MiddlewareOKResult<any>'.
Hello, I am building an trpc adapter for nestjs, and I am trying to extend some of trpc's functionality, specifically the procedure middleware by creating a procedure class.
import {
type DataTransformerOptions,
type RootConfig,
type unsetMarker,
} from '@trpc/server';

import type { MiddlewareFunction } from '@trpc/server/src/core/middleware';

type ProcedureParams<Context = any> = {
_config: RootConfig<{
ctx: object & Context;
meta: object;
errorShape: never;
transformer: DataTransformerOptions;
}>;
_ctx_out: object & Context;
_input_in: typeof unsetMarker;
_input_out: typeof unsetMarker;
_output_in: typeof unsetMarker;
_output_out: typeof unsetMarker;
_meta: object;
}

export interface TRPCProcedure<Context = object> {
use: MiddlewareFunction<ProcedureParams<Context>, ProcedureParams<Context>>
}
import {
type DataTransformerOptions,
type RootConfig,
type unsetMarker,
} from '@trpc/server';

import type { MiddlewareFunction } from '@trpc/server/src/core/middleware';

type ProcedureParams<Context = any> = {
_config: RootConfig<{
ctx: object & Context;
meta: object;
errorShape: never;
transformer: DataTransformerOptions;
}>;
_ctx_out: object & Context;
_input_in: typeof unsetMarker;
_input_out: typeof unsetMarker;
_output_in: typeof unsetMarker;
_output_out: typeof unsetMarker;
_meta: object;
}

export interface TRPCProcedure<Context = object> {
use: MiddlewareFunction<ProcedureParams<Context>, ProcedureParams<Context>>
}
I am using the MiddlewareFunction here so it will provide the correct opts implementation. Everything works perfectly, but when I am trying to run it, I am getting a typescript error:
../../node_modules/.pnpm/@trpc+server@10.18.0/node_modules/@trpc/server/src/core/internals/procedureBuilder.ts:387:20 - error TS2339: Property 'error' does not exist on type 'MiddlewareResult<any>'.
Property 'error' does not exist on type 'MiddlewareOKResult<any>'.

387 throw result.error;
~~~~~

../../node_modules/.pnpm/@trpc+server@10.18.0/node_modules/@trpc/server/src/deprecated/internals/procedure.ts:244:20 - error TS2339: Property 'error' does not exist on type 'MiddlewareResult<any>'.
Property 'error' does not exist on type 'MiddlewareOKResult<any>'.

244 throw result.error;
~~~~~

[6:26:38 PM] Found 2 errors. Watching for file changes.
../../node_modules/.pnpm/@trpc+server@10.18.0/node_modules/@trpc/server/src/core/internals/procedureBuilder.ts:387:20 - error TS2339: Property 'error' does not exist on type 'MiddlewareResult<any>'.
Property 'error' does not exist on type 'MiddlewareOKResult<any>'.

387 throw result.error;
~~~~~

../../node_modules/.pnpm/@trpc+server@10.18.0/node_modules/@trpc/server/src/deprecated/internals/procedure.ts:244:20 - error TS2339: Property 'error' does not exist on type 'MiddlewareResult<any>'.
Property 'error' does not exist on type 'MiddlewareOKResult<any>'.

244 throw result.error;
~~~~~

[6:26:38 PM] Found 2 errors. Watching for file changes.
Anyone has an idea of how to solve this? I thought about using the ProcedureBuilder interface but couldn't make it to work.
1 replies
TtRPC
Created by DangerZone on 6/16/2024 in #❓-help
Validating inputs and outputs only via typescript
Hey everyone, I am trying to create an appRouter with ts-morph but I am having trouble with passing the zod schema/object instance to it (since I need to pass the original implementation, not the instance). I was thinking about a way to work around this and was wondering if I could somehow just pass the zod typescript type in the inputs and outputs - keep in mind that my usecase only calls for the type definitions to work, not the actual implementation.
import { initTRPC } from '@trpc/server';
import { z } from 'zod';

const t = initTRPC.create();
const publicProcedure = t.procedure;

const randomSchema = z.object({
name: z.string(),
});

type RandomType = z.infer<typeof randomSchema>;

const appRouter = t.router({
UserRouter: {
hello: publicProcedure.input<RandomType>().query((opts) => {
const name = opts.input.name;
return {
greeting: `Hello ${opts.input.name}`,
};
}),
},
});
export type AppRouter = typeof appRouter;
import { initTRPC } from '@trpc/server';
import { z } from 'zod';

const t = initTRPC.create();
const publicProcedure = t.procedure;

const randomSchema = z.object({
name: z.string(),
});

type RandomType = z.infer<typeof randomSchema>;

const appRouter = t.router({
UserRouter: {
hello: publicProcedure.input<RandomType>().query((opts) => {
const name = opts.input.name;
return {
greeting: `Hello ${opts.input.name}`,
};
}),
},
});
export type AppRouter = typeof appRouter;
here you can see I am trying to pass on;y the RandomType, but ofc I don't really know what I am doing 😄
2 replies
TtRPC
Created by DangerZone on 4/2/2023 in #❓-help
How can I generate trpc Generic Types?
I am building a trpc library, and as part of my implementation, I need the generic types of t, t.mergeRouters, t.procedure, t.router and t.middleware. I've haven't been able to locate those generic types in the @trpc/server and since trpc has such a complex type structure, would love to see an implementation for a generic set of types for the ones listed about.
4 replies
TtRPC
Created by DangerZone on 3/24/2023 in #❓-help
Express middleware router type isn't correct
3 replies