@vonage/verify
Version:
Verify API provides a choice of routes for sending a code to a user. You can use this to confirm a user's contact information, as a second factor when authenticating users, or for step-up authentication.
234 lines • 9.22 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Verify = void 0;
const server_client_1 = require("@vonage/server-client");
const enums_1 = require("./enums");
const lodash_omit_1 = __importDefault(require("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;
* ```
*/
class Verify extends server_client_1.Client {
authType = server_client_1.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);
* }
* ```
*/
async sendControl(command, requestId) {
const data = {
request_id: requestId,
cmd: command,
};
const resp = await this.sendPostRequest(`${this.config.apiHost}/verify/control/json`, data);
return server_client_1.Client.transformers.camelCaseObjectKeys(resp.data, true, true);
}
/**
* 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);
* }
* ```
*
*/
async cancel(requestId) {
return this.sendControl(enums_1.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);
* }
* ```
*/
async trigger(requestId) {
return this.sendControl(enums_1.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.');
* }
* ```
*/
async check(requestId, code) {
const resp = await this.sendPostRequest(`${this.config.apiHost}/verify/check/json`, {
request_id: requestId,
code: code,
});
return server_client_1.Client.transformers.camelCaseObjectKeys(resp.data, true, true);
}
/**
* 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}`);
* }
* ```
*/
async search(requestId) {
this.authType = server_client_1.AuthenticationType.QUERY_KEY_SECRET;
const resp = await this.sendGetRequest(`${this.config.apiHost}/verify/search/json`, { request_id: requestId });
this.authType = server_client_1.AuthenticationType.KEY_SECRET;
return server_client_1.Client.transformers.camelCaseObjectKeys(resp.data, true, true);
}
/**
* 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}`);
* }
* ```
*/
async start(request) {
const url = 'brand' in request
? `${this.config.apiHost}/verify/json`
: `${this.config.apiHost}/verify/psd2/json`;
const resp = await this.sendPostRequest(url, server_client_1.Client.transformers.snakeCaseObjectKeys((0, lodash_omit_1.default)(request, ['language'])));
return server_client_1.Client.transformers.camelCaseObjectKeys(resp.data, true, true);
}
}
exports.Verify = Verify;
//# sourceMappingURL=verify.js.map