Skip to content

Commit

Permalink
added expandAllContentLinks and test
Browse files Browse the repository at this point in the history
  • Loading branch information
AgilityCMS committed Sep 1, 2020
1 parent 6efbeb2 commit 059fd94
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@agility/content-fetch",
"version": "0.8.2",
"version": "0.9.0",
"description": "JavaScript library for the Agility Fetch API (node and browser)",
"main": "dist/agility-content-fetch.node.js",
"scripts": {
Expand Down
10 changes: 7 additions & 3 deletions src/methods/getContentList.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {buildPathUrl, buildRequestUrlPath, buildAuthHeader } from '../utils'
* @param {Object} requestParams - The parameters for the API request.
* @param {string} requestParams.referenceName - The unique reference name of the content list you wish to retrieve in the specified language.
* @param {string} requestParams.languageCode - The language code of the content you want to retrieve.
* @param {number} [requestParams.contentLinkDepth] - The depth, representing the levels in which you want linked content auto-resolved. Default is 1.
* @param {number} [requestParams.contentLinkDepth] - The depth, representing the levels in which you want linked content auto-resolved. Default is *1*.
* @param {boolean} [requestParams.expandAllContentLinks] - Whether or not to expand entire linked content references, includings lists and items that are rendered in the CMS as Grid or Link. Default is *false*
* @param {number} [requestParams.take] - The maximum number of items to retrieve in this request. Default is **10**. Maximum allowed is **50**.
* @param {number} [requestParams.skip] - The number of items to skip from the list. Default is **0**. Used for implementing pagination.
* @param {string} [requestParams.sort] - The field to sort the results by. Example *fields.title* or *properties.modified*.
Expand Down Expand Up @@ -64,7 +65,7 @@ function getContentList(requestParams) {
requestParams = {...defaultParams, ...requestParams};

const req = {
url: buildPathUrl("list", requestParams.referenceName, requestParams.skip, requestParams.take, requestParams.sort, requestParams.direction, requestParams.filters, requestParams.filtersLogicOperator, requestParams.contentLinkDepth),
url: buildPathUrl("list", requestParams.referenceName, requestParams.skip, requestParams.take, requestParams.sort, requestParams.direction, requestParams.filters, requestParams.filtersLogicOperator, requestParams.contentLinkDepth, requestParams.expandAllContentLinks),
method: 'get',
baseURL: buildRequestUrlPath(this.config, requestParams.languageCode),
headers: buildAuthHeader(this.config),
Expand Down Expand Up @@ -117,13 +118,16 @@ function validateRequestParams(requestParams) {
}
} else if (requestParams.filtersLogicOperator && requestParams.filtersLogicOperator.toLowerCase() !== 'and' && requestParams.filtersLogicOperator.toLowerCase() !== 'or') {
throw new TypeError('FiltersLogicOperator parameter must have a value of "AND" or "OR"');
} else if(requestParams.expandAllContentLinks && typeof requestParams.expandAllContentLinks !== 'boolean') {
throw new TypeError('ExpandAllContentLinks parameter must be a value of true or false');
}

return true;
}

const defaultParams = {
contentLinkDepth: 1
contentLinkDepth: 1,
expandAllContentLinks: false
}

export default getContentList;
7 changes: 6 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function buildRequestUrlPath(config, languageCode) {
return urlPath;
}

function buildPathUrl(contentType, referenceName, skip, take, sort, direction, filters, filtersLogicOperator, contentLinkDepth) {
function buildPathUrl(contentType, referenceName, skip, take, sort, direction, filters, filtersLogicOperator, contentLinkDepth, expandAllContentLinks) {
let url = `/${contentType}/${referenceName}?contentLinkDepth=${contentLinkDepth}&`;
filtersLogicOperator = filtersLogicOperator ? ` ${filtersLogicOperator} ` : ' AND ';

Expand All @@ -46,6 +46,11 @@ function buildPathUrl(contentType, referenceName, skip, take, sort, direction, f
}
url += '&';
}

if(expandAllContentLinks) {
url += `expandAllContentLinks=${expandAllContentLinks}&`;
}

return url;
}

Expand Down
26 changes: 20 additions & 6 deletions test/getContentList.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,25 @@ describe('getContentList:', function() {
filters: [{property: 'contentID', operator: api.types.FilterOperators.EQUAL_TO, value: '16'}, {property: 'properties.referenceName', operator: api.types.FilterOperators.LIKE, value: 'posts'}],
filtersLogicOperator: api.types.FilterLogicOperators.AND
})
.then(function(contentList) {
assert.strictEqual(contentList.items[0].contentID, 16);
assert.strictEqual(contentList.items.length, 1);
done();
})
.catch(done);
.then(function(contentList) {
assert.strictEqual(contentList.items[0].contentID, 16);
assert.strictEqual(contentList.items.length, 1);
done();
})
.catch(done);
});

it('should expand all content links when expandContentLinks are set to true', function(done) {
var api = createApiClient();
api.getContentList({
referenceName: 'posts',
languageCode: 'en-us',
expandAllContentLinks: true
})
.then(function(contentList) {
console.log(contentList);
done();
})
.catch(done);
})
});

0 comments on commit 059fd94

Please sign in to comment.