-
Notifications
You must be signed in to change notification settings - Fork 15
refactor(ensapi): apply operation result pattern #1545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/ensnode-result-type
Are you sure you want to change the base?
Changes from 10 commits
e80b9f4
56aa797
902d556
412e722
02fbeb3
ac5bb11
1cd4b10
a649edd
d6575b5
1246ce7
2d86cc2
6565097
159d210
fe3ffcc
8ea9155
229d8d8
35fa0b7
50b5e18
f5d75f7
d4261da
59f3700
9b46b51
3c4b632
13d5336
ad74cb7
8526bfd
a01108d
b64544c
aef406a
4f1302e
6395275
5768d6b
a4cb033
860d697
42c6d24
0cb64fd
bf40907
3cb3cf6
3e58e07
1b33cad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,13 +2,21 @@ import { minutesToSeconds } from "date-fns"; | |
| import { describeRoute } from "hono-openapi"; | ||
| import z from "zod/v4"; | ||
|
|
||
| import type { Duration } from "@ensnode/ensnode-sdk"; | ||
| import { | ||
| type AmIRealtimeResult, | ||
| buildResultInternalServerError, | ||
| buildResultOk, | ||
| buildResultServiceUnavailable, | ||
| type Duration, | ||
| ResultCodes, | ||
| } from "@ensnode/ensnode-sdk"; | ||
| import { makeDurationSchema } from "@ensnode/ensnode-sdk/internal"; | ||
|
|
||
| import { errorResponse } from "@/lib/handlers/error-response"; | ||
| import { params } from "@/lib/handlers/params.schema"; | ||
| import { buildRouteResponsesDescription } from "@/lib/handlers/route-responses-description"; | ||
| import { validate } from "@/lib/handlers/validate"; | ||
| import { factory } from "@/lib/hono-factory"; | ||
| import { resultIntoHttpResponse } from "@/lib/result/result-into-http-response"; | ||
|
|
||
| const app = factory.createApp(); | ||
|
|
||
|
|
@@ -24,16 +32,22 @@ app.get( | |
| summary: "Check indexing progress", | ||
| description: | ||
| "Checks if the indexing progress is guaranteed to be within a requested worst-case distance of realtime", | ||
| responses: { | ||
| 200: { | ||
| responses: buildRouteResponsesDescription<AmIRealtimeResult>({ | ||
| [ResultCodes.Ok]: { | ||
| description: | ||
| "Indexing progress is guaranteed to be within the requested distance of realtime", | ||
| }, | ||
| 503: { | ||
| [ResultCodes.InvalidRequest]: { | ||
| description: "Invalid request parameters", | ||
| }, | ||
| [ResultCodes.InternalServerError]: { | ||
| description: "Indexing progress cannot be determined due to an internal server error", | ||
| }, | ||
| [ResultCodes.ServiceUnavailable]: { | ||
| description: | ||
| "Indexing progress is not guaranteed to be within the requested distance of realtime or indexing status unavailable", | ||
| }, | ||
| }, | ||
| }), | ||
| }), | ||
| validate( | ||
| "query", | ||
|
|
@@ -46,41 +60,46 @@ app.get( | |
| }), | ||
| ), | ||
| async (c) => { | ||
| // context must be set by the required middleware | ||
| // Invariant: Indexing Status must be available in application context | ||
tk-o marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (c.var.indexingStatus === undefined) { | ||
| throw new Error(`Invariant(amirealtime-api): indexingStatusMiddleware required.`); | ||
| const result = buildResultInternalServerError( | ||
| `Invariant(amirealtime-api): Indexing Status must be available in application context.`, | ||
tk-o marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ); | ||
|
|
||
| return resultIntoHttpResponse(c, result); | ||
| } | ||
|
|
||
| // return 503 response error with details on prerequisite being unavailable | ||
| // Invariant: Indexing Status must be resolved successfully before 'maxWorstCaseDistance' can be applied | ||
tk-o marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (c.var.indexingStatus instanceof Error) { | ||
| return errorResponse( | ||
| c, | ||
| `Invariant(amirealtime-api): Indexing Status has to be resolved successfully before 'maxWorstCaseDistance' can be applied.`, | ||
| 503, | ||
| const result = buildResultServiceUnavailable( | ||
| `Invariant(amirealtime-api): Indexing Status must be resolved successfully before 'maxWorstCaseDistance' can be applied.`, | ||
tk-o marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ); | ||
|
|
||
| return resultIntoHttpResponse(c, result); | ||
| } | ||
|
|
||
| const { maxWorstCaseDistance } = c.req.valid("query"); | ||
| const { worstCaseDistance, snapshot } = c.var.indexingStatus; | ||
| const { slowestChainIndexingCursor } = snapshot; | ||
|
|
||
| // return 503 response error with details on | ||
| // requested `maxWorstCaseDistance` vs. actual `worstCaseDistance` | ||
| // Case: worst-case distance exceeds requested maximum | ||
| if (worstCaseDistance > maxWorstCaseDistance) { | ||
| return errorResponse( | ||
| c, | ||
| const result = buildResultServiceUnavailable( | ||
|
||
| `Indexing Status 'worstCaseDistance' must be below or equal to the requested 'maxWorstCaseDistance'; worstCaseDistance = ${worstCaseDistance}; maxWorstCaseDistance = ${maxWorstCaseDistance}`, | ||
| 503, | ||
| ); | ||
|
|
||
| return resultIntoHttpResponse(c, result); | ||
| } | ||
|
|
||
| // return 200 response OK with current details on `maxWorstCaseDistance`, | ||
| // `slowestChainIndexingCursor`, and `worstCaseDistance` | ||
| return c.json({ | ||
| maxWorstCaseDistance, | ||
| slowestChainIndexingCursor, | ||
| worstCaseDistance, | ||
| }); | ||
| // Case: worst-case distance is within requested maximum | ||
| return resultIntoHttpResponse( | ||
| c, | ||
| buildResultOk({ | ||
tk-o marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| maxWorstCaseDistance, | ||
| slowestChainIndexingCursor, | ||
| worstCaseDistance, | ||
| }), | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.