@jsonbored/metagraphed 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,6 +1,48 @@
1
1
  // src/metagraphed-client.ts
2
+ var MetagraphedError = class extends Error {
3
+ status;
4
+ code;
5
+ envelope;
6
+ constructor(message, status, code, envelope) {
7
+ super(message);
8
+ this.name = "MetagraphedError";
9
+ this.status = status;
10
+ this.code = code;
11
+ this.envelope = envelope;
12
+ }
13
+ };
14
+ function isErrorEnvelope(body) {
15
+ return typeof body === "object" && body !== null && body.ok === false;
16
+ }
17
+ async function readJsonBody(response) {
18
+ const text = await response.text();
19
+ if (!text) {
20
+ return void 0;
21
+ }
22
+ try {
23
+ return JSON.parse(text);
24
+ } catch {
25
+ throw new MetagraphedError(
26
+ `Response body was not valid JSON (status ${response.status})`,
27
+ response.status
28
+ );
29
+ }
30
+ }
31
+ function resolveSignal(signal, timeoutMs) {
32
+ if (signal) {
33
+ return signal;
34
+ }
35
+ return timeoutMs > 0 ? AbortSignal.timeout(timeoutMs) : void 0;
36
+ }
2
37
  async function metagraphedFetch(path, options = {}) {
3
- const { baseUrl = "https://api.metagraph.sh", pathParams, query, ...init } = options;
38
+ const {
39
+ baseUrl = "https://api.metagraph.sh",
40
+ pathParams,
41
+ query,
42
+ timeoutMs = 3e4,
43
+ signal,
44
+ ...init
45
+ } = options;
4
46
  const resolvedPath = interpolatePath(
5
47
  String(path),
6
48
  pathParams
@@ -17,9 +59,83 @@ async function metagraphedFetch(path, options = {}) {
17
59
  headers: {
18
60
  accept: "application/json",
19
61
  ...init.headers || {}
62
+ },
63
+ signal: resolveSignal(signal, timeoutMs)
64
+ });
65
+ const body = await readJsonBody(response);
66
+ if (!response.ok) {
67
+ const envelope = isErrorEnvelope(body) ? body : void 0;
68
+ throw new MetagraphedError(
69
+ envelope?.error?.message ?? `GET ${url.pathname} failed with status ${response.status}`,
70
+ response.status,
71
+ envelope?.error?.code,
72
+ envelope
73
+ );
74
+ }
75
+ return body;
76
+ }
77
+ async function* metagraphedPaginate(path, options = {}) {
78
+ const baseQuery = {
79
+ ...options.query
80
+ };
81
+ let cursor = baseQuery.cursor;
82
+ for (; ; ) {
83
+ if (cursor !== void 0 && cursor !== null) {
84
+ baseQuery.cursor = cursor;
85
+ }
86
+ const page = await metagraphedFetch(path, {
87
+ ...options,
88
+ query: baseQuery
89
+ });
90
+ yield page;
91
+ const next = page?.meta?.pagination?.next_cursor;
92
+ if (next === void 0 || next === null) {
93
+ return;
20
94
  }
95
+ cursor = next;
96
+ }
97
+ }
98
+ async function metagraphedRpc(network, request, options = {}) {
99
+ const {
100
+ baseUrl = "https://api.metagraph.sh",
101
+ timeoutMs = 3e4,
102
+ signal,
103
+ id = 1
104
+ } = options;
105
+ const url = new URL(`/rpc/v1/${encodeURIComponent(network)}`, baseUrl);
106
+ const response = await fetch(url, {
107
+ method: "POST",
108
+ headers: {
109
+ "content-type": "application/json",
110
+ accept: "application/json"
111
+ },
112
+ body: JSON.stringify({
113
+ jsonrpc: "2.0",
114
+ id,
115
+ method: request.method,
116
+ params: request.params ?? []
117
+ }),
118
+ signal: resolveSignal(signal, timeoutMs)
21
119
  });
22
- return await response.json();
120
+ const body = await readJsonBody(response);
121
+ if (!response.ok) {
122
+ const envelope = isErrorEnvelope(body) ? body : void 0;
123
+ throw new MetagraphedError(
124
+ envelope?.error?.message ?? `RPC ${request.method} failed with status ${response.status}`,
125
+ response.status,
126
+ envelope?.error?.code,
127
+ envelope
128
+ );
129
+ }
130
+ const rpcError = body?.error;
131
+ if (rpcError) {
132
+ throw new MetagraphedError(
133
+ typeof rpcError.message === "string" ? rpcError.message : "JSON-RPC error",
134
+ response.status,
135
+ rpcError.code === void 0 || rpcError.code === null ? void 0 : String(rpcError.code)
136
+ );
137
+ }
138
+ return body?.result;
23
139
  }
24
140
  function interpolatePath(path, params) {
25
141
  if (!params) {
@@ -34,4 +150,4 @@ function interpolatePath(path, params) {
34
150
  });
35
151
  }
36
152
 
37
- export { metagraphedFetch };
153
+ export { MetagraphedError, metagraphedFetch, metagraphedPaginate, metagraphedRpc };
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@jsonbored/metagraphed",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "Typed TypeScript client for the metagraph.sh backend API — operational metadata, health, schemas, and interface discovery for Bittensor subnets.",
5
- "license": "MIT",
5
+ "license": "Apache-2.0",
6
6
  "type": "module",
7
7
  "homepage": "https://metagraph.sh",
8
8
  "repository": {
@@ -41,7 +41,10 @@
41
41
  "prepublishOnly": "npm run build"
42
42
  },
43
43
  "devDependencies": {
44
- "tsup": "^8.5.0",
45
- "typescript": "^5.9.0"
44
+ "tsup": "^8.5.1",
45
+ "typescript": "^5.9.3"
46
+ },
47
+ "overrides": {
48
+ "esbuild": "^0.28.1"
46
49
  }
47
50
  }