Skip to content

Commit

Permalink
improve nodejs docs (iotaledger#569)
Browse files Browse the repository at this point in the history
* improve nodejs docs

* add port
  • Loading branch information
Thoralf-M authored May 18, 2021
1 parent 4b159da commit 57c5a33
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 19 deletions.
54 changes: 45 additions & 9 deletions bindings/nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ Node.js binding to the IOTA client library.
## Installation

- Using NPM:
```

```bash
$ npm i @iota/client
```

- Using yarn:
```

```bash
$ yarn add @iota/client
```

Expand Down Expand Up @@ -52,7 +55,7 @@ Adds an IOTA node with authentication to the client pool.
| Param | Type | Description |
| ----------- | -------------------------------------------- | -------------------------------- |
| url | <code>string</code> | A node URL |
| authOptions | <code>[BrokerOptions](#brokeroptions)</code> | Options for node authentication |
| authOptions | <code>[NodeAuthOptions](#nodeauthoptions)</code> | Options for node authentication |

**Returns** the client builder instance for chained calls.

Expand All @@ -63,7 +66,7 @@ Add a node to always connect first to with optional authentication.
| Param | Type | Description |
| ----------- | -------------------------------------------- | -------------------------------- |
| url | <code>string</code> | A node URL |
| authOptions | <code>[BrokerOptions](#brokeroptions)</code> | Options for node authentication |
| authOptions | <code>[NodeAuthOptions](#nodeauthoptions)</code> | Options for node authentication |

**Returns** the client builder instance for chained calls.

Expand All @@ -74,7 +77,7 @@ Add a node to always connect first to when using remote PoW with optional authen
| Param | Type | Description |
| ----------- | -------------------------------------------- | -------------------------------- |
| url | <code>string</code> | A node URL |
| authOptions | <code>[BrokerOptions](#brokeroptions)</code> | Options for node authentication |
| authOptions | <code>[NodeAuthOptions](#nodeauthoptions)</code> | Options for node authentication |

**Returns** the client builder instance for chained calls.

Expand Down Expand Up @@ -509,6 +512,24 @@ Promotes the message associated with the given id.

### TopicSubscriber

Possible topics:

```bash
milestones/latest
milestones/confirmed

messages
messages/referenced
messages/indexation/{index}
messages/{messageId}/metadata
transactions/{transactionId}/included-message

outputs/{outputId}

addresses/{address}/outputs
addresses/ed25519/{address}/outputs
```

#### topic(topic): TopicSubscriber

Adds a topic to this manager instance.
Expand Down Expand Up @@ -724,6 +745,16 @@ Defines that public and internal address will be returned instead of only public

**Returns** the address finder instance for chained calls.

#### bech32Hrp(bech32Hrp): AddressGetter

Defines the bech32Hrp for the bech32 encoded addresses, required when generating addresses offline(with disableNodeSync()).

| Param | Type | Description |
| --------- | ------------------- | ------------------------------- |
| bech32Hrp | <code>string</code> | The bech32Hrp for the addresses |

**Returns** the address finder instance for chained calls.

#### get(): Address[] | [Address, bool][]

Performs the operation.
Expand Down Expand Up @@ -828,10 +859,15 @@ Gets the metadata of the given message.

### BrokerOptions

| Field | Type | Description |
| ------------------- | ------------------- | ----------------------------------------------------------------------------------------------------- |
| automaticDisconnect | <code>number</code> | Whether the MQTT broker should be automatically disconnected when all topics are unsubscribed or not. |
| timeout | <code>number</code> | MQTT connection timeout in secods |
All fields are optional.

| Field | Type | Description |
| ----------------------- | ------------------- | ----------------------------------------------------------------------------------------------------- |
| automaticDisconnect | <code>number</code> | Whether the MQTT broker should be automatically disconnected when all topics are unsubscribed or not. |
| timeout | <code>number</code> | MQTT connection timeout in secods |
| useWs | <code>bool</code> | Defines if websockets should be used (true) or TCP (false) |
| maxReconnectionAttempts | <code>number</code> | Defines the maximum reconnection attempts before it returns an error |
| port | <code>number</code> | Defines the port to be used for the MQTT connection |

### NodeAuthOptions

Expand Down
4 changes: 3 additions & 1 deletion bindings/nodejs/examples/04_get_balance.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ async function run() {
// client will connect to testnet by default
const client = new ClientBuilder().build();

// Get the balance of a single known address
console.log(
await client.getAddressBalance("atoi1qp9427varyc05py79ajku89xarfgkj74tpel5egr9y7xu3wpfc4lkpx0l86")
);

// Get the balance of addresses from an account
const balance = await client.getBalance(IOTA_SEED_SECRET)
.accountIndex(0)
.initialAddressIndex(0)
.get();

console.log(balance);
console.log("Account balance: " + balance);
}

run()
4 changes: 2 additions & 2 deletions bindings/nodejs/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export declare class ClientBuilder {
primaryPowNode(url: string, authOptions?: NodeAuthOptions): ClientBuilder
nodes(urls: string[]): ClientBuilder
nodePoolUrls(urls: string[]): ClientBuilder
network(network_name: string): ClientBuilder
network(networkName: string): ClientBuilder
quorum(enabled: boolean): ClientBuilder
quorumSize(size: number): ClientBuilder
quorumThreshold(threshold: number): ClientBuilder
Expand Down Expand Up @@ -64,7 +64,7 @@ export declare class UnspentAddressGetter {
export declare class AddressGetter {
accountIndex(index: number): AddressGetter
range(start: number, end: number): AddressGetter
bech32Hrp(bech32_hrp: string): AddressGetter
bech32Hrp(bech32Hrp: string): AddressGetter
includeInternal(): AddressGetter
get(): Promise<Address[] | [Address, boolean][]>
}
Expand Down
4 changes: 2 additions & 2 deletions bindings/nodejs/lib/types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ export declare interface MilestoneUTXOChanges {

export declare interface BrokerOptions {
automaticDisconnect?: boolean
// timeout in milliseconds
// timeout in seconds
timeout?: number
use_ws?: boolean
useWs?: boolean
port?: number
maxReconnectionAttempts?: number
}
Expand Down
3 changes: 1 addition & 2 deletions docs/libraries/nodejs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,13 @@ Example of output:

```json
{
"address_type":0,
"address":"atoi1qp9427varyc05py79ajku89xarfgkj74tpel5egr9y7xu3wpfc4lkpx0l86",
"balance":10000000,
"dustAllowed":false
}
Account balance: 0
```

* `address_type` indicates type of address. Value 0 denotes a Ed25519 address (currently the default for IOTA 1.5 network)
* `dustAllowed` indicates whether the given address is allowed to accepts a dust due to [dust protection mechanism](https://chrysalis.docs.iota.org/guides/dev_guide.html#dust-protection)

`Client.getBalance(seed)` performs a several tasks under the hood.
Expand Down
6 changes: 3 additions & 3 deletions iota-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ pub struct BrokerOptions {
pub(crate) automatic_disconnect: bool,
#[serde(default = "default_broker_timeout")]
pub(crate) timeout: Duration,
#[serde(default = "default_broker_use_ws", rename = "defaultBrokerUseWs")]
#[serde(default = "default_broker_use_ws", rename = "useWs")]
pub(crate) use_ws: bool,
#[serde(default = "default_broker_port", rename = "defaultBrokerPort")]
#[serde(default = "default_broker_port")]
pub(crate) port: u16,
#[serde(rename = "maxReconnectionAttempts", default)]
#[serde(default = "default_max_reconnection_attempts", rename = "maxReconnectionAttempts")]
pub(crate) max_reconnection_attempts: usize,
}

Expand Down

0 comments on commit 57c5a33

Please sign in to comment.