Skip to content

Commit 15f5e68

Browse files
committed
added docs and param checking for getContentList
1 parent 32d5bf3 commit 15f5e68

File tree

3 files changed

+133
-3
lines changed

3 files changed

+133
-3
lines changed

src/methods/getContentList.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import utils from '../utils'
55
* @memberof AgilityFetch.Client
66
* @param {Object} requestParams - The parameters for the API request.
77
* @param {string} requestParams.referenceName - The unique reference name of the content list you wish to retrieve in the current language.
8+
* @param {number} [requestParams.take] - The maximum number of items to retrieve in this request. Default is **10**. Maximum allowed is **50**.
9+
* @param {number} [requestParams.skip] - The number of items to skip from the list. Default is **0**. Used for implementing pagination.
10+
* @param {number} [requestParams.sort] - The field to sort the results by. Example *fields.title* or *properties.created*.
11+
* @param {number} [requestParams.direction] - The direction to sort the results by. Default is **asc**. Valid values are **asc**, **desc**.
12+
* @param {string} [requestParams.filter] - *Note: This parameter has not been implemented, but will be in a future version.*
813
* @returns {Promise<AgilityFetch.Types.ContentList>} - Returns a list of content items.
914
* @example
1015
*
@@ -17,7 +22,8 @@ import utils from '../utils'
1722
* });
1823
*
1924
* api.getContentList({
20-
* referenceName: 'posts'
25+
* referenceName: 'posts',
26+
*
2127
* })
2228
* .then(function(contentList) {
2329
* console.log(contentList);
@@ -45,7 +51,26 @@ function getContentList(requestParams) {
4551

4652
function validateRequestParams(requestParams) {
4753
if(!requestParams.referenceName) {
54+
//must have a referenceName
4855
throw new TypeError('You must include a content referenceName in your request params.');
56+
} else if(requestParams.take && isNaN(requestParams.take)) {
57+
//take parameter must be a number
58+
throw new TypeError('Take parameter must be a number.')
59+
} else if((requestParams.take || requestParams.take == 0) && !isNaN(requestParams.take) && requestParams.take < 1) {
60+
//take parameter must be greater than 0
61+
throw new TypeError('Take parameter must be greater than 0.');
62+
} else if(requestParams.take && !isNaN(requestParams.take) && requestParams.take > 50) {
63+
//take parameter cannot be greater than 50
64+
throw new TypeError('Take parameter must be 50 or less.');
65+
} else if(requestParams.skip && isNaN(requestParams.skip)) {
66+
//skip parameter must be a number
67+
throw new TypeError('Skip parameter must be a number.');
68+
} else if(requestParams.skip && !isNaN(requestParams.skip) && requestParams.skip < 0) {
69+
//skip parameter must be a number greater than 0
70+
throw new TypeError('Skip parameter must be 0 or greater');
71+
} else if (requestParams.direction && (requestParams.direction !== 'desc' || requestParams.direction !== 'asc')){
72+
//check if the request direction parameter is valid
73+
throw new TypeError('Direction parameter must have a value of "asc" or "desc"');
4974
} else {
5075
return;
5176
}

src/types/ContentList.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/**
22
* Defines a **Content List** in the CMS.
3-
* @typedef {Array.<AgilityFetch.Types.ContentItem>} ContentList
3+
* @typedef ContentList
44
* @memberof AgilityFetch.Types
5+
* @property {Array.<AgilityFetch.Types.ContentItem>} items - The paginated array of items returned by the request. Default *take* is **10** unless otherwise specified (Maximum allowed it **50**)
6+
* @property {number} totalCount - The total amount of items in the list. Use this along with *skip* and *take* for pagination.
57
*/
68

test/api.js

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function createPreviewApiClient() {
2525

2626
describe('Api', function() {
2727

28-
this.timeout(5000);
28+
this.timeout("30s");
2929

3030
/* GET API CLIENT */
3131
it('should return an api client object from getApi', function(done) {
@@ -191,6 +191,109 @@ describe('Api', function() {
191191
done();
192192
})
193193

194+
it('should throw error if take parameter is NOT a number in getContentList', function(done) {
195+
expect(function() {
196+
var api = createApiClient();
197+
api.getContentList({
198+
referenceName: 'posts',
199+
take: 'ten'
200+
})
201+
.then(function(contentList) {
202+
assert.strictEqual(contentList[0].contentID, 24);
203+
assert.strictEqual(contentList[1].contentID, 25);
204+
done();
205+
})
206+
.catch(done);
207+
}).to.throw( TypeError );
208+
done();
209+
})
210+
211+
it('should throw error if take parameter is a number less than 1 in getContentList', function(done) {
212+
expect(function() {
213+
var api = createApiClient();
214+
api.getContentList({
215+
referenceName: 'posts',
216+
take: 0
217+
})
218+
.then(function(contentList) {
219+
assert.strictEqual(contentList[0].contentID, 24);
220+
assert.strictEqual(contentList[1].contentID, 25);
221+
done();
222+
})
223+
.catch(done);
224+
}).to.throw( TypeError );
225+
done();
226+
})
227+
228+
it('should throw error if take parameter is a number greater than 50 in getContentList', function(done) {
229+
expect(function() {
230+
var api = createApiClient();
231+
api.getContentList({
232+
referenceName: 'posts',
233+
take: 51
234+
})
235+
.then(function(contentList) {
236+
assert.strictEqual(contentList[0].contentID, 24);
237+
assert.strictEqual(contentList[1].contentID, 25);
238+
done();
239+
})
240+
.catch(done);
241+
}).to.throw( TypeError );
242+
done();
243+
})
244+
245+
it('should throw error if skip parameter is a number less than 0 in getContentList', function(done) {
246+
expect(function() {
247+
var api = createApiClient();
248+
api.getContentList({
249+
referenceName: 'posts',
250+
skip: -1
251+
})
252+
.then(function(contentList) {
253+
assert.strictEqual(contentList[0].contentID, 24);
254+
assert.strictEqual(contentList[1].contentID, 25);
255+
done();
256+
})
257+
.catch(done);
258+
}).to.throw( TypeError );
259+
done();
260+
})
261+
262+
it('should throw error if skip parameter is NOT a number in getContentList', function(done) {
263+
expect(function() {
264+
var api = createApiClient();
265+
api.getContentList({
266+
referenceName: 'posts',
267+
skip: 'ten'
268+
})
269+
.then(function(contentList) {
270+
assert.strictEqual(contentList[0].contentID, 24);
271+
assert.strictEqual(contentList[1].contentID, 25);
272+
done();
273+
})
274+
.catch(done);
275+
}).to.throw( TypeError );
276+
done();
277+
})
278+
279+
it('should throw error if direction parameter is NOT "asc" or "desc" in getContentList', function(done) {
280+
expect(function() {
281+
var api = createApiClient();
282+
api.getContentList({
283+
referenceName: 'posts',
284+
sort: 'fields.title',
285+
direction: 'up'
286+
})
287+
.then(function(contentList) {
288+
assert.strictEqual(contentList[0].contentID, 24);
289+
assert.strictEqual(contentList[1].contentID, 25);
290+
done();
291+
})
292+
.catch(done);
293+
}).to.throw( TypeError );
294+
done();
295+
})
296+
194297
/* GET PAGE *********************************************************/
195298
it('should retrieve a page in live mode', function(done) {
196299
var api = createApiClient();

0 commit comments

Comments
 (0)