ng-http-caching
Version:
Cache for HTTP requests in Angular application.
228 lines (227 loc) • 8.8 kB
TypeScript
import { InjectionToken } from '@angular/core';
import { HttpRequest, HttpResponse, HttpEvent, HttpContextToken, HttpContext, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/internal/Observable';
import { NgHttpCachingStorageInterface } from './storage/ng-http-caching-storage.interface';
import * as i0 from "@angular/core";
export type NgHttpCachingContext = Pick<NgHttpCachingConfig, 'getKey' | 'isCacheable' | 'isExpired' | 'isValid'>;
export declare const NG_HTTP_CACHING_CONTEXT: HttpContextToken<NgHttpCachingContext>;
export declare const withNgHttpCachingContext: (value: NgHttpCachingContext, context?: HttpContext) => HttpContext;
export declare const checkCacheHeaders: (headers: HttpHeaders) => boolean;
export interface NgHttpCachingEntry<K = any, T = any> {
/**
* URL
*/
url: string;
/**
* HttpResponse
*/
response: HttpResponse<T>;
/**
* HttpRequest
*/
request: HttpRequest<K>;
/**
* Timestamp of add to cache time
*/
addedTime: number;
/**
* Cache version
*/
version: string;
}
export declare const NG_HTTP_CACHING_CONFIG: InjectionToken<NgHttpCachingContext>;
export declare enum NgHttpCachingStrategy {
/**
* All request are cacheable if HTTP method is into `allowedMethod`
*/
ALLOW_ALL = "ALLOW_ALL",
/**
* Only the request with `X-NG-HTTP-CACHING-ALLOW-CACHE` header are cacheable if HTTP method is into `allowedMethod`
*/
DISALLOW_ALL = "DISALLOW_ALL"
}
export declare enum NgHttpCachingHeaders {
/**
* Request is cacheable if HTTP method is into `allowedMethod`
*/
ALLOW_CACHE = "X-NG-HTTP-CACHING-ALLOW-CACHE",
/**
* Request isn't cacheable
*/
DISALLOW_CACHE = "X-NG-HTTP-CACHING-DISALLOW-CACHE",
/**
* Specific cache lifetime for the request
*/
LIFETIME = "X-NG-HTTP-CACHING-LIFETIME",
/**
* You can tag multiple request by adding this header with the same tag and
* using `NgHttpCachingService.clearCacheByTag(tag: string)` for delete all the tagged request
*/
TAG = "X-NG-HTTP-CACHING-TAG"
}
export declare const NgHttpCachingHeadersList: NgHttpCachingHeaders[];
export declare const NG_HTTP_CACHING_SECOND_IN_MS = 1000;
export declare const NG_HTTP_CACHING_MINUTE_IN_MS: number;
export declare const NG_HTTP_CACHING_HOUR_IN_MS: number;
export declare const NG_HTTP_CACHING_DAY_IN_MS: number;
export declare const NG_HTTP_CACHING_WEEK_IN_MS: number;
export declare const NG_HTTP_CACHING_MONTH_IN_MS: number;
export declare const NG_HTTP_CACHING_YEAR_IN_MS: number;
export interface NgHttpCachingConfig {
/**
* Set the cache store. You can implement your custom store by implement the `NgHttpCachingStorageInterface` interface, eg.:
*/
store?: NgHttpCachingStorageInterface;
/**
* Number of millisecond that a response is stored in the cache.
* You can set specific "lifetime" for each request by add the header `X-NG-HTTP-CACHING-LIFETIME` (see example below).
*/
lifetime?: number;
/**
* Array of allowed HTTP methods to cache.
* You can allow multiple methods, eg.: `['GET', 'POST', 'PUT', 'DELETE', 'HEAD']` or
* allow all methods by: `['ALL']`. If `allowedMethod` is an empty array (`[]`), no response are cached.
* *Warning!* `NgHttpCaching` use the full url (url with query parameters) as unique key for the cached response,
* this is correct for the `GET` request but is _potentially_ wrong for other type of request (eg. `POST`, `PUT`).
* You can set a different "key" by customizing the `getKey` config method (see `getKey` section).
*/
allowedMethod?: string[];
/**
* Set the cache strategy, possible strategies are:
* - `NgHttpCachingStrategy.ALLOW_ALL`: All request are cacheable if HTTP method is into `allowedMethod`;
* - `NgHttpCachingStrategy.DISALLOW_ALL`: Only the request with `X-NG-HTTP-CACHING-ALLOW-CACHE` header are cacheable if HTTP method is into `allowedMethod`;
*/
cacheStrategy?: NgHttpCachingStrategy;
/**
* Cache version. When you have a breaking change, change the version, and it'll delete the current cache automatically.
* The default value is Angular major version (eg. 13), in this way, the cache is invalidated on every Angular upgrade.
*/
version?: string;
/**
* If true response headers cache-control and expires are respected.
*/
checkResponseHeaders?: boolean;
/**
* If this function return `true` the request is expired and a new request is send to backend, if return `false` isn't expired.
* If the result is `undefined`, the normal behaviour is provided.
*/
isExpired?: <K, T>(entry: NgHttpCachingEntry<K, T>) => boolean | undefined | void;
/**
* If this function return `true` the request is cacheable, if return `false` isn't cacheable.
* If the result is `undefined`, the normal behaviour is provided.
*/
isCacheable?: <K>(req: HttpRequest<K>) => boolean | undefined | void;
/**
* This function return the unique key (`string`) for store the response into the cache.
* If the result is `undefined`, the normal behaviour is provided.
*/
getKey?: <K>(req: HttpRequest<K>) => string | undefined | void;
/**
* If this function return `true` the cache entry is valid and can be stored, if return `false` isn't valid.
* If the result is `undefined`, the normal behaviour is provided.
*/
isValid?: <K, T>(entry: NgHttpCachingEntry<K, T>) => boolean | undefined | void;
}
export interface NgHttpCachingDefaultConfig extends NgHttpCachingConfig {
store: NgHttpCachingStorageInterface;
lifetime: number;
allowedMethod: string[];
cacheStrategy: NgHttpCachingStrategy;
version: string;
checkResponseHeaders: boolean;
}
export declare const NgHttpCachingConfigDefault: Readonly<NgHttpCachingDefaultConfig>;
export declare class NgHttpCachingService {
private readonly queue;
private readonly config;
private gcLock;
private devMode;
constructor();
/**
* Return the config
*/
getConfig(): Readonly<NgHttpCachingConfig>;
/**
* Return the queue map
*/
getQueue(): Readonly<Map<string, Observable<HttpEvent<any>>>>;
/**
* Return the cache store
*/
getStore(): Readonly<NgHttpCachingStorageInterface>;
/**
* Return response from cache
*/
getFromCache<K, T>(req: HttpRequest<K>): Readonly<HttpResponse<T>> | undefined;
/**
* Add response to cache
*/
addToCache<K, T>(req: HttpRequest<K>, res: HttpResponse<T>): boolean;
/**
* Delete response from cache
*/
deleteFromCache<K>(req: HttpRequest<K>): boolean;
/**
* Clear the cache
*/
clearCache(): void;
/**
* Clear the cache by key
*/
clearCacheByKey(key: string): boolean;
/**
* Clear the cache by keys
*/
clearCacheByKeys(keys: Array<string>): number;
/**
* Clear the cache by regex
*/
clearCacheByRegex<K, T>(regex: RegExp): number;
/**
* Clear the cache by TAG
*/
clearCacheByTag<K, T>(tag: string): number;
/**
* Run garbage collector (delete expired cache entry)
*/
runGc<K, T>(): boolean;
/**
* Return true if cache entry is expired
*/
isExpired<K, T>(entry: NgHttpCachingEntry<K, T>): boolean;
/**
* Return true if cache entry is valid for store in the cache
* Default behaviour is whether the status code falls in the 2xx range and response headers cache-control and expires allow cache.
*/
isValid<K, T>(entry: NgHttpCachingEntry<K, T>): boolean;
/**
* Return true if the request is cacheable
*/
isCacheable<K>(req: HttpRequest<K>): boolean;
/**
* Return the cache key.
* Default key is http method plus url with query parameters, eg.:
* `GET@https://github.com/nigrosimone/ng-http-caching`
*/
getKey<K>(req: HttpRequest<K>): string;
/**
* Return observable from cache
*/
getFromQueue<K, T>(req: HttpRequest<K>): Observable<HttpEvent<T>> | undefined;
/**
* Add observable to cache
*/
addToQueue<K, T>(req: HttpRequest<K>, obs: Observable<HttpEvent<T>>): void;
/**
* Delete observable from cache
*/
deleteFromQueue<K>(req: HttpRequest<K>): boolean;
/**
* Recursively Object.freeze simple Javascript structures consisting of plain objects, arrays, and primitives.
* Make the data immutable.
* @returns immutable object
*/
private deepFreeze;
static ɵfac: i0.ɵɵFactoryDeclaration<NgHttpCachingService, never>;
static ɵprov: i0.ɵɵInjectableDeclaration<NgHttpCachingService>;
}