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
This commit is contained in:
dominikeinkemmer 2019-09-11 23:26:36 +09:00 committed by Mine Starks
parent 8fd7686963
commit 1c9bb251d1
2 changed files with 54 additions and 15 deletions

View File

@ -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);

View File

@ -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 <https://github.com/GausSim>
// Dominik Einkemmer <https://github.com/dominikeinkemmer>
// 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<T>(...args: any[]): Promise<any>;
set<T>(...args: any[]): Promise<any>;
mget?<T>(...args: any[]): Promise<any>;
mset?<T>(...args: any[]): Promise<any>;
del?<T>(...args: any[]): Promise<any>;
setex?<T>(...args: any[]): Promise<any>;
reset?<T>(...args: any[]): Promise<any>;
keys?<T>(...args: any[]): Promise<any>;
ttl?<T>(...args: any[]): Promise<any>;
}
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<T> = (error: any, result: T) => void;
export type WrapArgsType<T> = string | ((callback: CallbackFunc<T>) => void) | CachingConfig | CallbackFunc<T>;
export interface Cache {
set<T>(key: string, value: T, options: CachingConfig, callback?: (error: any) => void): void;
set<T>(key: string, value: T, ttl: number, callback?: (error: any) => void): void;
set<T>(key: string, value: T, options: CachingConfig): Promise<any>;
set<T>(key: string, value: T, ttl: number): Promise<any>;
wrap<T>(key: string, wrapper: (callback: (error: any, result: T) => void) => void, options: CachingConfig, callback: (error: any, result: T) => void): void;
wrap<T>(key: string, wrapper: (callback: (error: any, result: T) => void) => void, callback: (error: any, result: T) => void): void;
wrap<T>(key: string, wrapper: (callback: (error: any, result: T) => void) => any, options: CachingConfig): Promise<any>;
wrap<T>(key: string, wrapper: (callback: (error: any, result: T) => void) => void): Promise<any>;
// 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<T>(...keys: string[], work: (callback: (error: any, result: T) => void) => void, options: CachingConfig, callback: (error: any, result: T) => void): void
// wrap<T>(...keys: string[], work: (callback: (error: any, result: T) => void) => void, callback: (error: any, result: T) => void): void
// wrap<T>(...keys: string[], work: (callback: (error: any, result: T) => void) => void, options: CachingConfig): void
// wrap<T>(...keys: string[], work: (callback: (error: any, result: T) => void) => void): Promise<any>;
wrap<T>(...args: WrapArgsType<T>[]): Promise<any>;
get<T>(key: string, callback: (error: any, result: T) => void): void;
get<T>(key: string): Promise<any>;
@ -35,5 +73,5 @@ export interface Cache {
del(key: string): Promise<any>;
}
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;