Skip to content

Commit d8f65bb

Browse files
author
James Vidler
authored
Revert "removed cache adapter"
1 parent 7ce1dbc commit d8f65bb

File tree

9 files changed

+86
-15
lines changed

9 files changed

+86
-15
lines changed

package-lock.json

Lines changed: 16 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@agility/content-fetch",
3-
"version": "1.1.0",
3+
"version": "1.0.3",
44
"description": "JavaScript library for the Agility Fetch API (node and browser)",
55
"main": "dist/agility-content-fetch.node.js",
66
"scripts": {
@@ -38,6 +38,7 @@
3838
"webpack-cli": "^3.2.3"
3939
},
4040
"dependencies": {
41-
"axios": "^0.21.1"
41+
"axios": "^0.21.1",
42+
"axios-cache-adapter": "^2.4.1"
4243
}
4344
}

src/api-client.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios from 'axios'
2+
import { setupCache } from 'axios-cache-adapter'
23
import getSitemapFlat from './methods/getSitemapFlat'
34
import getSitemapNested from './methods/getSitemapNested'
45
import getContentItem from './methods/getContentItem'
@@ -93,6 +94,14 @@ export default function createClient(userConfig) {
9394

9495
let adapter = null;
9596

97+
//should we turn on caching?
98+
if (config.caching.maxAge > 0) {
99+
const cache = setupCache({
100+
maxAge: config.caching.maxAge,
101+
exclude: { query: false }
102+
});
103+
adapter = cache.adapter;
104+
}
96105

97106
const https= require("https")
98107

@@ -103,11 +112,14 @@ export default function createClient(userConfig) {
103112
api = axios.create({
104113
httpsAgent: new https.Agent({
105114
rejectUnauthorized: false
106-
})
115+
}),
116+
adapter: adapter,
107117
})
108118
} else {
109119
//we can't use the https.Agent
110-
api = axios.create({})
120+
api = axios.create({
121+
adapter: adapter,
122+
})
111123
}
112124

113125

@@ -123,12 +135,17 @@ export default function createClient(userConfig) {
123135
return api(reqConfig).then(async (response) => {
124136

125137
let data = response.data;
138+
//if our response is from cache, inject that property in the data response
139+
if (response.request.fromCache) {
140+
data['fromCache'] = true;
141+
}
142+
126143
return data;
127144

128145

129146
})
130147
.catch(async (error) => {
131-
logError(`AgilityCMS Fetch API ERROR: Request failed for ${reqConfig.baseURL}${reqConfig.url} ... ${error}.`)
148+
logError(`AgilityCMS Fetch API ERROR: Request failed for ${reqConfig.baseURL}${reqConfig.url} ... ${error} ... Does the item exist?`)
132149
});
133150
}
134151

