Repository navigation
Expand file tree
/
Copy pathexample.ts
More file actions
60 lines (53 loc) · 2.37 KB
/
Copy pathexample.ts
File metadata and controls
60 lines (53 loc) · 2.37 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
/**
* Example: typed Lens client usage.
*
* Every request and response type below comes from `schema.d.ts`, which is
* generated from `openapi.yaml` by `npm run client:gen`. Nothing here is
* hand-maintained, so a spec change shows up as a type error at compile time.
*
* Run it against a local server with:
*
* npx tsx clients/typescript/src/example.ts
*
* Paths carry the `/v1` prefix because that is the canonical form the spec
* documents. The unversioned aliases still answer, but they respond with a
* `Deprecation` header — see docs/api-versioning.md.
*/
import { createLensClient, type paths } from "./client.js";
/** A price response, derived from the spec — never written by hand. */
type AggregatedPrice = paths["/v1/price/{assetA}/{assetB}"]["get"]["responses"][200]["content"]["application/json"];
const client = createLensClient({ baseUrl: "http://localhost:3002" });
async function main(): Promise<void> {
const status = await client.GET("/v1/status");
if (status.error) {
throw new Error(`GET /status failed with ${status.response.status}`);
}
console.log(`watching ${status.data.watchedPairs?.length ?? 0} pairs`);
console.log(`last indexed ledger: ${status.data.lastIndexedLedger}`);
const price = await client.GET("/v1/price/{assetA}/{assetB}", {
params: { path: { assetA: "XLM", assetB: "USDC" } },
});
if (price.error) {
throw new Error(`GET /price failed with ${price.response.status}`);
}
const data: AggregatedPrice | undefined = price.data;
if (!data) {
throw new Error("GET /price returned no body");
}
console.log(`XLM/USDC best route: ${data.bestRoute}`);
console.log(`XLM/USDC vwap1h: ${data.vwap1h}`);
const snapshots = await client.GET("/v1/prices/history", {
params: { query: { pair: "XLM/USDC", interval: "1h" } },
});
if (snapshots.error) {
throw new Error(`GET /prices/history failed with ${snapshots.response.status}`);
}
// This route answers text/csv as well as application/json, so the generated
// types give a union of `string | { count?: number, ... }` and a field read
// has to narrow first. That the compiler insists on it is the point of
// generating the client from the spec rather than hand-writing it.
const body = snapshots.data;
const count = typeof body === "object" && body !== null ? (body.count ?? 0) : 0;
console.log(`${count} snapshots returned`);
}
void main();