Skip to content

Commit

Permalink
explorer logo api
Browse files Browse the repository at this point in the history
  • Loading branch information
igorls committed Nov 9, 2024
1 parent c8fcba5 commit 9199353
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/api/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,6 @@ export function registerRoutes(server: FastifyInstance) {
addRedirect(server, '/v2/history', '/v2/docs/index.html#/history');
addRedirect(server, '/v2/state', '/v2/docs/index.html#/state');
addRedirect(server, '/v1/chain', '/v2/docs/index.html#/chain');
addRedirect(server, '/explorer', '/v2/explore');
addRedirect(server, '/explore', '/v2/explore');
// addRedirect(server, '/explorer', '/v2/explore');
// addRedirect(server, '/explore', '/v2/explore');
}
27 changes: 27 additions & 0 deletions src/api/routes/v2/explorer_logo/explorer_logo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";
import {request as undiciRequest} from 'undici';

let logo: Buffer | null = null;
let contentType: string | null = null;

export function explorerLogoHandler(fastify: FastifyInstance, route: string) {
return async (_: FastifyRequest, reply: FastifyReply) => {
if (logo !== null && contentType !== null) {
reply.header('Content-Type', contentType);
reply.send(logo);
} else {
const {body, statusCode, headers} = await undiciRequest(fastify.manager.config.api.chain_logo_url);
if (statusCode !== 200) {
reply.code(statusCode).send({
error: 'Not Found',
message: 'The requested resource could not be found'
});
} else {
logo = Buffer.from(await body.arrayBuffer());
contentType = headers['content-type'] as string;
reply.header('Content-Type', contentType);
reply.send(logo);
}
}
}
}
15 changes: 15 additions & 0 deletions src/api/routes/v2/explorer_logo/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {FastifyInstance, FastifySchema} from "fastify";
import {addApiRoute, getRouteName} from "../../../helpers/functions.js";
import {explorerLogoHandler} from "./explorer_logo.js";

export default function (fastify: FastifyInstance, opts: any, next: () => void) {
const schema: FastifySchema = {hide: true};
addApiRoute(
fastify,
'GET',
getRouteName(import.meta.filename),
explorerLogoHandler,
schema
);
next();
}

0 comments on commit 9199353

Please sign in to comment.