Skip to content

Commit 1340b9e

Browse files
committed
refactored tests and added optional caching
1 parent 8466c3d commit 1340b9e

18 files changed

+797
-553
lines changed

package-lock.json

Lines changed: 15 additions & 14 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": "0.2.2",
3+
"version": "0.2.3",
44
"description": "JavaScript library for the Agility Fetch API (node and browser)",
55
"main": "dist/agility-content-fetch.node.js",
66
"scripts": {
@@ -34,6 +34,7 @@
3434
"webpack-cli": "^3.2.3"
3535
},
3636
"dependencies": {
37-
"axios": "^0.18.0"
37+
"axios": "^0.18.0",
38+
"axios-cache-adapter": "^2.3.0"
3839
}
3940
}

src/api-client.js

Lines changed: 40 additions & 6 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'
@@ -7,28 +8,61 @@ import getPage from './methods/getPage'
78

89
const defaultConfig = {
910
fetchBaseUrl: 'https://g5s2z5b3.stackpathcdn.com',
10-
previewBaseUrl: 'https://g5s2z5b3.stackpathcdn.com',
11+
previewBaseUrl: 'https://e5q8t3m6.stackpathcdn.com',
12+
baseUrl: null,
1113
isPreview: false,
1214
instanceID: null,
1315
accessToken: null,
14-
languageCode: null
16+
languageCode: null,
17+
caching: {
18+
maxAge: 0 //caching disabled by default
19+
}
1520
};
1621

