From 1c9bb251d1339ce2fddbdd37766711a985fd7929 Mon Sep 17 00:00:00 2001 From: dominikeinkemmer Date: Wed, 11 Sep 2019 23:26:36 +0900 Subject: [PATCH] update typings for newest cache-manager version (#37824) * fix: update typings for newest cache-manager version fix: update typings for newest cache-manager version * update StoreConfig --- types/cache-manager/cache-manager-tests.ts | 9 ++-- types/cache-manager/index.d.ts | 60 ++++++++++++++++++---- 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/types/cache-manager/cache-manager-tests.ts b/types/cache-manager/cache-manager-tests.ts index d8ab154aa1..47ca62c020 100644 --- a/types/cache-manager/cache-manager-tests.ts +++ b/types/cache-manager/cache-manager-tests.ts @@ -26,22 +26,23 @@ function getUser(id: number, cb: Function) { const userId = 123; const key = 'user_' + userId; +const key2 = 'user_' + userId + '4'; // Note: ttl is optional in wrap() -memoryCache.wrap<{ id: number, name: string }>(key, (cb) => { +memoryCache.wrap<{ id: number, name: string }>(key, (cb: any) => { getUser(userId, cb); -}, { ttl: ttl }, (err, user) => { +}, { ttl: ttl }, (err: any, user: { id: number, name: string }) => { //console.log(user); // Second time fetches user from memoryCache - memoryCache.wrap<{ id: number, name: string }>(key, (cb) => { + memoryCache.wrap<{ id: number, name: string }>(key, key2, (cb: any) => { getUser(userId, cb); - }, (err, user) => { + }, (err: any, user: { id: number, name: string }) => { //console.log(user); diff --git a/types/cache-manager/index.d.ts b/types/cache-manager/index.d.ts index 91d4e73458..9fb162084d 100644 --- a/types/cache-manager/index.d.ts +++ b/types/cache-manager/index.d.ts @@ -1,7 +1,9 @@ -// Type definitions for cache-manager v1.2.0 +// 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; @@ -11,22 +13,58 @@ export interface TtlFunction { (result: any): number; } -export interface StoreConfig extends CachingConfig { - store: string; - max?: number; - isCacheableValue?: (value: any) => boolean; +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; - wrap(key: string, wrapper: (callback: (error: any, result: T) => void) => void, options: CachingConfig, callback: (error: any, result: T) => void): void; - wrap(key: string, wrapper: (callback: (error: any, result: T) => void) => void, callback: (error: any, result: T) => void): void; - wrap(key: string, wrapper: (callback: (error: any, result: T) => void) => any, options: CachingConfig): Promise; - wrap(key: string, wrapper: (callback: (error: any, result: T) => void) => void): 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; @@ -35,5 +73,5 @@ export interface Cache { del(key: string): Promise; } -export function caching(IConfig: StoreConfig): Cache; -export function multiCaching(Caches: Cache[]): Cache; +export function caching(IConfig: StoreConfig & CacheOptions): Cache; +export function multiCaching(Caches: Cache[], options?: CacheOptions): Cache;