From 6d2d25b2997a94cdf48ed200a8b145dee6ed5c89 Mon Sep 17 00:00:00 2001 From: Nico Flaig Date: Thu, 27 Jun 2024 12:08:10 +0100 Subject: [PATCH] docs: update api package readme example --- packages/api/README.md | 23 ++++++++--------------- packages/api/package.json | 2 +- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/packages/api/README.md b/packages/api/README.md index 3bcd397a9139..67cca2d06cd6 100644 --- a/packages/api/README.md +++ b/packages/api/README.md @@ -7,30 +7,23 @@ > This package is part of [ChainSafe's Lodestar](https://lodestar.chainsafe.io) project -Typescript REST client for the [Ethereum Consensus API spec](https://github.com/ethereum/beacon-apis) +Typescript REST client for the [Ethereum Consensus API](https://github.com/ethereum/beacon-apis) ## Usage -We use more typesafe approach for the API client, where all the errors are returned not thrown. This approach is more easy to document and better to handle all possible error cases. +The REST client extends the native [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API), it behaves very similar in terms of error and response handling. It returns the same [Response object](https://developer.mozilla.org/en-US/docs/Web/API/Response) with additional methods to simplify usage and it allows to override all [Request options](https://developer.mozilla.org/en-US/docs/Web/API/Request/Request#options) if needed. ```typescript -import {getClient, HttpError} from "@lodestar/api"; +import {getClient} from "@lodestar/api"; import {config} from "@lodestar/config/default"; const api = getClient({baseUrl: "http://localhost:9596"}, {config}); -api.beacon - .getStateValidator( - "head", - "0x933ad9491b62059dd065b560d256d8957a8c402cc6e8d8ee7290ae11e8f7329267a8811c397529dac52ae1342ba58c95" - ) - .then((res) => { - if (res.ok) { - console.log("Your balance is:", res.response.data.balance, res.ok, res.status); - } else { - console.error(res.status, res.error.code, res.error.message); - } - }); +const res = await api.beacon.getStateValidator({stateId: "head", validatorId: 0}); + +const validator = res.value(); + +console.log("The validator balance is: ", validator.balance); ``` ## Prerequisites diff --git a/packages/api/package.json b/packages/api/package.json index 67b966fcfb30..cd989ec0003f 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -1,6 +1,6 @@ { "name": "@lodestar/api", - "description": "A Typescript implementation of the Ethereum Consensus light client", + "description": "A Typescript REST client for the Ethereum Consensus API", "license": "Apache-2.0", "author": "ChainSafe Systems", "homepage": "https://github.com/ChainSafe/lodestar#readme",