UNPKG

@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.

243 lines (241 loc) 8.79 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // lib/verify.ts var verify_exports = {}; __export(verify_exports, { Verify: () => Verify }); module.exports = __toCommonJS(verify_exports); var import_server_client = require("@vonage/server-client"); var { omit } = import_server_client.Client.transformers; var Verify = class extends import_server_client.Client { authType = import_server_client.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 import_server_client.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("cancel" /* 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("trigger_next_event" /* 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 }); return import_server_client.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 = import_server_client.AuthenticationType.QUERY_KEY_SECRET; const resp = await this.sendGetRequest( `${this.config.apiHost}/verify/search/json`, { request_id: requestId } ); this.authType = import_server_client.AuthenticationType.KEY_SECRET; return import_server_client.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, import_server_client.Client.transformers.snakeCaseObjectKeys(omit(["language"], request)) ); return import_server_client.Client.transformers.camelCaseObjectKeys( resp.data, true, true ); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Verify });