From c32479b284b13f5a876bb6a823d6011d98ad04d0 Mon Sep 17 00:00:00 2001 From: Peter Muriuki Date: Wed, 6 Dec 2023 20:11:13 +0300 Subject: [PATCH 1/3] Make it possible to add caching headers to fhir server service --- .../react-utils/src/helpers/dataLoaders.ts | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/packages/react-utils/src/helpers/dataLoaders.ts b/packages/react-utils/src/helpers/dataLoaders.ts index c191d76cc..c55451eec 100644 --- a/packages/react-utils/src/helpers/dataLoaders.ts +++ b/packages/react-utils/src/helpers/dataLoaders.ts @@ -11,11 +11,12 @@ import { history } from '@onaio/connected-reducer-registry'; import { refreshToken } from '@onaio/gatekeeper'; import { getAccessToken, isTokenExpired } from '@onaio/session-reducer'; import { Dictionary } from '@onaio/utils'; -import { EXPRESS_TOKEN_REFRESH_URL } from '../constants'; +import { EXPRESS_TOKEN_REFRESH_URL, INVALIDATE_CACHE_PARAM } from '../constants'; import { getAllConfigs } from '@opensrp/pkg-config'; import FHIR from 'fhirclient'; import { fhirclient } from 'fhirclient/lib/types'; import type { IResource } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IResource'; +import { URLSearchParams } from 'url'; const configs = getAllConfigs(); @@ -77,6 +78,21 @@ export class FHIRServiceClass { this.signal = signal; } + public getCacheControlHeaders(searchParams?: URLSearchParams): HeadersInit { + if (!searchParams) { + return {}; + } + const resourcesToInvalidate = searchParams.get(INVALIDATE_CACHE_PARAM); + if (resourcesToInvalidate) { + if (resourcesToInvalidate.split(',').includes(this.resourceType)) { + return { + 'cache-control': 'no-cache', + }; + } + } + return {}; + } + public buildQueryParams(params: URLParams | null) { if (params) { const urlParams = new URLSearchParams(); @@ -112,17 +128,19 @@ export class FHIRServiceClass { return serve.update(payload as fhirclient.FHIR.Resource, { signal: this.signal }); } - public async list(params: URLParams | null = null) { + public async list(params: URLParams | null = null, searchParams?: URLSearchParams) { const accessToken = await OpenSRPService.processAcessToken(this.accessTokenOrCallBack); const queryStr = this.buildQueryParams(params); const serve = FHIR.client(this.buildState(accessToken)); - return serve.request(queryStr); + const cacheHeaders = this.getCacheControlHeaders(searchParams); + return serve.request({ url: queryStr, headers: { ...cacheHeaders } }); } - public async read(id: string) { + public async read(id: string, searchParams?: URLSearchParams) { const accessToken = await OpenSRPService.processAcessToken(this.accessTokenOrCallBack); const serve = FHIR.client(this.buildState(accessToken)); - return serve.request(`${this.resourceType}/${id}`); + const cacheHeaders = this.getCacheControlHeaders(searchParams); + return serve.request({ url: `${this.resourceType}/${id}`, headers: { ...cacheHeaders } }); } public async delete(id: string) { From 7eec094812e70ded4fbee7e29ce5920edbb03cff Mon Sep 17 00:00:00 2001 From: Peter Muriuki Date: Wed, 6 Dec 2023 20:16:47 +0300 Subject: [PATCH 2/3] Hardcode cache headers on the fhirservice --- .../react-utils/src/helpers/dataLoaders.ts | 31 +++++-------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/packages/react-utils/src/helpers/dataLoaders.ts b/packages/react-utils/src/helpers/dataLoaders.ts index c55451eec..44dd6cb77 100644 --- a/packages/react-utils/src/helpers/dataLoaders.ts +++ b/packages/react-utils/src/helpers/dataLoaders.ts @@ -11,12 +11,11 @@ import { history } from '@onaio/connected-reducer-registry'; import { refreshToken } from '@onaio/gatekeeper'; import { getAccessToken, isTokenExpired } from '@onaio/session-reducer'; import { Dictionary } from '@onaio/utils'; -import { EXPRESS_TOKEN_REFRESH_URL, INVALIDATE_CACHE_PARAM } from '../constants'; +import { EXPRESS_TOKEN_REFRESH_URL } from '../constants'; import { getAllConfigs } from '@opensrp/pkg-config'; import FHIR from 'fhirclient'; import { fhirclient } from 'fhirclient/lib/types'; import type { IResource } from '@smile-cdr/fhirts/dist/FHIR-R4/interfaces/IResource'; -import { URLSearchParams } from 'url'; const configs = getAllConfigs(); @@ -78,21 +77,6 @@ export class FHIRServiceClass { this.signal = signal; } - public getCacheControlHeaders(searchParams?: URLSearchParams): HeadersInit { - if (!searchParams) { - return {}; - } - const resourcesToInvalidate = searchParams.get(INVALIDATE_CACHE_PARAM); - if (resourcesToInvalidate) { - if (resourcesToInvalidate.split(',').includes(this.resourceType)) { - return { - 'cache-control': 'no-cache', - }; - } - } - return {}; - } - public buildQueryParams(params: URLParams | null) { if (params) { const urlParams = new URLSearchParams(); @@ -128,19 +112,20 @@ export class FHIRServiceClass { return serve.update(payload as fhirclient.FHIR.Resource, { signal: this.signal }); } - public async list(params: URLParams | null = null, searchParams?: URLSearchParams) { + public async list(params: URLParams | null = null) { const accessToken = await OpenSRPService.processAcessToken(this.accessTokenOrCallBack); const queryStr = this.buildQueryParams(params); const serve = FHIR.client(this.buildState(accessToken)); - const cacheHeaders = this.getCacheControlHeaders(searchParams); - return serve.request({ url: queryStr, headers: { ...cacheHeaders } }); + return serve.request({ url: queryStr, headers: { 'cache-control': 'no-cache' } }); } - public async read(id: string, searchParams?: URLSearchParams) { + public async read(id: string) { const accessToken = await OpenSRPService.processAcessToken(this.accessTokenOrCallBack); const serve = FHIR.client(this.buildState(accessToken)); - const cacheHeaders = this.getCacheControlHeaders(searchParams); - return serve.request({ url: `${this.resourceType}/${id}`, headers: { ...cacheHeaders } }); + return serve.request({ + url: `${this.resourceType}/${id}`, + headers: { 'cache-control': 'no-cache' }, + }); } public async delete(id: string) { From 663f3ea8d406e4afeb57b6f14247830c259cb855 Mon Sep 17 00:00:00 2001 From: Peter Muriuki Date: Thu, 7 Dec 2023 09:43:56 +0300 Subject: [PATCH 3/3] Fix test regressions --- .../src/helpers/tests/dataLoaders.test.ts | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/react-utils/src/helpers/tests/dataLoaders.test.ts b/packages/react-utils/src/helpers/tests/dataLoaders.test.ts index 1736667e4..022165ba0 100644 --- a/packages/react-utils/src/helpers/tests/dataLoaders.test.ts +++ b/packages/react-utils/src/helpers/tests/dataLoaders.test.ts @@ -219,7 +219,16 @@ describe('dataloaders/FHIRService', () => { const fhir = new FHIRServiceClass('https://test.fhir.org', 'CareTeam'); const result = await fhir.list(); await flushPromises(); - expect(requestMock.mock.calls).toEqual([['CareTeam']]); + expect(requestMock.mock.calls).toEqual([ + [ + { + headers: { + 'cache-control': 'no-cache', + }, + url: 'CareTeam', + }, + ], + ]); expect(result).toEqual(fixtures.careTeams); // make sure every item of fhirlist returns the CareTeam expect(result.entry.every((e) => e.resource.resourceType === 'CareTeam')).toBeTruthy(); @@ -239,7 +248,16 @@ describe('dataloaders/FHIRService', () => { // without url params const result = await fhir.list({ _count: '100' }); await flushPromises(); - expect(requestMock.mock.calls).toEqual([['CareTeam/_search?_count=100']]); + expect(requestMock.mock.calls).toEqual([ + [ + { + headers: { + 'cache-control': 'no-cache', + }, + url: 'CareTeam/_search?_count=100', + }, + ], + ]); expect(result).toEqual(fixtures.careTeams); // make sure every item of fhirlist returns the CareTeam expect(result.entry.every((e) => e.resource.resourceType === 'CareTeam')).toBeTruthy();