From 7efb8f9f16591962392c97d45da86957d2406742 Mon Sep 17 00:00:00 2001 From: Antoine Sein Date: Tue, 21 May 2024 15:32:35 +0200 Subject: [PATCH] DEVEXP-435: Support 'Accept-Language' header for SMS Verification --- .../src/verification/verifications/sms/start-sms.ts | 3 +++ .../start-verification-request.ts | 2 ++ .../src/rest/v1/verifications/verifications-api.ts | 5 +++++ .../tests/rest/v1/verifications/verifications-api.test.ts | 7 +++++-- 4 files changed, 15 insertions(+), 2 deletions(-) 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 846413a7..75abfea2 100644 --- a/examples/simple-examples/src/verification/verifications/sms/start-sms.ts +++ b/examples/simple-examples/src/verification/verifications/sms/start-sms.ts @@ -16,6 +16,9 @@ import { const requestData = Verification.startVerificationHelper.buildSmsRequest( verificationIdentity, `test-reference-for-sms-verification_${verificationIdentity}`, + { + locale: 'sv-SE', + }, ); const verificationService = initVerificationService(); diff --git a/packages/verification/src/models/v1/start-verification-request/start-verification-request.ts b/packages/verification/src/models/v1/start-verification-request/start-verification-request.ts index 87a7b652..cc32b2d3 100644 --- a/packages/verification/src/models/v1/start-verification-request/start-verification-request.ts +++ b/packages/verification/src/models/v1/start-verification-request/start-verification-request.ts @@ -36,6 +36,8 @@ export interface SmsOptions { codeType?: CodeType; /** The SMS template must include a placeholder `{{CODE}}` where the verification code will be inserted, and it can otherwise be customized as desired. */ template?: string; + /** A `language-region` identifier according to [IANA](https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry). Only a subset of those identifiers is accepted. */ + locale?: string; } export type CodeType = 'Numeric' | 'Alpha' | 'Alphanumeric'; diff --git a/packages/verification/src/rest/v1/verifications/verifications-api.ts b/packages/verification/src/rest/v1/verifications/verifications-api.ts index 36502254..1a606086 100644 --- a/packages/verification/src/rest/v1/verifications/verifications-api.ts +++ b/packages/verification/src/rest/v1/verifications/verifications-api.ts @@ -254,6 +254,11 @@ export class VerificationsApi extends VerificationDomainApi { 'Content-Type': 'application/json; charset=UTF-8', 'Accept': 'application/json', }; + if (data.startVerificationWithSmsRequestBody.smsOptions?.locale !== undefined) { + headers['Accept-Language'] = data.startVerificationWithSmsRequestBody.smsOptions?.locale; + // Remove the `locale` property from the body as it is used as a header parameter for the API call + delete data.startVerificationWithSmsRequestBody.smsOptions?.locale; + } // Special fields handling: see method for details const requestDataBody = this.performStartSmsRequestBodyTransformation(data.startVerificationWithSmsRequestBody); 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 e084c97b..947c58ff 100644 --- a/packages/verification/tests/rest/v1/verifications/verifications-api.test.ts +++ b/packages/verification/tests/rest/v1/verifications/verifications-api.test.ts @@ -35,12 +35,15 @@ describe('VerificationsApi', () => { describe ('startVerification', () => { it('should make a POST request to start a verification with an SMS', async () => { // Given - const requestData = Verification.startVerificationHelper.buildSmsRequest('+46700000000'); + 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: 'Your verification code is {{CODE}}. Verified by Sinch', + template: 'Din verifieringskod är {{CODE}}.', interceptionTimeout: 298, }, _links,