diff --git a/packages/ajax/src/Ajax.js b/packages/ajax/src/Ajax.js index d5267d157..4f84d66eb 100644 --- a/packages/ajax/src/Ajax.js +++ b/packages/ajax/src/Ajax.js @@ -72,14 +72,19 @@ export class Ajax { this.addRequestInterceptor(createXsrfRequestInterceptor(xsrfCookieName, xsrfHeaderName)); } - const { cacheOptions } = this.__config; - if (cacheOptions.useCache || this.__config.addCaching) { - const { cacheRequestInterceptor, cacheResponseInterceptor } = createCacheInterceptors( - cacheOptions.getCacheIdentifier, - cacheOptions, - ); - this.addRequestInterceptor(cacheRequestInterceptor); - this.addResponseInterceptor(cacheResponseInterceptor); + // eslint-disable-next-line prefer-destructuring + const cacheOptions = /** @type {import('@lion/ajax').CacheOptionsWithIdentifier} */ ( + this.__config.cacheOptions + ); + if ((cacheOptions && cacheOptions.useCache) || this.__config.addCaching) { + if (cacheOptions.getCacheIdentifier) { + const { cacheRequestInterceptor, cacheResponseInterceptor } = createCacheInterceptors( + cacheOptions.getCacheIdentifier, + cacheOptions, + ); + this.addRequestInterceptor(cacheRequestInterceptor); + this.addResponseInterceptor(cacheResponseInterceptor); + } } } diff --git a/packages/ajax/test/Ajax.test.js b/packages/ajax/test/Ajax.test.js index 0b4c06412..c8b8ca53b 100644 --- a/packages/ajax/test/Ajax.test.js +++ b/packages/ajax/test/Ajax.test.js @@ -72,7 +72,10 @@ describe('Ajax', () => { // TODO: fix AjaxConfig types => e.g. create FullAjaxConfig with everything "mandatory" and then AjaxConfig (= Partial of it) for user // @ts-ignore const ajax1 = new Ajax(config); - const defaultCacheIdentifierFunction = ajax1.options?.cacheOptions?.getCacheIdentifier; + + const defaultCacheIdentifierFunction = /** @type {() => void} */ ( + ajax1.options?.cacheOptions?.getCacheIdentifier + ); // Then expect(defaultCacheIdentifierFunction).not.to.be.undefined; expect(defaultCacheIdentifierFunction).to.be.a('function');