-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtwins.ts
More file actions
116 lines (107 loc) · 4.43 KB
/
twins.ts
File metadata and controls
116 lines (107 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { Twin } from "@threefold/tfchain_client";
import { TFClient } from "../clients/tf-grid/client";
import { GridClientConfig } from "../config";
import { expose } from "../helpers/expose";
import { validateInput } from "../helpers/validator";
import { TwinCreateModel, TwinGetByAccountIdModel, TwinGetModel } from "./models";
import { checkBalance } from "./utils";
class Twins {
client: TFClient;
/**
* Represents a class for managing Twin operations using a TFClient instance.
* This class provides methods for creating, updating, and retrieving Twin information.
* It also includes functionality to get the current user's Twin ID and retrieve Twin ID by account ID.
*
* @class Twins
* @param {GridClientConfig} config - The configuration object for initializing the client.
*/
constructor(public config: GridClientConfig) {
this.client = config.tfclient;
}
/**
* Creates a new Twin using the provided options.
*
* This method first creates a Twin using the `TFClient` instance associated with the Twins class.
* It then applies the created Twin.
*
* @param {TwinCreateModel} options - The options for creating the Twin.
* @returns {Promise<Twin>} A promise that resolves with the result of applying the created Twin.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
* - `@checkBalance`: Checks the balance to ensure there are enough funds available.
*/
@expose
@validateInput
@checkBalance
async create(options: TwinCreateModel): Promise<Twin> {
return (await this.client.twins.create(options)).apply();
}
/**
* Updates an existing Twin using the provided options.
*
* This method first updates a Twin using the `TFClient` instance associated with the Twins class.
* It then applies the updated Twin.
*
* @param {TwinCreateModel} options - The options for updating the Twin.
* @returns {Promise<Twin>} A promise that resolves with the result of applying the updated Twin.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
* - `@checkBalance`: Checks the balance to ensure there are enough funds available.
*/
@expose
@validateInput
@checkBalance
async update(options: TwinCreateModel): Promise<Twin> {
return (await this.client.twins.update(options)).apply();
}
/**
* Retrieves Twin information based on the provided options.
*
* This method retrieves Twin information using the `TFClient` instance associated with the `Twins` class.
*
* @param {TwinGetModel} options - The options for retrieving Twin information.
* @returns {Promise<Twin>} A promise that resolves with the retrieved Twin information.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async get(options: TwinGetModel): Promise<Twin> {
return await this.client.twins.get(options);
}
/**
* Retrieves the Twin ID of the current user.
*
* This method calls the `getMyTwinId` function from the `TFClient` instance associated with the `Twins` class to retrieve the Twin ID of the current user.
*
* @returns {Promise<number>} A promise that resolves with the Twin ID of the current user.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async get_my_twin_id(): Promise<number> {
return await this.client.twins.getMyTwinId();
}
/**
* Retrieves the `Twin ID` associated with the provided `account ID`.
*
* This method calls the `getTwinIdByAccountId` function from the `TFClient` instance associated with the `Twins` class to retrieve the `Twin ID` based on the provided `account ID`.
*
* @param {TwinGetByAccountIdModel} options - The options containing the `account ID` for which to retrieve the `Twin ID`.
* @returns {Promise<number>} A promise that resolves with the `Twin ID` associated with the provided `account ID`.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async get_twin_id_by_account_id(options: TwinGetByAccountIdModel): Promise<number> {
return await this.client.twins.getTwinIdByAccountId({ accountId: options.public_key });
}
}
export { Twins as twins };