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

Revert "removed cache adapter" #72

Merged
merged 1 commit into from
Mar 9, 2021
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
19 changes: 16 additions & 3 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@agility/content-fetch",
"version": "1.1.0",
"version": "1.0.3",
"description": "JavaScript library for the Agility Fetch API (node and browser)",
"main": "dist/agility-content-fetch.node.js",
"scripts": {
Expand Down Expand Up @@ -38,6 +38,7 @@
"webpack-cli": "^3.2.3"
},
"dependencies": {
"axios": "^0.21.1"
"axios": "^0.21.1",
"axios-cache-adapter": "^2.4.1"
}
}
23 changes: 20 additions & 3 deletions src/api-client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios from 'axios'
import { setupCache } from 'axios-cache-adapter'
import getSitemapFlat from './methods/getSitemapFlat'
import getSitemapNested from './methods/getSitemapNested'
import getContentItem from './methods/getContentItem'
Expand Down Expand Up @@ -93,6 +94,14 @@ export default function createClient(userConfig) {

let adapter = null;

//should we turn on caching?
if (config.caching.maxAge > 0) {
const cache = setupCache({
maxAge: config.caching.maxAge,
exclude: { query: false }
});
adapter = cache.adapter;
}

const https= require("https")

Expand All @@ -103,11 +112,14 @@ export default function createClient(userConfig) {
api = axios.create({
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
}),
adapter: adapter,
})
} else {
//we can't use the https.Agent
api = axios.create({})
api = axios.create({
adapter: adapter,
})
}


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

let data = response.data;
//if our response is from cache, inject that property in the data response
if (response.request.fromCache) {
data['fromCache'] = true;
}

return data;


})
.catch(async (error) => {
logError(`AgilityCMS Fetch API ERROR: Request failed for ${reqConfig.baseURL}${reqConfig.url} ... ${error}.`)
logError(`AgilityCMS Fetch API ERROR: Request failed for ${reqConfig.baseURL}${reqConfig.url} ... ${error} ... Does the item exist?`)
});
}

Expand Down
10 changes: 5 additions & 5 deletions src/content-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import { isHttps } from './utils'
* @param {string} config.guid - The guid that represents your instance.
* @param {string} config.apiKey - The secret token that represents your application.
* @param {boolean} [config.isPreview] - If your access token is for preview, then set this to true.
* @param {Object} [config.caching] - Optional Caching options. Caching is disabled by default.
* @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).
* @param {string} [config.baseUrl] - Optionally override the default API Base Url.
* @return {AgilityFetch.Client}
* @example
Expand All @@ -69,14 +71,12 @@ function getApi(config) {

function validateConfigParams(configParams) {

if(configParams.caching) {
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.');
}

if(!configParams.guid || configParams.guid.length == 0) {
throw new TypeError('You must provide an guid.');
} else if(!configParams.apiKey || configParams.apiKey.length == 0) {
throw new TypeError('You must provide an access token.');
throw new TypeError('You must provide an access token.');
} else if(configParams.caching && isNaN(configParams.caching.maxAge)) {
throw new TypeError('When specifying a cache maxAge, you must set a number value in miliseconds, i.e. 180000 (3 mins).');
} else if(configParams.baseUrl && !isHttps(configParams.baseUrl)) {
throw new TypeError(`When specifying a baseUrl (${configParams.baseUrl}), it must be over HTTPs.`);
} else {
Expand Down
12 changes: 12 additions & 0 deletions test/apiClients.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ function createApiClientWithNewCdn() {
return api;
}

function createCachedApiClient() {
var api = agility.getApi({
guid: guid,
apiKey: apiKeyFetch,
caching: {
maxAge: 5 * 60 * 1000 //==5mins
}
});
return api;
}

function createPreviewApiClient() {
var api = agility.getApi({
guid: guid,
Expand All @@ -33,6 +44,7 @@ function createPreviewApiClient() {

export {
createApiClient,
createCachedApiClient,
createPreviewApiClient,
createApiClientWithNewCdn
}
27 changes: 27 additions & 0 deletions test/getContentItem.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const expect = chai.expect;
import {
createApiClient,
createPreviewApiClient,
createCachedApiClient,
createApiClientWithNewCdn
} from './apiClients.config'

Expand Down Expand Up @@ -49,6 +50,32 @@ describe('getContentItem:', function() {
.catch(done);
})

it('should retrieve a content item in live/fetch mode, then subsequent requests are returned from cache (memory)', function(done) {
var api = createCachedApiClient();
api.getContentItem({
contentID: ref.updatesMadeToPublishedContentItemID,
languageCode: 'en-us'
})
.then(function(contentItem) {

assert.strictEqual(contentItem.contentID, ref.updatesMadeToPublishedContentItemID, 'retrieved content item we asked for');
assert.notExists(contentItem.fromCache, 'content item should not be served from cache on first request')

api.getContentItem({
contentID: ref.updatesMadeToPublishedContentItemID,
languageCode: 'en-us'
})
.then(function(contentItem2) {

assert.strictEqual(contentItem2.contentID, ref.updatesMadeToPublishedContentItemID, 'retrieved content item we asked for');
assert.strictEqual(contentItem2.fromCache, true, 'content item was retrieved from memory cache')
done();
})
.catch(done)

})
.catch(done);
})

it('should throw error if contentID not passed as argument for getContentItem', function(done) {
expect(function() {
Expand Down
1 change: 1 addition & 0 deletions test/getContentList.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const expect = chai.expect;
import {
createApiClient,
createPreviewApiClient,
createCachedApiClient,
createApiClientWithNewCdn
} from './apiClients.config'

Expand Down
2 changes: 1 addition & 1 deletion test/getSyncContentItems.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import chai from 'chai'
const assert = chai.assert;
const expect = chai.expect;

import { createApiClient, createPreviewApiClient } from './apiClients.config'
import { createApiClient, createPreviewApiClient, createCachedApiClient } from './apiClients.config'
import { SSL_OP_SSLEAY_080_CLIENT_DH_BUG } from 'constants';

/*
Expand Down
2 changes: 1 addition & 1 deletion test/getSyncPageItems.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import chai from 'chai'
const assert = chai.assert;
const expect = chai.expect;

import { createApiClient, createPreviewApiClient } from './apiClients.config'
import { createApiClient, createPreviewApiClient, createCachedApiClient } from './apiClients.config'
import { SSL_OP_SSLEAY_080_CLIENT_DH_BUG } from 'constants';

/*
Expand Down