1722
export default function createClient(userConfig) {
1823

19-
//merge our config
24+
//set default baseUrl
25+
if(userConfig.isPreview) {
26+
defaultConfig.baseUrl = defaultConfig.previewBaseUrl
27+
} else {
28+
defaultConfig.baseUrl = defaultConfig.fetchBaseUrl
29+
}
30+
31+
//merge our config - user values will override our defaults
2032
const config = {
2133
...defaultConfig,
2234
...userConfig
2335
};
2436

37+
let adapter = null;
38+
39+
//should we turn on caching?
40+
if(config.caching.maxAge > 0) {
41+
const cache = setupCache({
42+
maxAge: config.caching.maxAge
43+
});
44+
adapter = cache.adapter;
45+
}
46+
47+
//create apply the adapter to our axios instance
48+
const api = axios.create({
49+
adapter: adapter
50+
})
51+
52+
//the function that actually makes ALL our requests
2553
function makeRequest(reqConfig) {
26-
27-
return axios(reqConfig).then(response => {
28-
return response.data;
54+
//make the request using our axios instance
55+
return api(reqConfig).then(async (response) => {
56+
let data = response.data;
57+
//if our response is from cache, inject that property in the data response
58+
if(response.request.fromCache) {
59+
data['fromCache'] = true;
60+
}
61+
return data;
2962
});
3063
}
3164

65+
//export only these properties:
3266
return {
3367
config: config,
3468
makeRequest: makeRequest,

src/content-fetch.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,29 @@
1616

1717

1818
import createClient from './api-client'
19+
import { isHttps } from './utils'
1920

2021
/**
21-
* This is the description for create client
22+
* How to create an instance of an an API client for the Agility Content Fetch REST API.
2223
* @func
2324
* @name getApi
2425
* @memberof AgilityFetch
25-
* @param {Object} config - API intialization params
26+
* @param {Object} config - API intialization params.
2627
* @param {string} config.instanceID - The guid that represents your instance.
2728
* @param {string} config.accessToken - The secret token that represents your application.
2829
* @param {boolean} [config.isPreview] - If your access token is for preview, then set this to true.
30+
* @param {Object} [config.caching] - Optional Caching options. Caching is disabled by default.
31+
* @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).
32+
* @param {string} [config.baseUrl] - Optionally override the default API Base Url.
2933
* @return {AgilityFetch.Client}
3034
* @example
3135
*
3236
* import agility from '@agility/content-fetch'
3337
*
3438
* const api = agility.getApi({
3539
* instanceID: '1234-1234',
36-
* accessToken: 'fEpTcRnWO3EahHbojDCeY3PwGwAzpw2gveDuPn2l0nuqFbQYVcWrQ+a3/DHcWgCgn7UL2tgbSOS0AqrEOiXkTg=='
40+
* accessToken: 'fEpTcRnWO3EahHbojDCeY3PwGwAzpw2gveDuPn2l0nuqFbQYVcWrQ+a3/DHcWgCgn7UL2tgbSOS0AqrEOiXkTg==',
41+
* isPreview: false
3742
* });
3843
*/
3944

@@ -45,10 +50,13 @@ function getApi(config) {
4550
function validateConfigParams(configParams) {
4651

4752
if(!configParams.instanceID || configParams.instanceID.length == 0) {
48-
throw new TypeError('You must provide an instanceID');
49-
}
50-
else if(!configParams.accessToken || configParams.accessToken.length == 0) {
51-
throw new TypeError('You must provide an access token');
53+
throw new TypeError('You must provide an instanceID.');
54+
} else if(!configParams.accessToken || configParams.accessToken.length == 0) {
55+
throw new TypeError('You must provide an access token.');
56+
} else if(configParams.caching && isNaN(configParams.caching.maxAge)) {
57+
throw new TypeError('When specifying a cache maxAge, you must set a number value in miliseconds, i.e. 180000 (3 mins).');
58+
} else if(configParams.baseUrl && !isHttps(configParams.baseUrl)) {
59+
throw new TypeError(`When specifying a baseUrl (${configParams.baseUrl}), it must be over HTTPs.`);
5260
} else {
5361
return;
5462
}

src/methods/getContentItem.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import utils from '../utils'
1+
import { buildRequestUrlPath, buildAuthHeader } from '../utils'
22

33
/**
44
* Gets the details of a content item by their Content ID.
@@ -36,8 +36,8 @@ function getContentItem(requestParams) {
3636
const req = {
3737
url: `/item/${requestParams.contentID}`,
3838
method: 'get',
39-
baseURL: utils.buildRequestUrlPath(this.config, requestParams.languageCode),
40-
headers: utils.buildAuthHeader(this.config),
39+
baseURL: buildRequestUrlPath(this.config, requestParams.languageCode),
40+
headers: buildAuthHeader(this.config),
4141
params:{}
4242
};
4343

src/methods/getContentList.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import utils from '../utils'
1+
import { buildRequestUrlPath, buildAuthHeader } from '../utils'
22

33
/**
44
* Retrieves a list of content items by reference name.
@@ -45,8 +45,8 @@ function getContentList(requestParams) {
4545
const req = {
4646
url: `/list/${requestParams.referenceName}`,
4747
method: 'get',
48-
baseURL: utils.buildRequestUrlPath(this.config, requestParams.languageCode),
49-
headers: utils.buildAuthHeader(this.config),
48+
baseURL: buildRequestUrlPath(this.config, requestParams.languageCode),
49+
headers: buildAuthHeader(this.config),
5050
params:{}
5151
};
5252

src/methods/getPage.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import utils from '../utils'
1+
import { buildRequestUrlPath, buildAuthHeader } from '../utils'
22

33
/**
44
* Gets the details of a page by its Page ID.
@@ -36,8 +36,8 @@ function getPage(requestParams) {
3636
const req = {
3737
url: `/page/${requestParams.pageID}`,
3838
method: 'get',
39-
baseURL: utils.buildRequestUrlPath(this.config, requestParams.languageCode),
40-
headers: utils.buildAuthHeader(this.config),
39+
baseURL: buildRequestUrlPath(this.config, requestParams.languageCode),
40+
headers: buildAuthHeader(this.config),
4141
params:{}
4242
};
4343

src/methods/getSitemapFlat.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import utils from '../utils'
1+
import { buildRequestUrlPath, buildAuthHeader } from '../utils'
22

33
/**
44
* The sitemap, returned in a flat list, where the dictionary key is the page path. This method is ideal for page routing.
@@ -35,8 +35,8 @@ function getSitemapFlat(requestParams) {
3535
const req = {
3636
url: `/Sitemap/Flat/${requestParams.channelName}`,
3737
method: 'get',
38-
baseURL: utils.buildRequestUrlPath(this.config, requestParams.languageCode),
39-
headers: utils.buildAuthHeader(this.config),
38+
baseURL: buildRequestUrlPath(this.config, requestParams.languageCode),
39+
headers: buildAuthHeader(this.config),
4040
params:{}
4141
};
4242

src/methods/getSitemapNested.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import utils from '../utils'
1+
import { buildRequestUrlPath, buildAuthHeader } from '../utils'
22

33
/**
44
* Gets the sitemap as an array in a nested format, ideal for generating menus.
@@ -35,8 +35,8 @@ function getSitemapNested(requestParams) {
3535
const req = {
3636
url: `/Sitemap/Nested/${requestParams.channelName}`,
3737
method: 'get',
38-
baseURL: utils.buildRequestUrlPath(this.config, requestParams.languageCode),
39-
headers: utils.buildAuthHeader(this.config),
38+
baseURL: buildRequestUrlPath(this.config, requestParams.languageCode),
39+
headers: buildAuthHeader(this.config),
4040
params:{}
4141
};
4242

src/utils.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
function buildRequestUrlPath(config, languageCode) {
22
let urlPath = null;
3-
if(config.isPreview) {
4-
urlPath = config.previewBaseUrl
5-
} else {
6-
urlPath = config.fetchBaseUrl
7-
}
8-
urlPath = `${urlPath}/${config.instanceID}/${languageCode}`;
3+
urlPath = `${config.baseUrl}/${config.instanceID}/${languageCode}`;
94
return urlPath;
105
}
116

@@ -15,7 +10,16 @@ function buildAuthHeader(config) {
1510
}
1611
}
1712

18-
export default {
13+
function isHttps(url) {
14+
if(!url.toLowerCase().startsWith('https://')) {
15+
return false;
16+
}
17+
return true;
18+
}
19+
20+
21+
export {
1922
buildAuthHeader,
20-
buildRequestUrlPath
23+
buildRequestUrlPath,
24+
isHttps
2125
}

0 commit comments

Comments
 (0)