Skip to content

Commit ad755d7

Browse files
committed
feat: Add option to allow http in serviceUrl, default false
1 parent b386c72 commit ad755d7

4 files changed

Lines changed: 52 additions & 18 deletions

File tree

spec/src/utils/helpers.js

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -584,30 +584,58 @@ describe('ConstructorIO - Utils - Helpers', () => {
584584
});
585585

586586
describe('addHTTPSToString', () => {
587-
it('Should return the url without any modification', () => {
588-
const testUrl = 'https://www.constructor.io';
587+
describe('when allowHttp is not given', () => {
588+
it('Should return the url without any modification', () => {
589+
const testUrl = 'https://www.constructor.io';
589590

590-
expect(addHTTPSToString(testUrl)).to.equal(testUrl);
591-
});
591+
expect(addHTTPSToString(testUrl)).to.equal(testUrl);
592+
});
592593

593-
it('Should return url with no protocol with https at the beginning', () => {
594-
const testUrl = 'www.constructor.io';
595-
const expectedUrl = 'https://www.constructor.io';
594+
it('Should return url with no protocol with https at the beginning', () => {
595+
const testUrl = 'www.constructor.io';
596+
const expectedUrl = 'https://www.constructor.io';
596597

597-
expect(addHTTPSToString(testUrl)).to.equal(expectedUrl);
598-
});
598+
expect(addHTTPSToString(testUrl)).to.equal(expectedUrl);
599+
});
599600

600-
it('Should return url with an http protocol with https at the beginning', () => {
601-
const testUrl = 'http://www.constructor.io';
602-
const expectedUrl = 'https://www.constructor.io';
601+
it('Should return url with an http protocol with https at the beginning', () => {
602+
const testUrl = 'http://www.constructor.io';
603+
const expectedUrl = 'https://www.constructor.io';
603604

604-
expect(addHTTPSToString(testUrl)).to.equal(expectedUrl);
605+
expect(addHTTPSToString(testUrl)).to.equal(expectedUrl);
606+
});
607+
608+
it('Should return null if param is not a string', () => {
609+
const testUrl = {};
610+
611+
expect(addHTTPSToString(testUrl)).to.equal(null);
612+
});
605613
});
614+
describe('when allowHttp is not given', () => {
615+
it('Should return the url without any modification', () => {
616+
const testUrl = 'https://www.constructor.io';
606617

607-
it('Should return null if param is not a string', () => {
608-
const testUrl = {};
618+
expect(addHTTPSToString(testUrl, true)).to.equal(testUrl);
619+
});
609620

610-
expect(addHTTPSToString(testUrl)).to.equal(null);
621+
it('Should return url with no protocol with https at the beginning', () => {
622+
const testUrl = 'www.constructor.io';
623+
const expectedUrl = 'https://www.constructor.io';
624+
625+
expect(addHTTPSToString(testUrl, true)).to.equal(expectedUrl);
626+
});
627+
628+
it('Should return url with an http protocol without any modification', () => {
629+
const testUrl = 'http://localhost:8080';
630+
631+
expect(addHTTPSToString(testUrl, true)).to.equal(testUrl);
632+
});
633+
634+
it('Should return null if param is not a string', () => {
635+
const testUrl = {};
636+
637+
expect(addHTTPSToString(testUrl, true)).to.equal(null);
638+
});
611639
});
612640
});
613641

src/constructorio.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class ConstructorIO {
3838
* @param {object} parameters - Parameters for client instantiation
3939
* @param {string} parameters.apiKey - Constructor.io API key
4040
* @param {string} [parameters.serviceUrl='https://ac.cnstrc.com'] - API URL endpoint
41+
* @param {string} [parameters.allowHttpServiceUrl=false] - Allow HTTP protocol in API URL endpoint
4142
* @param {string} [parameters.quizzesServiceUrl='https://quizzes.cnstrc.com'] - Quizzes API URL endpoint
4243
* @param {string} [parameters.agentServiceUrl='https://agent.cnstrc.com'] - AI Shopping Agent API URL endpoint
4344
* @param {string} [parameters.mediaServiceUrl='https://media-cnstrc.com'] - Media API URL endpoint
@@ -73,6 +74,7 @@ class ConstructorIO {
7374
apiKey,
7475
version: versionFromOptions,
7576
serviceUrl,
77+
allowHttpServiceUrl,
7678
quizzesServiceUrl,
7779
agentServiceUrl,
7880
assistantServiceUrl,
@@ -121,7 +123,7 @@ class ConstructorIO {
121123
this.options = {
122124
apiKey,
123125
version: versionFromOptions || versionFromGlobal || computePackageVersion(),
124-
serviceUrl: helpers.addHTTPSToString(normalizedServiceUrl) || 'https://ac.cnstrc.com',
126+
serviceUrl: helpers.addHTTPSToString(normalizedServiceUrl, allowHttpServiceUrl) || 'https://ac.cnstrc.com',
125127
quizzesServiceUrl: (quizzesServiceUrl && quizzesServiceUrl.replace(/\/$/, '')) || 'https://quizzes.cnstrc.com',
126128
agentServiceUrl: (agentServiceUrl && agentServiceUrl.replace(/\/$/, '')) || 'https://agent.cnstrc.com',
127129
assistantServiceUrl: (assistantServiceUrl && assistantServiceUrl.replace(/\/$/, '')) || 'https://assistant.cnstrc.com',

src/types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export interface ConstructorClientOptions {
5252
apiKey: string;
5353
version?: string;
5454
serviceUrl?: string;
55+
allowHttpServiceUrl?: boolean;
5556
quizzesServiceUrl?: string;
5657
agentServiceUrl?: string;
5758
mediaServiceUrl?: string;

src/utils/helpers.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ const utils = {
350350

351351
return utils.throwHttpErrorFromResponse(new Error(), response);
352352
},
353-
addHTTPSToString(url) {
353+
addHTTPSToString(url, allowHttp = false) {
354354
if (typeof url !== 'string') {
355355
return null;
356356
}
@@ -359,6 +359,9 @@ const utils = {
359359
const doesUrlStartWithHTTP = url.startsWith('http://');
360360

361361
if (!doesUrlIncludeHTTPS && doesUrlStartWithHTTP) {
362+
if (allowHttp) {
363+
return url;
364+
}
362365
return url.replace('http', 'https');
363366
}
364367

0 commit comments

Comments
 (0)