Skip to content

Commit 717b223

Browse files
authored
[NOCSL] Add request_url to sabr request responses (#224)
* add request_url to sabr requests * Add request url to returned promise * remove unused * revert test * Add missing tests
1 parent a3e2936 commit 717b223

File tree

10 files changed

+195
-55
lines changed

10 files changed

+195
-55
lines changed

spec/src/modules/autocomplete.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,16 @@ describe('ConstructorIO - Autocomplete', () => {
400400
});
401401
});
402402

403+
it('Should include requestUrl in the promise for getAutocompleteResults', () => {
404+
const { autocomplete } = new ConstructorIO({
405+
...validOptions,
406+
fetch: fetchSpy,
407+
});
408+
409+
const promise = autocomplete.getAutocompleteResults(query);
410+
expect(promise).to.have.property('requestUrl').that.is.a('string');
411+
});
412+
403413
it('Should return a variations_map object in the response', (done) => {
404414
const variationsMap = {
405415
group_by: [

spec/src/modules/browse.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,16 @@ describe('ConstructorIO - Browse', () => {
705705
});
706706
});
707707

708+
it('Should include requestUrl in the promise for getBrowseResults', () => {
709+
const { browse } = new ConstructorIO({
710+
...validOptions,
711+
fetch: fetchSpy,
712+
});
713+
714+
const promise = browse.getBrowseResults(filterName, filterValue);
715+
expect(promise).to.have.property('requestUrl').that.is.a('string');
716+
});
717+
708718
it('Should be rejected when invalid filterName is provided', () => {
709719
const { browse } = new ConstructorIO(validOptions);
710720

@@ -846,6 +856,7 @@ describe('ConstructorIO - Browse', () => {
846856

847857
browse.getBrowseResultsForItemIds(ids).then((res) => {
848858
const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy);
859+
849860
expect(res).to.have.property('request').to.be.an('object');
850861
expect(res).to.have.property('response').to.be.an('object');
851862
expect(res).to.have.property('result_id').to.be.an('string');
@@ -858,6 +869,17 @@ describe('ConstructorIO - Browse', () => {
858869
});
859870
});
860871

872+
it('Should include requestUrl in the promise for getBrowseResultsForItemIds', () => {
873+
const itemIds = ['item1', 'item2'];
874+
const { browse } = new ConstructorIO({
875+
...validOptions,
876+
fetch: fetchSpy,
877+
});
878+
879+
const promise = browse.getBrowseResultsForItemIds(itemIds);
880+
expect(promise).to.have.property('requestUrl').that.is.a('string');
881+
});
882+
861883
it('should return a response with valid ids, client id and session id', (done) => {
862884
const userParameters = {
863885
clientId: 'example client id',
@@ -1375,6 +1397,16 @@ describe('ConstructorIO - Browse', () => {
13751397
});
13761398
});
13771399

1400+
it('Should include requestUrl in the promise for getBrowseGroups', () => {
1401+
const { browse } = new ConstructorIO({
1402+
...validOptions,
1403+
fetch: fetchSpy,
1404+
});
1405+
1406+
const promise = browse.getBrowseGroups({});
1407+
expect(promise).to.have.property('requestUrl').that.is.a('string');
1408+
});
1409+
13781410
it('Should return a response with valid ids and user id', (done) => {
13791411
const userId = 'user-id';
13801412
const { browse } = new ConstructorIO({
@@ -1731,6 +1763,16 @@ describe('ConstructorIO - Browse', () => {
17311763
});
17321764
});
17331765

1766+
it('Should include requestUrl in the promise for getBrowseFacets', () => {
1767+
const { browse } = new ConstructorIO({
1768+
...validOptions,
1769+
fetch: fetchSpy,
1770+
});
1771+
1772+
const promise = browse.getBrowseFacets({});
1773+
expect(promise).to.have.property('requestUrl').that.is.a('string');
1774+
});
1775+
17341776
it('Should be rejected when invalid apiKey is provided', () => {
17351777
const { browse } = new ConstructorIO({ apiKey: 'fyzs7tfF8L161VoAXQ8u' });
17361778

@@ -1914,6 +1956,16 @@ describe('ConstructorIO - Browse', () => {
19141956
});
19151957
});
19161958

1959+
it('Should include requestUrl in the promise for getBrowseFacetOptions', () => {
1960+
const { browse } = new ConstructorIO({
1961+
...validOptions,
1962+
fetch: fetchSpy,
1963+
});
1964+
1965+
const promise = browse.getBrowseFacetOptions(facetName);
1966+
expect(promise).to.have.property('requestUrl').that.is.a('string');
1967+
});
1968+
19171969
it('Should be rejected when no facetName is provided', () => {
19181970
const { browse } = new ConstructorIO(validOptions);
19191971

spec/src/modules/quizzes.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,16 @@ describe('ConstructorIO - Quizzes', () => {
216216

217217
return expect(quizzes.getQuizNextQuestion(validQuizId, {})).to.eventually.be.rejected;
218218
});
219+
220+
it('Should include requestUrl in the promise for getQuizNextQuestion', () => {
221+
const { quizzes } = new ConstructorIO({
222+
apiKey: quizApiKey,
223+
fetch: fetchSpy,
224+
});
225+
226+
const promise = quizzes.getQuizNextQuestion(validQuizId, {}, { clientId, sessionId });
227+
expect(promise).to.have.property('requestUrl').that.is.a('string');
228+
});
219229
});
220230

221231
describe('getQuizResults', () => {
@@ -470,5 +480,15 @@ describe('ConstructorIO - Quizzes', () => {
470480
return expect(quizzes.getQuizResults(validQuizId, { answers: validAnswers })).to.eventually.be.rejectedWith('The operation was aborted.');
471481
});
472482
}
483+
484+
it('Should include requestUrl in the promise for getQuizResults', () => {
485+
const { quizzes } = new ConstructorIO({
486+
apiKey: quizApiKey,
487+
fetch: fetchSpy,
488+
});
489+
490+
const promise = quizzes.getQuizResults(validQuizId, { answers: validAnswers });
491+
expect(promise).to.have.property('requestUrl').that.is.a('string');
492+
});
473493
});
474494
});

spec/src/modules/recommendations.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,16 @@ describe('ConstructorIO - Recommendations', () => {
656656
)).to.eventually.be.rejectedWith('The operation was aborted.');
657657
});
658658
}
659+
660+
it('Should include requestUrl in the promise for getRecommendations', () => {
661+
const { recommendations } = new ConstructorIO({
662+
...validOptions,
663+
fetch: fetchSpy,
664+
});
665+
666+
const promise = recommendations.getRecommendations(podId, { itemIds: itemId });
667+
expect(promise).to.have.property('requestUrl').that.is.a('string');
668+
});
659669
});
660670

