-
Notifications
You must be signed in to change notification settings - Fork 180
/
verify.ts
300 lines (287 loc) · 9.53 KB
/
verify.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
import { Client, AuthenticationType } from '@vonage/server-client';
import { Command } from './enums';
import {
VerifyControl,
VerifyControlError,
VerifyControlErrorResponse,
VerifyControlResponse,
VerifyCheckError,
VerifyCheck,
VerifyCheckResponse,
VerifyRequestResponse,
VerifySearchResponse,
VerifySearch,
VerifySearchError,
VerifyRequestErrorResponse,
VerifyError,
VerifyRequest,
VerificationParameters,
PSD2Parameters,
} from './types';
import omit from 'lodash.omit';
/**
* The Verify class provides methods for managing and performing verification processes using the Vonage Verify API.
*
* It allows you to initiate new verification requests, check verification codes, search for verification request
* details, and perform control actions like canceling or triggering the next event for a verification process.
*
* @example
* Create a standalone Verify client
*
* ```ts
* import { Verify } from '@vonage/verify';
*
* const verifyClient = new Verify({
* apiKey: VONAGE_API_KEY,
* apiSecret: VONAGE_API_SECRET
* });
* ```
*
* @example
* Create an Verify client from the Vonage client
*
* ```ts
* import { Vonage } from '@vonage/server-client';
*
* const vonage = new Vonage({
* apiKey: VONAGE_API_KEY,
* apiSecret: VONAGE_API_SECRET
* });
*
* const verifyClient = vonage.verify;
* ```
*/
export class Verify extends Client {
protected authType?: AuthenticationType = AuthenticationType.KEY_SECRET;
/**
* Sends a control command for a specific verification request.
*
* @param {Command} command - The control command to send, either "cancel" or "trigger_next_event".
* @param {string} requestId - The request ID of the verification to control.
* @return {Promise<VerifyControl | VerifyControlError>} A Promise that resolves to a `VerifyControl` object containing the control response on success or a `VerifyControlError` object on error.
* @throws {VerifyControlError} If an error occurs while sending the control command.
*
* @example
* Cancel a verification request
* ```ts
* import { Command, CheckStatus } from '@vonage/verify';
*
* const result = await verifyClient.sendControl(Command.CANCEL, 'REQUEST_ID')
* if (result.status === CheckStatus.SUCCESS) {
* console.log('Verification request canceled.');
* console.log(result.status);
* } else {
* console.log('Error canceling verification request.');
* console.log(result.errorText);
* }
* ```
*
* @example
* Trigger the next event for a verification request
* ```ts
* import { Command, CheckStatus } from '@vonage/verify';
*
* const result = await verifyClient.sendControl(Command.TRIGGER_NEXT_EVENT, 'REQUEST_ID')
* if (result.status === CheckStatus.SUCCESS) {
* console.log('Next event triggered');
* console.log(result.status);
* } else {
* console.log('Error triggering next event');
* console.log(result.errorText);
* }
* ```
*/
public async sendControl(
command: Command,
requestId: string,
): Promise<VerifyControl | VerifyControlError> {
const data = {
request_id: requestId,
cmd: command,
};
// eslint-disable-next-line max-len
const resp = await this.sendPostRequest<
VerifyControlResponse | VerifyControlErrorResponse
>(`${this.config.apiHost}/verify/control/json`, data);
return Client.transformers.camelCaseObjectKeys(
resp.data,
true,
true,
) as VerifyControl | VerifyControlError;
}
/**
* Cancels a specific verification request.
*
* @param {string} requestId - The request ID of the verification to cancel.
* @return {Promise<VerifyControl | VerifyControlError>} A Promise that resolves to a `VerifyControl` object containing the control response on success or a `VerifyControlError` object on error.
* @throws {VerifyControlError} If an error occurs while canceling the verification request.
*
* @example
* ```ts
* import { CheckStatus } from '@vonage/verify';
*
* const result = await verifyClient.cancel('REQUEST_ID')
*
* if (result.status === CheckStatus.SUCCESS) {
* console.log('Verification request canceled.');
* console.log(result.status);
* } else {
* console.log('Error canceling verification request.');
* console.log(result.errorText);
* }
* ```
*
*/
public async cancel(
requestId: string,
): Promise<VerifyControl | VerifyControlError> {
return this.sendControl(Command.CANCEL, requestId);
}
/**
* Triggers the next verification event for a specific verification request.
*
* @param {string} requestId - The request ID of the verification to trigger the next event for.
* @return {Promise<VerifyControl | VerifyControlError>} A Promise that resolves to a `VerifyControl` object containing the control response on success or a `VerifyControlError` object on error.
* @throws {VerifyControlError} If an error occurs while triggering the next verification event.
*
* @example
* ```ts
* import { CheckStatus } from '@vonage/verify';
*
* const result = await verifyClient.trigger('REQUEST_ID')
*
* if (result.status === CheckStatus.SUCCESS) {
* console.log('Verification request canceled.');
* console.log(result.status);
* } else {
* console.log('Error canceling verification request.');
* console.log(result.errorText);
* }
* ```
*/
public async trigger(
requestId: string,
): Promise<VerifyControl | VerifyControlError> {
return this.sendControl(Command.TRIGGER_NEXT_EVENT, requestId);
}
/**
* Checks the verification code for a specific verification request.
*
* @param {string} requestId - The request ID of the verification to check.
* @param {string} code - The verification code to check against.
* @return {Promise<VerifyCheck | VerifyCheckError>} A Promise that resolves to a `VerifyCheck` object containing the verification result on success or a `VerifyCheckError` object on error.
* @throws {VerifyCheckError} If an error occurs during the verification check.
*
* @example
* ```ts
* import { CheckStatus } from '@vonage/verify';
*
* const result = await verifyClient.check('REQUEST_ID', 'CODE')
* if (result.status === CheckStatus.SUCCESS) {
* console.log('Verification code is valid.');
* } else {
* console.log('Verification code is invalid.');
* }
* ```
*/
public async check(
requestId: string,
code: string,
): Promise<VerifyCheck | VerifyCheckError> {
// eslint-disable-next-line max-len
const resp = await this.sendPostRequest<
VerifyRequestResponse | VerifyRequestErrorResponse
>(`${this.config.apiHost}/verify/check/json`, {
request_id: requestId,
code: code,
});
return Client.transformers.camelCaseObjectKeys(
resp.data,
true,
true,
) as VerifyCheck | VerifyCheckError;
}
/**
* Searches for the status of a verification request by its request ID.
*
* @param {string} requestId - The request ID of the verification to search for.
* @return {Promise<VerifySearch | VerifySearchError>} A `VerifySearch` object containing the verification details on success or a `VerifySearchError` object on error.
*
* @example
* ```ts
* const result = await verifyClient.search('REQUEST_ID')
* if (result.errorText) {
* console.log(`Request found with error ${result.errorText}`);
* } else {
* console.log(`Request found and submitted on ${result.dateSubmitted}`);
* }
* ```
*/
public async search(
requestId: string,
): Promise<VerifySearch | VerifySearchError> {
this.authType = AuthenticationType.QUERY_KEY_SECRET;
const resp = await this.sendGetRequest<VerifySearchResponse>(
`${this.config.apiHost}/verify/search/json`,
{ request_id: requestId },
);
this.authType = AuthenticationType.KEY_SECRET;
return Client.transformers.camelCaseObjectKeys(
resp.data,
true,
true,
) as VerifySearch | VerifySearchError;
}
/**
* Starts a verification request.
*
* @param {VerificationParameters | PSD2Parameters} request - The verification parameters for the request.
* @return {Promise<VerifyError | VerifyRequest>} A `VerifyError` object on error or a `VerifyRequest` object on success.
*
* @example
* ```ts
* const result = await verifyClient.start({
* number: TO_NUMBER,
* brand: BRAND_NAME
* });
*
* if (result.requestId) {
* console.log(`Request started with id ${result.requestId}`);
* } else {
* console.log(`Request failed with error: ${result.errorText}`);
* }
* ```
*
* @example
* Start a request with PSD2 parameters
* ```ts
* const result = await verifyClient.start({
* number: TO_NUMBER,
* payee: PAYEE,
* amount: AMOUNT,
* })
* if (result.requestId) {
* console.log(`Request started with id ${result.requestId}`);
* } else {
* console.log(`Request failed with error: ${result.errorText}`);
* }
* ```
*/
public async start(
request: VerificationParameters | PSD2Parameters,
): Promise<VerifyError | VerifyRequest> {
const url
= 'brand' in request
? `${this.config.apiHost}/verify/json`
: `${this.config.apiHost}/verify/psd2/json`;
const resp = await this.sendPostRequest<VerifyCheckResponse>(
url,
Client.transformers.snakeCaseObjectKeys(omit(request, ['language'])),
);
return Client.transformers.camelCaseObjectKeys(
resp.data,
true,
true,
) as VerifyError | VerifyRequest;
}
}