Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CSL 3342] Add parameters to getRecommendationsPod #196

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions spec/src/modules/recommendations.js
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,38 @@ describe('ConstructorIO - Recommendations', () => {
});
});

it('Should add the section query param when section parameter is provided', (done) => {
const { recommendations } = new ConstructorIO({
...validOptions,
fetch: fetchSpy,
});
const parameters = { section: 'Products' };

recommendations.getRecommendationPods(null, parameters).then(() => {
const url = helpers.extractUrlFromFetch(fetchSpy);
const queryParam = '&section=Products';

expect(url).to.contain(queryParam);
done();
});
});

it('Should not add the section query param if it is not provided', (done) => {
const { recommendations } = new ConstructorIO({
...validOptions,
fetch: fetchSpy,
});
const parameters = {};

recommendations.getRecommendationPods(null, parameters).then(() => {
const url = helpers.extractUrlFromFetch(fetchSpy);
const queryParam = '&section=Products';

expect(url).not.to.contain(queryParam);
done();
});
});

it('Should be rejected when invalid apiKey is provided', () => {
const { recommendations } = new ConstructorIO({ ...validOptions, apiKey: 'fyzs7tfF8L161VoAXQ8u' });

Expand Down
9 changes: 7 additions & 2 deletions src/modules/recommendations.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,13 @@ class Recommendations {
* @function getRecommendationPods
* @param {object} [networkParameters] - Parameters relevant to the network request
* @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
* @param {object} [parameters] - Additional parameters to refine results
* @param {string} [parameters.section] - The section to return results from
* @returns {Promise}
* @example
* constructorio.recommendations.getRecommendationPods();
*/
getRecommendationPods(networkParameters = {}) {
getRecommendationPods(networkParameters = {}, parameters = {}) {
const {
apiKey,
serviceUrl,
Expand All @@ -214,7 +216,10 @@ class Recommendations {
const controller = new AbortController();
const { signal } = controller;
const headers = {};
const requestUrl = `${serviceUrl}/v1/recommendation_pods?key=${apiKey}`;
const { section } = parameters;
const rawQueryParams = section ? qs.stringify({ section }) : '';
const queryParams = rawQueryParams ? `&${rawQueryParams}` : '';
const requestUrl = `${serviceUrl}/v1/recommendation_pods?key=${apiKey}${queryParams}`.trim();

Object.assign(headers, helpers.combineCustomHeaders(this.options, networkParameters));

Expand Down
Loading