express-aggressive-cache
Version:
An aggressive yet obedient cache middleware for express
93 lines (92 loc) • 2.83 kB
TypeScript
/// <reference types="node" />
import http from "http";
import { Request, Response } from "express";
export declare type DefaultGetCacheKey = (args: {
req: Request;
}) => string;
declare type GetCacheKeySync = (args: {
req: Request;
res: Response;
normalizedPath: string;
}) => string;
declare type GetCacheKeyAsync = (args: {
req: Request;
res: Response;
normalizedPath: string;
}) => Promise<string>;
export declare type GetCacheKey = GetCacheKeySync | GetCacheKeyAsync;
declare type GetCacheTagSync = (args: {
req: Request;
res: Response;
}) => string | undefined;
declare type GetCacheTagAsync = (args: {
req: Request;
res: Response;
}) => Promise<string | undefined>;
export declare type GetCacheTag = GetCacheTagSync | GetCacheTagAsync;
declare type OnCacheSync = (args: {
req: Request;
res: Response;
}) => void;
declare type OnCacheAsync = (args: {
req: Request;
res: Response;
}) => Promise<void>;
export declare type OnCache = OnCacheSync | OnCacheAsync;
export declare type Chunk = string | Buffer;
export declare type PurgeFunction = (tag: string) => Promise<void>;
export interface Options {
/**
* If the response has a max-age header, it will use it as the TTL.
* Value should be provided in seconds.
* Otherwise, it will expire the resource using the maxAge option (defaults to Infinity).
*/
maxAge?: number;
/**
* Specify a different data store. Default to in-memory caching.
*/
store?: <T>() => Store<T>;
/**
* Function used to generate cache keys.
*/
getCacheKey?: GetCacheKey;
/**
* Function to obtain the purge tag which will be associated to the cache entry.
* It should return undefined if there is no cache tag for the response.
*/
getCacheTag?: GetCacheTag;
/**
* Function to perform a behavior on a cache hit. For example: set a response header.
*/
onCacheHit?: OnCache;
/**
* Function to perform a behavior on a cache miss. For example: set a response header.
*/
onCacheMiss?: OnCache;
/**
* A flag to toggle debug logs
* Defaults to false.
*/
debug?: boolean;
}
export interface CachedResponse {
requestId: string;
chunks: string[];
statusCode: number;
headers: http.OutgoingHttpHeaders;
maxAge: number | undefined;
isSealed: boolean;
}
export interface Store<T> {
del: (key: string) => Promise<void>;
expire: (key: string, seconds: number) => Promise<void>;
get: (key: string) => Promise<T | undefined>;
has: (keys: string[]) => Promise<boolean>;
set: (key: string, value: T, maxAge?: number | undefined) => Promise<void>;
}
export interface ExtendedResponse extends Response {
aggressiveCache?: {
chunks?: string[];
};
}
export {};