-
Notifications
You must be signed in to change notification settings - Fork 180
/
secrets.ts
118 lines (113 loc) · 3.67 KB
/
secrets.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
import { AuthenticationType, Client } from '@vonage/server-client';
import { APISecretResponse, ListAPISecretsResponse } from './types';
/**
* Client class to interact with the Account API to create secrets in
* their Vonage API Account programmatically.
*
* @remarks
* This client is only available as a standalone client. It cannot be
* instantiated from the server-sdk package.
*
* @example
* Create a standalone Secret client
*
* ```ts
* import { Secrets } from '@vonage/account';
*
* const secretClient = new Secrets({
* apiKey: VONAGE_API_KEY,
* apiSecret: VONAGE_API_SECRET
* });
* ```
*
*/
export class Secrets extends Client {
protected authType = AuthenticationType.BASIC;
/**
* Create a new API secret for a given API key.
*
* @param {string} apiKey - The API key to manage secrets for.
* @param {string} secret - The new secret. It must follow the provided rules:
* - Minimum 8 characters.
* - Maximum 25 characters.
* - At least 1 lowercase character.
* - At least 1 uppercase character.
* - At least 1 digit.
*
* @return {Promise<APISecretResponse>} A promise that resolves to an object representing the created API secret.
*
* @example
* const { id } = await secretClient.createSecret(
* 'new-api-key',
* 'SuperSecret123!'
* );
*
* console.log(`Created secret with ID ${id}`);
*/
public async createSecret(
apiKey: string,
secret: string,
): Promise<APISecretResponse> {
const response = await this.sendPostRequest<APISecretResponse>(
`${this.config.apiHost}/accounts/${apiKey}/secrets`,
{ secret },
);
return response.data;
}
/**
* Revoke (delete) an existing API secret for a given API key.
*
* @param {string} apiKey - The API key to manage secrets for.
* @param {string} id - The ID of the API secret to revoke.
* @return {Promise<void>} A promise that resolves when the secret has been revoked.
*
* @example
* await secretClient.deleteSecret('my-api-key', 'my-secret-id');
*/
public async deleteSecret(apiKey: string, id: string): Promise<void> {
await this.sendDeleteRequest<void>(
`${this.config.apiHost}/accounts/${apiKey}/secrets/${id}`,
);
}
/**
* Retrieve the details of a specific API secret for a given API key.
*
* @param {string} apiKey - The API key to manage secrets for.
* @param {string} id - The ID of the API secret to retrieve.
* @return {Promise<APISecretResponse>} A promise that resolves to an object representing the API secret.
*
* @example
* const { id } = await secretClient.getSecret('my-api-key', 'my-secret-id');
* console.log(`Secret with ID ${id} has been retrieved`);
*
*/
public async getSecret(
apiKey: string,
id: string,
): Promise<APISecretResponse> {
const response = await this.sendGetRequest<APISecretResponse>(
`${this.config.apiHost}/accounts/${apiKey}/secrets/${id}`,
);
return response.data;
}
/**
* List all the secrets associated with a particular API key.
*
* @param {string} apiKey - The API key for which to list secrets.
*
* @return {Promise<ListAPISecretsResponse>} A promise that resolves to an object containing a list of API secrets.
*
* @example
* const response = await secretClient.listSecrets('my-api-key');
*
* for (const secret of response._embedded.secrets) {
* console.log(`Secret with ID ${secret.id} has been retrieved`);
* }
*/
public async listSecrets(apiKey: string): Promise<ListAPISecretsResponse> {
const response = await this.sendGetRequest<ListAPISecretsResponse>(
`${this.config.apiHost}/accounts/${apiKey}/secrets`,
);
return response.data;
}
}