diff --git a/examples/integrated-flows-examples/src/verification/app.ts b/examples/integrated-flows-examples/src/verification/app.ts index f3b00a82..138a6830 100644 --- a/examples/integrated-flows-examples/src/verification/app.ts +++ b/examples/integrated-flows-examples/src/verification/app.ts @@ -55,7 +55,7 @@ dotenv.config(); const startSmsVerificationFlow = async (phoneNumber: string) => { const requestData = Verification.startVerificationHelper.buildSmsRequest(phoneNumber); - const response = await sinch.verification.startVerifications.startSms(requestData); + const response = await sinch.verification.verifications.startSms(requestData); console.log('Verification request sent! Please check the SMS on your you phone to get the OTP.'); const answers = await inquirer.prompt([ { @@ -74,7 +74,7 @@ dotenv.config(); const startPhoneCallVerificationFlow = async (phoneNumber: string) => { const requestData = Verification.startVerificationHelper.buildPhoneCallRequest(phoneNumber); - const response = await sinch.verification.startVerifications.startPhoneCall(requestData); + const response = await sinch.verification.verifications.startPhoneCall(requestData); console.log('Verification request sent! Please answer to the phone call ans listen to the OTP.'); const answers = await inquirer.prompt([ { @@ -91,7 +91,7 @@ dotenv.config(); const startFlashCallVerificationFlow = async (phoneNumber: string) => { const requestData = Verification.startVerificationHelper.buildFlashCallRequest(phoneNumber); - const response = await sinch.verification.startVerifications.startFlashCall(requestData); + const response = await sinch.verification.verifications.startFlashCall(requestData); console.log('Verification request sent! Please check the phone number calling you.'); const answers = await inquirer.prompt([ { @@ -112,7 +112,7 @@ dotenv.config(); const requestData = Verification.startVerificationHelper.buildDataRequest(phoneNumber); let response; try { - response = await sinch.verification.startVerifications.startData(requestData); + response = await sinch.verification.verifications.startData(requestData); } catch (error: any) { console.log(`Impossible to process the seamless verification: ${error.data})`); return; diff --git a/examples/simple-examples/README.md b/examples/simple-examples/README.md index 05cab7c4..500f9f06 100644 --- a/examples/simple-examples/README.md +++ b/examples/simple-examples/README.md @@ -151,16 +151,16 @@ yarn run numbers:regions:list | Service | Sample application name and location | Required parameters | |---------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------| -| Start Verifications | [./src/verification/start/start-sms.ts](./src/verification/start/start-sms.ts) | `VERIFICATION_IDENTITY` | -| | [./src/verification/start/start-phonecall.ts](./src/verification/start/start-phonecall.ts) | `VERIFICATION_IDENTITY` | -| | [./src/verification/start/start-flashcall.ts](./src/verification/start/start-flashcall.ts) | `VERIFICATION_IDENTITY` | -| | [./src/verification/start/start-data.ts](./src/verification/start/start-data.ts) | `VERIFICATION_IDENTITY` | +| Start Verifications | [./src/verification/verifications/sms/start-sms.ts](./src/verification/verifications/sms/start-sms.ts) | `VERIFICATION_IDENTITY` | +| | [./src/verification/verifications/phonecall/start-phonecall.ts](./src/verification/verifications/phonecall/start-phonecall.ts) | `VERIFICATION_IDENTITY` | +| | [./src/verification/verifications/flashcall/start-flashcall.ts](./src/verification/verifications/flashcall/start-flashcall.ts) | `VERIFICATION_IDENTITY` | +| | [./src/verification/verifications/data/start-data.ts](./src/verification/verifications/data/start-data.ts) | `VERIFICATION_IDENTITY` | | Verifications | [./src/verification/verifications/sms/report-with-id_sms.ts](./src/verification/verifications/sms/report-with-id_sms.ts) | `VERIFICATION_ID` + `VERIFICATION_CODE` | | | [./src/verification/verifications/sms/report-with-identity_sms.ts](./src/verification/verifications/sms/report-with-identity_sms.ts) | `VERIFICATION_IDENTITY` + `VERIFICATION_CODE` | | | [./src/verification/verifications/flashcall/report-with-id_flashcall.ts](./src/verification/verifications/flashcall/report-with-id_flashcall.ts) | `VERIFICATION_ID` + `VERIFICATION_CLI` | | | [./src/verification/verifications/flashcall/report-with-identity_flashcall.ts](./src/verification/verifications/flashcall/report-with-identity_flashcall.ts) | `VERIFICATION_IDENTITY` + `VERIFICATION_CLI` | -| | [./src/verification/verifications/callout/report-with-id_callout.ts](./src/verification/verifications/callout/report-with-id_callout.ts) | `VERIFICATION_ID` + `VERIFICATION_CODE` | -| | [./src/verification/verifications/callout/report-with-identity_callout.ts](./src/verification/verifications/callout/report-with-identity_callout.ts) | `VERIFICATION_IDENTITY` + `VERIFICATION_CODE` | +| | [./src/verification/verifications/callout/report-with-id_callout.ts](./src/verification/verifications/phonecall/report-with-id_callout.ts) | `VERIFICATION_ID` + `VERIFICATION_CODE` | +| | [./src/verification/verifications/callout/report-with-identity_callout.ts](./src/verification/verifications/phonecall/report-with-identity_callout.ts) | `VERIFICATION_IDENTITY` + `VERIFICATION_CODE` | | Verification-status | [./src/verification/verification-status/verification-by-id.ts](./src/verification/verification-status/verification-by-id.ts) | `VERIFICATION_ID` | | | [./src/verification/verification-status/verification-by-identity.ts](./src/verification/verification-status/verification-by-identity.ts) | `VERIFICATION_IDENTITY` | | | [./src/verification/verification-status/verification-by-reference.ts](./src/verification/verification-status/verification-by-reference.ts) | `VERIFICATION_REFERENCE` | diff --git a/examples/simple-examples/package.json b/examples/simple-examples/package.json index 9f12ae68..4b72e1bb 100644 --- a/examples/simple-examples/package.json +++ b/examples/simple-examples/package.json @@ -153,14 +153,14 @@ "sms:inbounds:list": "ts-node src/sms/inbounds/list.ts", "sms:inbounds:get": "ts-node src/sms/inbounds/get.ts", "verification:verifications:start-sms": "ts-node src/verification/verifications/sms/start-sms.ts", - "verification:verifications:start-callout": "ts-node src/verification/verifications/callout/start-callout.ts", + "verification:verifications:start-phonecall": "ts-node src/verification/verifications/phonecall/start-phonecall.ts", "verification:verifications:start-flashcall": "ts-node src/verification/verifications/flashcall/start-flashcall.ts", - "verification:verifications:start-seamless": "ts-node src/verification/verifications/seamless/start-seamless.ts", + "verification:verifications:start-data": "ts-node src/verification/verifications/data/start-data.ts", "verification:verifications:report-with-id:sms": "ts-node src/verification/verifications/sms/report-with-id_sms.ts", - "verification:verifications:report-with-id:callout": "ts-node src/verification/verifications/callout/report-with-id_callout.ts", + "verification:verifications:report-with-id:callout": "ts-node src/verification/verifications/phonecall/report-with-id_callout.ts", "verification:verifications:report-with-id:flashcall": "ts-node src/verification/verifications/flashcall/report-with-id_flashcall.ts", "verification:verifications:report-with-identity:sms": "ts-node src/verification/verifications/sms/report-with-identity_sms.ts", - "verification:verifications:report-with-identity:callout": "ts-node src/verification/verifications/callout/report-with-identity_callout.ts", + "verification:verifications:report-with-identity:callout": "ts-node src/verification/verifications/phonecall/report-with-identity_callout.ts", "verification:verifications:report-with-identity:flashcall": "ts-node src/verification/verifications/flashcall/report-with-identity_flashcall.ts", "verification:verification-status:verification-by-id": "ts-node src/verification/verification-status/verification-by-id.ts", "verification:verification-status:verification-by-identity": "ts-node src/verification/verification-status/verification-by-identity.ts", diff --git a/examples/simple-examples/src/verification/start/start-flashcall.ts b/examples/simple-examples/src/verification/start/start-flashcall.ts deleted file mode 100644 index 6bc29856..00000000 --- a/examples/simple-examples/src/verification/start/start-flashcall.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { Verification } from '@sinch/sdk-core'; -import { - getPrintFormat, - getVerificationIdentityFromConfig, - initVerificationService, - printFullResponse, -} from '../../config'; - -(async () => { - console.log('*********************************'); - console.log('* StartVerification - flashCall *'); - console.log('*********************************'); - - const verificationIdentity = getVerificationIdentityFromConfig(); - - const requestData = Verification.startVerificationHelper.buildFlashCallRequest( - verificationIdentity, - `test-reference-for-flashCall-verification_${verificationIdentity}`, - 20, - ); - - const verificationService = initVerificationService(); - const response = await verificationService.startVerifications.startFlashCall(requestData); - - const printFormat = getPrintFormat(process.argv); - - if (printFormat === 'pretty') { - console.log(`Verification ID = ${response.id}`); - console.log(`FlashCall verification specific field: cliFilter = ${response.flashCall?.cliFilter}`); - } else { - printFullResponse(response); - } -})(); diff --git a/examples/simple-examples/src/verification/start/start-sms.ts b/examples/simple-examples/src/verification/start/start-sms.ts deleted file mode 100644 index 51869c94..00000000 --- a/examples/simple-examples/src/verification/start/start-sms.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { Verification } from '@sinch/sdk-core'; -import { - getPrintFormat, - getVerificationIdentityFromConfig, - initVerificationService, - printFullResponse, -} from '../../config'; - -(async () => { - console.log('***************************'); - console.log('* StartVerification - SMS *'); - console.log('***************************'); - - const verificationIdentity = getVerificationIdentityFromConfig(); - - const requestData = Verification.startVerificationHelper.buildSmsRequest( - verificationIdentity, - `test-reference-for-sms-verification_${verificationIdentity}`, - { - locale: 'sv-SE', - }, - ); - - const verificationService = initVerificationService(); - const response = await verificationService.startVerifications.startSms(requestData); - - const printFormat = getPrintFormat(process.argv); - - if (printFormat === 'pretty') { - console.log(`Verification ID = ${response.id}`); - console.log(`SMS verification specific field: template = ${response.sms?.template}`); - } else { - printFullResponse(response); - } - -})(); diff --git a/examples/simple-examples/src/verification/start/start-data.ts b/examples/simple-examples/src/verification/verifications/data/start-data.ts similarity index 89% rename from examples/simple-examples/src/verification/start/start-data.ts rename to examples/simple-examples/src/verification/verifications/data/start-data.ts index 9bd7307e..3c0f2f7b 100644 --- a/examples/simple-examples/src/verification/start/start-data.ts +++ b/examples/simple-examples/src/verification/verifications/data/start-data.ts @@ -4,7 +4,7 @@ import { getVerificationIdentityFromConfig, initVerificationService, printFullResponse, -} from '../../config'; +} from '../../../config'; (async () => { console.log('****************************'); @@ -19,7 +19,7 @@ import { ); const verificationService = initVerificationService(); - const response = await verificationService.startVerifications.startData(requestData); + const response = await verificationService.verifications.startData(requestData); const printFormat = getPrintFormat(process.argv); diff --git a/examples/simple-examples/src/verification/verifications/seamless/start-seamless.ts b/examples/simple-examples/src/verification/verifications/data/start-seamless.ts similarity index 100% rename from examples/simple-examples/src/verification/verifications/seamless/start-seamless.ts rename to examples/simple-examples/src/verification/verifications/data/start-seamless.ts diff --git a/examples/simple-examples/src/verification/verifications/flashcall/start-flashcall.ts b/examples/simple-examples/src/verification/verifications/flashcall/start-flashcall.ts index 07615bad..125cf583 100644 --- a/examples/simple-examples/src/verification/verifications/flashcall/start-flashcall.ts +++ b/examples/simple-examples/src/verification/verifications/flashcall/start-flashcall.ts @@ -6,7 +6,6 @@ import { printFullResponse, } from '../../../config'; -/** @deprecated see ../../start/start-flashcall.ts instead */ (async () => { console.log('*********************************'); console.log('* StartVerification - flashCall *'); diff --git a/examples/simple-examples/src/verification/verifications/callout/report-with-id_callout.ts b/examples/simple-examples/src/verification/verifications/phonecall/report-with-id_callout.ts similarity index 100% rename from examples/simple-examples/src/verification/verifications/callout/report-with-id_callout.ts rename to examples/simple-examples/src/verification/verifications/phonecall/report-with-id_callout.ts diff --git a/examples/simple-examples/src/verification/verifications/callout/report-with-identity_callout.ts b/examples/simple-examples/src/verification/verifications/phonecall/report-with-identity_callout.ts similarity index 100% rename from examples/simple-examples/src/verification/verifications/callout/report-with-identity_callout.ts rename to examples/simple-examples/src/verification/verifications/phonecall/report-with-identity_callout.ts diff --git a/examples/simple-examples/src/verification/verifications/callout/start-callout.ts b/examples/simple-examples/src/verification/verifications/phonecall/start-callout.ts similarity index 100% rename from examples/simple-examples/src/verification/verifications/callout/start-callout.ts rename to examples/simple-examples/src/verification/verifications/phonecall/start-callout.ts diff --git a/examples/simple-examples/src/verification/start/start-phonecall.ts b/examples/simple-examples/src/verification/verifications/phonecall/start-phonecall.ts similarity index 87% rename from examples/simple-examples/src/verification/start/start-phonecall.ts rename to examples/simple-examples/src/verification/verifications/phonecall/start-phonecall.ts index fe5aa1d7..27f5c7b7 100644 --- a/examples/simple-examples/src/verification/start/start-phonecall.ts +++ b/examples/simple-examples/src/verification/verifications/phonecall/start-phonecall.ts @@ -4,7 +4,7 @@ import { getVerificationIdentityFromConfig, initVerificationService, printFullResponse, -} from '../../config'; +} from '../../../config'; (async () => { console.log('**********************************'); @@ -19,7 +19,7 @@ import { ); const verificationService = initVerificationService(); - const response = await verificationService.startVerifications.startPhoneCall(requestData); + const response = await verificationService.verifications.startPhoneCall(requestData); const printFormat = getPrintFormat(process.argv); diff --git a/examples/simple-examples/src/verification/verifications/sms/start-sms.ts b/examples/simple-examples/src/verification/verifications/sms/start-sms.ts index 11308321..75abfea2 100644 --- a/examples/simple-examples/src/verification/verifications/sms/start-sms.ts +++ b/examples/simple-examples/src/verification/verifications/sms/start-sms.ts @@ -6,7 +6,6 @@ import { printFullResponse, } from '../../../config'; -/** @deprecated see ../../start/start-sms.ts instead */ (async () => { console.log('***************************'); console.log('* StartVerification - SMS *'); diff --git a/packages/verification/src/rest/v1/index.ts b/packages/verification/src/rest/v1/index.ts index e586074f..248310a9 100644 --- a/packages/verification/src/rest/v1/index.ts +++ b/packages/verification/src/rest/v1/index.ts @@ -1,5 +1,4 @@ export * from './callbacks'; -export * from './start-verifications'; export * from './verification-status'; export * from './verifications'; export * from './enums'; diff --git a/packages/verification/src/rest/v1/start-verifications/index.ts b/packages/verification/src/rest/v1/start-verifications/index.ts deleted file mode 100644 index 9d5217d0..00000000 --- a/packages/verification/src/rest/v1/start-verifications/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './start-verifications-api'; -export * from './start-verifications-api.jest.fixture'; diff --git a/packages/verification/src/rest/v1/start-verifications/start-verifications-api.jest.fixture.ts b/packages/verification/src/rest/v1/start-verifications/start-verifications-api.jest.fixture.ts deleted file mode 100644 index 1521206f..00000000 --- a/packages/verification/src/rest/v1/start-verifications/start-verifications-api.jest.fixture.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { StartVerificationsApi } from './start-verifications-api'; -import { - StartDataVerificationRequestData, - StartDataVerificationResponse, - StartFlashCallVerificationRequestData, - StartFlashCallVerificationResponse, - StartPhoneCallVerificationRequestData, - StartPhoneCallVerificationResponse, - StartSmsVerificationRequestData, - StartSmsVerificationResponse, -} from '../../../models'; - -export class StartVerificationsApiFixture implements Partial> { - /** - * Fixture associated to function startSms - */ - public startSms: jest.Mock, - [StartSmsVerificationRequestData]> = jest.fn(); - /** - * Fixture associated to function startFlashCall - */ - public startFlashCall: jest.Mock< - Promise, - [StartFlashCallVerificationRequestData]> = jest.fn(); - /** - * Fixture associated to function startPhoneCall - */ - public startPhoneCall: jest.Mock, - [StartPhoneCallVerificationRequestData]> = jest.fn(); - /** - * Fixture associated to function startData - */ - public startData: jest.Mock< - Promise, - [StartDataVerificationRequestData]> = jest.fn(); -} diff --git a/packages/verification/src/rest/v1/start-verifications/start-verifications-api.ts b/packages/verification/src/rest/v1/start-verifications/start-verifications-api.ts deleted file mode 100644 index 6b1fb016..00000000 --- a/packages/verification/src/rest/v1/start-verifications/start-verifications-api.ts +++ /dev/null @@ -1,203 +0,0 @@ -import { VerificationDomainApi } from '../verification-domain-api'; -import { RequestBody, SinchClientParameters } from '@sinch/sdk-client'; -import { - StartDataVerificationRequestData, - StartDataVerificationResponse, - StartFlashCallVerificationRequestData, - StartFlashCallVerificationResponse, - StartPhoneCallVerificationRequestData, - StartPhoneCallVerificationResponse, - StartSmsVerificationRequestData, - StartSmsVerificationResponse, - StartVerificationWithPhoneCall, - StartVerificationWithSms, -} from '../../../models'; - -export class StartVerificationsApi extends VerificationDomainApi { - - /** - * Initialize your interface - * - * @param {SinchClientParameters} sinchClientParameters - The parameters used to initialize the API Client. - */ - constructor(sinchClientParameters: SinchClientParameters) { - super(sinchClientParameters, 'VerificationsApi'); - } - - /** - * Start verification with SMS - * This method is used by the mobile and web Verification SDKs to start a verification. It can also be used to request a verification from your backend, by making a request. - * @param { StartSmsVerificationRequestData } data - The data to provide to the API call. - */ - public async startSms(data: StartSmsVerificationRequestData): Promise { - this.client = this.getSinchClient(); - (data.startVerificationWithSmsRequestBody as any).method = 'sms'; - const getParams = this.client.extractQueryParams(data, [] as never[]); - const headers: { [key: string]: string | undefined } = { - 'Content-Type': 'application/json', - 'Accept': 'application/json', - }; - if (data.startVerificationWithSmsRequestBody.smsOptions?.locale !== undefined) { - headers['Accept-Language'] = data.startVerificationWithSmsRequestBody.smsOptions.locale; - } - - // Special fields handling: see method for details - const requestDataBody = this.performStartSmsRequestBodyTransformation(data.startVerificationWithSmsRequestBody); - - const body: RequestBody = requestDataBody - ? JSON.stringify(requestDataBody) - : '{}'; - const path = '/verification/v1/verifications'; - const basePathUrl = this.client.apiClientOptions.hostname + path; - - const requestOptions - = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); - - return this.client.processCall({ - url, - requestOptions, - apiName: this.apiName, - operationId: 'StartVerificationWithSms', - }); - } - - performStartSmsRequestBodyTransformation(body: StartVerificationWithSms): StartVerificationWithSms { - const requestDataBody = { ...body }; - if (requestDataBody.smsOptions?.expiry !== undefined) { - const expiry = requestDataBody.smsOptions?.expiry; - if (expiry instanceof Date) { - requestDataBody.smsOptions.expiry = this.formatTime(expiry); - } - } - // Remove the `locale` property from the body as it is used as a header parameter for the API call - delete requestDataBody.smsOptions?.locale; - - return requestDataBody; - } - - formatTime(date: Date): string { - // Assume the hours needs to be set at the UTC time + Pad single-digit components with leading zeros - const formattedHours = String(date.getUTCHours()).padStart(2, '0'); - const formattedMinutes = String(date.getUTCMinutes()).padStart(2, '0'); - const formattedSeconds = String(date.getUTCSeconds()).padStart(2, '0'); - - // Concatenate the components with colons to form the formatted time string - return `${formattedHours}:${formattedMinutes}:${formattedSeconds}`; - } - - /** - * Start verification with a FlashCall - * This method is used by the mobile and web Verification SDKs to start a verification. It can also be used to request a verification from your backend, by making a request. - * @param { StartFlashCallVerificationRequestData } data - The data to provide to the API call. - */ - public async startFlashCall( - data: StartFlashCallVerificationRequestData, - ): Promise { - this.client = this.getSinchClient(); - (data.startVerificationWithFlashCallRequestBody as any).method = 'flashcall'; - const getParams = this.client.extractQueryParams(data, [] as never[]); - const headers: { [key: string]: string | undefined } = { - 'Content-Type': 'application/json', - 'Accept': 'application/json', - }; - - const body: RequestBody = data['startVerificationWithFlashCallRequestBody'] - ? JSON.stringify(data['startVerificationWithFlashCallRequestBody']) - : '{}'; - const path = '/verification/v1/verifications'; - const basePathUrl = this.client.apiClientOptions.hostname + path; - - const requestOptions - = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); - - return this.client.processCall({ - url, - requestOptions, - apiName: this.apiName, - operationId: 'StartVerificationWithFlashCall', - }); - } - - /** - * Start verification with a phone call - * This method is used by the mobile and web Verification SDKs to start a verification. It can also be used to request a verification from your backend, by making a request. - * @param { StartPhoneCallVerificationRequestData } data - The data to provide to the API call. - */ - public async startPhoneCall( - data: StartPhoneCallVerificationRequestData, - ): Promise { - this.client = this.getSinchClient(); - (data.startVerificationWithPhoneCallRequestBody as any).method = 'callout'; - const getParams = this.client.extractQueryParams(data, [] as never[]); - const headers: { [key: string]: string | undefined } = { - 'Content-Type': 'application/json', - 'Accept': 'application/json', - }; - - // Special fields handling: see method for details - const requestDataBody = this.performStartPhoneCallRequestBodyTransformation( - data.startVerificationWithPhoneCallRequestBody); - - const body: RequestBody = requestDataBody - ? JSON.stringify(requestDataBody) - : '{}'; - - const path = '/verification/v1/verifications'; - const basePathUrl = this.client.apiClientOptions.hostname + path; - - const requestOptions - = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); - - return this.client.processCall({ - url, - requestOptions, - apiName: this.apiName, - operationId: 'StartVerificationWithPhoneCall', - }); - } - - performStartPhoneCallRequestBodyTransformation(body: StartVerificationWithPhoneCall): StartVerificationWithPhoneCall { - const requestDataBody = { ...body }; - if (requestDataBody.phoneCallOptions !== undefined) { - (requestDataBody as any).calloutOptions = { ...requestDataBody.phoneCallOptions }; - delete requestDataBody.phoneCallOptions; - } - return requestDataBody; - } - - /** - * Start data verification - * This method is used by the mobile and web Verification SDKs to start a verification. It can also be used to request a verification from your backend, by making a request. - * @param { StartDataVerificationRequestData } data - The data to provide to the API call. - */ - public async startData(data: StartDataVerificationRequestData): Promise { - this.client = this.getSinchClient(); - (data.startDataVerificationRequestBody as any).method = 'seamless'; - const getParams = this.client.extractQueryParams(data, [] as never[]); - const headers: { [key: string]: string | undefined } = { - 'Content-Type': 'application/json', - 'Accept': 'application/json', - }; - - const body: RequestBody = data['startDataVerificationRequestBody'] - ? JSON.stringify(data['startDataVerificationRequestBody']) - : '{}'; - const path = '/verification/v1/verifications'; - const basePathUrl = this.client.apiClientOptions.hostname + path; - - const requestOptions - = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined, path); - const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); - - return this.client.processCall({ - url, - requestOptions, - apiName: this.apiName, - operationId: 'StartDataVerification', - }); - } - -} diff --git a/packages/verification/src/rest/v1/verification-service.ts b/packages/verification/src/rest/v1/verification-service.ts index bb06aae7..ba1f98d3 100644 --- a/packages/verification/src/rest/v1/verification-service.ts +++ b/packages/verification/src/rest/v1/verification-service.ts @@ -1,7 +1,6 @@ import { SinchClientParameters } from '@sinch/sdk-client'; import { VerificationStatusApi } from './verification-status'; import { VerificationsApi } from './verifications'; -import { StartVerificationsApi } from './start-verifications'; /** * The Verification Service exposes the following APIs: @@ -10,8 +9,6 @@ import { StartVerificationsApi } from './start-verifications'; */ export class VerificationService { public readonly verificationStatus: VerificationStatusApi; - public readonly startVerifications: StartVerificationsApi; - /** @deprecated */ public readonly verifications: VerificationsApi; /** @@ -25,7 +22,6 @@ export class VerificationService { */ constructor(params: SinchClientParameters) { this.verificationStatus = new VerificationStatusApi(params); - this.startVerifications = new StartVerificationsApi(params); this.verifications = new VerificationsApi(params); } @@ -35,7 +31,6 @@ export class VerificationService { */ public setHostname(hostname: string) { this.verificationStatus.setHostname(hostname); - this.startVerifications.setHostname(hostname); this.verifications.setHostname(hostname); } } diff --git a/packages/verification/src/rest/v1/verifications/verifications-api.jest.fixture.ts b/packages/verification/src/rest/v1/verifications/verifications-api.jest.fixture.ts index 0343520e..ee77028b 100644 --- a/packages/verification/src/rest/v1/verifications/verifications-api.jest.fixture.ts +++ b/packages/verification/src/rest/v1/verifications/verifications-api.jest.fixture.ts @@ -17,6 +17,10 @@ import { ReportSmsVerificationByIdentityRequestData, ReportFlashCallVerificationByIdentityRequestData, ReportCalloutVerificationByIdentityRequestData, + StartDataVerificationRequestData, + StartPhoneCallVerificationResponse, + StartPhoneCallVerificationRequestData, + StartDataVerificationResponse, } from '../../../models'; export class VerificationsApiFixture implements Partial> { @@ -72,6 +76,18 @@ export class VerificationsApiFixture implements Partial, [StartFlashCallVerificationRequestData]> = jest.fn(); + /** + * Fixture associated to function startPhoneCall + */ + public startPhoneCall: jest.Mock, + [StartPhoneCallVerificationRequestData]> = jest.fn(); + /** + * Fixture associated to function startData + */ + public startData: jest.Mock< + Promise, + [StartDataVerificationRequestData]> = jest.fn(); /** * Fixture associated to function startCallout */ diff --git a/packages/verification/src/rest/v1/verifications/verifications-api.ts b/packages/verification/src/rest/v1/verifications/verifications-api.ts index e89e90a6..0b34400d 100644 --- a/packages/verification/src/rest/v1/verifications/verifications-api.ts +++ b/packages/verification/src/rest/v1/verifications/verifications-api.ts @@ -16,18 +16,21 @@ import { StartFlashCallVerificationRequestData, StartCalloutVerificationRequestData, StartSeamlessVerificationRequestData, + StartVerificationWithSms, + StartPhoneCallVerificationRequestData, + StartPhoneCallVerificationResponse, + StartVerificationWithPhoneCall, + StartDataVerificationRequestData, + StartDataVerificationResponse, } from '../../../models'; import { RequestBody, SinchClientParameters, } from '@sinch/sdk-client'; import { VerificationDomainApi } from '../verification-domain-api'; -import { StartVerificationsApi } from '../start-verifications/start-verifications-api'; export class VerificationsApi extends VerificationDomainApi { - startVerificationsApi: StartVerificationsApi; - /** * Initialize your interface * @@ -35,7 +38,6 @@ export class VerificationsApi extends VerificationDomainApi { */ constructor(sinchClientParameters: SinchClientParameters) { super(sinchClientParameters, 'VerificationsApi'); - this.startVerificationsApi = new StartVerificationsApi(sinchClientParameters); } /** @@ -248,10 +250,62 @@ export class VerificationsApi extends VerificationDomainApi { * Start verification with SMS * This method is used by the mobile and web Verification SDKs to start a verification. It can also be used to request a verification from your backend, by making a request. * @param { StartSmsVerificationRequestData } data - The data to provide to the API call. - * @deprecated */ public async startSms(data: StartSmsVerificationRequestData): Promise { - return this.startVerificationsApi.startSms(data); + this.client = this.getSinchClient(); + (data.startVerificationWithSmsRequestBody as any).method = 'sms'; + const getParams = this.client.extractQueryParams(data, [] as never[]); + const headers: { [key: string]: string | undefined } = { + 'Content-Type': 'application/json', + 'Accept': 'application/json', + }; + if (data.startVerificationWithSmsRequestBody.smsOptions?.locale !== undefined) { + headers['Accept-Language'] = data.startVerificationWithSmsRequestBody.smsOptions.locale; + } + + // Special fields handling: see method for details + const requestDataBody = this.performStartSmsRequestBodyTransformation(data.startVerificationWithSmsRequestBody); + + const body: RequestBody = requestDataBody + ? JSON.stringify(requestDataBody) + : '{}'; + const path = '/verification/v1/verifications'; + const basePathUrl = this.client.apiClientOptions.hostname + path; + + const requestOptions + = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined, path); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); + + return this.client.processCall({ + url, + requestOptions, + apiName: this.apiName, + operationId: 'StartVerificationWithSms', + }); + } + + performStartSmsRequestBodyTransformation(body: StartVerificationWithSms): StartVerificationWithSms { + const requestDataBody = { ...body }; + if (requestDataBody.smsOptions?.expiry !== undefined) { + const expiry = requestDataBody.smsOptions?.expiry; + if (expiry instanceof Date) { + requestDataBody.smsOptions.expiry = this.formatTime(expiry); + } + } + // Remove the `locale` property from the body as it is used as a header parameter for the API call + delete requestDataBody.smsOptions?.locale; + + return requestDataBody; + } + + formatTime(date: Date): string { + // Assume the hours needs to be set at the UTC time + Pad single-digit components with leading zeros + const formattedHours = String(date.getUTCHours()).padStart(2, '0'); + const formattedMinutes = String(date.getUTCMinutes()).padStart(2, '0'); + const formattedSeconds = String(date.getUTCSeconds()).padStart(2, '0'); + + // Concatenate the components with colons to form the formatted time string + return `${formattedHours}:${formattedMinutes}:${formattedSeconds}`; } @@ -259,12 +313,114 @@ export class VerificationsApi extends VerificationDomainApi { * Start verification with a FlashCall * This method is used by the mobile and web Verification SDKs to start a verification. It can also be used to request a verification from your backend, by making a request. * @param { StartFlashCallVerificationRequestData } data - The data to provide to the API call. - * @deprecated */ public async startFlashCall( data: StartFlashCallVerificationRequestData, ): Promise { - return this.startVerificationsApi.startFlashCall(data); + this.client = this.getSinchClient(); + (data.startVerificationWithFlashCallRequestBody as any).method = 'flashcall'; + const getParams = this.client.extractQueryParams(data, [] as never[]); + const headers: { [key: string]: string | undefined } = { + 'Content-Type': 'application/json', + 'Accept': 'application/json', + }; + + const body: RequestBody = data['startVerificationWithFlashCallRequestBody'] + ? JSON.stringify(data['startVerificationWithFlashCallRequestBody']) + : '{}'; + const path = '/verification/v1/verifications'; + const basePathUrl = this.client.apiClientOptions.hostname + path; + + const requestOptions + = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined, path); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); + + return this.client.processCall({ + url, + requestOptions, + apiName: this.apiName, + operationId: 'StartVerificationWithFlashCall', + }); + } + + /** + * Start verification with a phone call + * This method is used by the mobile and web Verification SDKs to start a verification. It can also be used to request a verification from your backend, by making a request. + * @param { StartPhoneCallVerificationRequestData } data - The data to provide to the API call. + */ + public async startPhoneCall( + data: StartPhoneCallVerificationRequestData, + ): Promise { + this.client = this.getSinchClient(); + (data.startVerificationWithPhoneCallRequestBody as any).method = 'callout'; + const getParams = this.client.extractQueryParams(data, [] as never[]); + const headers: { [key: string]: string | undefined } = { + 'Content-Type': 'application/json', + 'Accept': 'application/json', + }; + + // Special fields handling: see method for details + const requestDataBody = this.performStartPhoneCallRequestBodyTransformation( + data.startVerificationWithPhoneCallRequestBody); + + const body: RequestBody = requestDataBody + ? JSON.stringify(requestDataBody) + : '{}'; + + const path = '/verification/v1/verifications'; + const basePathUrl = this.client.apiClientOptions.hostname + path; + + const requestOptions + = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined, path); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); + + return this.client.processCall({ + url, + requestOptions, + apiName: this.apiName, + operationId: 'StartVerificationWithPhoneCall', + }); + } + + performStartPhoneCallRequestBodyTransformation(body: StartVerificationWithPhoneCall): StartVerificationWithPhoneCall { + const requestDataBody = { ...body }; + if (requestDataBody.phoneCallOptions !== undefined) { + (requestDataBody as any).calloutOptions = { ...requestDataBody.phoneCallOptions }; + delete requestDataBody.phoneCallOptions; + } + return requestDataBody; + } + + /** + * Start data verification + * This method is used by the mobile and web Verification SDKs to start a verification. It can also be used to request a verification from your backend, by making a request. + * @param { StartDataVerificationRequestData } data - The data to provide to the API call. + */ + public async startData(data: StartDataVerificationRequestData): Promise { + this.client = this.getSinchClient(); + (data.startDataVerificationRequestBody as any).method = 'seamless'; + const getParams = this.client.extractQueryParams(data, [] as never[]); + const headers: { [key: string]: string | undefined } = { + 'Content-Type': 'application/json', + 'Accept': 'application/json', + }; + + const body: RequestBody = data['startDataVerificationRequestBody'] + ? JSON.stringify(data['startDataVerificationRequestBody']) + : '{}'; + const path = '/verification/v1/verifications'; + const basePathUrl = this.client.apiClientOptions.hostname + path; + + const requestOptions + = await this.client.prepareOptions(basePathUrl, 'POST', getParams, headers, body || undefined, path); + const url = this.client.prepareUrl(requestOptions.hostname, requestOptions.queryParams); + + return this.client.processCall({ + url, + requestOptions, + apiName: this.apiName, + operationId: 'StartDataVerification', + }); } /** diff --git a/packages/verification/tests/rest/v1/start/start-verifications-api.test.ts b/packages/verification/tests/rest/v1/start/start-verifications-api.test.ts deleted file mode 100644 index 307d88fa..00000000 --- a/packages/verification/tests/rest/v1/start/start-verifications-api.test.ts +++ /dev/null @@ -1,189 +0,0 @@ -import { - ApiClientOptions, - SigningRequest, - Verification, - StartVerificationsApi, - StartVerificationsApiFixture, -} from '../../../../src'; - -const _links: Verification.LinksObject[] = [ - { - rel: 'status', - href: 'https://dc-euc1-std.verification.api.sinch.com/verification/v1/verifications/id/some_verification_id', - method: 'GET', - }, - { - rel: 'report', - href: 'https://dc-euc1-std.verification.api.sinch.com/verification/v1/verifications/id/some_verification_id', - method: 'PUT', - }, -]; - -describe('StartVerificationsApi', () => { - let startVerificationsApi: StartVerificationsApi; - let fixture: StartVerificationsApiFixture; - let apiClientOptions: ApiClientOptions; - - beforeEach(() => { - fixture = new StartVerificationsApiFixture(); - apiClientOptions = { - projectId: 'Test_ProjectId', - requestPlugins: [new SigningRequest('keyId', 'keySecret')], - }; - startVerificationsApi = new StartVerificationsApi(apiClientOptions); - }); - - describe ('startVerification', () => { - it('should make a POST request to start a verification with an SMS', async () => { - // Given - const smsOptions: Verification.SmsOptions = { - locale: 'sv-SE', - }; - const requestData = Verification.startVerificationHelper.buildSmsRequest('+46700000000', undefined, smsOptions); - const expectedResponse: Verification.StartSmsVerificationResponse = { - id: 'some_verification_id', - method: 'sms', - sms: { - template: 'Din verifieringskod är {{CODE}}.', - interceptionTimeout: 298, - }, - _links, - }; - - // When - fixture.startSms.mockResolvedValue(expectedResponse); - startVerificationsApi.startSms = fixture.startSms; - const response = await startVerificationsApi.startSms(requestData); - - // Then - expect(response).toEqual(expectedResponse); - expect(fixture.startSms).toHaveBeenCalledWith(requestData); - }); - - it('should format the expiry field', () => { - const requestData = Verification.startVerificationHelper.buildSmsRequest( - '+46700000000', - undefined, - { expiry: new Date('2024-02-10T13:22:34.685Z') }); - const expectedResult: Verification.StartVerificationWithSms = { - identity: { - endpoint: '+46700000000', - type: 'number', - }, - smsOptions: { - expiry: '13:22:34', - }, - }; - const formattedRequestData = startVerificationsApi.performStartSmsRequestBodyTransformation( - requestData.startVerificationWithSmsRequestBody); - expect(formattedRequestData).toEqual(expectedResult); - }); - - it('should leave the expiry field unchanged', () => { - const requestData = Verification.startVerificationHelper.buildSmsRequest( - '+46700000000', - undefined, - { expiry: '15:15:15' }); - const expectedResult: Verification.StartVerificationWithSms = { - identity: { - endpoint: '+46700000000', - type: 'number', - }, - smsOptions: { - expiry: '15:15:15', - }, - }; - const formattedRequestData= startVerificationsApi.performStartSmsRequestBodyTransformation( - requestData.startVerificationWithSmsRequestBody); - expect(formattedRequestData).toEqual(expectedResult); - }); - - it('should make a POST request to start a verification with a FlashCall', async () => { - // Given - const requestData = Verification.startVerificationHelper.buildFlashCallRequest('+46700000000', undefined, 30); - const expectedResponse: Verification.StartFlashCallVerificationResponse = { - id: 'some_verification_id', - method: 'flashcall', - flashCall: { - cliFilter: '(.*)70123(.*)', - interceptionTimeout: 60, - reportTimeout: 120, - denyCallAfter: 120, - }, - _links, - }; - - // When - fixture.startFlashCall.mockResolvedValue(expectedResponse); - startVerificationsApi.startFlashCall = fixture.startFlashCall; - const response = await startVerificationsApi.startFlashCall(requestData); - - // Then - expect(response).toEqual(expectedResponse); - expect(fixture.startFlashCall).toHaveBeenCalledWith(requestData); - }); - - it('should make a POST request to start a verification with a PhoneCall', async () => { - // Given - const requestData = Verification.startVerificationHelper.buildPhoneCallRequest('+46700000000'); - const expectedResponse: Verification.StartPhoneCallVerificationResponse = { - id: 'some_verification_id', - method: 'callout', - _links, - }; - - // When - fixture.startPhoneCall.mockResolvedValue(expectedResponse); - startVerificationsApi.startPhoneCall = fixture.startPhoneCall; - const response = await startVerificationsApi.startPhoneCall(requestData); - - // Then - expect(response).toEqual(expectedResponse); - expect(fixture.startPhoneCall).toHaveBeenCalledWith(requestData); - }); - - it('should format the startPhoneCall request body', () => { - const requestData = Verification.startVerificationHelper.buildPhoneCallRequest( - '+46700000000', - undefined, - 'en-US'); - const expectedResult: Verification.StartVerificationWithPhoneCallServerModel = { - identity: { - endpoint: '+46700000000', - type: 'number', - }, - calloutOptions: { - speech: { - locale: 'en-US', - }, - }, - }; - const formattedRequestData = startVerificationsApi.performStartPhoneCallRequestBodyTransformation( - requestData.startVerificationWithPhoneCallRequestBody); - expect(formattedRequestData).toEqual(expectedResult); - }); - - it('should make a POST request to start a data verification (seamless)', async () => { - // Given - const requestData = Verification.startVerificationHelper.buildDataRequest('+46700000000'); - const expectedResponse: Verification.StartDataVerificationResponse = { - id: 'some_verification_id', - method: 'seamless', - seamless: { - targetUri: 'https://target-uri.com', - }, - }; - - // When - fixture.startData.mockResolvedValue(expectedResponse); - startVerificationsApi.startData = fixture.startData; - const response = await startVerificationsApi.startData(requestData); - - // Then - expect(response).toEqual(expectedResponse); - expect(fixture.startData).toHaveBeenCalledWith(requestData); - }); - - }); - -}); diff --git a/packages/verification/tests/rest/v1/start/start.steps.ts b/packages/verification/tests/rest/v1/start/start.steps.ts index 523bb14d..f811b864 100644 --- a/packages/verification/tests/rest/v1/start/start.steps.ts +++ b/packages/verification/tests/rest/v1/start/start.steps.ts @@ -1,9 +1,9 @@ -import { StartVerificationsApi, VerificationService, Verification } from '../../../../src'; +import { VerificationsApi, VerificationService, Verification } from '../../../../src'; import { Given, When, Then } from '@cucumber/cucumber'; import * as assert from 'assert'; import { RequestFailedError } from '@sinch/sdk-client/src'; -let startVerificationApi: StartVerificationsApi; +let startVerificationApi: VerificationsApi; let startSmsVerificationResponse: Verification.StartSmsVerificationResponse; let startPhoneCallVerificationResponse: Verification.StartPhoneCallVerificationResponse; let startFlashCallVerificationResponse: Verification.StartFlashCallVerificationResponse; @@ -15,7 +15,7 @@ Given('the Verification service "Start" is available', () => { applicationSecret: 'appSecret', verificationHostname: 'http://localhost:3018', }); - startVerificationApi = verificationService.startVerifications; + startVerificationApi = verificationService.verifications; }); When('I send a request to start a verification with a SMS', async () => { diff --git a/packages/verification/tests/rest/v1/verifications/verifications-api.test.ts b/packages/verification/tests/rest/v1/verifications/verifications-api.test.ts index b9c97f5d..5c2d42dc 100644 --- a/packages/verification/tests/rest/v1/verifications/verifications-api.test.ts +++ b/packages/verification/tests/rest/v1/verifications/verifications-api.test.ts @@ -84,6 +84,67 @@ describe('VerificationsApi', () => { expect(fixture.startFlashCall).toHaveBeenCalledWith(requestData); }); + it('should make a POST request to start a verification with a PhoneCall', async () => { + // Given + const requestData = Verification.startVerificationHelper.buildPhoneCallRequest('+46700000000'); + const expectedResponse: Verification.StartPhoneCallVerificationResponse = { + id: 'some_verification_id', + method: 'callout', + _links, + }; + + // When + fixture.startPhoneCall.mockResolvedValue(expectedResponse); + verificationsApi.startPhoneCall = fixture.startPhoneCall; + const response = await verificationsApi.startPhoneCall(requestData); + + // Then + expect(response).toEqual(expectedResponse); + expect(fixture.startPhoneCall).toHaveBeenCalledWith(requestData); + }); + + it('should format the startPhoneCall request body', () => { + const requestData = Verification.startVerificationHelper.buildPhoneCallRequest( + '+46700000000', + undefined, + 'en-US'); + const expectedResult: Verification.StartVerificationWithPhoneCallServerModel = { + identity: { + endpoint: '+46700000000', + type: 'number', + }, + calloutOptions: { + speech: { + locale: 'en-US', + }, + }, + }; + const formattedRequestData = verificationsApi.performStartPhoneCallRequestBodyTransformation( + requestData.startVerificationWithPhoneCallRequestBody); + expect(formattedRequestData).toEqual(expectedResult); + }); + + it('should make a POST request to start a data verification (seamless)', async () => { + // Given + const requestData = Verification.startVerificationHelper.buildDataRequest('+46700000000'); + const expectedResponse: Verification.StartDataVerificationResponse = { + id: 'some_verification_id', + method: 'seamless', + seamless: { + targetUri: 'https://target-uri.com', + }, + }; + + // When + fixture.startData.mockResolvedValue(expectedResponse); + verificationsApi.startData = fixture.startData; + const response = await verificationsApi.startData(requestData); + + // Then + expect(response).toEqual(expectedResponse); + expect(fixture.startData).toHaveBeenCalledWith(requestData); + }); + it('should make a POST request to start a verification with a Callout', async () => { // Given const requestData = Verification.startVerificationHelper.buildCalloutRequest('+46700000000'); @@ -103,7 +164,7 @@ describe('VerificationsApi', () => { expect(fixture.startCallout).toHaveBeenCalledWith(requestData); }); - it('should make a POST request to start a data verification (seamless)', async () => { + it('should make a POST request to start a seamless verification', async () => { // Given const requestData = Verification.startVerificationHelper.buildSeamlessRequest('+46700000000'); const expectedResponse: Verification.StartSeamlessVerificationResponse = {