@jsonbored/metagraphed 0.1.0
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/README.md +47 -0
- package/dist/index.cjs +39 -0
- package/dist/index.d.cts +5906 -0
- package/dist/index.d.ts +5906 -0
- package/dist/index.js +37 -0
- package/package.json +47 -0
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# @jsonbored/metagraphed
|
|
2
|
+
|
|
3
|
+
Typed TypeScript client for the [metagraph.sh](https://metagraph.sh) backend API —
|
|
4
|
+
operational metadata, health, schemas, and public-interface discovery for
|
|
5
|
+
Bittensor subnets.
|
|
6
|
+
|
|
7
|
+
The client is generated from the live, versioned `openapi.json`, so request
|
|
8
|
+
paths, query parameters, and response shapes are fully typed and stay in lockstep
|
|
9
|
+
with the API contract.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install @jsonbored/metagraphed
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { metagraphedFetch } from "@jsonbored/metagraphed";
|
|
21
|
+
|
|
22
|
+
// Fully typed path + query params + response envelope.
|
|
23
|
+
const subnets = await metagraphedFetch("/api/v1/subnets", {
|
|
24
|
+
query: { limit: 10, sort: "completeness_score", order: "desc" },
|
|
25
|
+
});
|
|
26
|
+
console.log(subnets.data, subnets.meta.pagination);
|
|
27
|
+
|
|
28
|
+
// One call for everything a subnet page needs.
|
|
29
|
+
const overview = await metagraphedFetch("/api/v1/subnets/{netuid}/overview", {
|
|
30
|
+
pathParams: { netuid: 7 },
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Point at a different origin (e.g. a preview deployment).
|
|
34
|
+
const health = await metagraphedFetch("/api/v1/health", {
|
|
35
|
+
baseUrl: "https://metagraph.sh",
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Every response is the standard envelope `{ ok, schema_version, data, meta }`
|
|
40
|
+
(`meta.pagination` on list routes, `meta.published_at` for freshness). See the
|
|
41
|
+
[API stability guide](https://github.com/JSONbored/metagraphed/blob/main/docs/api-stability.md)
|
|
42
|
+
for the envelope, pagination, caching, error codes, and `x-metagraph-*` headers.
|
|
43
|
+
|
|
44
|
+
## Versioning
|
|
45
|
+
|
|
46
|
+
The package tracks the `/api/v1` contract; changes within v1 are additive. The
|
|
47
|
+
exported types are regenerated from `openapi.json` on each release.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/metagraphed-client.ts
|
|
4
|
+
async function metagraphedFetch(path, options = {}) {
|
|
5
|
+
const { baseUrl = "https://api.metagraph.sh", pathParams, query, ...init } = options;
|
|
6
|
+
const resolvedPath = interpolatePath(
|
|
7
|
+
String(path),
|
|
8
|
+
pathParams
|
|
9
|
+
);
|
|
10
|
+
const url = new URL(resolvedPath, baseUrl);
|
|
11
|
+
for (const [key, value] of Object.entries(query || {})) {
|
|
12
|
+
if (value !== void 0 && value !== null) {
|
|
13
|
+
url.searchParams.set(key, String(value));
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const response = await fetch(url, {
|
|
17
|
+
...init,
|
|
18
|
+
method: "GET",
|
|
19
|
+
headers: {
|
|
20
|
+
accept: "application/json",
|
|
21
|
+
...init.headers || {}
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
return await response.json();
|
|
25
|
+
}
|
|
26
|
+
function interpolatePath(path, params) {
|
|
27
|
+
if (!params) {
|
|
28
|
+
return path;
|
|
29
|
+
}
|
|
30
|
+
return path.replace(/\{([^}]+)\}/g, (_match, key) => {
|
|
31
|
+
const value = params[key];
|
|
32
|
+
if (value === void 0 || value === null) {
|
|
33
|
+
throw new Error(`Missing path parameter: ${key}`);
|
|
34
|
+
}
|
|
35
|
+
return encodeURIComponent(String(value));
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
exports.metagraphedFetch = metagraphedFetch;
|