-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhealth.ts
More file actions
75 lines (72 loc) · 2.18 KB
/
health.ts
File metadata and controls
75 lines (72 loc) · 2.18 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { OpenSeaAPIError, type OpenSeaClient } from "./client.js"
import type { HealthResult } from "./types/index.js"
export async function checkHealth(
client: OpenSeaClient,
): Promise<HealthResult> {
const keyPrefix = client.getApiKeyPrefix()
// Step 1: Check basic connectivity with a public endpoint
try {
await client.get("/api/v2/collections", { limit: 1 })
} catch (error) {
let message: string
if (error instanceof OpenSeaAPIError) {
message =
error.statusCode === 429
? "Rate limited: too many requests"
: `API error (${error.statusCode}): ${error.responseBody}`
} else {
message = `Network error: ${(error as Error).message}`
}
return {
status: "error",
key_prefix: keyPrefix,
authenticated: false,
rate_limited:
error instanceof OpenSeaAPIError && error.statusCode === 429,
message,
}
}
// Step 2: Validate authentication with an endpoint that requires a valid API key
try {
await client.get("/api/v2/listings/collection/boredapeyachtclub/all", {
limit: 1,
})
return {
status: "ok",
key_prefix: keyPrefix,
authenticated: true,
rate_limited: false,
message: "Connectivity and authentication are working",
}
} catch (error) {
if (error instanceof OpenSeaAPIError) {
if (error.statusCode === 429) {
return {
status: "error",
key_prefix: keyPrefix,
authenticated: false,
rate_limited: true,
message: "Rate limited: too many requests",
}
}
if (error.statusCode === 401 || error.statusCode === 403) {
return {
status: "error",
key_prefix: keyPrefix,
authenticated: false,
rate_limited: false,
message: `Authentication failed (${error.statusCode}): invalid API key`,
}
}
}
// Non-auth error on listings endpoint — connectivity works but auth is unverified
return {
status: "ok",
key_prefix: keyPrefix,
authenticated: false,
rate_limited: false,
message:
"Connectivity is working but authentication could not be verified",
}
}
}