661671
describe('getRecommendationPods', () => {

spec/src/modules/search.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,16 @@ describe('ConstructorIO - Search', () => {
668668
});
669669
});
670670

671+
it('Should include requestUrl in the promise for getSearchResults', () => {
672+
const { search } = new ConstructorIO({
673+
...validOptions,
674+
fetch: fetchSpy,
675+
});
676+
677+
const promise = search.getSearchResults(query, { section });
678+
expect(promise).to.have.property('requestUrl').that.is.a('string');
679+
});
680+
671681
it('Should be rejected when invalid query is provided', () => {
672682
const { search } = new ConstructorIO(validOptions);
673683

src/modules/autocomplete.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class Autocomplete {
205205
// Handle network timeout if specified
206206
helpers.applyNetworkTimeout(this.options, networkParameters, controller);
207207

208-
return fetch(requestUrl, { headers, signal }).then((response) => {
208+
const promise = fetch(requestUrl, { headers, signal }).then((response) => {
209209
if (response.ok) {
210210
return response.json();
211211
}
@@ -234,6 +234,10 @@ class Autocomplete {
234234

235235
throw new Error('getAutocompleteResults response data is malformed');
236236
});
237+
238+
promise.requestUrl = requestUrl;
239+
240+
return promise;
237241
}
238242
}
239243

src/modules/browse.js

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ class Browse {
295295
// Handle network timeout if specified
296296
helpers.applyNetworkTimeout(this.options, networkParameters, controller);
297297

298-
return fetch(requestUrl, { headers, signal }).then((response) => {
298+
const promise = fetch(requestUrl, { headers, signal }).then((response) => {
299299
if (response.ok) {
300300
return response.json();
301301
}
@@ -321,6 +321,10 @@ class Browse {
321321

322322
throw new Error('getBrowseResults response data is malformed');
323323
});
324+
325+
promise.requestUrl = requestUrl;
326+
327+
return promise;
324328
}
325329

326330
/**
@@ -375,27 +379,29 @@ class Browse {
375379
// Handle network timeout if specified
376380
helpers.applyNetworkTimeout(this.options, networkParameters, controller);
377381

378-
return fetch(requestUrl, { headers, signal })
379-
.then((response) => {
380-
if (response.ok) {
381-
return response.json();
382-
}
382+
const promise = fetch(requestUrl, { headers, signal }).then((response) => {
383+
if (response.ok) {
384+
return response.json();
385+
}
383386

384-
return helpers.throwHttpErrorFromResponse(new Error(), response);
385-
})
386-
.then((json) => {
387-
if (json.response && json.response.results) {
388-
if (json.result_id) {
389-
// Append `result_id` to each result item
390-
json.response.results.forEach((result) => {
391-
// eslint-disable-next-line no-param-reassign
392-
result.result_id = json.result_id;
393-
});
394-
}
395-
return json;
387+
return helpers.throwHttpErrorFromResponse(new Error(), response);
388+
}).then((json) => {
389+
if (json.response && json.response.results) {
390+
if (json.result_id) {
391+
// Append `result_id` to each result item
392+
json.response.results.forEach((result) => {
393+
// eslint-disable-next-line no-param-reassign
394+
result.result_id = json.result_id;
395+
});
396396
}
397-
throw new Error('getBrowseResultsForItemIds response data is malformed');
398-
});
397+
return json;
398+
}
399+
throw new Error('getBrowseResultsForItemIds response data is malformed');
400+
});
401+
402+
promise.requestUrl = requestUrl;
403+
404+
return promise;
399405
}
400406

401407
/**
@@ -443,13 +449,17 @@ class Browse {
443449
// Handle network timeout if specified
444450
helpers.applyNetworkTimeout(this.options, networkParameters, controller);
445451

446-
return fetch(requestUrl, { headers, signal }).then((response) => {
452+
const promise = fetch(requestUrl, { headers, signal }).then((response) => {
447453
if (response.ok) {
448454
return response.json();
449455
}
450456

451457
return helpers.throwHttpErrorFromResponse(new Error(), response);
452458
});
459+
460+
promise.requestUrl = requestUrl;
461+
462+
return promise;
453463
}
454464

455465
/**
@@ -498,7 +508,7 @@ class Browse {
498508
// Handle network timeout if specified
499509
helpers.applyNetworkTimeout(this.options, networkParameters, controller);
500510

501-
return fetch(requestUrl, {
511+
const promise = fetch(requestUrl, {
502512
headers: { ...headers, ...helpers.createAuthHeader(this.options) },
503513
signal,
504514
}).then((response) => {
@@ -508,6 +518,10 @@ class Browse {
508518

509519
return helpers.throwHttpErrorFromResponse(new Error(), response);
510520
});
521+
522+
promise.requestUrl = requestUrl;
523+
524+
return promise;
511525
}
512526

513527
/**
@@ -553,7 +567,7 @@ class Browse {
553567
// Handle network timeout if specified
554568
helpers.applyNetworkTimeout(this.options, networkParameters, controller);
555569

556-
return fetch(requestUrl, {
570+
const promise = fetch(requestUrl, {
557571
headers: { ...headers, ...helpers.createAuthHeader(this.options) },
558572
signal,
559573
}).then((response) => {
@@ -563,6 +577,10 @@ class Browse {
563577

564578
return helpers.throwHttpErrorFromResponse(new Error(), response);
565579
});
580+
581+
promise.requestUrl = requestUrl;
582+
583+
return promise;
566584
}
567585
}
568586

0 commit comments

Comments
 (0)