From f742be8efe67f8e6716d43afc1ae227faea55fa7 Mon Sep 17 00:00:00 2001 From: Hank Stoever Date: Wed, 27 Sep 2023 12:38:02 -0700 Subject: [PATCH] feat: openapi endpoint for all inscribed names --- api/src/app.ts | 5 ++ api/src/routes/bridge-routes.ts | 29 ++++++++++++ api/src/routes/trpc/bridge-router.ts | 68 ++++++++++++++-------------- api/src/swagger.ts | 1 + 4 files changed, 68 insertions(+), 35 deletions(-) create mode 100644 api/src/routes/bridge-routes.ts diff --git a/api/src/app.ts b/api/src/app.ts index 92e8074..e2f83f8 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -4,6 +4,7 @@ import { prismaPlugin } from './prisma-plugin'; import { aliasRoutes } from './routes/alias-routes'; import { metadataRoutes } from './routes/metadata-routes'; import { appRouter } from './routes/trpc'; +import { bridgeRoutes } from './routes/bridge-routes'; import cors from '@fastify/cors'; import { fastifyTRPCPlugin } from '@trpc/server/adapters/fastify'; import { createContext } from './routes/trpc/context'; @@ -122,6 +123,10 @@ export async function makeApp({ prefix: '/v1', }); + await app.register(bridgeRoutes, { + prefix: '/l1', + }); + app.get('/', (req, res) => { return res.send({ success: true }); }); diff --git a/api/src/routes/bridge-routes.ts b/api/src/routes/bridge-routes.ts new file mode 100644 index 0000000..46c8138 --- /dev/null +++ b/api/src/routes/bridge-routes.ts @@ -0,0 +1,29 @@ +import { createContext } from '@routes/trpc/context'; +import type { FastifyPlugin } from './api-types'; +import { bridgeRouter, inscribedNamesResultsSchema } from './trpc/bridge-router'; + +export const bridgeRoutes: FastifyPlugin = (fastify, opts, done) => { + fastify.get( + '/inscribed-names', + { + schema: { + description: 'Fetch all BNS names inscribed and bridge to L1', + summary: 'Fetch all inscribed names', + tags: ['L1'], + response: { + 200: inscribedNamesResultsSchema, + }, + }, + }, + async (req, res) => { + const caller = bridgeRouter.createCaller(createContext({ req, res })); + const inscribedNames = await caller.inscribedNames(); + return res.status(200).send(inscribedNames); + } + ); + + // fastify.get('/inscription-by-name/:name', async (req, res) => { + // } + + done(); +}; diff --git a/api/src/routes/trpc/bridge-router.ts b/api/src/routes/trpc/bridge-router.ts index b351413..3aed54e 100644 --- a/api/src/routes/trpc/bridge-router.ts +++ b/api/src/routes/trpc/bridge-router.ts @@ -17,46 +17,44 @@ const inscribedNameResult = z.object({ txid: z.string(), }); +export const inscribedNamesResultsSchema = z.object({ + results: z.array(inscribedNameResult), +}); + export const bridgeRouter = router({ - inscribedNames: procedure - .output( - z.object({ - results: z.array(inscribedNameResult), + inscribedNames: procedure.output(inscribedNamesResultsSchema).query(async ({ ctx }) => { + expectDb(ctx.prisma); + expectDb(ctx.bnsxDb); + + const inscribedNames = ( + await ctx.bnsxDb.inscribedNames.findMany({ + include: { + name: true, + }, + orderBy: { + blockHeight: 'desc', + }, }) ) - .query(async ({ ctx }) => { - expectDb(ctx.prisma); - expectDb(ctx.bnsxDb); + .map(row => { + if (row.name === null) return null; - const inscribedNames = ( - await ctx.bnsxDb.inscribedNames.findMany({ - include: { - name: true, - }, - orderBy: { - blockHeight: 'desc', - }, - }) - ) - .map(row => { - if (row.name === null) return null; - - const name = convertDbName(row.name); - - return { - inscriptionId: inscriptionBuffToId(hexToBytes(row.inscription_id)), - id: Number(row.id), - name: name.combined, - blockHeight: row.blockHeight, - txid: bytesToHex(row.txid), - }; - }) - .filter((n): n is NonNullable => n !== null); + const name = convertDbName(row.name); - return { - results: inscribedNames, - }; - }), + return { + inscriptionId: inscriptionBuffToId(hexToBytes(row.inscription_id)), + id: Number(row.id), + name: name.combined, + blockHeight: row.blockHeight, + txid: bytesToHex(row.txid), + }; + }) + .filter((n): n is NonNullable => n !== null); + + return { + results: inscribedNames, + }; + }), getInscriptionByName: procedure .input(z.object({ name: z.string() })) diff --git a/api/src/swagger.ts b/api/src/swagger.ts index 75039e4..001bf10 100644 --- a/api/src/swagger.ts +++ b/api/src/swagger.ts @@ -22,6 +22,7 @@ export const OpenApiOptions: SwaggerOptions = { tags: [ { name: 'BNS', description: 'BNS Endpoints' }, { name: 'Backwards Compatible', description: 'Stacks API compatible Endpoints' }, + { name: 'L1', description: 'Endpoints for querying the BNS-L1 bridge' }, ], }, hideUntagged: true,