Skip to content

Commit

Permalink
docs: update api package readme example (#6915)
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig authored Jun 27, 2024
1 parent 8cb08ff commit d87a901
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
23 changes: 8 additions & 15 deletions packages/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit d87a901

Please sign in to comment.