Skip to content

Commit 91cc585

Browse files
authored
docs: Fullnode Typings [10] - Version retrieval (#1012)
1 parent 0de2aef commit 91cc585

File tree

4 files changed

+75
-67
lines changed

4 files changed

+75
-67
lines changed

src/api/version.js

Lines changed: 0 additions & 62 deletions
This file was deleted.

src/api/version.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Copyright (c) Hathor Labs and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import { createRequestInstance } from './axiosInstance';
9+
import { ApiVersion } from '../types';
10+
11+
/**
12+
* Api calls for version
13+
*
14+
* @namespace ApiVersion
15+
*/
16+
17+
const versionApi = {
18+
/**
19+
* Get version of full node running in connected server
20+
*
21+
* @param resolve Method to be called after response arrives
22+
*
23+
* @deprecated Use asyncGetVersion instead
24+
* @return Promise that resolves to void (result is passed through callback)
25+
* @memberof ApiVersion
26+
* @inner
27+
*/
28+
// TODO: This method uses a callback pattern but also returns a Promise, which is an anti-pattern
29+
// NOTE: createRequestInstance has legacy typing (resolve?: null) that doesn't match actual usage.
30+
getVersion(resolve: (data: ApiVersion) => void) {
31+
return createRequestInstance(resolve as unknown as null)
32+
.get<ApiVersion>(`version`)
33+
.then(
34+
res => {
35+
resolve(res.data);
36+
},
37+
res => {
38+
return Promise.reject(res);
39+
}
40+
);
41+
},
42+
43+
/**
44+
* Get version of full node running in connected server
45+
*
46+
* @return Promise resolving to the version data
47+
* @memberof ApiVersion
48+
* @inner
49+
*/
50+
async asyncGetVersion(): Promise<ApiVersion> {
51+
// FIXME: This function wraps a Promise around another Promise, which is an anti-pattern.
52+
return new Promise((resolve, reject) => {
53+
// NOTE: createRequestInstance has legacy typing (resolve?: null) that doesn't match actual usage.
54+
createRequestInstance(resolve as unknown as null)
55+
.get<ApiVersion>(`version`)
56+
.then(
57+
res => {
58+
resolve(res.data);
59+
},
60+
err => {
61+
reject(err);
62+
}
63+
);
64+
});
65+
},
66+
};
67+
68+
export default versionApi;

src/new/wallet.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import {
5555
import { ErrorMessages } from '../errorMessages';
5656
import P2SHSignature from '../models/p2sh_signature';
5757
import {
58+
ApiVersion,
5859
AddressScanPolicyData,
5960
AuthorityType,
6061
FullNodeVersionData,
@@ -448,8 +449,8 @@ class HathorWallet extends EventEmitter {
448449
* */
449450
// eslint-disable-next-line class-methods-use-this -- The server address is fetched directly from the configs
450451
async getVersionData(): Promise<FullNodeVersionData> {
451-
const versionData: any = await new Promise((resolve, reject) => {
452-
versionApi.getVersion(resolve).catch((error: any) => reject(error));
452+
const versionData: ApiVersion = await new Promise((resolve, reject) => {
453+
versionApi.getVersion(resolve).catch(error => reject(error));
453454
});
454455

455456
return {
@@ -1612,7 +1613,7 @@ class HathorWallet extends EventEmitter {
16121613
* 'password': password to decrypt xpriv information. Required if not set in object.
16131614
* }
16141615
*/
1615-
async start(optionsParams: any = {}): Promise<any> {
1616+
async start(optionsParams: any = {}): Promise<ApiVersion> {
16161617
const options: any = { pinCode: null, password: null, ...optionsParams };
16171618
const pinCode: any = options.pinCode || this.pinCode;
16181619
const password: any = options.password || this.password;
@@ -1672,7 +1673,7 @@ class HathorWallet extends EventEmitter {
16721673
this.walletStopped = false;
16731674
this.setState(HathorWallet.CONNECTING);
16741675

1675-
const info = await new Promise((resolve, reject) => {
1676+
const info = await new Promise<ApiVersion>((resolve, reject) => {
16761677
versionApi.getVersion(resolve).catch(error => reject(error));
16771678
});
16781679
if (info.network.indexOf(this.conn.network) >= 0) {

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,8 @@ export interface IFillTxOptions {
481481
export interface ApiVersion {
482482
version: string;
483483
network: string;
484-
// min_weight: number; // DEPRECATED
484+
/** @deprecated */
485+
min_weight: number;
485486
min_tx_weight: number;
486487
min_tx_weight_coefficient: number;
487488
min_tx_weight_k: number;

0 commit comments

Comments
 (0)