Skip to content

Commit

Permalink
DEVEXP-435: Support 'Accept-Language' header for SMS Verification (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
asein-sinch authored May 21, 2024
1 parent 2360660 commit 404ad49
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import {
const requestData = Verification.startVerificationHelper.buildSmsRequest(
verificationIdentity,
`test-reference-for-sms-verification_${verificationIdentity}`,
{
locale: 'sv-SE',
},
);

const verificationService = initVerificationService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ 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;
}

// Special fields handling: see method for details
const requestDataBody = this.performStartSmsRequestBodyTransformation(data.startVerificationWithSmsRequestBody);
Expand Down Expand Up @@ -284,6 +287,8 @@ export class VerificationsApi extends VerificationDomainApi {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 404ad49

Please sign in to comment.