@obliczeniowo/elementary
Version:
Library made in Angular version 20
120 lines (114 loc) • 4.26 kB
TypeScript
import { HttpClient, HttpHeaders, HttpContext, HttpParams, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
import { Subject, Observable } from 'rxjs';
import * as i0 from '@angular/core';
interface CacheOptions<Res = any, Ext = any> {
/**
* collect - collecting response body as table, maxSize parameter controls max size of table
* reload - force to reload cache (send request)
*/
type?: 'collect' | 'reload';
/**
* Work with type='collect'
*/
maxSize?: number;
data?: any;
name?: string;
external?: Ext;
withCacheItem?: boolean;
/** list of keys to clear */
clear?: string[];
/** list of keys to retrigger */
retrigger?: string[];
convert?: (data: Res) => any;
reloadOn?: (data: CacheItem<Res, Ext>) => boolean;
}
declare class CacheModel<Res = any, Ext = any> {
options?: CacheOptions<Res, Ext> | undefined;
constructor(options?: CacheOptions<Res, Ext> | undefined);
}
interface CacheItem<Res, Ext = any> {
body: Res;
reqBody?: any;
triggeredAt: Date;
method: 'get' | 'post' | 'put' | 'patch' | 'delete' | string;
responseAt?: Date;
cache?: CacheOptions;
fullPath: string;
external?: Ext;
reloading?: true;
}
declare class CacheService {
protected cache: Map<string, CacheItem<any, any>>;
/**
* triggered when cache name value is changed or removed, not triggered when all cache was cleared
*/
changed: Subject<{
name: string;
item?: CacheItem<any>;
}>;
/**
* triggered when one name of cache was cleared (emit string as name of removed one) or all was cleared (emit undefined)
*/
cleared: Subject<string | undefined>;
set(name: string, item: CacheItem<any>): void;
get(name: string): CacheItem<any> | undefined;
has(name: string): boolean;
/**
* when no name clear all, single one when name is known
*/
clear(name?: string): void;
static ɵfac: i0.ɵɵFactoryDeclaration<CacheService, never>;
static ɵprov: i0.ɵɵInjectableDeclaration<CacheService>;
}
interface HttpClientOptions {
body?: any;
headers?: HttpHeaders | {
[header: string]: string | string[];
};
context?: HttpContext;
observe?: 'body';
params?: HttpParams | {
[param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>;
};
responseType?: 'json';
reportProgress?: boolean;
withCredentials?: boolean;
cache?: CacheOptions;
}
declare class HttpCacheService {
http: HttpClient;
constructor(http: HttpClient);
request<R>(method: string, url: string, options?: {
body?: any;
headers?: HttpHeaders | {
[header: string]: string | string[];
};
context?: HttpContext;
observe?: 'body';
params?: HttpParams | {
[param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>;
};
responseType?: 'json';
reportProgress?: boolean;
withCredentials?: boolean;
cache?: CacheOptions;
}): Observable<R>;
get<R>(url: string, options?: HttpClientOptions): Observable<R>;
patch<R>(url: string, body: any, options?: HttpClientOptions): Observable<R>;
post<R>(url: string, body: any, options?: HttpClientOptions): Observable<R>;
put<R>(url: string, body: any, options?: HttpClientOptions): Observable<R>;
delete<R>(url: string, options?: HttpClientOptions): Observable<R>;
retrigger(cacheItem: CacheItem<any> | undefined): void;
static ɵfac: i0.ɵɵFactoryDeclaration<HttpCacheService, never>;
static ɵprov: i0.ɵɵInjectableDeclaration<HttpCacheService>;
}
declare class CacheInterceptorService {
private readonly cache;
private readonly http;
constructor(cache: CacheService, http: HttpCacheService);
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>;
static ɵfac: i0.ɵɵFactoryDeclaration<CacheInterceptorService, never>;
static ɵprov: i0.ɵɵInjectableDeclaration<CacheInterceptorService>;
}
export { CacheInterceptorService, CacheModel, CacheService, HttpCacheService };
export type { CacheItem, CacheOptions, HttpClientOptions };