Skip to content

Commit e94d760

Browse files
authored
Merge pull request #49 from agility/joel
added url redirections
2 parents 2e8fea4 + b6a354c commit e94d760

File tree

6 files changed

+189
-23
lines changed

6 files changed

+189
-23
lines changed

src/api-client.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import getContentItem from './methods/getContentItem'
66
import getContentList from './methods/getContentList'
77
import getPage from './methods/getPage'
88
import getGallery from './methods/getGallery'
9+
import getUrlRedirections from './methods/getUrlRedirections'
10+
911
import getSyncContent from './methods/getSyncContent'
1012
import getSyncPages from './methods/getSyncPages'
13+
1114
import FilterOperators from './types/FilterOperator'
1215
import FilterLogicOperators from './types/FilterLogicOperator'
1316
import SortDirections from './types/SortDirection'
@@ -80,10 +83,13 @@ export default function createClient(userConfig) {
8083
adapter = cache.adapter;
8184
}
8285

86+
8387
//create apply the adapter to our axios instance
8488
const api = axios.create({
85-
adapter: adapter
86-
})
89+
adapter: adapter,
90+
})
91+
92+
8793

8894
//the function that actually makes ALL our requests
8995
function makeRequest(reqConfig) {
@@ -99,8 +105,20 @@ export default function createClient(userConfig) {
99105
//if our response is from cache, inject that property in the data response
100106
if (response.request.fromCache) {
101107
data['fromCache'] = true;
102-
}
103-
return data;
108+
}
109+
110+
if (! reqConfig.returnHeaders) {
111+
//if they just wanted the data...
112+
return data;
113+
} else {
114+
//if they indicated they wanted the headers as well as the data...
115+
return {
116+
headers: response.headers,
117+
data
118+
}
119+
}
120+
121+
104122
})
105123
.catch(async (error) => {
106124
logError(`AgilityCMS Fetch API ERROR: Request failed for ${reqConfig.baseURL}${reqConfig.url} ... ${error} ... Does the item exist?`)
@@ -110,15 +128,16 @@ export default function createClient(userConfig) {
110128
//export only these properties:
111129
return {
112130
config: config,
113-
makeRequest: makeRequest,
114-
getSitemapFlat: getSitemapFlat,
115-
getSitemapNested: getSitemapNested,
116-
getContentItem: getContentItem,
117-
getContentList: getContentList,
118-
getPage: getPage,
119-
getGallery: getGallery,
120-
getSyncContent: getSyncContent,
121-
getSyncPages: getSyncPages,
131+
makeRequest,
132+
getSitemapFlat,
133+
getSitemapNested,
134+
getContentItem,
135+
getContentList,
136+
getPage,
137+
getGallery,
138+
getSyncContent,
139+
getSyncPages,
140+
getUrlRedirections,
122141
types: {
123142
FilterOperators: FilterOperators,
124143
FilterLogicOperators: FilterLogicOperators,

src/content-fetch.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import createClient from './api-client'
4141
import { isHttps } from './utils'
4242

43-
/**
43+
/**
4444
* How to create an instance of an an API client for the Agility Content Fetch REST API.
4545
* @func
4646
* @name getApi
@@ -54,9 +54,9 @@ import { isHttps } from './utils'
5454
* @param {string} [config.baseUrl] - Optionally override the default API Base Url.
5555
* @return {AgilityFetch.Client}
5656
* @example
57-
*
57+
*
5858
* import agility from '@agility/content-fetch'
59-
*
59+
*
6060
* const api = agility.getApi({
6161
* guid: '191309ca-e675-4be2-bb29-351879528707',
6262
* apiKey: 'aGd13M.fa30c36e553a36f871860407e902da9a7375322457acd6bcda038e60af699411',

src/methods/getContentItem.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import { buildRequestUrlPath, buildAuthHeader } from '../utils'
99
* @param {number} [requestParams.contentLinkDepth] - The depth, representing the levels in which you want linked content auto-resolved. Default is 1.
1010
* @returns {Promise<AgilityFetch.Types.ContentItem>} - Returns a content item object.
1111
* @example
12-
*
12+
*
1313
* import agility from '@agility/content-fetch'
14-
*
14+
*
1515
* const api = agility.getApi({
1616
* guid: 'ade6cf3c',
1717
* apiKey: 'defaultlive.201ffdd0841cacad5bb647e76547e918b0c9ecdb8b5ddb3cf92e9a79b03623cb',
1818
* });
19-
*
19+
*
2020
* api.getContentItem({
2121
* contentID: 22,
2222
* languageCode: 'en-us'
@@ -27,7 +27,7 @@ import { buildRequestUrlPath, buildAuthHeader } from '../utils'
2727
* .catch(function(error) {
2828
* console.log(error);
2929
* });
30-
*
30+
*
3131
*/
3232
function getContentItem(requestParams) {
3333

@@ -41,9 +41,9 @@ function getContentItem(requestParams) {
4141
method: 'get',
4242
baseURL: buildRequestUrlPath(this.config, requestParams.languageCode),
4343
headers: buildAuthHeader(this.config),
44-
params:{}
44+
params:{}
4545
};
46-
46+
4747
return this.makeRequest(req);
4848
}
4949

src/methods/getUrlRedirections.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { buildRequestUrlPath, buildAuthHeader } from '../utils'
2+
3+
/**
4+
* Gets the details of a gallery by their Gallery ID.
5+
* @memberof AgilityFetch.Client.Pages
6+
* @param {Object} requestParams - The parameters for the API request.
7+
* @param {Date} [requestParams.lastAccessDate] - A Date object representing the last access date and time for the URL Redirections list. This value should be pulled from a previous request.
8+
* @returns {Promise<AgilityFetch.Types.Gallery>} - Returns a gallery object.
9+
* @example
10+
*
11+
* import agility from '@agility/content-fetch'
12+
*
13+
* const api = agility.getApi({
14+
* guid: 'ade6cf3c',
15+
* apiKey: 'defaultlive.201ffdd0841cacad5bb647e76547e918b0c9ecdb8b5ddb3cf92e9a79b03623cb',
16+
* });
17+
*
18+
* let dateObj = null;
19+
*
20+
* api.getUrlRedirections({
21+
* lastAccessDate: dateObj
22+
* })
23+
* .then(function({urlRedirections, lastAccessDate}) {
24+
* console.log(urlRedirections.length, lastAccessDate);
25+
* })
26+
* .catch(function(error) {
27+
* console.log(error);
28+
* });
29+
*
30+
*/
31+
function getUrlRedirections(requestParams) {
32+
33+
validateRequestParams(requestParams);
34+
35+
let url = "";
36+
if (requestParams.lastAccessDate) {
37+
url = `/?lastAccessDate=${requestParams.lastAccessDate.toISOString()}`
38+
}
39+
40+
41+
const req = {
42+
url: url,
43+
method: 'get',
44+
baseURL: buildRequestUrlPath(this.config, 'urlredirection'),
45+
headers: buildAuthHeader(this.config),
46+
params: {},
47+
returnHeaders: true
48+
};
49+
50+
const self = this;
51+
let promise = new Promise(function (resolve, reject) {
52+
53+
self.makeRequest(req)
54+
.then(response => {
55+
56+
if (response == undefined || ! response) {
57+
reject(new Error("The URL redirections could not be retrieved."));
58+
} else {
59+
60+
const {headers, data} = response;
61+
62+
//pull the lastAccessDate from the header
63+
let lastAccessDate = null;
64+
if (headers) {
65+
let dateStr = headers["x-lastaccessdate"];
66+
if (dateStr) lastAccessDate = new Date(dateStr);
67+
}
68+
69+
//return an object with items and lastAccessDate
70+
resolve({ lastAccessDate, items: data });
71+
}
72+
})
73+
.catch(error => {
74+
console.log("error in url redir")
75+
reject(error);
76+
}
77+
);
78+
});
79+
return promise;
80+
}
81+
82+
function validateRequestParams(requestParams) {
83+
84+
if (requestParams.lastAccessDate && ! requestParams.lastAccessDate.toISOString) {
85+
throw new TypeError('You must include a valid Datetime for the lastAccessDate.');
86+
} else {
87+
return;
88+
}
89+
}
90+
91+
92+
export default getUrlRedirections;

test/apiClients.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const apiKeyPreview = 'UnitTestsPreview.69e6bca345ced0b7ca5ab358b351ea5c870790a5
88
function createApiClient() {
99
var api = agility.getApi({
1010
guid: guid,
11-
apiKey: apiKeyFetch
11+
apiKey: apiKeyFetch
1212
});
1313
return api;
1414
}

test/getUrlRedirections.tests.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import chai from 'chai'
2+
const assert = chai.assert;
3+
const expect = chai.expect;
4+
5+
import { createApiClient } from './apiClients.config'
6+
7+
/*
8+
This file contains static references to content from the instance configured in the apiClient.config file.
9+
*/
10+
11+
/* GET URL REDIRECTIONS *********************************************************/
12+
13+
14+
describe('getUrlRedirections:', function() {
15+
16+
this.timeout('120s');
17+
18+
let persistedLastAccessDate = null;
19+
20+
it('should retrieve the list of URL redirections', function(done) {
21+
var api = createApiClient();
22+
api.getUrlRedirections({
23+
lastAccessDate: null
24+
})
25+
.then(function({lastAccessDate, items}) {
26+
27+
assert.isArray(items, "the items should be an array.");
28+
assert.isDefined(lastAccessDate.getUTCFullYear, "the lastAccessDate should be a date object.");
29+
assert.isTrue(lastAccessDate.getUTCFullYear() > 2000, "the lastAccessDate should be a recent date.")
30+
31+
persistedLastAccessDate = lastAccessDate;
32+
33+
done();
34+
})
35+
.catch(done);
36+
});
37+
38+
it('should retrieve an empty list of URL redirections with saved last access date', function(done) {
39+
var api = createApiClient();
40+
api.getUrlRedirections({
41+
lastAccessDate: persistedLastAccessDate
42+
})
43+
.then(function({lastAccessDate, items}) {
44+
45+
assert.isArray(items, "the items should be an array.");
46+
47+
assert.equal(items.length, 0, "the list of items should be empty.")
48+
49+
done();
50+
})
51+
.catch(done);
52+
});
53+
54+
55+
});

0 commit comments

Comments
 (0)