diff --git a/src/services/request.ts b/src/services/request.ts index ddbd097..5e0205b 100644 --- a/src/services/request.ts +++ b/src/services/request.ts @@ -6,6 +6,7 @@ export interface IRequestParameters { body?: any; forceHttp?: boolean; 'bearer-token'?: string; + 'allow-cache'?: boolean; // default false } export default async function request (obj: IRequestParameters): Promise { @@ -25,10 +26,6 @@ export default async function request (obj: IRequestParameters): Promise { const XHR = typeof XMLHttpRequest === 'undefined' ? xhrPolyfill : XMLHttpRequest; const request: XMLHttpRequest = new XHR(); - if (obj['bearer-token']) { - request.setRequestHeader('Authorization', `Bearer ${obj['bearer-token']}`); - } - request.onload = () => { if (request.status >= 200 && request.status < 300) { resolve(request.responseText); @@ -56,6 +53,14 @@ export default async function request (obj: IRequestParameters): Promise { request.open(obj.method || 'GET', url); + if (obj['bearer-token']) { + request.setRequestHeader('Authorization', `Bearer ${obj['bearer-token']}`); + } + + if (!obj['allow-cache']) { + request.setRequestHeader('Cache-Control', 'no-cache, no-store, max-age=0'); + } + if (obj.body) { request.send(JSON.stringify(obj.body)); } else { diff --git a/tests/services/request.test.ts b/tests/services/request.test.ts index 5208c67..9b9b95c 100644 --- a/tests/services/request.test.ts +++ b/tests/services/request.test.ts @@ -34,7 +34,9 @@ function MockXMLHttpRequestFactory ({ isSuccessCase }: { isSuccessCase: boolean onload (): any {} // eslint-disable-next-line @typescript-eslint/no-empty-function - setRequestHeader (name: string, value: string): any {} + setRequestHeader (name: string, value: string): any { + this.headers[name] = value; + } }; } @@ -99,6 +101,52 @@ describe('Services Request test suite', function () { }); }); + describe('given the allow-cache option is undefined', function () { + it('should set the Cache-Control header to disallow caching', async function () { + // @ts-expect-error open takes params but does not pick them up from the class definition and TS complains... + const setRequestHeaderStub = sinon.stub<[string, string]>(MockXMLHttpRequestSuccess.prototype, 'setRequestHeader'); + await request({ + url: 'https://www.test.com' + }); + expect(setRequestHeaderStub.getCall(0).args).toEqual(['Cache-Control', 'no-cache, no-store, max-age=0']); + }); + }); + + describe('given the allow-cache option is false', function () { + it('should set the Cache-Control header to disallow caching', async function () { + // @ts-expect-error open takes params but does not pick them up from the class definition and TS complains... + const setRequestHeaderStub = sinon.stub<[string, string]>(MockXMLHttpRequestSuccess.prototype, 'setRequestHeader'); + await request({ + url: 'https://www.test.com', + 'allow-cache': false + }); + expect(setRequestHeaderStub.getCall(0).args).toEqual(['Cache-Control', 'no-cache, no-store, max-age=0']); + }); + }); + + describe('given the allow-cache option is true', function () { + it('should not set the Cache-Control header', async function () { + // @ts-expect-error open takes params but does not pick them up from the class definition and TS complains... + const setRequestHeaderStub = sinon.stub<[string, string]>(MockXMLHttpRequestSuccess.prototype, 'setRequestHeader'); + await request({ + url: 'https://www.test.com', + 'allow-cache': true + }); + expect(setRequestHeaderStub.calledOnce).toBe(false); + }); + }); + + describe('given the allow-cache option is false', function () { + it('should set the Cache-Control header to disallow caching', async function () { + // @ts-expect-error open takes params but does not pick them up from the class definition and TS complains... + const setRequestHeaderStub = sinon.stub<[string, string]>(MockXMLHttpRequestSuccess.prototype, 'setRequestHeader'); + await request({ + url: 'https://www.test.com' + }); + expect(setRequestHeaderStub.getCall(0).args).toEqual(['Cache-Control', 'no-cache, no-store, max-age=0']); + }); + }); + describe('method option', function () { let openStub;