From e36c90fb816f60509bdf3984d4851ad6a63446a7 Mon Sep 17 00:00:00 2001 From: Simon Gausmann Date: Thu, 15 Oct 2015 14:43:40 +0200 Subject: [PATCH 1/3] adding typings for node-cache-manager lib --- cache-manager/cache-manager-tests.ts | 51 ++++++++++++++++++++++++++++ cache-manager/cache-manager.d.ts | 36 ++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 cache-manager/cache-manager-tests.ts create mode 100644 cache-manager/cache-manager.d.ts diff --git a/cache-manager/cache-manager-tests.ts b/cache-manager/cache-manager-tests.ts new file mode 100644 index 0000000000..36d2a559a0 --- /dev/null +++ b/cache-manager/cache-manager-tests.ts @@ -0,0 +1,51 @@ +/// + +import * as cacheManager from 'cache-manager' + +const memoryCache = cacheManager.caching({store: 'memory', max: 100, ttl: 10/*seconds*/}); +const ttl = 5; + +memoryCache.set('foo', 'bar', {ttl: ttl}, (err) => { + + if (err) { + throw err; + } + + memoryCache.get('foo', (err, result) => { + + // console.log(result); + + memoryCache.del('foo', (err) => { + }); + + }); +}); + +function getUser(id:number, cb:Function) { + + cb(null, {id: id, name: 'Bob'}); +} + +const userId = 123; +const key = 'user_' + userId; + +// Note: ttl is optional in wrap() +memoryCache.wrap<{id: number, name: string}>(key, (cb) => { + + getUser(userId, cb); + +}, {ttl: ttl}, (err, user) => { + + //console.log(user); + + // Second time fetches user from memoryCache + memoryCache.wrap<{id: number, name: string}>(key, (cb)=> { + + getUser(userId, cb); + + }, (err, user) => { + + //console.log(user); + + }); +}); \ No newline at end of file diff --git a/cache-manager/cache-manager.d.ts b/cache-manager/cache-manager.d.ts new file mode 100644 index 0000000000..e7cad31740 --- /dev/null +++ b/cache-manager/cache-manager.d.ts @@ -0,0 +1,36 @@ +// Type definitions for cache-manager v1.2.0 +// Project: https://github.com/BryanDonovan/node-cache-manager +// Definitions by: Simon Gausmann www.gausmann-media.de +// Definitions: https://github.com/borisyankov/DefinitelyTyped +declare module 'cache-manager' { + + + interface ICachingConfig { + ttl: number; + } + interface IStoreConfig extends ICachingConfig { + store:string; + max?: number; + isCacheableValue?:(value:any)=>boolean; + } + interface ICache { + set(key:string, value:T, options:ICachingConfig, callback?:(error:any)=>void):void; + set(key:string, value:T, ttl:number, callback?:(error:any)=>void):void; + + wrap(key:string, wrapper:(callback:(error:any, result:T)=>void)=>void, options:ICachingConfig, 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; + + get(key:string, callback:(error:any, result:T)=>void):void; + + del(key:string, callback?:(error:any)=>void):void; + } + + + module cacheManager { + function caching(ICongig:IStoreConfig):ICache; + + function multiCaching(Caches:ICache[]):ICache; + } + + export = cacheManager; +} \ No newline at end of file From d1b97507ac1b9097e48a8cf2abd23f1a5c0c38d1 Mon Sep 17 00:00:00 2001 From: Simon Gausmann Date: Thu, 15 Oct 2015 15:05:49 +0200 Subject: [PATCH 2/3] website in comments --- cache-manager/cache-manager.d.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cache-manager/cache-manager.d.ts b/cache-manager/cache-manager.d.ts index e7cad31740..d912f9f7bd 100644 --- a/cache-manager/cache-manager.d.ts +++ b/cache-manager/cache-manager.d.ts @@ -1,6 +1,6 @@ // Type definitions for cache-manager v1.2.0 // Project: https://github.com/BryanDonovan/node-cache-manager -// Definitions by: Simon Gausmann www.gausmann-media.de +// Definitions by: Simon Gausmann // Definitions: https://github.com/borisyankov/DefinitelyTyped declare module 'cache-manager' { @@ -26,6 +26,7 @@ declare module 'cache-manager' { } + module cacheManager { function caching(ICongig:IStoreConfig):ICache; @@ -33,4 +34,4 @@ declare module 'cache-manager' { } export = cacheManager; -} \ No newline at end of file +} From 1fbadc073327fd627065c772509069b23c755d30 Mon Sep 17 00:00:00 2001 From: Simon Gausmann Date: Wed, 21 Oct 2015 20:45:16 +0200 Subject: [PATCH 3/3] removing I from Interface-Names --- cache-manager/cache-manager-tests.ts | 16 ++++++++-------- cache-manager/cache-manager.d.ts | 27 +++++++++++++-------------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/cache-manager/cache-manager-tests.ts b/cache-manager/cache-manager-tests.ts index 36d2a559a0..36eeb4c141 100644 --- a/cache-manager/cache-manager-tests.ts +++ b/cache-manager/cache-manager-tests.ts @@ -2,10 +2,10 @@ import * as cacheManager from 'cache-manager' -const memoryCache = cacheManager.caching({store: 'memory', max: 100, ttl: 10/*seconds*/}); +const memoryCache = cacheManager.caching({ store: 'memory', max: 100, ttl: 10/*seconds*/ }); const ttl = 5; -memoryCache.set('foo', 'bar', {ttl: ttl}, (err) => { +memoryCache.set('foo', 'bar', { ttl: ttl }, (err) => { if (err) { throw err; @@ -21,25 +21,25 @@ memoryCache.set('foo', 'bar', {ttl: ttl}, (err) => { }); }); -function getUser(id:number, cb:Function) { +function getUser(id: number, cb: Function) { - cb(null, {id: id, name: 'Bob'}); + cb(null, { id: id, name: 'Bob' }); } const userId = 123; const key = 'user_' + userId; // Note: ttl is optional in wrap() -memoryCache.wrap<{id: number, name: string}>(key, (cb) => { +memoryCache.wrap<{ id: number, name: string }>(key, (cb) => { getUser(userId, cb); -}, {ttl: ttl}, (err, user) => { +}, { ttl: ttl }, (err, user) => { //console.log(user); // Second time fetches user from memoryCache - memoryCache.wrap<{id: number, name: string}>(key, (cb)=> { + memoryCache.wrap<{ id: number, name: string }>(key, (cb) => { getUser(userId, cb); @@ -48,4 +48,4 @@ memoryCache.wrap<{id: number, name: string}>(key, (cb) => { //console.log(user); }); -}); \ No newline at end of file +}); diff --git a/cache-manager/cache-manager.d.ts b/cache-manager/cache-manager.d.ts index d912f9f7bd..9570857f01 100644 --- a/cache-manager/cache-manager.d.ts +++ b/cache-manager/cache-manager.d.ts @@ -5,32 +5,31 @@ declare module 'cache-manager' { - interface ICachingConfig { + interface CachingConfig { ttl: number; } - interface IStoreConfig extends ICachingConfig { - store:string; + interface StoreConfig extends CachingConfig { + store: string; max?: number; - isCacheableValue?:(value:any)=>boolean; + isCacheableValue?: (value: any) => boolean; } - interface ICache { - set(key:string, value:T, options:ICachingConfig, callback?:(error:any)=>void):void; - set(key:string, value:T, ttl:number, callback?:(error:any)=>void):void; + 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; - wrap(key:string, wrapper:(callback:(error:any, result:T)=>void)=>void, options:ICachingConfig, 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) => 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; - get(key:string, callback:(error:any, result:T)=>void):void; + get(key: string, callback: (error: any, result: T) => void): void; - del(key:string, callback?:(error:any)=>void):void; + del(key: string, callback?: (error: any) => void): void; } module cacheManager { - function caching(ICongig:IStoreConfig):ICache; - - function multiCaching(Caches:ICache[]):ICache; + function caching(ICongig: StoreConfig): Cache; + function multiCaching(Caches: Cache[]): Cache; } export = cacheManager;