-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathrealtime-api.ts
More file actions
48 lines (40 loc) · 1.67 KB
/
realtime-api.ts
File metadata and controls
48 lines (40 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { errorResponse } from "@/lib/handlers/error-response";
import { createApp } from "@/lib/hono-factory";
import { realtimeGetMeta } from "./realtime-api.routes";
const app = createApp();
// allow performance monitoring clients to read HTTP Status for the provided
// `maxWorstCaseDistance` param
app.openapi(realtimeGetMeta, async (c) => {
// context must be set by the required middleware
if (c.var.indexingStatus === undefined) {
throw new Error(`Invariant(realtime-api): indexingStatusMiddleware required.`);
}
// return 503 response error with details on prerequisite being unavailable
if (c.var.indexingStatus instanceof Error) {
return errorResponse(
c,
`Invariant(realtime-api): Indexing Status has to be resolved successfully before 'maxWorstCaseDistance' can be applied.`,
503,
);
}
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`
if (worstCaseDistance > maxWorstCaseDistance) {
return errorResponse(
c,
`Indexing Status 'worstCaseDistance' must be below or equal to the requested 'maxWorstCaseDistance'; worstCaseDistance = ${worstCaseDistance}; maxWorstCaseDistance = ${maxWorstCaseDistance}`,
503,
);
}
// return 200 response OK with current details on `maxWorstCaseDistance`,
// `slowestChainIndexingCursor`, and `worstCaseDistance`
return c.json({
maxWorstCaseDistance,
slowestChainIndexingCursor,
worstCaseDistance,
});
});
export default app;