Skip to content

Commit 08cb347

Browse files
committed
add set api url
1 parent 1b1d245 commit 08cb347

2 files changed

Lines changed: 100 additions & 1 deletion

File tree

sdk/src/QonversionConfigBuilder.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export class QonversionConfigBuilder {
2020
private environment = Environment.Production;
2121
private logLevel = LogLevel.Info;
2222
private logTag = DEFAULT_LOG_TAG;
23+
private apiUrl = API_URL;
2324

2425
/**
2526
* Creates an instance of a builder
@@ -65,6 +66,35 @@ export class QonversionConfigBuilder {
6566
return this;
6667
};
6768

69+
/**
70+
* Define the base API URL that the SDK will use for all network requests.
71+
*
72+
* This is primarily useful when routing the SDK through a trusted HTTPS proxy.
73+
*
74+
* @param apiUrl the desired HTTPS base API URL.
75+
* @return builder instance for chain calls.
76+
*/
77+
setApiUrl(apiUrl: string): QonversionConfigBuilder {
78+
if (!apiUrl) {
79+
throw new QonversionError(QonversionErrorCode.ConfigPreparation, "API URL is empty");
80+
}
81+
82+
let normalizedUrl: URL;
83+
try {
84+
normalizedUrl = new URL(apiUrl);
85+
} catch {
86+
throw new QonversionError(QonversionErrorCode.ConfigPreparation, "API URL is invalid");
87+
}
88+
89+
if (normalizedUrl.protocol !== 'https:') {
90+
throw new QonversionError(QonversionErrorCode.ConfigPreparation, "API URL must use HTTPS");
91+
}
92+
93+
this.apiUrl = normalizedUrl.toString().replace(/\/$/, '');
94+
95+
return this;
96+
};
97+
6898
/**
6999
* Generate {@link QonversionConfig} instance with all the provided configurations.
70100
*
@@ -89,7 +119,7 @@ export class QonversionConfigBuilder {
89119

90120
const networkConfig: NetworkConfig = {
91121
canSendRequests: true,
92-
apiUrl: API_URL,
122+
apiUrl: this.apiUrl,
93123
};
94124

95125
return {

sdk/src/__tests__/QonversionConfigBuilder.test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ test('setting log tag', () => {
5858
expect(builder["logTag"]).toBe(logTag);
5959
});
6060

61+
test('setting api url', () => {
62+
// given
63+
const builder = new QonversionConfigBuilder('test');
64+
const apiUrl = 'https://proxy.example.com';
65+
66+
// when
67+
builder.setApiUrl(apiUrl);
68+
69+
// then
70+
expect(builder["apiUrl"]).toBe(apiUrl);
71+
});
72+
6173
test('successful build with full list of arguments', () => {
6274
// given
6375
const mockLogLevel = LogLevel.Warning;
@@ -132,6 +144,36 @@ test('successful build without full list of arguments', () => {
132144
expect(result).toStrictEqual(expResult);
133145
});
134146

147+
test('successful build with custom api url', () => {
148+
// given
149+
const projectKey = "test key";
150+
const apiUrl = 'https://proxy.example.com';
151+
152+
const builder = new QonversionConfigBuilder(projectKey);
153+
builder.setApiUrl(apiUrl);
154+
155+
// when
156+
const result = builder.build()
157+
158+
// then
159+
expect(result.networkConfig.apiUrl).toBe(apiUrl);
160+
});
161+
162+
test('normalizes custom api url by removing trailing slash', () => {
163+
// given
164+
const projectKey = "test key";
165+
const apiUrl = 'https://proxy.example.com/';
166+
167+
const builder = new QonversionConfigBuilder(projectKey);
168+
builder.setApiUrl(apiUrl);
169+
170+
// when
171+
const result = builder.build()
172+
173+
// then
174+
expect(result.networkConfig.apiUrl).toBe('https://proxy.example.com');
175+
});
176+
135177
test('building with blank project key', () => {
136178
// given
137179
const builder = new QonversionConfigBuilder("");
@@ -140,3 +182,30 @@ test('building with blank project key', () => {
140182
// when and then
141183
expectQonversionError(QonversionErrorCode.ConfigPreparation, testingMethod);
142184
});
185+
186+
test('building with blank api url throws', () => {
187+
// given
188+
const builder = new QonversionConfigBuilder("test");
189+
const testingMethod = () => builder.setApiUrl("");
190+
191+
// when and then
192+
expectQonversionError(QonversionErrorCode.ConfigPreparation, testingMethod);
193+
});
194+
195+
test('building with invalid api url throws', () => {
196+
// given
197+
const builder = new QonversionConfigBuilder("test");
198+
const testingMethod = () => builder.setApiUrl("not a url");
199+
200+
// when and then
201+
expectQonversionError(QonversionErrorCode.ConfigPreparation, testingMethod);
202+
});
203+
204+
test('building with non-https api url throws', () => {
205+
// given
206+
const builder = new QonversionConfigBuilder("test");
207+
const testingMethod = () => builder.setApiUrl("http://proxy.example.com");
208+
209+
// when and then
210+
expectQonversionError(QonversionErrorCode.ConfigPreparation, testingMethod);
211+
});

0 commit comments

Comments
 (0)