|
1 | | -const origin = (process.env.BEATAPI_BASE_URL || "https://api.beatapi.io").replace(/\/+$/, ""); |
| 1 | +const origin = ( |
| 2 | + process.env.BEATAPI_BASE_URL || "https://api.beatapi.io" |
| 3 | +).replace(/\/+$/, ""); |
2 | 4 | const apiKey = process.env.BEATAPI_API_KEY; |
3 | 5 |
|
4 | 6 | async function call(path, body, authenticated = false) { |
5 | | - const response = await fetch(origin + path, { |
6 | | - method: body === undefined ? "GET" : "POST", |
7 | | - redirect: "error", |
8 | | - signal: AbortSignal.timeout(30_000), |
9 | | - headers: { |
10 | | - accept: "application/json", |
11 | | - ...(body === undefined ? {} : { "content-type": "application/json" }), |
12 | | - ...(authenticated && apiKey ? { authorization: `Bearer ${apiKey}` } : {}), |
13 | | - }, |
14 | | - ...(body === undefined ? {} : { body: JSON.stringify(body) }), |
15 | | - }); |
16 | | - const payload = await response.json().catch(() => null); |
17 | | - if (!response.ok) { |
18 | | - throw new Error(payload?.error?.message || `BeatAPI ${response.status} at ${path}`); |
19 | | - } |
20 | | - return payload.data; |
| 7 | + const response = await fetch(origin + path, { |
| 8 | + method: body === undefined ? "GET" : "POST", |
| 9 | + redirect: "error", |
| 10 | + signal: AbortSignal.timeout(30_000), |
| 11 | + headers: { |
| 12 | + accept: "application/json", |
| 13 | + ...(body === undefined ? {} : { "content-type": "application/json" }), |
| 14 | + ...(authenticated && apiKey ? { authorization: `Bearer ${apiKey}` } : {}), |
| 15 | + }, |
| 16 | + ...(body === undefined ? {} : { body: JSON.stringify(body) }), |
| 17 | + }); |
| 18 | + const payload = await response.json().catch(() => null); |
| 19 | + if (!response.ok) { |
| 20 | + throw new Error( |
| 21 | + payload?.error?.message || `BeatAPI ${response.status} at ${path}`, |
| 22 | + ); |
| 23 | + } |
| 24 | + return payload.data; |
21 | 25 | } |
22 | 26 |
|
23 | 27 | if (apiKey) { |
24 | | - const usage = await call("/v1/usage", undefined, true); |
25 | | - console.log({ authentication: "verified", object: usage.object }); |
| 28 | + const usage = await call("/v1/usage", undefined, true); |
| 29 | + console.log({ authentication: "verified", object: usage.object }); |
26 | 30 | } else { |
27 | | - console.error("BEATAPI_API_KEY is not set; running anonymous catalog discovery only."); |
| 31 | + console.error( |
| 32 | + "BEATAPI_API_KEY is not set; running anonymous catalog discovery only.", |
| 33 | + ); |
28 | 34 | } |
29 | 35 |
|
30 | 36 | const page = await call("/v1/capabilities/search", { |
31 | | - query: "image", |
32 | | - kind: "model", |
33 | | - limit: 5, |
| 37 | + query: "image", |
| 38 | + kind: "model", |
| 39 | + limit: 5, |
34 | 40 | }); |
35 | | -const candidate = page.data?.find((item) => item.reference?.startsWith("model:")); |
| 41 | +const candidate = page.data?.find((item) => |
| 42 | + item.reference?.startsWith("model:"), |
| 43 | +); |
36 | 44 | if (!candidate) throw new Error("No model match; refine the catalog search."); |
37 | 45 |
|
38 | 46 | const contract = await call("/v1/capabilities/inspect", { |
39 | | - reference: candidate.reference, |
| 47 | + reference: candidate.reference, |
40 | 48 | }); |
41 | 49 | console.log({ |
42 | | - search: { count: page.data.length, next_cursor: page.next_cursor }, |
43 | | - capability: { |
44 | | - reference: contract.reference, |
45 | | - title: contract.title, |
46 | | - status: contract.status, |
47 | | - execution: contract.execution, |
48 | | - pricing: contract.pricing, |
49 | | - validation: contract.validation, |
50 | | - }, |
| 50 | + search: { count: page.data.length, next_cursor: page.next_cursor }, |
| 51 | + capability: { |
| 52 | + reference: contract.reference, |
| 53 | + title: contract.title, |
| 54 | + readiness: contract.readiness, |
| 55 | + schema_hash: contract.schema_hash, |
| 56 | + next: contract.next, |
| 57 | + execution: contract.execution, |
| 58 | + pricing: contract.pricing, |
| 59 | + validation: contract.validation, |
| 60 | + }, |
51 | 61 | }); |
0 commit comments