// Type definitions for cache-manager v2.10.0 // Project: https://github.com/BryanDonovan/node-cache-manager // Definitions by: Simon Gausmann // Dominik Einkemmer // Definitions: https://github.com/borisyankov/DefinitelyTyped // TypeScript Version: 2.8 export interface CachingConfig { ttl: number | TtlFunction; } export interface TtlFunction { (result: any): number; } export interface Store { // These functions will just be bound to the Cache object if they exist so args can be anything get(...args: any[]): Promise; set(...args: any[]): Promise; mget?(...args: any[]): Promise; mset?(...args: any[]): Promise; del?(...args: any[]): Promise; setex?(...args: any[]): Promise; reset?(...args: any[]): Promise; keys?(...args: any[]): Promise; ttl?(...args: any[]): Promise; } export interface StoreConfig extends CachingConfig { store: 'memory' | 'none' | Store | { create(...args: any[]): Store; }; max?: number; /** * You may pass in any other arguments these will be passed on to the `create` method of your store, * otherwise they will be ignored. */ [key: string]: any; } export interface CacheOptions { /** * Promise library to replace global.Promise */ promiseDependency?: any; isCacheableValue?(value: any): boolean; } export type CallbackFunc = (error: any, result: T) => void; export type WrapArgsType = string | ((callback: CallbackFunc) => void) | CachingConfig | CallbackFunc; export interface Cache { set(key: string, value: T, options: CachingConfig, callback?: (error: any) => void): void; set(key: string, value: T, ttl: number, callback?: (error: any) => void): void; set(key: string, value: T, options: CachingConfig): Promise; set(key: string, value: T, ttl: number): Promise; // Because the library accepts multiple keys as arguments but not as an array and rather as individual parameters // of the function, the type definition had to be changed to this rather than specific ones // actual definitions would looks like this (impossible in typescript): // wrap(...keys: string[], work: (callback: (error: any, result: T) => void) => void, options: CachingConfig, callback: (error: any, result: T) => void): void // wrap(...keys: string[], work: (callback: (error: any, result: T) => void) => void, callback: (error: any, result: T) => void): void // wrap(...keys: string[], work: (callback: (error: any, result: T) => void) => void, options: CachingConfig): void // wrap(...keys: string[], work: (callback: (error: any, result: T) => void) => void): Promise; wrap(...args: WrapArgsType[]): Promise; get(key: string, callback: (error: any, result: T) => void): void; get(key: string): Promise; del(key: string, callback: (error: any) => void): void; del(key: string): Promise; } export function caching(IConfig: StoreConfig & CacheOptions): Cache; export function multiCaching(Caches: Cache[], options?: CacheOptions): Cache;