Skip to content

Commit 173877b

Browse files
committed
feat: improve back-end caching for common services requests
1 parent 0e571e1 commit 173877b

File tree

5 files changed

+90
-4
lines changed

5 files changed

+90
-4
lines changed

package-lock.json

+39
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"@ucast/mongo2js": "^1.3.3",
4646
"apollo-server-cache-redis": "^3.3.1",
4747
"axios": "^1.4.0",
48+
"axios-cache-interceptor": "^1.6.2",
4849
"body-parser": "^1.20.1",
4950
"bson": "^4.2.3",
5051
"config": "^3.3.9",

src/server/common-services.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Axios from 'axios';
2+
import {
3+
AxiosCacheInstance,
4+
setupCache,
5+
buildKeyGenerator,
6+
} from 'axios-cache-interceptor/dev';
7+
8+
let axios: AxiosCacheInstance;
9+
10+
/**
11+
* Create a dedicated axios instance for Common Services
12+
* Add cache mechanism
13+
* Cached requests expire after 5 minutes.
14+
*
15+
* @returns Common Services axios instance
16+
*/
17+
export default () => {
18+
if (!axios) {
19+
const instance = Axios.create();
20+
axios = setupCache(instance, {
21+
methods: ['get', 'post'],
22+
// Avoid using cache control set to false
23+
interpretHeader: false,
24+
debug: console.log,
25+
// Generate an unique key, based on all parameters listed
26+
generateKey: buildKeyGenerator((request) => ({
27+
method: request.method,
28+
baseURL: request.baseURL,
29+
params: request.params,
30+
url: request.url,
31+
data: request.data,
32+
custom: request.headers.Authorization,
33+
})),
34+
});
35+
}
36+
return axios;
37+
};

src/utils/form/getDisplayText.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { Context } from '../../server/apollo/context';
22
import { CustomAPI } from '../../server/apollo/dataSources';
33
import config from 'config';
44
import { logger } from '@services/logger.service';
5-
import axios, { AxiosHeaders } from 'axios';
5+
import axios, { AxiosHeaders, AxiosStatic } from 'axios';
66
import get from 'lodash/get';
77
import jsonpath from 'jsonpath';
8+
import commonServices from '@server/common-services';
9+
import { AxiosCacheInstance } from 'axios-cache-interceptor';
810

911
/**
1012
* Gets display text from choice value.
@@ -114,6 +116,7 @@ export const getFullChoices = async (
114116
}
115117
}
116118
} else if (field.choicesByGraphQL) {
119+
let sender: AxiosCacheInstance | AxiosStatic = axios;
117120
const url: string = field.choicesByGraphQL.url;
118121
let choices: any[] = [];
119122
const valueField = get(field, 'choicesByGraphQL.value', null);
@@ -126,13 +129,14 @@ export const getFullChoices = async (
126129
url.includes(config.get('commonServices.url'))
127130
) {
128131
headers.setAuthorization(`Bearer ${context.accesstoken}`);
132+
sender = commonServices();
129133
} else {
130134
headers.setAuthorization(context.token);
131135
if (context.accesstoken) {
132136
headers.set('accesstoken', context.accesstoken);
133137
}
134138
}
135-
await axios({
139+
await sender({
136140
url,
137141
method: 'post',
138142
headers,

src/utils/proxy/getChoices.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import fetch from 'node-fetch';
22
import get from 'lodash/get';
33
import jsonpath from 'jsonpath';
4-
import axios, { AxiosHeaders } from 'axios';
4+
import { AxiosHeaders, AxiosStatic } from 'axios';
55
import config from 'config';
66
import { Request } from 'express';
7+
import commonServices from '@server/common-services';
8+
import { AxiosCacheInstance } from 'axios-cache-interceptor';
9+
import axios from 'axios';
710

811
/**
912
* Fetches the choices for a question field by URL
@@ -63,11 +66,13 @@ export const getChoices = async (req: Request, field: any): Promise<any[]> => {
6366
const url = get(field, 'choicesByGraphQL.url', null);
6467
const valueField = get(field, 'choicesByGraphQL.value', null);
6568
const textField = get(field, 'choicesByGraphQL.text', null);
69+
let sender: AxiosCacheInstance | AxiosStatic = axios;
6670
if (
6771
config.get('commonServices.url') &&
6872
url.includes(config.get('commonServices.url'))
6973
) {
7074
headers.setAuthorization(`Bearer ${req.headers.accesstoken}`);
75+
sender = commonServices();
7176
} else {
7277
headers.setAuthorization(req.headers.authorization);
7378
if (req.headers.accesstoken) {
@@ -76,7 +81,7 @@ export const getChoices = async (req: Request, field: any): Promise<any[]> => {
7681
}
7782
try {
7883
let choices: any[] = [];
79-
await axios({
84+
await sender({
8085
url,
8186
method: 'post',
8287
headers,

0 commit comments

Comments
 (0)