diff --git a/.editorconfig b/.editorconfig index 918816ea..a6a8efc3 100644 --- a/.editorconfig +++ b/.editorconfig @@ -2,9 +2,9 @@ root = true [*] end_of_line = lf -insert_final_newline = true +insert_final_newline = true trim_trailing_whitespace = true max_line_length = 80 indent_style = space -indent_size = 4 +indent_size = 2 charset = utf-8 diff --git a/package-lock.json b/package-lock.json index fe1f3e6f..f06608c5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11083,6 +11083,11 @@ "dev": true, "license": "MIT" }, + "node_modules/lodash.isobject": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/lodash.isobject/-/lodash.isobject-3.0.2.tgz", + "integrity": "sha512-3/Qptq2vr7WeJbB4KHUSKlq8Pl7ASXi3UG6CMbBm8WRtXi8+GHm7mKaU3urfpSEzWe2wCIChs6/sdocUsTKJiA==" + }, "node_modules/lodash.merge": { "version": "4.6.2", "license": "MIT" @@ -15762,6 +15767,7 @@ "@vonage/auth": "^1.0.9", "@vonage/vetch": "^1.0.10", "lodash.camelcase": "^4.3.0", + "lodash.isobject": "3.0.2", "lodash.partial": "^4.2.1", "lodash.snakecase": "^4.1.1" } @@ -15805,7 +15811,8 @@ "dependencies": { "@vonage/auth": "^1.0.9", "@vonage/server-client": "^1.0.15", - "@vonage/vetch": "^1.0.10" + "@vonage/vetch": "^1.0.10", + "lodash.omit": "^4.5.0" }, "devDependencies": { "nock": "^13.2.9" @@ -19193,6 +19200,7 @@ "@vonage/auth": "^1.0.9", "@vonage/vetch": "^1.0.10", "lodash.camelcase": "^4.3.0", + "lodash.isobject": "3.0.2", "lodash.partial": "^4.2.1", "lodash.snakecase": "^4.1.1" } @@ -19229,6 +19237,7 @@ "@vonage/auth": "^1.0.9", "@vonage/server-client": "^1.0.15", "@vonage/vetch": "^1.0.10", + "lodash.omit": "^4.5.0", "nock": "^13.2.9" } }, @@ -23106,6 +23115,11 @@ "version": "4.4.0", "dev": true }, + "lodash.isobject": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/lodash.isobject/-/lodash.isobject-3.0.2.tgz", + "integrity": "sha512-3/Qptq2vr7WeJbB4KHUSKlq8Pl7ASXi3UG6CMbBm8WRtXi8+GHm7mKaU3urfpSEzWe2wCIChs6/sdocUsTKJiA==" + }, "lodash.merge": { "version": "4.6.2" }, diff --git a/packages/server-client/__tests__/__dataSets__/camelCase.ts b/packages/server-client/__tests__/__dataSets__/camelCase.ts index 75a942cc..7c753062 100644 --- a/packages/server-client/__tests__/__dataSets__/camelCase.ts +++ b/packages/server-client/__tests__/__dataSets__/camelCase.ts @@ -107,4 +107,26 @@ export default [ fooBar: null, }, }, + { + label: 'Transform to camel deep array only', + transformFn: 'camelCaseObjectKeys', + original: { + foo_bar: [ + { + fizz_buzz: 'baz_bat', + }, + ], + }, + parameters: [ + true, // deep + false, // preserve + ], + expected: { + fooBar: [ + { + fizzBuzz: 'baz_bat', + }, + ], + }, + }, ]; diff --git a/packages/server-client/lib/transformers.ts b/packages/server-client/lib/transformers.ts index a0ec41ee..bd9d06fa 100644 --- a/packages/server-client/lib/transformers.ts +++ b/packages/server-client/lib/transformers.ts @@ -1,6 +1,7 @@ import camelCase from 'lodash.camelcase'; import snakeCase from 'lodash.snakecase'; import partial from 'lodash.partial'; +import isObject from 'lodash.isobject'; export const transformObjectKeys = ( transformFn: (key: string | number) => string, @@ -8,32 +9,44 @@ export const transformObjectKeys = ( deep = false, preserve = false, ): Record => { - const transformedObject = {}; + const transformedObject = { + ...(preserve ? objectToTransform : {}), + }; for (const prop in objectToTransform) { if (!Object.prototype.hasOwnProperty.call(objectToTransform, prop)) { continue; } - + const newKey = transformFn(prop); const value = objectToTransform[prop]; - transformedObject[transformFn(prop)] - = deep - && typeof value === 'object' - && value !== null - && !Array.isArray(value) - ? transformObjectKeys( - transformFn, - value as Record, - deep, - preserve, - ) - : value; + if (!deep || !isObject(value)) { + transformedObject[newKey] = value; + continue; + } + + if (Array.isArray(value)) { + transformedObject[newKey] = value.map((t) => + isObject(t) + ? transformObjectKeys( + transformFn, + t as Record, + deep, + preserve, + ) + : t, + ); + continue; + } + + transformedObject[newKey] = transformObjectKeys( + transformFn, + value as Record, + deep, + preserve, + ); } - return { - ...transformedObject, - ...(preserve ? objectToTransform : {}), - }; + return transformedObject; }; export const camelCaseObjectKeys = partial(transformObjectKeys, camelCase); diff --git a/packages/server-client/package.json b/packages/server-client/package.json index bd1093a4..c8f06033 100644 --- a/packages/server-client/package.json +++ b/packages/server-client/package.json @@ -30,6 +30,7 @@ "@vonage/auth": "^1.0.9", "@vonage/vetch": "^1.0.10", "lodash.camelcase": "^4.3.0", + "lodash.isobject": "3.0.2", "lodash.partial": "^4.2.1", "lodash.snakecase": "^4.1.1" }, diff --git a/packages/verify/__tests__/__dataSets__/cancel.ts b/packages/verify/__tests__/__dataSets__/cancel.ts new file mode 100644 index 00000000..1b847458 --- /dev/null +++ b/packages/verify/__tests__/__dataSets__/cancel.ts @@ -0,0 +1,64 @@ +import { Command, CheckStatus } from '../../lib/enums/index'; +import { + VerifyCancelResponse, + VerifyControlErrorResponse, +} from '../../lib/interfaces/Response/index'; +import { VerifyControl, VerifyControlError } from '../../lib/interfaces/index'; + +export default [ + { + label: 'cancel a verify request', + request: [ + '/verify/control/json', + 'POST', + { + api_key: '12345', + api_secret: 'ABCDE', + request_id: 'abcdef0123456789abcdef0123456789', + cmd: 'cancel', + }, + ], + response: [ + 200, + { + status: CheckStatus.SUCCESS, + command: Command.CANCEL, + } as VerifyCancelResponse, + ], + method: 'post', + clientMethod: 'cancel', + parameters: ['abcdef0123456789abcdef0123456789'], + expected: { + status: CheckStatus.SUCCESS, + command: Command.CANCEL, + } as VerifyControl, + }, + { + label: 'cancel a verify request with an error', + request: [ + '/verify/control/json', + 'POST', + { + api_key: '12345', + api_secret: 'ABCDE', + request_id: 'abcdef0123456789abcdef0123456789', + cmd: 'cancel', + }, + ], + response: [ + 200, + { + status: CheckStatus.THROTTLED, + error_text: 'Your request is throttled', + } as VerifyControlErrorResponse, + ], + method: 'post', + clientMethod: 'cancel', + parameters: ['abcdef0123456789abcdef0123456789'], + expected: { + status: CheckStatus.THROTTLED, + error_text: 'Your request is throttled', + errorText: 'Your request is throttled', + } as VerifyControlError, + }, +]; diff --git a/packages/verify/__tests__/__dataSets__/check.ts b/packages/verify/__tests__/__dataSets__/check.ts new file mode 100644 index 00000000..30b05ef2 --- /dev/null +++ b/packages/verify/__tests__/__dataSets__/check.ts @@ -0,0 +1,42 @@ +import { VerifyCheckResponse, VerifyCheck } from '../../lib/interfaces/index'; + +export default [ + { + label: 'check a verify request', + request: [ + '/verify/check/json', + 'POST', + { + api_key: '12345', + api_secret: 'ABCDE', + request_id: 'abcdef0123456789abcdef0123456789', + code: '1234', + }, + ], + response: [ + 200, + { + request_id: 'abcdef0123456789abcdef0123456789', + event_id: '0A00000012345678', + status: '0', + price: '0.10000000', + currency: 'EUR', + estimated_price_messages_sent: '0.03330000', + } as VerifyCheckResponse, + ], + method: 'post', + clientMethod: 'check', + parameters: ['abcdef0123456789abcdef0123456789', '1234'], + expected: { + request_id: 'abcdef0123456789abcdef0123456789', + requestId: 'abcdef0123456789abcdef0123456789', + event_id: '0A00000012345678', + eventId: '0A00000012345678', + status: '0', + price: '0.10000000', + currency: 'EUR', + estimated_price_messages_sent: '0.03330000', + estimatedPriceMessagesSent: '0.03330000', + } as VerifyCheck, + }, +]; diff --git a/packages/verify/__tests__/__dataSets__/index.ts b/packages/verify/__tests__/__dataSets__/index.ts new file mode 100644 index 00000000..38a87e7e --- /dev/null +++ b/packages/verify/__tests__/__dataSets__/index.ts @@ -0,0 +1,23 @@ +import checkTests from './check'; +import cancelTests from './cancel'; +import searchTests from './search'; +import startTests from './start'; + +export default [ + { + label: 'Verify Check', + tests: checkTests, + }, + { + label: 'Verify Cancel', + tests: cancelTests, + }, + { + label: 'Verify Search', + tests: searchTests, + }, + { + label: 'Verify Start', + tests: startTests, + }, +]; diff --git a/packages/verify/__tests__/__dataSets__/search.ts b/packages/verify/__tests__/__dataSets__/search.ts new file mode 100644 index 00000000..7b62a4fc --- /dev/null +++ b/packages/verify/__tests__/__dataSets__/search.ts @@ -0,0 +1,119 @@ +import { + VerifySearchResponse, + VerifySearch, + VerifySearchErrorResponse, + VerifySearchError, +} from '../../lib/interfaces/index'; +import { + SearchStatus, + SearchCheckStatus, + SearchEventTypes, +} from '../../lib/enums/index'; + +const params = new URLSearchParams({ + api_key: '12345', + api_secret: 'ABCDE', + request_id: 'abcdef0123456789abcdef0123456789', +}); + +const response = { + request_id: 'abcdef0123456789abcdef0123456789', + account_id: '0A00000012345678', + status: SearchStatus.SUCCESS, + number: '12128675309', + price: '0.10000000', + currency: 'EUR', + sender_id: 'mySenderId', + date_submitted: '2020-01-01 12:00:00', + date_finalized: '2020-01-01 12:00:00', + first_event_date: '2020-01-01 12:00:00', + last_event_date: '2020-01-01 12:00:00', + checks: [ + { + date_received: '2020-01-01 12:00:00', + code: '987654', + status: SearchCheckStatus.INVALID, + ip_address: '20.73.345.200', + }, + ], + events: [ + { + type: SearchEventTypes.TTS, + id: '47402bc5-2e87-4e19-8cb5-c03d5f23949f', + }, + ], + estimated_price_messages_sent: '0.03330000', +} as VerifySearchResponse; + +export default [ + { + label: 'search for a request', + request: [`/verify/search/json?${params.toString()}`, 'GET'], + response: [200, response], + method: 'get', + clientMethod: 'search', + parameters: ['abcdef0123456789abcdef0123456789'], + expected: { + request_id: 'abcdef0123456789abcdef0123456789', + requestId: 'abcdef0123456789abcdef0123456789', + account_id: '0A00000012345678', + accountId: '0A00000012345678', + status: SearchStatus.SUCCESS, + number: '12128675309', + price: '0.10000000', + currency: 'EUR', + sender_id: 'mySenderId', + senderId: 'mySenderId', + date_submitted: '2020-01-01 12:00:00', + dateSubmitted: '2020-01-01 12:00:00', + date_finalized: '2020-01-01 12:00:00', + dateFinalized: '2020-01-01 12:00:00', + first_event_date: '2020-01-01 12:00:00', + firstEventDate: '2020-01-01 12:00:00', + last_event_date: '2020-01-01 12:00:00', + lastEventDate: '2020-01-01 12:00:00', + checks: [ + { + date_received: '2020-01-01 12:00:00', + dateReceived: '2020-01-01 12:00:00', + code: '987654', + status: SearchCheckStatus.INVALID, + ip_address: '20.73.345.200', + ipAddress: '20.73.345.200', + }, + ], + events: [ + { + type: SearchEventTypes.TTS, + id: '47402bc5-2e87-4e19-8cb5-c03d5f23949f', + }, + ], + estimated_price_messages_sent: '0.03330000', + estimatedPriceMessagesSent: '0.03330000', + } as VerifySearch, + }, + { + label: 'search for a request with error', + request: [`/verify/search/json?${params.toString()}`, 'GET'], + response: [ + 200, + { + request_id: 'abcdef0123456789abcdef0123456789', + status: SearchStatus.FAILED, + error_text: `Your user entered an incorrect code more than three times.`, + } as VerifySearchErrorResponse, + ], + method: 'get', + clientMethod: 'search', + parameters: ['abcdef0123456789abcdef0123456789'], + expected: { + request_id: 'abcdef0123456789abcdef0123456789', + requestId: 'abcdef0123456789abcdef0123456789', + status: SearchStatus.FAILED, + error_text: + 'Your user entered an incorrect code more than three times.', + errorText: + 'Your user entered an incorrect code more than three times.', + } as VerifySearchError, + }, +]; diff --git a/packages/verify/__tests__/__dataSets__/start.ts b/packages/verify/__tests__/__dataSets__/start.ts new file mode 100644 index 00000000..4ca0ad50 --- /dev/null +++ b/packages/verify/__tests__/__dataSets__/start.ts @@ -0,0 +1,298 @@ +import { + CheckStatus, + VerifyLanguages, + VerifyWorkflows, +} from '../../lib/enums/index'; +import { + VerifyRequestResponse, + VerifyRequest, + VerifyRequestErrorResponse, + VerifyError, +} from '../../lib/interfaces/index'; +import { PSD2Parameters, VerificationParameters } from '../../lib/types/index'; +import { PSD2, Verification } from '../../lib/classes/index'; + +export default [ + { + label: 'request a verification', + request: [ + '/verify/json', + 'POST', + { + api_key: '12345', + api_secret: 'ABCDE', + number: '12128675309', + brand: 'My Brand', + }, + ], + response: [ + 200, + { + request_id: 'abcdef0123456789abcdef0123456789', + status: CheckStatus.SUCCESS, + } as VerifyRequestResponse, + ], + method: 'post', + clientMethod: 'start', + parameters: [ + { + number: '12128675309', + brand: 'My Brand', + } as VerificationParameters, + ], + expected: { + request_id: 'abcdef0123456789abcdef0123456789', + requestId: 'abcdef0123456789abcdef0123456789', + status: CheckStatus.SUCCESS, + } as VerifyRequest, + }, + { + label: 'request a verification using class', + request: [ + '/verify/json', + 'POST', + { + api_key: '12345', + api_secret: 'ABCDE', + number: '12128675309', + brand: 'My Brand', + lg: VerifyLanguages.WELSH_UK, + }, + ], + response: [ + 200, + { + request_id: 'abcdef0123456789abcdef0123456789', + status: CheckStatus.SUCCESS, + } as VerifyRequestResponse, + ], + method: 'post', + clientMethod: 'start', + parameters: [ + new Verification( + '12128675309', + 'My Brand', + null, + null, + null, + VerifyLanguages.WELSH_UK, + ), + ], + expected: { + request_id: 'abcdef0123456789abcdef0123456789', + requestId: 'abcdef0123456789abcdef0123456789', + status: CheckStatus.SUCCESS, + } as VerifyRequest, + }, + { + label: 'request a verification with all parameters', + request: [ + '/verify/json', + 'POST', + { + api_key: '12345', + api_secret: 'ABCDE', + brand: 'My Brand', + number: '12128675309', + country: 'us', + sender_id: 'v8eeWetk884', + code_length: 6, + lg: VerifyLanguages.ENGLISH_US, + pin_expiry: 3600, + next_event_wait: 120, + workflow_id: VerifyWorkflows.TTS_TTS, + pin_code: '305699', + }, + ], + response: [ + 200, + { + request_id: 'abcdef0123456789abcdef0123456789', + status: CheckStatus.SUCCESS, + } as VerifyRequestResponse, + ], + method: 'post', + clientMethod: 'start', + parameters: [ + { + number: '12128675309', + brand: 'My Brand', + country: 'us', + senderId: 'v8eeWetk884', + codeLength: 6, + lg: VerifyLanguages.ENGLISH_US, + pinExpiry: 3600, + nextEventWait: 120, + workflowId: VerifyWorkflows.TTS_TTS, + pinCode: '305699', + } as VerificationParameters, + ], + expected: { + request_id: 'abcdef0123456789abcdef0123456789', + requestId: 'abcdef0123456789abcdef0123456789', + status: CheckStatus.SUCCESS, + } as VerifyRequest, + }, + { + label: 'request a verification returns error', + request: [ + '/verify/json', + 'POST', + { + api_key: '12345', + api_secret: 'ABCDE', + number: '12128675309', + brand: 'My Brand', + }, + ], + response: [ + 200, + { + request_id: 'abcdef0123456789abcdef0123456789', + status: CheckStatus.MISSING_REQUIRED_PARAM, + error_text: `Your request is incomplete and missing the mandatory parameter 'number'`, + network: '305699', + } as VerifyRequestErrorResponse, + ], + method: 'post', + clientMethod: 'start', + parameters: [ + { + number: '12128675309', + brand: 'My Brand', + } as VerificationParameters, + ], + expected: { + request_id: 'abcdef0123456789abcdef0123456789', + requestId: 'abcdef0123456789abcdef0123456789', + status: CheckStatus.MISSING_REQUIRED_PARAM, + error_text: `Your request is incomplete and missing the mandatory parameter 'number'`, + errorText: `Your request is incomplete and missing the mandatory parameter 'number'`, + network: '305699', + } as VerifyError, + }, + { + label: 'request a PSD2 verification', + request: [ + '/verify/psd2/json', + 'POST', + { + api_key: '12345', + api_secret: 'ABCDE', + number: '12128675309', + payee: 'My Payee', + amount: 42.0, + lg: VerifyLanguages.SWEDISH, + }, + ], + response: [ + 200, + { + request_id: 'abcdef0123456789abcdef0123456789', + status: CheckStatus.SUCCESS, + } as VerifyRequestResponse, + ], + method: 'post', + clientMethod: 'start', + parameters: [ + new PSD2( + '12128675309', + 'My Payee', + 42.0, + null, + null, + VerifyLanguages.SWEDISH, + ), + ], + expected: { + request_id: 'abcdef0123456789abcdef0123456789', + requestId: 'abcdef0123456789abcdef0123456789', + status: CheckStatus.SUCCESS, + } as VerifyRequest, + }, + { + label: 'request a PSD2 verification with class', + request: [ + '/verify/psd2/json', + 'POST', + { + api_key: '12345', + api_secret: 'ABCDE', + number: '12128675309', + payee: 'My Payee', + amount: 42.0, + }, + ], + response: [ + 200, + { + request_id: 'abcdef0123456789abcdef0123456789', + status: CheckStatus.SUCCESS, + } as VerifyRequestResponse, + ], + method: 'post', + clientMethod: 'start', + parameters: [ + { + number: '12128675309', + payee: 'My Payee', + amount: 42.0, + } as PSD2Parameters, + ], + expected: { + request_id: 'abcdef0123456789abcdef0123456789', + requestId: 'abcdef0123456789abcdef0123456789', + status: CheckStatus.SUCCESS, + } as VerifyRequest, + }, + { + label: 'request a PSD2 verification with all parameters', + request: [ + '/verify/psd2/json', + 'POST', + { + api_key: '12345', + api_secret: 'ABCDE', + number: '12128675309', + payee: 'My Payee', + amount: 42.0, + country: 'US', + sender_id: 'v8eeWetk884', + code_length: 6, + lg: VerifyLanguages.ENGLISH_US, + pin_expiry: 3600, + next_event_wait: 120, + workflow_id: VerifyWorkflows.TTS_TTS, + }, + ], + response: [ + 200, + { + request_id: 'abcdef0123456789abcdef0123456789', + status: CheckStatus.SUCCESS, + } as VerifyRequestResponse, + ], + method: 'post', + clientMethod: 'start', + parameters: [ + { + number: '12128675309', + payee: 'My Payee', + amount: 42.0, + country: 'US', + sender_id: 'v8eeWetk884', + code_length: 6, + lg: VerifyLanguages.ENGLISH_US, + pinExpiry: 3600, + nextEventWait: 120, + workflowId: VerifyWorkflows.TTS_TTS, + codeLength: 6, + } as PSD2Parameters, + ], + expected: { + request_id: 'abcdef0123456789abcdef0123456789', + requestId: 'abcdef0123456789abcdef0123456789', + status: CheckStatus.SUCCESS, + } as VerifyRequest, + }, +]; diff --git a/packages/verify/__tests__/common.ts b/packages/verify/__tests__/common.ts new file mode 100644 index 00000000..68a7132f --- /dev/null +++ b/packages/verify/__tests__/common.ts @@ -0,0 +1 @@ +export const BASE_URL = 'https://api.nexmo.com'; diff --git a/packages/verify/__tests__/verify.test.ts b/packages/verify/__tests__/verify.test.ts index a9872fe3..8dbc841d 100644 --- a/packages/verify/__tests__/verify.test.ts +++ b/packages/verify/__tests__/verify.test.ts @@ -1,245 +1,31 @@ import { PSD2, Verification, Verify } from '../lib/index'; import nock from 'nock'; import { Auth } from '@vonage/auth'; +import { BASE_URL } from './common'; +import testDataSets from './__dataSets__/index'; -const BASE_URL = 'https://api.nexmo.com'.replace(/\/+$/, ''); - -describe('Verify', () => { +describe.each(testDataSets)('$label', ({ tests }) => { let client; + let scope; beforeEach(function () { client = new Verify(new Auth({ apiKey: '12345', apiSecret: 'ABCDE' })); + scope = nock(BASE_URL).persist(); }); afterEach(function () { client = null; + scope = null; + nock.cleanAll(); }); - test('can check a verify request', async () => { - const resp = { - request_id: 'abcdef0123456789abcdef0123456789', - event_id: '0A00000012345678', - status: '0', - price: '0.10000000', - currency: 'EUR', - estimated_price_messages_sent: '0.03330000', - }; - nock(BASE_URL) - .persist() - .post('/verify/check/json', { - api_key: '12345', - api_secret: 'ABCDE', - request_id: 'abcdef0123456789abcdef0123456789', - code: '1234', - }) - .reply(200, resp); - - const results = await client.check( - 'abcdef0123456789abcdef0123456789', - '1234', - ); - expect(results['event_id']).toEqual('0A00000012345678'); - }); - - test('can search for a verify request', async () => { - const resp = { - request_id: 'abcdef0123456789abcdef0123456789', - account_id: 'abcdef01', - status: 'IN PROGRESS', - number: '447700900000', - price: '0.10000000', - currency: 'EUR', - sender_id: 'mySenderId', - date_submitted: '2020-01-01 12:00:00', - date_finalized: '2020-01-01 12:00:00', - first_event_date: '2020-01-01 12:00:00', - last_event_date: '2020-01-01 12:00:00', - checks: [ - { - date_received: '2020-01-01 12:00:00', - code: '987654', - status: 'abc123', - ip_address: '123.0.0.255', - }, - ], - events: [ - { - type: 'abc123', - id: 'abc123', - }, - ], - estimated_price_messages_sent: '0.03330000', - }; - nock(BASE_URL) - .persist() - .get('/verify/search/json') - .query({ - api_key: '12345', - api_secret: 'ABCDE', - request_id: 'abcdef0123456789abcdef0123456789', - }) - .reply(200, resp); - - const results = await client.search('abcdef0123456789abcdef0123456789'); - expect(results['sender_id']).toEqual('mySenderId'); - }); - - test('can cancel a verify request', async () => { - const resp = { - status: '0', - command: 'cancel', - }; - nock(BASE_URL) - .persist() - .post('/verify/control/json', { - api_key: '12345', - api_secret: 'ABCDE', - request_id: 'abcdef0123456789abcdef0123456789', - cmd: 'cancel', - }) - .reply(200, resp); - - const results = await client.cancel('abcdef0123456789abcdef0123456789'); - expect(results['status']).toEqual('0'); - }); - - test('can trigger the next event in a verify request', async () => { - const resp = { - status: '0', - command: 'trigger_next_event', - }; - nock(BASE_URL) - .persist() - .post('/verify/control/json', { - api_key: '12345', - api_secret: 'ABCDE', - request_id: 'abcdef0123456789abcdef0123456789', - cmd: 'trigger_next_event', - }) - .reply(200, resp); - - const results = await client.trigger('abcdef0123456789abcdef0123456789'); - expect(results['status']).toEqual('0'); - }); - - const startVerifyCases = [ - [ - ['15556661234', 'Acme Inc'], - { - api_key: '12345', - api_secret: 'ABCDE', - number: '15556661234', - brand: 'Acme Inc', - }, - { request_id: 'abcdef0123456789abcdef0123456789', status: '0' }, - ], - [ - ['15556661234', 'Acme Inc', 'US', null, 4], - { - api_key: '12345', - api_secret: 'ABCDE', - number: '15556661234', - brand: 'Acme Inc', - country: 'US', - code_length: 4, - }, - { request_id: 'abcdef0123456789abcdef0123456789', status: '0' }, - ], - [ - ['15556661234', 'Acme Inc', 'US', null, 4], - { - api_key: '12345', - api_secret: 'ABCDE', - number: '15556661234', - brand: 'Acme Inc', - country: 'US', - code_length: 4, - }, - { request_id: 'abcdef0123456789abcdef0123456789', status: '0' }, - ], - [ - [ - '15556661234', - 'Acme Inc', - 'US', - 'VERIFY', - 4, - 'en-us', - 3600, - 900, - 5, - ], - { - api_key: '12345', - api_secret: 'ABCDE', - number: '15556661234', - brand: 'Acme Inc', - country: 'US', - code_length: 4, - sender_id: 'VERIFY', - lg: 'en-us', - pin_expiry: 3600, - next_event_wait: 900, - workflow_id: 5, - }, - { request_id: 'abcdef0123456789abcdef0123456789', status: '0' }, - ], - ]; - - test.each(startVerifyCases)( - 'given %p and %p, can request a verification %p', - async (objectData, expectedBody, expectedResponse) => { - nock(BASE_URL) - .persist() - .post('/verify/json', expectedBody) - .reply(200, expectedResponse); - - const results = await client.start(new Verification(...objectData)); - expect(results['status']).toEqual('0'); - }, - ); - - const startVerifyPSD2Cases = [ - [ - ['15556661234', 'Acme Inc', 10.5], - { - api_key: '12345', - api_secret: 'ABCDE', - number: '15556661234', - payee: 'Acme Inc', - amount: 10.5, - }, - { request_id: 'abcdef0123456789abcdef0123456789', status: '0' }, - ], - [ - ['15556661234', 'Acme Inc', 10.5, 'US', 4, 'en-us', 3600, 900, 5], - { - api_key: '12345', - api_secret: 'ABCDE', - number: '15556661234', - payee: 'Acme Inc', - amount: 10.5, - country: 'US', - code_length: 4, - lg: 'en-us', - pin_expiry: 3600, - next_event_wait: 900, - workflow_id: 5, - }, - { request_id: 'abcdef0123456789abcdef0123456789', status: '0' }, - ], - ]; - - test.each(startVerifyPSD2Cases)( - 'given %p and %p, can request a psd2 verification %p', - async (objectData, expectedBody, expectedResponse) => { - nock(BASE_URL) - .persist() - .post('/verify/psd2/json', expectedBody) - .reply(200, expectedResponse); + test.each(tests)( + 'Can $label using: $clientMethod', + async ({ request, response, clientMethod, parameters, expected }) => { + scope.intercept(...request).reply(...response); - const results = await client.start(new PSD2(...objectData)); - expect(results['status']).toEqual('0'); + const results = await client[clientMethod](...parameters); + expect(results).toEqual(expected); }, ); }); diff --git a/packages/verify/lib/classes/PSD2.ts b/packages/verify/lib/classes/PSD2.ts index dd8dcb32..5a92fa34 100644 --- a/packages/verify/lib/classes/PSD2.ts +++ b/packages/verify/lib/classes/PSD2.ts @@ -1,52 +1,59 @@ -import { PSD2Request } from '../interfaces/PSD2Request' +import { PSD2Parameters } from '../types/index'; +import { VerifyWorkflows, VerifyLanguages } from '../enums/index'; -export class PSD2 implements PSD2Request { - number: string - payee: string - amount: string - country?: string - codeLength?: number - language?: string - pinExpiry?: number - nextEventWait?: number - workflowId?: number +export class PSD2 implements PSD2Parameters { + number: string; + payee: string; + amount: number; + country?: string; + codeLength?: number; + lg?: VerifyLanguages; + /** + * @deprecated + */ + language?: string; + pinExpiry?: number; + nextEventWait?: number; + workflowId?: VerifyWorkflows; - constructor( - phoneNumber: string, - payee: string, - amount: string, - country?: string, - codeLength?: number, - language?: string, - pinExpiry?: number, - nextEventWait?: number, - workflowId?: number - ) { - this.number = phoneNumber + constructor( + phoneNumber: string, + payee: string, + amount: number, + country?: string, + codeLength?: number, + language?: VerifyLanguages, + lg?: VerifyLanguages, + pinExpiry?: number, + nextEventWait?: number, + workflowId?: VerifyWorkflows, + ) { + this.number = phoneNumber; - if (country) { - this.country = country - } - if (payee) { - this.payee = payee - } - if (amount) { - this.amount = amount - } - if (codeLength) { - this.codeLength = codeLength - } - if (language) { - this.language = language - } - if (pinExpiry) { - this.pinExpiry = pinExpiry - } - if (nextEventWait) { - this.nextEventWait = nextEventWait - } - if (workflowId) { - this.workflowId = workflowId - } + if (country) { + this.country = country; } + if (payee) { + this.payee = payee; + } + if (amount) { + this.amount = amount; + } + 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; + } + } } diff --git a/packages/verify/lib/classes/Verification.ts b/packages/verify/lib/classes/Verification.ts index 72f650bc..93ef4b51 100644 --- a/packages/verify/lib/classes/Verification.ts +++ b/packages/verify/lib/classes/Verification.ts @@ -1,50 +1,57 @@ -import { VerificationRequest } from '../interfaces/VerificationRequest' +import { VerificationParameters } from '../types/index'; +import { VerifyWorkflows, VerifyLanguages } from '../enums/index'; -export class Verification implements VerificationRequest { - number: string - brand: string - country?: string - senderId?: string - codeLength?: number - language?: string - pinExpiry?: number - nextEventWait?: number - workflowId?: number +export class Verification implements VerificationParameters { + number: string; + brand: string; + country?: string; + senderId?: string; + codeLength?: number; + /** + * @deprecated + */ + language?: string; + lg?: VerifyLanguages; + pinExpiry?: number; + nextEventWait?: number; + workflowId?: VerifyWorkflows; - constructor( - phoneNumber: string, - brand: string, - country?: string, - senderId?: string, - codeLength?: number, - language?: string, - pinExpiry?: number, - nextEventWait?: number, - workflowId?: number - ) { - this.number = phoneNumber - this.brand = brand + constructor( + phoneNumber: string, + brand: string, + country?: string, + senderId?: string, + codeLength?: number, + language?: VerifyLanguages, + lg?: VerifyLanguages, + pinExpiry?: number, + nextEventWait?: number, + workflowId?: VerifyWorkflows, + ) { + this.number = phoneNumber; + this.brand = brand; - if (country) { - this.country = country - } - if (senderId) { - this.senderId = senderId - } - if (codeLength) { - this.codeLength = codeLength - } - if (language) { - this.language = language - } - if (pinExpiry) { - this.pinExpiry = pinExpiry - } - if (nextEventWait) { - this.nextEventWait = nextEventWait - } - if (workflowId) { - this.workflowId = workflowId - } + 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; + } + } } diff --git a/packages/verify/lib/classes/index.ts b/packages/verify/lib/classes/index.ts new file mode 100644 index 00000000..274894c3 --- /dev/null +++ b/packages/verify/lib/classes/index.ts @@ -0,0 +1,2 @@ +export * from './PSD2'; +export * from './Verification'; diff --git a/packages/verify/lib/enums/CheckStatus.ts b/packages/verify/lib/enums/CheckStatus.ts new file mode 100644 index 00000000..891b88ac --- /dev/null +++ b/packages/verify/lib/enums/CheckStatus.ts @@ -0,0 +1,18 @@ +export enum CheckStatus { + SUCCESS = '0', + THROTTLED = '1', + MISSING_REQUIRED_PARAM = '2', + INVALID_PARAM = '3', + INVALID_CREDENTIALS = '4', + INTERNAL_ERROR = '5', + FAILED_TO_PROCESS = '6', + BARRED_API_KEY = '8', + PARTNER_QUOTA_EXCEEDED = '9', + CONCURRENT_VERIFICATIONS = '10', + UNSUPPORTED_NETWORK = '15', + CODE_MISMATCH = '16', + INVALID_CODE = '17', + DELIEVERY_FAILED = '19', + PIN_NOT_SUPPORTED = '20', + NON_PREMITTED_DESTINATION = '29', +} diff --git a/packages/verify/lib/enums/SearchCheckStatus.ts b/packages/verify/lib/enums/SearchCheckStatus.ts new file mode 100644 index 00000000..d940c5c4 --- /dev/null +++ b/packages/verify/lib/enums/SearchCheckStatus.ts @@ -0,0 +1,4 @@ +export enum SearchCheckStatus { + INVALID = 'INVALID', + VALID = 'VALID', +} diff --git a/packages/verify/lib/enums/SearchEventTypes.ts b/packages/verify/lib/enums/SearchEventTypes.ts new file mode 100644 index 00000000..50b1081c --- /dev/null +++ b/packages/verify/lib/enums/SearchEventTypes.ts @@ -0,0 +1,4 @@ +export enum SearchEventTypes { + TTS = 'TTS', + SMS = 'SMS', +} diff --git a/packages/verify/lib/enums/SearchStatus.ts b/packages/verify/lib/enums/SearchStatus.ts new file mode 100644 index 00000000..3f734b4b --- /dev/null +++ b/packages/verify/lib/enums/SearchStatus.ts @@ -0,0 +1,7 @@ +export enum SearchStatus { + IN_PROGRESS = 'IN PROGRESS', + SUCCESS = 'SUCCESS', + FAILED = 'FAILED', + EXPIRED = 'EXPIRED', + CANCELLED = 'CANCELLED', +} diff --git a/packages/verify/lib/enums/VerifyLanguages.ts b/packages/verify/lib/enums/VerifyLanguages.ts new file mode 100644 index 00000000..371e2e38 --- /dev/null +++ b/packages/verify/lib/enums/VerifyLanguages.ts @@ -0,0 +1,41 @@ +export enum VerifyLanguages { + ARABIC = 'ar-xa', + CZECH = 'cs-cz', + WELSH = 'cy-cy', + WELSH_UK = 'cy-gb', + DANISH = 'da-dk', + GERMAN = 'de-de', + GREEK = 'el-gr', + ENGLISH_AUSTRALIA = 'en-au', + ENGLISH_UK = 'en-gb', + ENGLISH_INDIA = 'en-in', + ENGLISH_US = 'en-us', + SPANISH_SPAIN = 'es-es', + SPANISH_MEXICO = 'es-mx', + SPANISH_US = 'es-us', + FINNISH = 'fi-fi', + FILIPINO = 'fil-ph', + FRENCH_CANADA = 'fr-ca', + FRENCH_FRANCE = 'fr-fr', + HINDI = 'hi-in', + HUNGARIAN = 'hu-hu', + INDONESIAN = 'id-id', + ICELANDIC = 'is-is', + ITALIAN = 'it-it', + JAPANESE = 'ja-jp', + KOREAN = 'ko-kr', + NORWEGIAN = 'nb-no', + DUTCH = 'nl-nl', + POLISH = 'pl-pl', + PORTUGUESE_BRAZIL = 'pt-br', + PORTUGUESE_PORTUGAL = 'pt-pt', + ROMANIAN = 'ro-ro', + RUSSIAN = 'ru-ru', + SWEDISH = 'sv-se', + THAI = 'th-th', + TURKISH = 'tr-tr', + VIETNAMESE = 'vi-vn', + YUE_CHINESE_CANTONESE = 'yue-cn', + CHINESE_MAINLAND = 'zh-cn', + CHINESE_TAIWAN = 'zh-tw', +} diff --git a/packages/verify/lib/enums/VerifyStatus.ts b/packages/verify/lib/enums/VerifyStatus.ts new file mode 100644 index 00000000..713ca8c1 --- /dev/null +++ b/packages/verify/lib/enums/VerifyStatus.ts @@ -0,0 +1,18 @@ +export enum VerifyStatus { + SUCCESS = '0', + THROTTLED = '1', + MISSING_REQUIRED_PARAM = '2', + INVALID_PARAM = '3', + INVALID_CREDENTIALS = '4', + INTERNAL_ERROR = '5', + FAILED_TO_PROCESS = '6', + BARRED_API_KEY = '8', + PARTNER_QUOTA_EXCEEDED = '9', + CONCURRENT_VERIFICATIONS = '10', + UNSUPPORTED_NETWORK = '15', + CODE_MISMATCH = '16', + INVALID_CODE = '17', + DELIEVERY_FAILED = '19', + PIN_NOT_SUPPORTED = '20', + NON_PREMITTED_DESTINATION = '29', +} diff --git a/packages/verify/lib/enums/Workflows.ts b/packages/verify/lib/enums/Workflows.ts new file mode 100644 index 00000000..d2608c8c --- /dev/null +++ b/packages/verify/lib/enums/Workflows.ts @@ -0,0 +1,9 @@ +export enum VerifyWorkflows { + SMS_TTS_TTS = '1', + SMS_SMS_TTS = '2', + TTS_TTS = '3', + SMS_SMS = '4', + SMS_TTS = '5', + SMS = '6', + TTS = '7', +} diff --git a/packages/verify/lib/enums/index.ts b/packages/verify/lib/enums/index.ts new file mode 100644 index 00000000..d9099eff --- /dev/null +++ b/packages/verify/lib/enums/index.ts @@ -0,0 +1,7 @@ +export * from './CheckStatus'; +export * from './Command'; +export * from './SearchCheckStatus'; +export * from './SearchEventTypes'; +export * from './SearchStatus'; +export * from './VerifyLanguages'; +export * from './Workflows'; diff --git a/packages/verify/lib/index.ts b/packages/verify/lib/index.ts index b1c5eea3..311b7ae0 100644 --- a/packages/verify/lib/index.ts +++ b/packages/verify/lib/index.ts @@ -1,3 +1,5 @@ -export { PSD2 } from './classes/PSD2' -export { Verification } from './classes/Verification' -export { Verify } from './verify' +export { Verify } from './verify'; +export * from './classes/index'; +export * from './enums/index'; +export * from './interfaces/index'; +export * from './types/index'; diff --git a/packages/verify/lib/interfaces/PSD2Request.ts b/packages/verify/lib/interfaces/PSD2Request.ts index 0606c119..c2b1edc1 100644 --- a/packages/verify/lib/interfaces/PSD2Request.ts +++ b/packages/verify/lib/interfaces/PSD2Request.ts @@ -1,11 +1,6 @@ -export interface PSD2Request { - number: string - payee: string - amount: string - country?: string - codeLength?: number - language?: string - pinExpiry?: number - nextEventWait?: number - workflowId?: number -} +import { PSD2Parameters } from '../types/index'; + +/** + * @deprecated please use PSD2Parameters + */ +export type PSD2Request = PSD2Parameters diff --git a/packages/verify/lib/interfaces/Response/CheckInformation.ts b/packages/verify/lib/interfaces/Response/CheckInformation.ts index b861eb5b..391657c5 100644 --- a/packages/verify/lib/interfaces/Response/CheckInformation.ts +++ b/packages/verify/lib/interfaces/Response/CheckInformation.ts @@ -1,6 +1,6 @@ -export interface CheckInformation { - date_recieved: string - code: string - status: string - ip_address: string -} +import { SearchEventInformationResponse } from './SearchEventInformationResponse'; + +/** + * @deprecated Please use SearchEventInformation instead + */ +export type CheckInformation = SearchEventInformationResponse diff --git a/packages/verify/lib/interfaces/Response/EventInformation.ts b/packages/verify/lib/interfaces/Response/EventInformation.ts index 81d200c6..8594554a 100644 --- a/packages/verify/lib/interfaces/Response/EventInformation.ts +++ b/packages/verify/lib/interfaces/Response/EventInformation.ts @@ -1,4 +1,6 @@ -export interface EventInformation { - type: string - id: string -} +import { SearchEventInformationResponse } from './SearchEventInformationResponse'; + +/** + * @deprecated please use SearchEventInformation instead + **/ +export type EventInformation = SearchEventInformationResponse diff --git a/packages/verify/lib/interfaces/Response/PSD2RequestResponse.ts b/packages/verify/lib/interfaces/Response/PSD2RequestResponse.ts index 574b86a0..d7d52f76 100644 --- a/packages/verify/lib/interfaces/Response/PSD2RequestResponse.ts +++ b/packages/verify/lib/interfaces/Response/PSD2RequestResponse.ts @@ -1,4 +1,3 @@ -export interface VerifyRequestResponse { - request_id: string - status: string -} +import { VerifyRequestResponse } from './VerifyRequestResponse'; + +export type VerifyPDS2Response = VerifyRequestResponse diff --git a/packages/verify/lib/interfaces/Response/SearchCheckInformationResponse.ts b/packages/verify/lib/interfaces/Response/SearchCheckInformationResponse.ts new file mode 100644 index 00000000..262ddf40 --- /dev/null +++ b/packages/verify/lib/interfaces/Response/SearchCheckInformationResponse.ts @@ -0,0 +1,8 @@ +import { SearchCheckStatus } from '../../enums/index'; + +export interface SearchCheckInformationResponse { + date_received: string + code: string + status: SearchCheckStatus + ip_address: string +} diff --git a/packages/verify/lib/interfaces/Response/SearchEventInformationResponse.ts b/packages/verify/lib/interfaces/Response/SearchEventInformationResponse.ts new file mode 100644 index 00000000..ee42ab15 --- /dev/null +++ b/packages/verify/lib/interfaces/Response/SearchEventInformationResponse.ts @@ -0,0 +1,6 @@ +import { SearchEventTypes } from '../../enums/index'; + +export interface SearchEventInformationResponse { + type: SearchEventTypes + id: string +} diff --git a/packages/verify/lib/interfaces/Response/VerifyCancelResponse.ts b/packages/verify/lib/interfaces/Response/VerifyCancelResponse.ts new file mode 100644 index 00000000..3de69387 --- /dev/null +++ b/packages/verify/lib/interfaces/Response/VerifyCancelResponse.ts @@ -0,0 +1,9 @@ +import { VerifyControlResponse } from './VerifyControlResponse'; +import { Command } from '../../enums/index'; + +/** + * @deprecated Please use VerifyControlResponse instead + */ +export interface VerifyCancelResponse extends VerifyControlResponse { + command: Command.CANCEL +} diff --git a/packages/verify/lib/interfaces/Response/VerifyCheckError.ts b/packages/verify/lib/interfaces/Response/VerifyCheckError.ts index 91ec2e54..3faefca9 100644 --- a/packages/verify/lib/interfaces/Response/VerifyCheckError.ts +++ b/packages/verify/lib/interfaces/Response/VerifyCheckError.ts @@ -1,5 +1,6 @@ -export interface VerifyRequestError { - request_id: string - status: string - error_text: string -} +import { VerifyCheckResponse } from './VerifyCheckResponse'; + +/** + * @deprecated Please use VerifyCheckResponse instead + */ +export type VerifyRequestError = VerifyCheckResponse diff --git a/packages/verify/lib/interfaces/Response/VerifyCheckErrorResponse.ts b/packages/verify/lib/interfaces/Response/VerifyCheckErrorResponse.ts new file mode 100644 index 00000000..e18f69b5 --- /dev/null +++ b/packages/verify/lib/interfaces/Response/VerifyCheckErrorResponse.ts @@ -0,0 +1,7 @@ +import { CheckStatus } from '../../enums/index'; + +export interface VerifyCheckErrorResponse { + request_id: string + status: CheckStatus + error_text: string +} diff --git a/packages/verify/lib/interfaces/Response/VerifyCheckResponse.ts b/packages/verify/lib/interfaces/Response/VerifyCheckResponse.ts index bc39525c..4f9530da 100644 --- a/packages/verify/lib/interfaces/Response/VerifyCheckResponse.ts +++ b/packages/verify/lib/interfaces/Response/VerifyCheckResponse.ts @@ -1,8 +1,11 @@ +import { CheckStatus } from '../../enums/index'; + export interface VerifyCheckResponse { request_id: string - event_id: string - status: string - price: string - currency: string - estimated_price_messages_sent: string + status: CheckStatus + event_id?: string + price?: string + currency?: string + estimated_price_messages_sent?: string + error_text?: string } diff --git a/packages/verify/lib/interfaces/Response/VerifyControlError.ts b/packages/verify/lib/interfaces/Response/VerifyControlError.ts index f4b82eab..316bb28f 100644 --- a/packages/verify/lib/interfaces/Response/VerifyControlError.ts +++ b/packages/verify/lib/interfaces/Response/VerifyControlError.ts @@ -1,4 +1,6 @@ -export interface VerifyControlError { - status: string +import { CheckStatus } from '../../enums/index'; + +export interface VerifyControlErrorResponse { + status: CheckStatus error_text: string } diff --git a/packages/verify/lib/interfaces/Response/VerifyControlResponse.ts b/packages/verify/lib/interfaces/Response/VerifyControlResponse.ts index cad5cbd3..6c4e210e 100644 --- a/packages/verify/lib/interfaces/Response/VerifyControlResponse.ts +++ b/packages/verify/lib/interfaces/Response/VerifyControlResponse.ts @@ -1,4 +1,6 @@ +import { Command } from '../../enums/index'; + export interface VerifyControlResponse { status: string - command: string + command: Command } diff --git a/packages/verify/lib/interfaces/Response/VerifyRequestError.ts b/packages/verify/lib/interfaces/Response/VerifyRequestError.ts index 91ec2e54..104a1df1 100644 --- a/packages/verify/lib/interfaces/Response/VerifyRequestError.ts +++ b/packages/verify/lib/interfaces/Response/VerifyRequestError.ts @@ -1,5 +1,6 @@ -export interface VerifyRequestError { - request_id: string +export interface VerifyRequestErrorResponse { + request_id?: string status: string error_text: string + network?: string } diff --git a/packages/verify/lib/interfaces/Response/VerifyResponse.ts b/packages/verify/lib/interfaces/Response/VerifyResponse.ts index fefb6f3d..35932fb0 100644 --- a/packages/verify/lib/interfaces/Response/VerifyResponse.ts +++ b/packages/verify/lib/interfaces/Response/VerifyResponse.ts @@ -1,3 +1,5 @@ -import { VetchResponse } from '@vonage/vetch' - -export interface VoiceResponse extends VetchResponse {} +import { CheckStatus } from '../../enums/index'; +export interface VerifyResponse { + request_id: string + status: CheckStatus +} diff --git a/packages/verify/lib/interfaces/Response/VerifySearchErrorResponse.ts b/packages/verify/lib/interfaces/Response/VerifySearchErrorResponse.ts new file mode 100644 index 00000000..4fb80976 --- /dev/null +++ b/packages/verify/lib/interfaces/Response/VerifySearchErrorResponse.ts @@ -0,0 +1,7 @@ +import { SearchStatus } from '../../enums/index'; + +export interface VerifySearchErrorResponse { + request_id: string + status: SearchStatus + error_text: string +} diff --git a/packages/verify/lib/interfaces/Response/VerifySearchResponse.ts b/packages/verify/lib/interfaces/Response/VerifySearchResponse.ts index dcdeb511..b610829d 100644 --- a/packages/verify/lib/interfaces/Response/VerifySearchResponse.ts +++ b/packages/verify/lib/interfaces/Response/VerifySearchResponse.ts @@ -1,19 +1,20 @@ -import { CheckInformation } from './CheckInformation' -import { EventInformation } from './EventInformation' +import { SearchCheckInformationResponse } from './SearchCheckInformationResponse'; +import { SearchEventInformationResponse } from './SearchEventInformationResponse'; +import { SearchStatus } from './../../enums/index'; export interface VerifySearchResponse { request_id: string account_id: string - status: string + status: SearchStatus number: string price: string currency: string sender_id: string date_submitted: string date_finalized: string - first_event_date?: string - last_event_date?: string - checks?: CheckInformation[] - events?: EventInformation[] - estimated_price_messages_sent + first_event_date: string + last_event_date: string + checks: SearchCheckInformationResponse[] + events: SearchEventInformationResponse[] + estimated_price_messages_sent: string } diff --git a/packages/verify/lib/interfaces/Response/index.ts b/packages/verify/lib/interfaces/Response/index.ts new file mode 100644 index 00000000..c3805bc1 --- /dev/null +++ b/packages/verify/lib/interfaces/Response/index.ts @@ -0,0 +1,17 @@ +export * from './CheckInformation'; +export * from './EventInformation'; +export * from './PSD2RequestResponse'; +export * from './SearchCheckInformationResponse'; +export * from './SearchEventInformationResponse'; +export * from './UnblockRequestResponse'; +export * from './VerifyCancelResponse'; +export * from './VerifyCheckError'; +export * from './VerifyCheckErrorResponse'; +export * from './VerifyCheckResponse'; +export * from './VerifyControlError'; +export * from './VerifyControlResponse'; +export * from './VerifyRequestError'; +export * from './VerifyRequestResponse'; +export * from './VerifyResponse'; +export * from './VerifySearchErrorResponse'; +export * from './VerifySearchResponse'; diff --git a/packages/verify/lib/interfaces/VerificationRequest.ts b/packages/verify/lib/interfaces/VerificationRequest.ts index d6b7ed0b..64a6f2a3 100644 --- a/packages/verify/lib/interfaces/VerificationRequest.ts +++ b/packages/verify/lib/interfaces/VerificationRequest.ts @@ -1,11 +1,6 @@ -export interface VerificationRequest { - number: string - brand: string - country?: string - senderId?: string - codeLength?: number - language?: string - pinExpiry?: number - nextEventWait?: number - workflowId?: number -} +import { VerificationParameters } from '../types/index'; + +/** + * @deprecated please use VerificationParameters + */ +export type VerificationRequest = VerificationParameters diff --git a/packages/verify/lib/interfaces/VerifyCheck.ts b/packages/verify/lib/interfaces/VerifyCheck.ts new file mode 100644 index 00000000..b37e4544 --- /dev/null +++ b/packages/verify/lib/interfaces/VerifyCheck.ts @@ -0,0 +1,8 @@ +import { VerifyRequestResponse } from './Response/index'; + +export interface VerifyCheck extends VerifyRequestResponse { + requestId: string + eventId?: string + errorText?: string + estimatedPriceMessagesSent?: string +} diff --git a/packages/verify/lib/interfaces/VerifyCheckError.ts b/packages/verify/lib/interfaces/VerifyCheckError.ts new file mode 100644 index 00000000..2ffdd7c3 --- /dev/null +++ b/packages/verify/lib/interfaces/VerifyCheckError.ts @@ -0,0 +1,6 @@ +import { VerifyCheckErrorResponse } from './Response/index'; + +export interface VerifyCheckError extends VerifyCheckErrorResponse { + requestId: string + errorId: string +} diff --git a/packages/verify/lib/interfaces/VerifyControl.ts b/packages/verify/lib/interfaces/VerifyControl.ts new file mode 100644 index 00000000..5f9af63e --- /dev/null +++ b/packages/verify/lib/interfaces/VerifyControl.ts @@ -0,0 +1,3 @@ +import { VerifyControlResponse } from './Response/index'; + +export type VerifyControl = VerifyControlResponse diff --git a/packages/verify/lib/interfaces/VerifyControlError.ts b/packages/verify/lib/interfaces/VerifyControlError.ts new file mode 100644 index 00000000..06cc0760 --- /dev/null +++ b/packages/verify/lib/interfaces/VerifyControlError.ts @@ -0,0 +1,6 @@ +import { CheckStatus } from '../../lib/enums/index'; + +export type VerifyControlError = { + status: CheckStatus + errorText: string +} diff --git a/packages/verify/lib/interfaces/VerifyError.ts b/packages/verify/lib/interfaces/VerifyError.ts new file mode 100644 index 00000000..1ae7b0a8 --- /dev/null +++ b/packages/verify/lib/interfaces/VerifyError.ts @@ -0,0 +1,7 @@ +import { VerifyRequest } from './VerifyRequest'; + +export interface VerifyError extends VerifyRequest { + error_text: string + errorText: string + network?: string +} diff --git a/packages/verify/lib/interfaces/VerifyRequest.ts b/packages/verify/lib/interfaces/VerifyRequest.ts new file mode 100644 index 00000000..f290ba1e --- /dev/null +++ b/packages/verify/lib/interfaces/VerifyRequest.ts @@ -0,0 +1,5 @@ +import { VerifyRequestResponse } from './Response/VerifyRequestResponse'; + +export interface VerifyRequest extends VerifyRequestResponse { + requestId: string +} diff --git a/packages/verify/lib/interfaces/VerifySearch.ts b/packages/verify/lib/interfaces/VerifySearch.ts new file mode 100644 index 00000000..1735deac --- /dev/null +++ b/packages/verify/lib/interfaces/VerifySearch.ts @@ -0,0 +1,14 @@ +import { VerifySearchResponse } from './Response/index'; +import { VerifySearchCheck } from './VerifySearchCheck'; + +export interface VerifySearch extends Omit { + requestId: string + accountId: string + senderId: string + dateSubmitted: string + dateFinalized: string + firstEventDate?: string + lastEventDate?: string + checks: VerifySearchCheck[] + estimatedPriceMessagesSent: string +} diff --git a/packages/verify/lib/interfaces/VerifySearchCheck.ts b/packages/verify/lib/interfaces/VerifySearchCheck.ts new file mode 100644 index 00000000..331b6b8f --- /dev/null +++ b/packages/verify/lib/interfaces/VerifySearchCheck.ts @@ -0,0 +1,6 @@ +import { VerifyCheckResponse } from './Response/index'; + +export interface VerifySearchCheck extends VerifyCheckResponse { + dateReceived: string + ipAddress: string +} diff --git a/packages/verify/lib/interfaces/VerifySearchError.ts b/packages/verify/lib/interfaces/VerifySearchError.ts new file mode 100644 index 00000000..24efef63 --- /dev/null +++ b/packages/verify/lib/interfaces/VerifySearchError.ts @@ -0,0 +1,6 @@ +import { VerifySearchErrorResponse } from './Response/index'; + +export interface VerifySearchError extends VerifySearchErrorResponse { + requestId: string + errorText: string +} diff --git a/packages/verify/lib/interfaces/index.ts b/packages/verify/lib/interfaces/index.ts new file mode 100644 index 00000000..e5439115 --- /dev/null +++ b/packages/verify/lib/interfaces/index.ts @@ -0,0 +1,11 @@ +export * from './Response/index'; +export * from './PSD2Request'; +export * from './VerificationRequest'; +export * from './VerifyCheck'; +export * from './VerifyCheckError'; +export * from './VerifySearch'; +export * from './VerifyControl'; +export * from './VerifyControlError'; +export * from './VerifySearchError'; +export * from './VerifyRequest'; +export * from './VerifyError'; diff --git a/packages/verify/lib/types/PSD2Parameters.ts b/packages/verify/lib/types/PSD2Parameters.ts new file mode 100644 index 00000000..aa4430bc --- /dev/null +++ b/packages/verify/lib/types/PSD2Parameters.ts @@ -0,0 +1,9 @@ +import { VerificationParameters } from './VerificationParams'; + +export type PSD2Parameters = Omit< + VerificationParameters, + 'brand' | 'senderId' | 'pinCode' +> & { + payee: string + amount: number +} diff --git a/packages/verify/lib/types/VerificationParams.ts b/packages/verify/lib/types/VerificationParams.ts new file mode 100644 index 00000000..b80a0ef9 --- /dev/null +++ b/packages/verify/lib/types/VerificationParams.ts @@ -0,0 +1,14 @@ +import { VerifyWorkflows, VerifyLanguages } from '../enums/index'; + +export interface VerificationParameters { + number: string + brand: string + country?: string + senderId?: string + codeLength?: number + lg?: VerifyLanguages + pinExpiry?: number + nextEventWait?: number + workflowId?: VerifyWorkflows + pinCode?: string +} diff --git a/packages/verify/lib/types/index.ts b/packages/verify/lib/types/index.ts new file mode 100644 index 00000000..9b00a97f --- /dev/null +++ b/packages/verify/lib/types/index.ts @@ -0,0 +1,3 @@ +export * from './PSD2Parameters'; +export * from './VerificationParams'; +export * from './VerifyClassParameters'; diff --git a/packages/verify/lib/verify.ts b/packages/verify/lib/verify.ts index 321d4b13..245e5f9a 100644 --- a/packages/verify/lib/verify.ts +++ b/packages/verify/lib/verify.ts @@ -1,120 +1,92 @@ import { Client } from '@vonage/server-client'; -import { Command } from './enums/Command'; -import { PSD2Request } from './interfaces/PSD2Request'; -import { VerifyCheckResponse } from './interfaces/Response/VerifyCheckResponse'; -import { VerifyControlResponse } from './interfaces/Response/VerifyControlResponse'; -import { VerifySearchResponse } from './interfaces/Response/VerifySearchResponse'; -import { VerificationRequest } from './interfaces/VerificationRequest'; - -const remapObjects = (mapping, newObject: T, oldObject: O): T => { - for (const key in mapping) { - if (oldObject[mapping[key]]) { - newObject[key] = oldObject[mapping[key]]; - delete oldObject[mapping[key]]; - } - } - newObject = { ...newObject, ...oldObject }; - return newObject; -}; +import { Command } from './enums/index'; +import { + VerifyControl, + VerifyControlError, + VerifyControlErrorResponse, + VerifyControlResponse, + VerifyCheckError, + VerifyCheck, + VerifyCheckResponse, + VerifyRequestResponse, + VerifySearchResponse, + VerifySearch, + VerifySearchError, + VerifyRequestErrorResponse, + VerifyError, + VerifyRequest, +} from './interfaces/index'; +import { VerificationParameters, PSD2Parameters } from './types/index'; +import omit from 'lodash.omit'; export class Verify extends Client { - public async cancel(requestId: string): Promise { + public async sendControl( + command: Command, + requestId: string, + ): Promise { const data = { request_id: requestId, cmd: Command.CANCEL, }; - const resp = await this.sendPostRequest( - `${this.config.apiHost}/verify/control/json`, - data, - ); - return resp.data; + // 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); + } + + public async cancel( + requestId: string, + ): Promise { + return this.sendControl(Command.CANCEL, requestId); + } + + public async trigger( + requestId: string, + ): Promise { + return this.sendControl(Command.TRIGGER_NEXT_EVENT, requestId); } public async check( requestId: string, code: string, - ): Promise { - const data = { - request_id: requestId, - code, - }; + ): Promise { + // eslint-disable-next-line max-len + const resp = await this.sendPostRequest< + VerifyRequestResponse | VerifyRequestErrorResponse + >(`${this.config.apiHost}/verify/check/json`, { + request_id: requestId, + code: code, + }); - const resp = await this.sendPostRequest( - `${this.config.apiHost}/verify/check/json`, - data, - ); - return resp.data; + return Client.transformers.camelCaseObjectKeys(resp.data, true, true); } - public async search(requestId: string): Promise { + public async search( + requestId: string, + ): Promise { const resp = await this.sendGetRequest( `${this.config.apiHost}/verify/search/json`, { request_id: requestId }, ); - return resp.data; + return Client.transformers.camelCaseObjectKeys(resp.data, true, true); } public async start( - request: VerificationRequest | PSD2Request, - ): Promise { - let url = ''; - if ('brand' in request) { - url = `${this.config.apiHost}/verify/json`; - } else { - url = `${this.config.apiHost}/verify/psd2/json`; - } - - let newRequest: VerificationRequest | PSD2Request; - let mapping: any; - if ('brand' in request) { - newRequest = { - number: request.number, - brand: request.brand, - }; - mapping = { - sender_id: 'senderId', - code_length: 'codeLength', - pin_expiry: 'pinExpiry', - lg: 'language', - next_event_wait: 'nextEventWait', - workflow_id: 'workflowId', - }; - } else { - newRequest = { - number: request.number, - payee: request.payee, - amount: request.amount, - }; - mapping = { - code_length: 'codeLength', - pin_expiry: 'pinExpiry', - lg: 'language', - next_event_wait: 'nextEventWait', - workflow_id: 'workflowId', - }; - } - - newRequest = remapObjects(mapping, newRequest, request); + request: VerificationParameters | PSD2Parameters, + ): Promise { + const url + = 'brand' in request + ? `${this.config.apiHost}/verify/json` + : `${this.config.apiHost}/verify/psd2/json`; const resp = await this.sendPostRequest( url, - newRequest, + Client.transformers.snakeCaseObjectKeys(omit(request, ['language'])), ); - return resp.data; - } - - public async trigger(requestId: string) { - const data = { - request_id: requestId, - cmd: Command.TRIGGER_NEXT_EVENT, - }; - const resp = await this.sendPostRequest( - `${this.config.apiHost}/verify/control/json`, - data, - ); - return resp.data; + return Client.transformers.camelCaseObjectKeys(resp.data, true, true); } } diff --git a/packages/verify/package.json b/packages/verify/package.json index b18afa80..a7a378eb 100644 --- a/packages/verify/package.json +++ b/packages/verify/package.json @@ -37,7 +37,8 @@ "dependencies": { "@vonage/auth": "^1.0.9", "@vonage/server-client": "^1.0.15", - "@vonage/vetch": "^1.0.10" + "@vonage/vetch": "^1.0.10", + "lodash.omit": "^4.5.0" }, "devDependencies": { "nock": "^13.2.9"