Skip to content

Commit

Permalink
fix: use specific handler for getBlock getBlockV2
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Aug 31, 2023
1 parent 16588b9 commit 4f58fc5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
14 changes: 8 additions & 6 deletions packages/api/src/beacon/routes/beacon/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export type Api = {
getBlock(
blockId: BlockId,
format?: BlockFormat
): Promise<ApiClientResponse<{[HttpStatusCode.OK]: {data: allForks.SignedBeaconBlock}}>>;
): Promise<ApiClientResponse<{[HttpStatusCode.OK]: Uint8Array | {data: allForks.SignedBeaconBlock}}>>;

/**
* Get block
Expand All @@ -79,11 +79,13 @@ export type Api = {
): Promise<
ApiClientResponse<
{
[HttpStatusCode.OK]: {
data: allForks.SignedBeaconBlock;
executionOptimistic: ExecutionOptimistic;
version: ForkName;
};
[HttpStatusCode.OK]:
| Uint8Array
| {
data: allForks.SignedBeaconBlock;
executionOptimistic: ExecutionOptimistic;
version: ForkName;
};
},
HttpStatusCode.BAD_REQUEST | HttpStatusCode.NOT_FOUND
>
Expand Down
39 changes: 37 additions & 2 deletions packages/api/src/beacon/server/beacon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,41 @@ import {ServerRoutes, getGenericJsonServer} from "../../utils/server/index.js";
import {ServerApi} from "../../interfaces.js";

export function getRoutes(config: ChainForkConfig, api: ServerApi<Api>): ServerRoutes<Api, ReqTypes> {
// All routes return JSON, use a server auto-generator
return getGenericJsonServer<ServerApi<Api>, ReqTypes>({routesData, getReturnTypes, getReqSerializers}, config, api);
const reqSerializers = getReqSerializers(config);
const returnTypes = getReturnTypes();

// Most of routes return JSON, use a server auto-generator
const serverRoutes = getGenericJsonServer<ServerApi<Api>, ReqTypes>(
{routesData, getReturnTypes, getReqSerializers},
config,
api
);
return {
...serverRoutes,
// Non-JSON routes. Return JSON or binary depending on "accept" header
getBlock: {
...serverRoutes.getBlock,
handler: async (req) => {
const response = await api.getBlock(...reqSerializers.getBlock.parseReq(req));
if (response instanceof Uint8Array) {
// Fastify 3.x.x will automatically add header `Content-Type: application/octet-stream` if Buffer
return Buffer.from(response);
} else {
return returnTypes.getBlock.toJson(response);
}
},
},
getBlockV2: {
...serverRoutes.getBlockV2,
handler: async (req) => {
const response = await api.getBlockV2(...reqSerializers.getBlockV2.parseReq(req));
if (response instanceof Uint8Array) {
// Fastify 3.x.x will automatically add header `Content-Type: application/octet-stream` if Buffer
return Buffer.from(response);
} else {
return returnTypes.getBlockV2.toJson(response);
}
},
},
};
}

0 comments on commit 4f58fc5

Please sign in to comment.