Skip to content

Commit

Permalink
Merge pull request #2079 from ing-bank/fix/export-ajax-types
Browse files Browse the repository at this point in the history
fix: export ajax types
  • Loading branch information
thepassle authored Sep 14, 2023
2 parents a58d8ce + 570f834 commit 0ea8b75
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-actors-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lion/ajax": patch
---

fix: export ajax types
21 changes: 13 additions & 8 deletions packages/ajax/src/Ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions packages/ajax/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
5 changes: 4 additions & 1 deletion packages/ajax/test/Ajax.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
14 changes: 7 additions & 7 deletions packages/ajax/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export interface LionRequestInit extends Omit<RequestInit, 'body'> {
}

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<Request | Response>;
Expand Down Expand Up @@ -46,7 +46,7 @@ export interface CacheOptions {
}

export interface CacheOptionsWithIdentifier extends CacheOptions {
getCacheIdentifier: () => string;
getCacheIdentifier?: () => string;
}

export interface ValidatedCacheOptions extends CacheOptions {
Expand Down

0 comments on commit 0ea8b75

Please sign in to comment.