-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathapiSdk.ts
442 lines (397 loc) · 11.5 KB
/
apiSdk.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
import {
SupportedUrl,
Group,
Invite,
GroupCreationDetails,
GroupUpdateDetails,
DashboardUrl
} from "./types"
import checkParameter from "./checkParameter"
import {
getGroups,
getGroup,
createGroups,
removeGroup,
removeGroups,
updateGroup,
updateGroups,
isGroupMember,
generateMerkleProof,
addMemberByApiKey,
addMembersByApiKey,
addMemberByInviteCode,
removeMemberByApiKey,
removeMembersByApiKey,
getGroupsByAdminId,
getGroupsByMemberId,
getCredentialGroupJoinUrl,
getMultipleCredentialsGroupJoinUrl
} from "./groups"
import { createInvite, getInvite, redeemInvite } from "./invites"
export default class ApiSdk {
private _url: string
private _config: object
/**
* Initializes the ApiSdk object with a Supported URL or custom URL.
* @param url Supported URL or custom URL.
* @param config [Axios](https://axios-http.com/docs/req_config) Request Config.
*/
constructor(url: SupportedUrl | string = SupportedUrl.PROD, config?: any) {
checkParameter(url, "url", "string")
if (config) {
if (!config.baseURL) {
this._config = {
baseURL: url,
...config
}
} else {
if (url !== config.baseURL)
throw new Error("The url and baseURL should be the same")
this._config = config
}
} else {
this._config = {
headers: {
"Content-Type": "application/json"
},
baseURL: url
}
}
this._url = url
}
/**
* Returns the API URL.
* @returns API URL.
*/
get url(): string {
return this._url
}
/**
* Returns the API Config.
* @returns API Config.
*/
get config(): object {
return this._config
}
/**
* Returns the list of groups.
* @returns List of groups.
*/
async getGroups(): Promise<Group[]> {
const groups = await getGroups(this._config)
return groups
}
/**
* Returns the list of groups by admin id.
* @param adminId Admin id.
* @returns List of groups by admin id.
*/
async getGroupsByAdminId(adminId: string): Promise<Group[]> {
const groups = await getGroupsByAdminId(this._config, adminId)
return groups
}
/**
* Returns the list of groups by member id.
* @param memberId Member id.
* @returns List of groups by member id.
*/
async getGroupsByMemberId(memberId: string): Promise<Group[]> {
const groups = await getGroupsByMemberId(this._config, memberId)
return groups
}
/**
* Creates a group using the API key.
* @param groupCreationDetails Data to create the group.
* @param apiKey The API key of the admin of the group.
* @returns The created group.
*/
async createGroup(
groupCreationDetails: GroupCreationDetails,
apiKey: string
): Promise<Group> {
if (
groupCreationDetails.treeDepth < 16 ||
groupCreationDetails.treeDepth > 32
) {
throw new Error("The tree depth must be between 16 and 32")
}
const groups = await createGroups(
this._config,
[groupCreationDetails],
apiKey
)
return groups[0]
}
/**
* Creates one or more groups using the API key.
* @param groupsCreationDetails Data to create the groups.
* @param apiKey The API key of the admin of the groups.
* @returns The created groups.
*/
async createGroups(
groupsCreationDetails: Array<GroupCreationDetails>,
apiKey: string
): Promise<Array<Group>> {
for (const group of groupsCreationDetails) {
if (group.treeDepth < 16 || group.treeDepth > 32) {
throw new Error("The tree depth must be between 16 and 32")
}
}
const groups = await createGroups(
this._config,
groupsCreationDetails,
apiKey
)
return groups
}
/**
* Removes a group using the API key.
* @param groupId The group id.
* @param apiKey The API key of the admin of the group.
* @returns undefined.
*/
async removeGroup(groupId: string, apiKey: string): Promise<void> {
await removeGroup(this._config, groupId, apiKey)
}
/**
* Removes one or more group using the API key.
* @param groupIds The group ids.
* @param apiKey The API key of the admin of the groups.
* @returns undefined.
*/
async removeGroups(groupIds: Array<string>, apiKey: string): Promise<void> {
await removeGroups(this._config, groupIds, apiKey)
}
/**
* Update a specific group using the API key.
* @param groupId The group id.
* @param groupUpdateDetails Data to update the group.
* @param apiKey The API key of the admin of the group.
* @returns The updated group.
*/
async updateGroup(
groupId: string,
groupUpdateDetails: GroupUpdateDetails,
apiKey: string
): Promise<Group> {
const group = await updateGroup(
this._config,
groupId,
groupUpdateDetails,
apiKey
)
return group
}
/**
* Updats one or more groups using the API key.
* @param groupIds The group ids.
* @param groupsUpdateDetails Data to update the groups.
* @param apiKey The API key of the admin of the groups.
* @returns The updated groups.
*/
async updateGroups(
groupIds: Array<string>,
groupsUpdateDetails: Array<GroupUpdateDetails>,
apiKey: string
): Promise<Array<Group>> {
const groups = await updateGroups(
this._config,
groupIds,
groupsUpdateDetails,
apiKey
)
return groups
}
/**
* Returns a specific group.
* @param groupId Group id.
* @returns Specific group.
*/
async getGroup(groupId: string): Promise<Group> {
const group = await getGroup(this._config, groupId)
return group
}
/**
* Returns true if the member is in the group and false otherwise.
* @param groupId Group id.
* @param memberId Member id.
* @returns true or false.
*/
async isGroupMember(groupId: string, memberId: string): Promise<boolean> {
const isMember = await isGroupMember(this._config, groupId, memberId)
return isMember
}
/**
* Returns the Merkle Proof for a member in a group.
* @param groupId Group id.
* @param memberId Member id.
* @returns the Merkle Proof.
*/
async generateMerkleProof(
groupId: string,
memberId: string
): Promise<string> {
const merkleProof = await generateMerkleProof(
this._config,
groupId,
memberId
)
return merkleProof
}
/**
* Adds a member to a group using an API Key.
* @param groupId Group id.
* @param memberId Member id.
* @param apiKey API Key of the admin of the group.
* @returns undefined.
*/
async addMemberByApiKey(
groupId: string,
memberId: string,
apiKey: string
): Promise<void> {
await addMemberByApiKey(this._config, groupId, memberId, apiKey)
}
/**
* Adds several members to a group using an API Key.
* @param groupId Group id.
* @param memberIds Member ids.
* @param apiKey API Key of the admin of the group.
* @returns undefined.
*/
async addMembersByApiKey(
groupId: string,
memberIds: string[],
apiKey: string
): Promise<void> {
await addMembersByApiKey(this._config, groupId, memberIds, apiKey)
}
/**
* Adds a member to a group using an Invite Code.
* @param groupId Group id.
* @param memberId Member id.
* @param inviteCode Invite Code.
* @returns undefined.
*/
async addMemberByInviteCode(
groupId: string,
memberId: string,
inviteCode: string
): Promise<void> {
await addMemberByInviteCode(this._config, groupId, memberId, inviteCode)
}
/**
* Removes a member from a group using an API Key.
* @param groupId Group id.
* @param memberId Member id.
* @param apiKey API Key of the admin of the group.
* @returns undefined.
*/
async removeMemberByApiKey(
groupId: string,
memberId: string,
apiKey: string
): Promise<void> {
await removeMemberByApiKey(this._config, groupId, memberId, apiKey)
}
/**
* Removes multiple members from a group using an API Key.
* @param groupId Group id.
* @param memberIds Member ids.
* @param apiKey API Key of the admin of the group.
* @returns undefined.
*/
async removeMembersByApiKey(
groupId: string,
memberIds: string[],
apiKey: string
): Promise<void> {
await removeMembersByApiKey(this._config, groupId, memberIds, apiKey)
}
/**
* Creates a new group invite.
* @param groupId The group identifier.
* @param apiKey The api key.
* @returns Specific invite.
*/
async createInvite(groupId: string, apiKey: string): Promise<Invite> {
const invite = await createInvite(this._config, groupId, apiKey)
return invite
}
/**
* Returns a specific invite.
* @param inviteCode Invite code.
* @returns Specific invite.
*/
async getInvite(inviteCode: string): Promise<Invite> {
const invite = await getInvite(this._config, inviteCode)
return invite
}
/**
* Redeems a specific invite.
* @param inviteCode Invite code.
* @param groupId Group id.
* @param apiKey The api key.
* @returns The updated invite.
*/
async redeemInvite(
inviteCode: string,
groupId: string,
apiKey: string
): Promise<Invite> {
const invite = await redeemInvite(
this._config,
inviteCode,
groupId,
apiKey
)
return invite
}
/**
* Generate a custom url for joining a credential group.
* @param dashboardUrl Dashboard base url.
* @param groupId Group id.
* @param commitment Identity commitment.
* @param providerName Group credential provider name.
* @param redirectUri Redirect uri.
* @returns Url string.
*/
getCredentialGroupJoinUrl(
dashboardUrl: DashboardUrl,
groupId: string,
commitment: string,
providerName: string,
redirectUri?: string
): string {
const url = getCredentialGroupJoinUrl(
dashboardUrl,
groupId,
commitment,
providerName,
redirectUri
)
return url
}
/**
* Generate a custom url for joining a multiple credentials group.
* @param dashboardUrl Dashboard base url.
* @param groupId Group id.
* @param commitment Identity commitment.
* @param redirectUri Redirect uri.
* @returns Url string.
*/
getMultipleCredentialsGroupJoinUrl(
dashboardUrl: DashboardUrl,
groupId: string,
commitment: string,
redirectUri?: string
): string {
const url = getMultipleCredentialsGroupJoinUrl(
dashboardUrl,
groupId,
commitment,
redirectUri
)
return url
}
}