/
apiComposer.ts
173 lines (155 loc) · 4.71 KB
/
apiComposer.ts
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Copyright (c) 2019 Burst Apps Team
*/
import {ChainService} from '../service';
import {
Api,
AccountApi,
AliasApi,
AssetApi,
BlockApi,
ContractApi,
MessageApi,
NetworkApi,
TransactionApi,
} from '../typings/api';
class ApiImpl implements Api {
constructor(service: ChainService) {
this.service = service;
}
service: ChainService;
alias: AliasApi;
account: AccountApi;
asset: AssetApi;
block: BlockApi;
message: MessageApi;
network: NetworkApi;
transaction: TransactionApi;
contract: ContractApi;
}
/**
* The API composer mounts the API for given service and selected methods
*
* Usually you would use [[composeApi]], which gives you _all_ available API methods.
* Unfortunately, this will import almost all dependencies, even if you need only a fraction
* of the methods. To take advantage of tree-shaking (dead code elimination) you can
* compose your own API with the methods you need. This can reduce your final bundle significantly.
*
* Usage:
* ```typescript
*
* const chainService = new ChainService({
* nodeHost: 'https://testnet.burst.fun',
* })
*
* const api = apiComposer
* .create(chainService)
* .withMessageApi({
* sendTextMessage
* })
* .withAccountApi({
* getAccountTransactions,
* getUnconfirmedAccountTransactions,
* getAccountBalance,
* generateSendTransactionQRCode,
* generateSendTransactionQRCodeAddress,
* })
* .compose();
* ```
*
* The `with<section>Api` uses factory methods from the [api.core.factories](/phoenix/docs/modules/core_api_factories.html) package
*
* @module core.api
* */
export class ApiComposer {
private readonly api: Api;
/**
* Creates the composer instance
* @param service
* @return the composer instance
*/
public static create(service: ChainService): ApiComposer {
return new ApiComposer(service);
}
private constructor(private service: ChainService) {
this.api = new ApiImpl(service);
}
private mapCreators(apiSection: string, creatorMap: any): void {
this.api[apiSection] = {};
Object.keys(creatorMap)
.forEach(
creatorName => this.api[apiSection][creatorName] = creatorMap[creatorName](this.service)
);
}
/**
* Adds the [[BlockApi]] to be composed
* @param creatorMap A map of creator/factory functions for the endpoints
*/
public withBlockApi(creatorMap: any): ApiComposer {
this.mapCreators('block', creatorMap);
return this;
}
/**
* Adds the [[AccountApi]] to be composed
* @param creatorMap A map of creator/factory functions for the endpoints
*/
public withAccountApi(creatorMap: any): ApiComposer {
this.mapCreators('account', creatorMap);
return this;
}
/**
* Adds the [[NetworkApi]] to be composed
* @param creatorMap A map of creator/factory functions for the endpoints
*/
public withNetworkApi(creatorMap: any): ApiComposer {
this.mapCreators('network', creatorMap);
return this;
}
/**
* Adds the [[MessageApi]] to be composed
* @param creatorMap A map of creator/factory functions for the endpoints
*/
public withMessageApi(creatorMap: any): ApiComposer {
this.mapCreators('message', creatorMap);
return this;
}
/**
* Adds the [[TransactionApi]] to be composed
* @param creatorMap A map of creator/factory functions for the endpoints
*/
public withTransactionApi(creatorMap: any): ApiComposer {
this.mapCreators('transaction', creatorMap);
return this;
}
/**
* Adds the [[AliasApi]] to be composed
* @param creatorMap A map of creator/factory functions for the endpoints
*/
public withAliasApi(creatorMap: any): ApiComposer {
this.mapCreators('alias', creatorMap);
return this;
}
/**
* Adds the [[AssetApi]] to be composed
* @param creatorMap A map of creator/factory functions for the endpoints
*/
public withAssetApi(creatorMap: any): ApiComposer {
this.mapCreators('asset', creatorMap);
return this;
}
/**
* Adds the [[ContractApi]] to be composed
* @param creatorMap A map of creator/factory functions for the endpoints
*/
public withContractApi(creatorMap: any): ApiComposer {
this.mapCreators('contract', creatorMap);
return this;
}
/**
* Composes the API
* Note: As of being a builder pattern, this need to call this method as last
*/
public compose(): Api {
return this.api;
}
}