@@ -3,16 +3,16 @@ import z from "zod/v4";
33
44import {
55 buildPageContext ,
6+ buildResultOkTimestamped ,
7+ buildResultServiceUnavailable ,
68 type Node ,
79 RECORDS_PER_PAGE_DEFAULT ,
810 RECORDS_PER_PAGE_MAX ,
911 type RegistrarActionsFilter ,
1012 RegistrarActionsOrders ,
11- RegistrarActionsResponseCodes ,
12- type RegistrarActionsResponseError ,
13- type RegistrarActionsResponseOk ,
13+ ResultCodes ,
1414 registrarActionsFilter ,
15- serializeRegistrarActionsResponse ,
15+ serializeNamedRegistrarActions ,
1616} from "@ensnode/ensnode-sdk" ;
1717import {
1818 makeLowercaseAddressSchema ,
@@ -26,6 +26,10 @@ import { validate } from "@/lib/handlers/validate";
2626import { factory } from "@/lib/hono-factory" ;
2727import { makeLogger } from "@/lib/logger" ;
2828import { findRegistrarActions } from "@/lib/registrar-actions/find-registrar-actions" ;
29+ import {
30+ resultCodeToHttpStatusCode ,
31+ resultIntoHttpResponse ,
32+ } from "@/lib/result/result-into-http-response" ;
2933import { registrarActionsApiMiddleware } from "@/middleware/registrar-actions.middleware" ;
3034
3135const app = factory . createApp ( ) ;
@@ -161,45 +165,48 @@ app.get(
161165 summary : "Get Registrar Actions" ,
162166 description : "Returns all registrar actions with optional filtering and pagination" ,
163167 responses : {
164- 200 : {
168+ [ resultCodeToHttpStatusCode ( ResultCodes . Ok ) ] : {
165169 description : "Successfully retrieved registrar actions" ,
166170 } ,
167- 400 : {
171+ [ resultCodeToHttpStatusCode ( ResultCodes . InvalidRequest ) ] : {
168172 description : "Invalid query" ,
169173 } ,
170- 500 : {
171- description : "Internal server error " ,
174+ [ resultCodeToHttpStatusCode ( ResultCodes . ServiceUnavailable ) ] : {
175+ description : "Registrar Actions API is unavailable at the moment " ,
172176 } ,
173177 } ,
174178 } ) ,
175179 validate ( "query" , registrarActionsQuerySchema ) ,
176180 async ( c ) => {
177181 try {
182+ // Middleware ensures indexingStatus is available and not an Error
183+ // This check is for TypeScript type safety
184+ if ( ! c . var . indexingStatus || c . var . indexingStatus instanceof Error ) {
185+ throw new Error ( "Invariant violation: indexingStatus should be validated by middleware" ) ;
186+ }
187+
178188 const query = c . req . valid ( "query" ) ;
179189 const { registrarActions, pageContext } = await fetchRegistrarActions ( undefined , query ) ;
180190
181- // respond with success response
182- return c . json (
183- serializeRegistrarActionsResponse ( {
184- responseCode : RegistrarActionsResponseCodes . Ok ,
185- registrarActions,
191+ // Get the accurateAsOf timestamp from the slowest chain indexing cursor
192+ const accurateAsOf = c . var . indexingStatus . snapshot . slowestChainIndexingCursor ;
193+
194+ const result = buildResultOkTimestamped (
195+ {
196+ registrarActions : serializeNamedRegistrarActions ( registrarActions ) ,
186197 pageContext,
187- } satisfies RegistrarActionsResponseOk ) ,
198+ } ,
199+ accurateAsOf ,
188200 ) ;
201+
202+ return resultIntoHttpResponse ( c , result ) ;
189203 } catch ( error ) {
190204 const errorMessage = error instanceof Error ? error . message : "Unknown error" ;
191205 logger . error ( errorMessage ) ;
192206
193- // respond with 500 error response
194- return c . json (
195- serializeRegistrarActionsResponse ( {
196- responseCode : RegistrarActionsResponseCodes . Error ,
197- error : {
198- message : `Registrar Actions API Response is unavailable` ,
199- } ,
200- } satisfies RegistrarActionsResponseError ) ,
201- 500 ,
202- ) ;
207+ const result = buildResultServiceUnavailable ( "Registrar Actions API Response is unavailable" ) ;
208+
209+ return resultIntoHttpResponse ( c , result ) ;
203210 }
204211 } ,
205212) ;
@@ -244,14 +251,14 @@ app.get(
244251 description :
245252 "Returns registrar actions filtered by parent node hash with optional additional filtering and pagination" ,
246253 responses : {
247- 200 : {
254+ [ resultCodeToHttpStatusCode ( ResultCodes . Ok ) ] : {
248255 description : "Successfully retrieved registrar actions" ,
249256 } ,
250- 400 : {
251- description : "Invalid input " ,
257+ [ resultCodeToHttpStatusCode ( ResultCodes . InvalidRequest ) ] : {
258+ description : "Invalid query " ,
252259 } ,
253- 500 : {
254- description : "Internal server error " ,
260+ [ resultCodeToHttpStatusCode ( ResultCodes . ServiceUnavailable ) ] : {
261+ description : "Registrar Actions API is unavailable at the moment " ,
255262 } ,
256263 } ,
257264 } ) ,
@@ -279,29 +286,22 @@ app.get(
279286 // Get the accurateAsOf timestamp from the slowest chain indexing cursor
280287 const accurateAsOf = c . var . indexingStatus . snapshot . slowestChainIndexingCursor ;
281288
282- // respond with success response
283- return c . json (
284- serializeRegistrarActionsResponse ( {
285- responseCode : RegistrarActionsResponseCodes . Ok ,
286- registrarActions,
289+ const result = buildResultOkTimestamped (
290+ {
291+ registrarActions : serializeNamedRegistrarActions ( registrarActions ) ,
287292 pageContext,
288- accurateAsOf ,
289- } satisfies RegistrarActionsResponseOk ) ,
293+ } ,
294+ accurateAsOf ,
290295 ) ;
296+
297+ return resultIntoHttpResponse ( c , result ) ;
291298 } catch ( error ) {
292299 const errorMessage = error instanceof Error ? error . message : "Unknown error" ;
293300 logger . error ( errorMessage ) ;
294301
295- // respond with 500 error response
296- return c . json (
297- serializeRegistrarActionsResponse ( {
298- responseCode : RegistrarActionsResponseCodes . Error ,
299- error : {
300- message : `Registrar Actions API Response is unavailable` ,
301- } ,
302- } satisfies RegistrarActionsResponseError ) ,
303- 500 ,
304- ) ;
302+ const result = buildResultServiceUnavailable ( "Registrar Actions API Response is unavailable" ) ;
303+
304+ return resultIntoHttpResponse ( c , result ) ;
305305 }
306306 } ,
307307) ;
0 commit comments