Merge pull request #2079 from ing-bank/fix/export-ajax-types
fix: export ajax types
This commit is contained in:
commit
0ea8b75349
5 changed files with 49 additions and 16 deletions
5
.changeset/long-actors-trade.md
Normal file
5
.changeset/long-actors-trade.md
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
"@lion/ajax": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix: export ajax types
|
||||||
|
|
@ -72,14 +72,19 @@ export class Ajax {
|
||||||
this.addRequestInterceptor(createXsrfRequestInterceptor(xsrfCookieName, xsrfHeaderName));
|
this.addRequestInterceptor(createXsrfRequestInterceptor(xsrfCookieName, xsrfHeaderName));
|
||||||
}
|
}
|
||||||
|
|
||||||
const { cacheOptions } = this.__config;
|
// eslint-disable-next-line prefer-destructuring
|
||||||
if (cacheOptions.useCache || this.__config.addCaching) {
|
const cacheOptions = /** @type {import('@lion/ajax').CacheOptionsWithIdentifier} */ (
|
||||||
const { cacheRequestInterceptor, cacheResponseInterceptor } = createCacheInterceptors(
|
this.__config.cacheOptions
|
||||||
cacheOptions.getCacheIdentifier,
|
);
|
||||||
cacheOptions,
|
if ((cacheOptions && cacheOptions.useCache) || this.__config.addCaching) {
|
||||||
);
|
if (cacheOptions.getCacheIdentifier) {
|
||||||
this.addRequestInterceptor(cacheRequestInterceptor);
|
const { cacheRequestInterceptor, cacheResponseInterceptor } = createCacheInterceptors(
|
||||||
this.addResponseInterceptor(cacheResponseInterceptor);
|
cacheOptions.getCacheIdentifier,
|
||||||
|
cacheOptions,
|
||||||
|
);
|
||||||
|
this.addRequestInterceptor(cacheRequestInterceptor);
|
||||||
|
this.addResponseInterceptor(cacheResponseInterceptor);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,3 +10,23 @@ export {
|
||||||
|
|
||||||
// globally available instance
|
// globally available instance
|
||||||
export const ajax = new Ajax();
|
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
|
||||||
|
*/
|
||||||
|
|
|
||||||
|
|
@ -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
|
// TODO: fix AjaxConfig types => e.g. create FullAjaxConfig with everything "mandatory" and then AjaxConfig (= Partial of it) for user
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const ajax1 = new Ajax(config);
|
const ajax1 = new Ajax(config);
|
||||||
const defaultCacheIdentifierFunction = ajax1.options?.cacheOptions?.getCacheIdentifier;
|
|
||||||
|
const defaultCacheIdentifierFunction = /** @type {() => void} */ (
|
||||||
|
ajax1.options?.cacheOptions?.getCacheIdentifier
|
||||||
|
);
|
||||||
// Then
|
// Then
|
||||||
expect(defaultCacheIdentifierFunction).not.to.be.undefined;
|
expect(defaultCacheIdentifierFunction).not.to.be.undefined;
|
||||||
expect(defaultCacheIdentifierFunction).to.be.a('function');
|
expect(defaultCacheIdentifierFunction).to.be.a('function');
|
||||||
|
|
|
||||||
|
|
@ -11,12 +11,12 @@ export interface LionRequestInit extends Omit<RequestInit, 'body'> {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AjaxConfig {
|
export interface AjaxConfig {
|
||||||
addAcceptLanguage: boolean;
|
addAcceptLanguage?: boolean;
|
||||||
addCaching: boolean;
|
addCaching?: boolean;
|
||||||
xsrfCookieName: string | null;
|
xsrfCookieName?: string | null;
|
||||||
xsrfHeaderName: string | null;
|
xsrfHeaderName?: string | null;
|
||||||
cacheOptions: CacheOptionsWithIdentifier;
|
cacheOptions?: CacheOptionsWithIdentifier;
|
||||||
jsonPrefix: string;
|
jsonPrefix?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type RequestInterceptor = (request: Request) => Promise<Request | Response>;
|
export type RequestInterceptor = (request: Request) => Promise<Request | Response>;
|
||||||
|
|
@ -46,7 +46,7 @@ export interface CacheOptions {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CacheOptionsWithIdentifier extends CacheOptions {
|
export interface CacheOptionsWithIdentifier extends CacheOptions {
|
||||||
getCacheIdentifier: () => string;
|
getCacheIdentifier?: () => string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ValidatedCacheOptions extends CacheOptions {
|
export interface ValidatedCacheOptions extends CacheOptions {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue