Skip to content

Commit

Permalink
fix(api): resolve getByBlockId and getBySlot procedure `trpc-open…
Browse files Browse the repository at this point in the history
…api` issues (#711)

* fix(api): resolve `getByBlockId` openapi param issue

Use zod string and then transform value by applying block id schemas as `trpc-openapi` only allows ZodString, ZodNumber, ZodBoolean, ZodBigInt or ZodDate schemas.

* fix(api): add missing output schema to `getBySlot`

* style(api): add explanatory comment
  • Loading branch information
PJColombo authored Feb 5, 2025
1 parent b90971b commit e9a35d7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
20 changes: 19 additions & 1 deletion packages/api/src/routers/block/getByBlockId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,25 @@ const blockHashSchema = hashSchema.refine((value) => value.length === 66, {

const blockNumberSchema = z.coerce.number().int().positive();

const blockIdSchema = z.union([blockHashSchema, blockNumberSchema]);
// We use zod's string schema and then parse the value by applying block hash or block number
// schemas as `trpc-opeanpi` doesn't support union types yet.
const blockIdSchema = z.string().transform((value, ctx) => {
const parsedHash = blockHashSchema.safeParse(value);
const parsedBlockNumber = blockNumberSchema.safeParse(value);

if (parsedHash.success) {
return parsedHash.data;
}

if (parsedBlockNumber.success) {
return parsedBlockNumber.data;
}

ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Block id must be a valid block number or block hash",
});
});

const inputSchema = z
.object({
Expand Down
5 changes: 4 additions & 1 deletion packages/api/src/routers/block/getBySlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from "../../middlewares/withFilters";
import { publicProcedure } from "../../procedures";
import type { BlockIdField } from "./common";
import { fetchBlock, serializeBlock } from "./common";
import { fetchBlock, serializeBlock, serializedBlockSchema } from "./common";

const inputSchema = z
.object({
Expand All @@ -21,6 +21,8 @@ const inputSchema = z
.merge(withSortFilterSchema)
.merge(createExpandsSchema(["transaction", "blob", "blob_data"]));

const outputSchema = serializedBlockSchema;

export const getBySlot = publicProcedure
.meta({
openapi: {
Expand All @@ -31,6 +33,7 @@ export const getBySlot = publicProcedure
},
})
.input(inputSchema)
.output(outputSchema)
.use(withExpands)
.use(withFilters)
.query(
Expand Down

0 comments on commit e9a35d7

Please sign in to comment.