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.

512 lines (499 loc) 16.6 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/index.ts var index_exports = {}; __export(index_exports, { CheckStatus: () => CheckStatus, Command: () => Command, PSD2: () => PSD2, SearchCheckStatus: () => SearchCheckStatus, SearchEventTypes: () => SearchEventTypes, SearchStatus: () => SearchStatus, Verification: () => Verification, Verify: () => Verify, VerifyLanguages: () => VerifyLanguages, VerifyWorkflows: () => VerifyWorkflows }); module.exports = __toCommonJS(index_exports); // lib/verify.ts var import_server_client = require("@vonage/server-client"); // lib/enums/CheckStatus.ts var CheckStatus = /* @__PURE__ */ ((CheckStatus2) => { CheckStatus2["SUCCESS"] = "0"; CheckStatus2["THROTTLED"] = "1"; CheckStatus2["MISSING_REQUIRED_PARAM"] = "2"; CheckStatus2["INVALID_PARAM"] = "3"; CheckStatus2["INVALID_CREDENTIALS"] = "4"; CheckStatus2["INTERNAL_ERROR"] = "5"; CheckStatus2["FAILED_TO_PROCESS"] = "6"; CheckStatus2["BARRED_API_KEY"] = "8"; CheckStatus2["PARTNER_QUOTA_EXCEEDED"] = "9"; CheckStatus2["CONCURRENT_VERIFICATIONS"] = "10"; CheckStatus2["UNSUPPORTED_NETWORK"] = "15"; CheckStatus2["CODE_MISMATCH"] = "16"; CheckStatus2["INVALID_CODE"] = "17"; CheckStatus2["DELIVERY_FAILED"] = "19"; CheckStatus2["PIN_NOT_SUPPORTED"] = "20"; CheckStatus2["NON_PERMITTED_DESTINATION"] = "29"; return CheckStatus2; })(CheckStatus || {}); // lib/enums/Command.ts var Command = /* @__PURE__ */ ((Command2) => { Command2["CANCEL"] = "cancel"; Command2["TRIGGER_NEXT_EVENT"] = "trigger_next_event"; return Command2; })(Command || {}); // lib/enums/SearchCheckStatus.ts var SearchCheckStatus = /* @__PURE__ */ ((SearchCheckStatus2) => { SearchCheckStatus2["INVALID"] = "INVALID"; SearchCheckStatus2["VALID"] = "VALID"; return SearchCheckStatus2; })(SearchCheckStatus || {}); // lib/enums/SearchEventTypes.ts var SearchEventTypes = /* @__PURE__ */ ((SearchEventTypes2) => { SearchEventTypes2["TTS"] = "TTS"; SearchEventTypes2["SMS"] = "SMS"; return SearchEventTypes2; })(SearchEventTypes || {}); // lib/enums/SearchStatus.ts var SearchStatus = /* @__PURE__ */ ((SearchStatus2) => { SearchStatus2["IN_PROGRESS"] = "IN PROGRESS"; SearchStatus2["SUCCESS"] = "SUCCESS"; SearchStatus2["FAILED"] = "FAILED"; SearchStatus2["EXPIRED"] = "EXPIRED"; SearchStatus2["CANCELLED"] = "CANCELLED"; return SearchStatus2; })(SearchStatus || {}); // lib/enums/VerifyLanguages.ts var VerifyLanguages = /* @__PURE__ */ ((VerifyLanguages2) => { VerifyLanguages2["ARABIC"] = "ar-xa"; VerifyLanguages2["CZECH"] = "cs-cz"; VerifyLanguages2["WELSH"] = "cy-cy"; VerifyLanguages2["WELSH_UK"] = "cy-gb"; VerifyLanguages2["DANISH"] = "da-dk"; VerifyLanguages2["GERMAN"] = "de-de"; VerifyLanguages2["GREEK"] = "el-gr"; VerifyLanguages2["ENGLISH_AUSTRALIA"] = "en-au"; VerifyLanguages2["ENGLISH_UK"] = "en-gb"; VerifyLanguages2["ENGLISH_INDIA"] = "en-in"; VerifyLanguages2["ENGLISH_US"] = "en-us"; VerifyLanguages2["SPANISH_SPAIN"] = "es-es"; VerifyLanguages2["SPANISH_MEXICO"] = "es-mx"; VerifyLanguages2["SPANISH_US"] = "es-us"; VerifyLanguages2["FINNISH"] = "fi-fi"; VerifyLanguages2["FILIPINO"] = "fil-ph"; VerifyLanguages2["FRENCH_CANADA"] = "fr-ca"; VerifyLanguages2["FRENCH_FRANCE"] = "fr-fr"; VerifyLanguages2["HINDI"] = "hi-in"; VerifyLanguages2["HUNGARIAN"] = "hu-hu"; VerifyLanguages2["INDONESIAN"] = "id-id"; VerifyLanguages2["ICELANDIC"] = "is-is"; VerifyLanguages2["ITALIAN"] = "it-it"; VerifyLanguages2["JAPANESE"] = "ja-jp"; VerifyLanguages2["KOREAN"] = "ko-kr"; VerifyLanguages2["NORWEGIAN"] = "nb-no"; VerifyLanguages2["DUTCH"] = "nl-nl"; VerifyLanguages2["POLISH"] = "pl-pl"; VerifyLanguages2["PORTUGUESE_BRAZIL"] = "pt-br"; VerifyLanguages2["PORTUGUESE_PORTUGAL"] = "pt-pt"; VerifyLanguages2["ROMANIAN"] = "ro-ro"; VerifyLanguages2["RUSSIAN"] = "ru-ru"; VerifyLanguages2["SWEDISH"] = "sv-se"; VerifyLanguages2["THAI"] = "th-th"; VerifyLanguages2["TURKISH"] = "tr-tr"; VerifyLanguages2["VIETNAMESE"] = "vi-vn"; VerifyLanguages2["YUE_CHINESE_CANTONESE"] = "yue-cn"; VerifyLanguages2["CHINESE_MAINLAND"] = "zh-cn"; VerifyLanguages2["CHINESE_TAIWAN"] = "zh-tw"; return VerifyLanguages2; })(VerifyLanguages || {}); // lib/enums/Workflows.ts var VerifyWorkflows = /* @__PURE__ */ ((VerifyWorkflows2) => { VerifyWorkflows2["SMS_TTS_TTS"] = "1"; VerifyWorkflows2["SMS_SMS_TTS"] = "2"; VerifyWorkflows2["TTS_TTS"] = "3"; VerifyWorkflows2["SMS_SMS"] = "4"; VerifyWorkflows2["SMS_TTS"] = "5"; VerifyWorkflows2["SMS"] = "6"; VerifyWorkflows2["TTS"] = "7"; return VerifyWorkflows2; })(VerifyWorkflows || {}); // lib/verify.ts 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 ); } }; // lib/classes/PSD2.ts var PSD2 = class { /** * The phone number to be verified. */ number; /** * The payee's name or identifier for the payment confirmation. */ payee; /** * The decimal amount of the payment to be confirmed, in Euros. */ amount; /** * The country code associated with the phone number (optional). */ country; /** * The desired length of the verification code (optional). */ codeLength; /** * @deprecated This property is deprecated. Use `lg` instead. */ language; /** * The language for sending verification messages (optional). */ lg; /** * The duration in seconds for which the verification code will be valid. */ pinExpiry; /** * The duration in seconds to wait before sending the next verification event. */ nextEventWait; /** * The workflow ID for customizing the verification process (optional). */ workflowId; constructor(phoneNumber, payee, amount, country, codeLength, language, lg, pinExpiry, nextEventWait, workflowId) { this.number = phoneNumber; this.amount = amount; this.payee = payee; if (country) { this.country = country; } if (codeLength) { this.codeLength = codeLength; } if (language) { this.lg = language; this.language = language; } if (pinExpiry) { this.pinExpiry = pinExpiry; } if (nextEventWait) { this.nextEventWait = nextEventWait; } if (workflowId) { this.workflowId = workflowId; } } }; // lib/classes/Verification.ts var Verification = class { /** * The phone number to be verified. */ number; /** * The brand or application name associated with the verification request. */ brand; /** * The country code associated with the phone number (optional). */ country; /** * The sender ID or phone number that will be used to send verification * messages (optional). */ senderId; /** * The desired length of the verification code (optional). */ codeLength; /** * @deprecated This property is deprecated. Use `lg` instead. */ language; /** * The language for sending verification messages (optional). */ lg; /** * The duration in seconds for which the verification code will be valid. */ pinExpiry; /** * The duration in seconds to wait before sending the next verification event. */ nextEventWait; /** * The workflow ID for customizing the verification process (optional). */ workflowId; constructor(phoneNumber, brand, country, senderId, codeLength, language, lg, pinExpiry, nextEventWait, workflowId) { this.number = phoneNumber; this.brand = brand; if (country) { this.country = country; } if (senderId) { this.senderId = senderId; } if (codeLength) { this.codeLength = codeLength; } if (language) { this.lg = language; this.language = language; } if (pinExpiry) { this.pinExpiry = pinExpiry; } if (nextEventWait) { this.nextEventWait = nextEventWait; } if (workflowId) { this.workflowId = workflowId; } } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { CheckStatus, Command, PSD2, SearchCheckStatus, SearchEventTypes, SearchStatus, Verification, Verify, VerifyLanguages, VerifyWorkflows });