src/content-fetch.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ import { isHttps } from './utils'
4949
* @param {string} config.guid - The guid that represents your instance.
5050
* @param {string} config.apiKey - The secret token that represents your application.
5151
* @param {boolean} [config.isPreview] - If your access token is for preview, then set this to true.
52+
* @param {Object} [config.caching] - Optional Caching options. Caching is disabled by default.
53+
* @param {number} [config.caching.maxAge] - In miliseconds. Default value is *0* (disabled). Recommeded value is *180000* (3 mins). Requests are cached in memory only (node or browser).
5254
* @param {string} [config.baseUrl] - Optionally override the default API Base Url.
5355
* @return {AgilityFetch.Client}
5456
* @example
@@ -69,14 +71,12 @@ function getApi(config) {
6971

7072
function validateConfigParams(configParams) {
7173

72-
if(configParams.caching) {
73-
console.warn('The built-in caching has been deprecated from @agility/content-fetch as of version `1.1.0`. The `caching` parameter will have no effect.');
74-
}
75-
7674
if(!configParams.guid || configParams.guid.length == 0) {
7775
throw new TypeError('You must provide an guid.');
7876
} else if(!configParams.apiKey || configParams.apiKey.length == 0) {
79-
throw new TypeError('You must provide an access token.');
77+
throw new TypeError('You must provide an access token.');
78+
} else if(configParams.caching && isNaN(configParams.caching.maxAge)) {
79+
throw new TypeError('When specifying a cache maxAge, you must set a number value in miliseconds, i.e. 180000 (3 mins).');
8080
} else if(configParams.baseUrl && !isHttps(configParams.baseUrl)) {
8181
throw new TypeError(`When specifying a baseUrl (${configParams.baseUrl}), it must be over HTTPs.`);
8282
} else {

test/apiClients.config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ function createApiClientWithNewCdn() {
2222
return api;
2323
}
2424

25+
function createCachedApiClient() {
26+
var api = agility.getApi({
27+
guid: guid,
28+
apiKey: apiKeyFetch,
29+
caching: {
30+
maxAge: 5 * 60 * 1000 //==5mins
31+
}
32+
});
33+
return api;
34+
}
35+
2536
function createPreviewApiClient() {
2637
var api = agility.getApi({
2738
guid: guid,
@@ -33,6 +44,7 @@ function createPreviewApiClient() {
3344

3445
export {
3546
createApiClient,
47+
createCachedApiClient,
3648
createPreviewApiClient,
3749
createApiClientWithNewCdn
3850
}

test/getContentItem.tests.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const expect = chai.expect;
55
import {
66
createApiClient,
77
createPreviewApiClient,
8+
createCachedApiClient,
89
createApiClientWithNewCdn
910
} from './apiClients.config'
1011

@@ -49,6 +50,32 @@ describe('getContentItem:', function() {
4950
.catch(done);
5051
})
5152

53+
it('should retrieve a content item in live/fetch mode, then subsequent requests are returned from cache (memory)', function(done) {
54+
var api = createCachedApiClient();
55+
api.getContentItem({
56+
contentID: ref.updatesMadeToPublishedContentItemID,
57+
languageCode: 'en-us'
58+
})
59+
.then(function(contentItem) {
60+
61+
assert.strictEqual(contentItem.contentID, ref.updatesMadeToPublishedContentItemID, 'retrieved content item we asked for');
62+
assert.notExists(contentItem.fromCache, 'content item should not be served from cache on first request')
63+
64+
api.getContentItem({
65+
contentID: ref.updatesMadeToPublishedContentItemID,
66+
languageCode: 'en-us'
67+
})
68+
.then(function(contentItem2) {
69+
70+
assert.strictEqual(contentItem2.contentID, ref.updatesMadeToPublishedContentItemID, 'retrieved content item we asked for');
71+
assert.strictEqual(contentItem2.fromCache, true, 'content item was retrieved from memory cache')
72+
done();
73+
})
74+
.catch(done)
75+
76+
})
77+
.catch(done);
78+
})
5279

5380
it('should throw error if contentID not passed as argument for getContentItem', function(done) {
5481
expect(function() {

test/getContentList.tests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const expect = chai.expect;
44
import {
55
createApiClient,
66
createPreviewApiClient,
7+
createCachedApiClient,
78
createApiClientWithNewCdn
89
} from './apiClients.config'
910

test/getSyncContentItems.tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import chai from 'chai'
22
const assert = chai.assert;
33
const expect = chai.expect;
44

5-
import { createApiClient, createPreviewApiClient } from './apiClients.config'
5+
import { createApiClient, createPreviewApiClient, createCachedApiClient } from './apiClients.config'
66
import { SSL_OP_SSLEAY_080_CLIENT_DH_BUG } from 'constants';
77

88
/*

test/getSyncPageItems.tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import chai from 'chai'
22
const assert = chai.assert;
33
const expect = chai.expect;
44

5-
import { createApiClient, createPreviewApiClient } from './apiClients.config'
5+
import { createApiClient, createPreviewApiClient, createCachedApiClient } from './apiClients.config'
66
import { SSL_OP_SSLEAY_080_CLIENT_DH_BUG } from 'constants';
77

88
/*

0 commit comments

Comments
 (0)