Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update api package readme example #6915

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading