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

Feature expandcontentlists #54

Merged
merged 2 commits into from
Sep 1, 2020
Merged
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
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.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"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": {
"test": "nyc --reporter=html --reporter=text mocha --require @babel/register --recursive ./test/",
"test-r": "nyc --reporter=html --reporter=text mocha --require @babel/register --recursive ./test/getUrlRedirections.tests.js",
"test-cl": "nyc --reporter=html --reporter=text mocha --require @babel/register --recursive ./test/getContentList.tests.js",
"test-ci": "nyc --reporter=html --reporter=text mocha --require @babel/register --recursive ./test/getContentItem.tests.js",
"test-pg": "nyc --reporter=html --reporter=text mocha --require @babel/register --recursive ./test/getPage.tests.js",
"generate-docs": "node_modules/.bin/jsdoc --configure .jsdoc.json --verbose",
"build": "webpack --config webpack.config -p"
},
Expand Down
10 changes: 7 additions & 3 deletions src/methods/getContentItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { buildRequestUrlPath, buildAuthHeader } from '../utils'
* @param {Object} requestParams - The paramters for the API request.
* @param {number} requestParams.contentID - The contentID of the requested item in this 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**
* @returns {Promise<AgilityFetch.Types.ContentItem>} - Returns a content item object.
* @example
*
Expand Down Expand Up @@ -37,7 +38,7 @@ function getContentItem(requestParams) {
requestParams = {...defaultParams, ...requestParams};

const req = {
url: `/item/${requestParams.contentID}?contentLinkDepth=${requestParams.contentLinkDepth}`,
url: `/item/${requestParams.contentID}?contentLinkDepth=${requestParams.contentLinkDepth}&expandAllContentLinks=${requestParams.expandAllContentLinks}`,
method: 'get',
baseURL: buildRequestUrlPath(this.config, requestParams.languageCode),
headers: buildAuthHeader(this.config),
Expand All @@ -55,13 +56,16 @@ function validateRequestParams(requestParams) {
throw new TypeError('You must include a contentID number in your request params.');
} else if(requestParams.contentLinkDepth && (isNaN(requestParams.contentLinkDepth) || requestParams.contentLinkDepth < 0)) {
throw new TypeError('When specifying contentLinkDepth, it must be a number greater than 0.');
} else if(requestParams.expandAllContentLinks && typeof requestParams.expandAllContentLinks !== 'boolean') {
throw new TypeError('ExpandAllContentLinks parameter must be a value of true or false');
} else {
return;
}
}

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

export default getContentItem;
18 changes: 14 additions & 4 deletions src/methods/getContentList.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ 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*.
* @param {string} [requestParams.sort] - The field to sort the results by. Example **fields.title** or **properties.modified**.
* @param {AgilityFetch.Types.SortDirection} [requestParams.direction] - The direction to sort the results by.
* @param {Array.<AgilityFetch.Types.Filter>} [requestParams.filters] - The collection of filters to filter the results by.
* @param {AgilityFetch.Types.FilterLogicOperator} [requestParams.filtersLogicOperator] - The logic operator to combine multiple filters.
Expand Down Expand Up @@ -60,11 +61,13 @@ function getContentList(requestParams) {

validateRequestParams(requestParams);

requestParams.referenceName = sanitizeReferenceName(requestParams.referenceName);

//merge default params with request params
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 All @@ -74,6 +77,10 @@ function getContentList(requestParams) {
return this.makeRequest(req);
}

function sanitizeReferenceName(referenceName) {
return referenceName.toLowerCase();
}

function validateRequestParams(requestParams) {
if(!requestParams.languageCode) {
throw new TypeError('You must include a languageCode in your request params.')
Expand Down Expand Up @@ -117,13 +124,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;
10 changes: 7 additions & 3 deletions src/methods/getPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { buildRequestUrlPath, buildAuthHeader } from '../utils'
* @param {Object} requestParams - The parameters for the API request.
* @param {number} requestParams.pageID - The unique page ID of the page you wish to retrieve in the current 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 2.
* @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.contentLinkDepth] - The depth, representing the levels in which you want linked content auto-resolved. Default is **2**.
* @returns {Promise<AgilityFetch.Types.Page>} - Returns a page item object.
* @example
*
Expand Down Expand Up @@ -36,7 +37,7 @@ function getPage(requestParams) {
requestParams = {...defaultParams, ...requestParams};

const req = {
url: `/page/${requestParams.pageID}?contentLinkDepth=${requestParams.contentLinkDepth}`,
url: `/page/${requestParams.pageID}?contentLinkDepth=${requestParams.contentLinkDepth}&expandAllContentLinks=${requestParams.expandAllContentLinks}`,
method: 'get',
baseURL: buildRequestUrlPath(this.config, requestParams.languageCode),
headers: buildAuthHeader(this.config),
Expand All @@ -51,13 +52,16 @@ function validateRequestParams(requestParams) {
throw new TypeError('You must include a languageCode in your request params.')
} else if(!requestParams.pageID) {
throw new TypeError('You must include a pageID in your request params.');
} else if(requestParams.expandAllContentLinks && typeof requestParams.expandAllContentLinks !== 'boolean') {
throw new TypeError('ExpandAllContentLinks parameter must be a value of true or false');
} else {
return;
}
}

const defaultParams = {
contentLinkDepth: 2
contentLinkDepth: 2,
expandAllContentLinks: true
}


Expand Down
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
58 changes: 58 additions & 0 deletions test/getContentItem.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,63 @@ describe('getContentItem:', function() {
done();
})

it('should throw error if expandAllContentLinks is not true/false', function(done) {
expect(function() {
var api = createApiClient();
api.getContentItem({
contentID: 22,
languageCode: 'en-us',
expandAllContentLinks: 'something'
})
.then(function(contentItem) {
assert.strictEqual(contentItem.contentID, 22);
done();
})
.catch(done);
}).to.throw( TypeError );
done();
})

it('should expand all content links when expandContentLinks are set to true', function(done) {
var api = createApiClient();
api.getContentItem({
contentID: 65, //item within listwithnestedcontentlinks
languageCode: 'en-us',
expandAllContentLinks: true
})
.then(function(contentItem) {
assert.strictEqual(Array.isArray(contentItem.fields.posts), true);
done();
})
.catch(done);
})

it('should NOT expand all content links when expandContentLinks are set to false', function(done) {
var api = createApiClient();
api.getContentItem({
contentID: 65, //item within listwithnestedcontentlinks
languageCode: 'en-us',
expandAllContentLinks: false
})
.then(function(contentItem) {
assert.strictEqual(Array.isArray(contentItem.fields.posts), false);
done();
})
.catch(done);
})

it('should NOT expand all content links when expandContentLinks is not set', function(done) {
var api = createApiClient();
api.getContentItem({
contentID: 65, //item within listwithnestedcontentlinks
languageCode: 'en-us',
})
.then(function(contentItem) {
assert.strictEqual(Array.isArray(contentItem.fields.posts), false);
done();
})
.catch(done);
})

});

53 changes: 47 additions & 6 deletions test/getContentList.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,52 @@ 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: 'listwithnestedcontentlink',
languageCode: 'en-us',
expandAllContentLinks: true
})
.then(function(contentList) {
assert.strictEqual(Array.isArray(contentList.items[0].fields.posts), true);
done();
})
.catch(done);
})

it('should NOT expand all content links when expandContentLinks are set to false', function(done) {
var api = createApiClient();
api.getContentList({
referenceName: 'listwithnestedcontentlink',
languageCode: 'en-us',
expandAllContentLinks: false
})
.then(function(contentList) {
assert.strictEqual(Array.isArray(contentList.items[0].fields.posts), false);
done();
})
.catch(done);
})

it('should NOT expand all content links when expandContentLinks is not set at all', function(done) {
var api = createApiClient();
api.getContentList({
referenceName: 'listwithnestedcontentlink',
languageCode: 'en-us'
})
.then(function(contentList) {
assert.strictEqual(Array.isArray(contentList.items[0].fields.posts), false);
done();
})
.catch(done);
})
});
16 changes: 16 additions & 0 deletions test/getPage.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,21 @@ import { createApiClient, createPreviewApiClient, createCatchedApiClient } from
}).to.throw( TypeError );
done();
})

it('should retrieve a page and expand all content links when expandAllContentLink is set to true', function(done) {
var api = createApiClient();
api.getPage({
pageID: 2,
languageCode: 'en-us',
expandAllContentLinks: true
})
.then(function(page) {
assert.strictEqual(Array.isArray(page.zones.MainContentZone[2].item.fields.posts), true);
done();
})
.catch(done);
})


})