-
Notifications
You must be signed in to change notification settings - Fork 4
/
delete-account.ts
73 lines (65 loc) · 2.37 KB
/
delete-account.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
/*!
* Copyright (c) 2009-2024 World Data Exchange Holdings Pty Limited (WDXH). All rights reserved.
*/
import { getRandomAlphaNumeric } from "./crypto";
import { sign } from "jsonwebtoken";
import { net, handleServerResponse } from "./net";
import { UserAccessToken } from "./types/user-access-token";
import { SDKConfiguration } from "./types/sdk-configuration";
import { ContractDetails } from "./types/common";
import { Response } from "got";
export interface DeleteAccountOptions {
contractDetails: ContractDetails;
userAccessToken: UserAccessToken;
accountId: string;
}
export interface DeleteAccountResponse {
deleted: boolean;
response: Response<unknown>;
}
const deleteAccount = async (
options: DeleteAccountOptions,
sdkConfig: SDKConfiguration
): Promise<DeleteAccountResponse> => {
const { userAccessToken, contractDetails, accountId } = options;
const { contractId, privateKey } = contractDetails;
const url = `${sdkConfig.baseUrl}permission-access/service/h:accountId`;
try {
const response = await net.delete(url, {
headers: {
contentType: "application/json",
accountId,
},
retry: sdkConfig.retryOptions,
hooks: {
beforeRequest: [
(options) => {
const jwt: string = sign(
{
access_token: userAccessToken.accessToken.value,
client_id: `${sdkConfig.applicationId}_${contractId}`,
nonce: getRandomAlphaNumeric(32),
timestamp: Date.now(),
},
privateKey.toString(),
{
algorithm: "PS512",
noTimestamp: true,
}
);
options.headers["Authorization"] = `Bearer ${jwt}`;
},
],
},
responseType: "json",
});
return {
deleted: true,
response,
};
} catch (error) {
handleServerResponse(error);
throw error;
}
};
export { deleteAccount };