diff --git a/src/rpc/createMethodHandler.ts b/src/rpc/createMethodHandler.ts index 4d6a7ccd..879cdbaf 100644 --- a/src/rpc/createMethodHandler.ts +++ b/src/rpc/createMethodHandler.ts @@ -1,14 +1,7 @@ import { z } from "zod" import type { RpcHandler } from "./rpcHandler" import { ApiVersion } from "../types/utils" - -export type RpcSchemaType = { - method: string - params: any - result: any -} - -export type RpcSchema = z.ZodType +import { bundlerRpcSchema } from "@alto/types" export type MethodHandler = { schema: T @@ -20,7 +13,9 @@ export type MethodHandler = { }) => Promise["result"]> | z.infer["result"] } -export const createMethodHandler = (handler: { +export const createMethodHandler = < + T extends typeof bundlerRpcSchema +>(handler: { schema: T method: z.infer["method"] handler: (args: { @@ -41,18 +36,26 @@ export const createMethodHandler = (handler: { schema: handler.schema, method: handler.method, handler: (args) => { - const freezeDeep = (obj: T): DeepReadonly => { + const freezeDeep = ( + obj: unknown + ): DeepReadonly["params"]> => { if (Array.isArray(obj)) { - return Object.freeze(obj.map(freezeDeep)) as DeepReadonly + return Object.freeze(obj.map(freezeDeep)) as DeepReadonly< + z.infer["params"] + > } if (obj !== null && typeof obj === "object") { const frozenObj = Object.create(Object.getPrototypeOf(obj)) for (const prop of Object.getOwnPropertyNames(obj)) { - frozenObj[prop] = freezeDeep(obj[prop as keyof T]) + frozenObj[prop] = freezeDeep( + obj[prop as string] as unknown + ) } - return Object.freeze(frozenObj) as DeepReadonly + return Object.freeze(frozenObj) as DeepReadonly< + z.infer["params"] + > } - return obj as DeepReadonly + return obj as DeepReadonly["params"]> } const frozenParams = freezeDeep(args.params)