From 3b895a1ad3e66ac5bc604e0b39d08bbdfdd0b371 Mon Sep 17 00:00:00 2001 From: thepassle Date: Wed, 13 Sep 2023 16:41:35 +0200 Subject: [PATCH 1/4] fix: export ajax types --- packages/ajax/src/index.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/packages/ajax/src/index.js b/packages/ajax/src/index.js index 46ff0c7635..f964b82901 100644 --- a/packages/ajax/src/index.js +++ b/packages/ajax/src/index.js @@ -10,3 +10,23 @@ export { // globally available instance export const ajax = new Ajax(); + +/** + * @typedef {import('../types/types.js').LionRequestInit} LionRequestInit + * @typedef {import('../types/types.js').AjaxConfig} AjaxConfig + * @typedef {import('../types/types.js').RequestInterceptor} RequestInterceptor + * @typedef {import('../types/types.js').ResponseInterceptor} ResponseInterceptor + * @typedef {import('../types/types.js').CacheConfig} CacheConfig + * @typedef {import('../types/types.js').RequestIdFunction} RequestIdFunction + * @typedef {import('../types/types.js').CacheOptions} CacheOptions + * @typedef {import('../types/types.js').CacheOptionsWithIdentifier} CacheOptionsWithIdentifier + * @typedef {import('../types/types.js').ValidatedCacheOptions} ValidatedCacheOptions + * @typedef {import('../types/types.js').CacheRequestExtension} CacheRequestExtension + * @typedef {import('../types/types.js').CacheResponseRequest} CacheResponseRequest + * @typedef {import('../types/types.js').CacheResponseExtension} CacheResponseExtension + * @typedef {import('../types/types.js').CacheRequest} CacheRequest + * @typedef {import('../types/types.js').CacheResponse} CacheResponse + * @typedef {import('../types/types.js').CachedRequests} CachedRequests + * @typedef {import('../types/types.js').CachedRequestInterceptor} CachedRequestInterceptor + * @typedef {import('../types/types.js').CachedResponseInterceptor} CachedResponseInterceptor + */ From 55e445c21ec6e84b4bc4ac3dc58b82390a8b3d24 Mon Sep 17 00:00:00 2001 From: Pascal Schilp Date: Wed, 13 Sep 2023 16:42:49 +0200 Subject: [PATCH 2/4] Create long-actors-trade.md --- .changeset/long-actors-trade.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/long-actors-trade.md diff --git a/.changeset/long-actors-trade.md b/.changeset/long-actors-trade.md new file mode 100644 index 0000000000..b27157ec65 --- /dev/null +++ b/.changeset/long-actors-trade.md @@ -0,0 +1,5 @@ +--- +"@lion/ajax": patch +--- + +fix: export ajax types From d79795acc30ca913832071ad1da58d5be66ec777 Mon Sep 17 00:00:00 2001 From: thepassle Date: Wed, 13 Sep 2023 17:00:23 +0200 Subject: [PATCH 3/4] fix: types are optional --- packages/ajax/types/types.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/ajax/types/types.ts b/packages/ajax/types/types.ts index c4b3ec36be..9a71351ac5 100644 --- a/packages/ajax/types/types.ts +++ b/packages/ajax/types/types.ts @@ -11,12 +11,12 @@ export interface LionRequestInit extends Omit { } export interface AjaxConfig { - addAcceptLanguage: boolean; - addCaching: boolean; - xsrfCookieName: string | null; - xsrfHeaderName: string | null; - cacheOptions: CacheOptionsWithIdentifier; - jsonPrefix: string; + addAcceptLanguage?: boolean; + addCaching?: boolean; + xsrfCookieName?: string | null; + xsrfHeaderName?: string | null; + cacheOptions?: CacheOptionsWithIdentifier; + jsonPrefix?: string; } export type RequestInterceptor = (request: Request) => Promise; @@ -46,7 +46,7 @@ export interface CacheOptions { } export interface CacheOptionsWithIdentifier extends CacheOptions { - getCacheIdentifier: () => string; + getCacheIdentifier?: () => string; } export interface ValidatedCacheOptions extends CacheOptions { From 570f834b85a7567a15312ab49c377264871f7bf7 Mon Sep 17 00:00:00 2001 From: thepassle Date: Thu, 14 Sep 2023 09:08:23 +0200 Subject: [PATCH 4/4] chore: fix types --- packages/ajax/src/Ajax.js | 21 +++++++++++++-------- packages/ajax/test/Ajax.test.js | 5 ++++- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/ajax/src/Ajax.js b/packages/ajax/src/Ajax.js index d5267d157e..4f84d66ebc 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 0b4c064129..c8b8ca53b6 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');