Skip to content

Commit

Permalink
doc: custom module usage in client [LIVE-9761] (#253)
Browse files Browse the repository at this point in the history
* doc: custom module usage in client

* chore: bump nextra deps to fix build issues

* refactor: add details and reorder some parts

* refactor: improve a bit the readability

* refactor: fix imports and use arrow function
  • Loading branch information
Justkant authored Oct 25, 2023
1 parent 77fb576 commit b5b0aae
Show file tree
Hide file tree
Showing 11 changed files with 1,053 additions and 563 deletions.
14 changes: 7 additions & 7 deletions apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
"author": "",
"license": "ISC",
"dependencies": {
"autoprefixer": "^10.4.15",
"next": "^13.4.19",
"nextra": "^2.12.3",
"nextra-theme-blog": "^2.12.3",
"nextra-theme-docs": "^2.12.3",
"postcss": "^8.4.29",
"autoprefixer": "^10.4.16",
"next": "^13.5.6",
"nextra": "^2.13.2",
"nextra-theme-blog": "^2.13.2",
"nextra-theme-docs": "^2.13.2",
"postcss": "^8.4.31",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"tailwindcss": "^3.3.3"
},
"devDependencies": {
"@types/node": "^20.5.9",
"@types/node": "^20.8.7",
"typescript": "^5.2.2"
}
}
99 changes: 99 additions & 0 deletions apps/docs/pages/core/modules/custom.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { Callout, Steps } from "nextra/components";

# Using the Custom Module in Wallet API Client

The Custom Module allows you to interact with any Custom Handler supported by the server.

## Custom Module Overview

<Callout type="info" emoji="ℹ️">
We are still working on improving the type safety of custom modules
</Callout>

<Steps>
### Step 1

First, you need a Custom Module, you can import one from another lib or create it.

To create a Custom Module, you need to import and extend the CustomModule class.

You can add any method you want in your class and do any logic you need to.
Use `this.request<ParamType, ReturnType>("custom.handler", param)` to call the Custom Handler methods supported by the server.

```javascript
import { CustomModule } from "@ledgerhq/wallet-api-client";

type CustomLogHandlersType = {
"custom.log": (message: string) => Promise<{ res: "hello" }>;
};

class CustomLog extends CustomModule {
log(message: string) {
return this.request<
Parameters<CustomLogHandlersType["custom.log"]>[0],
ReturnType<CustomLogHandlersType["custom.log"]>
>("custom.log", message);
}
}
```

### Step 2

Go back to your transport declaration from your [Wallet API Configuration](/core/configuration).

You need to adapt the WalletAPIClient by passing a function that returns your custom handler.

<Callout type="info" emoji="ℹ️">
You can use the `defaultLogger` export from the `@ledgerhq/wallet-api-client`
if you weren't using the second parameter of the WalletAPIClient
</Callout>

```javascript
import { WalletAPIClient, defaultLogger } from "@ledgerhq/wallet-api-client";

const walletApiClient = new WalletAPIClient(
transport,
defaultLogger,
(client) => {
return new CustomLog(client);
}
);
```

<Callout type="info" emoji="ℹ️">
You can also use multiple Custom Module in an object.
</Callout>

```javascript
import { WalletAPIClient, defaultLogger } from "@ledgerhq/wallet-api-client";

const walletApiClient = new WalletAPIClient(
transport,
defaultLogger,
(client) => {
return {
log: new CustomLog(client),
log2: new CustomLog2(client),
device: new CustomDevice(client),
};
}
);
```

### Step 3

Access the Custom Module via `walletApiClient.custom`.

```javascript
// Example using one module
const res = await walletApiClient.custom.log("test");
// Example using multiple modules
const res2 = await walletApiClient.custom.log.log("test");
```

</Steps>

## Handling Errors

Make sure to handle errors gracefully and provide appropriate feedback to the user.
Additionally, always remember to disconnect the `WindowMessageTransport` when you're done interacting with the Ledger Wallet API to free up resources.
8 changes: 4 additions & 4 deletions apps/wallet-api-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
"@radix-ui/react-dropdown-menu": "^2.0.5",
"@radix-ui/react-icons": "^1.3.0",
"@tailwindcss/typography": "^0.5.9",
"@types/node": "20.8.4",
"@types/node": "20.8.7",
"@types/react": "18.2.28",
"@types/react-dom": "18.2.13",
"@uiw/codemirror-extensions-langs": "^4.21.13",
"@uiw/react-codemirror": "^4.21.13",
"bufferutil": "^4.0.7",
"flowbite": "^1.8.1",
"next": "^13.4.19",
"next": "^13.5.6",
"react": "18.2.0",
"react-dom": "18.2.0",
"typescript": "5.2.2",
Expand All @@ -33,10 +33,10 @@
},
"devDependencies": {
"@types/uuid": "^9.0.3",
"autoprefixer": "^10.4.15",
"autoprefixer": "^10.4.16",
"eslint": "^8.48.0",
"eslint-config-custom": "workspace:*",
"postcss": "^8.4.29",
"postcss": "^8.4.31",
"postcss-import": "^15.1.0",
"tailwindcss": "^3.3.3"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/client-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"devDependencies": {
"@types/jest": "^29.5.4",
"@types/node": "^20.5.9",
"@types/node": "^20.8.7",
"@types/react": "^18.2.21",
"eslint": "^8.48.0",
"jest": "^29.6.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"devDependencies": {
"@types/jest": "^29.5.4",
"@types/node": "^20.5.9",
"@types/node": "^20.8.7",
"eslint": "^8.48.0",
"jest": "^29.6.4",
"jest-environment-jsdom": "^29.6.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"devDependencies": {
"@types/jest": "^29.5.4",
"@types/node": "^20.5.9",
"@types/node": "^20.8.7",
"@types/uuid": "^9.0.3",
"eslint": "^8.48.0",
"jest": "^29.6.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/manifest-validator-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"validate": "bin/cli.js"
},
"devDependencies": {
"@types/node": "^20.5.9",
"@types/node": "^20.8.7",
"eslint": "^8.48.0",
"prettier": "^3.0.3",
"typescript": "^5.2.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/manifest-validator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"devDependencies": {
"@types/jest": "^29.5.4",
"@types/node": "^20.5.9",
"@types/node": "^20.8.7",
"eslint": "^8.48.0",
"jest": "^29.6.4",
"jest-environment-jsdom": "^29.6.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"devDependencies": {
"@types/jest": "^29.5.4",
"@types/node": "^20.5.9",
"@types/node": "^20.8.7",
"@types/picomatch": "^2.3.0",
"@types/react": "^18.2.21",
"eslint": "^8.48.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/simulator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"devDependencies": {
"@types/jest": "^29.5.4",
"@types/node": "^20.5.9",
"@types/node": "^20.8.7",
"@types/ws": "^8.5.5",
"bignumber.js": "^9.1.2",
"eslint": "^8.48.0",
Expand Down
Loading

2 comments on commit b5b0aae

@vercel
Copy link

@vercel vercel bot commented on b5b0aae Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

wallet-api – ./apps/docs

wallet-api-ledgerhq.vercel.app
wallet-api-git-main-ledgerhq.vercel.app
wallet.api.live.ledger.com

@vercel
Copy link

@vercel vercel bot commented on b5b0aae Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

wallet-api-wallet-api-tools – ./apps/wallet-api-tools

wallet-api-wallet-api-tools.vercel.app
wallet-api-wallet-api-tools-git-main-ledgerhq.vercel.app
wallet-api-wallet-api-tools-ledgerhq.vercel.app

Please sign in to comment.