diff --git a/types/stale-lru-cache/index.d.ts b/types/stale-lru-cache/index.d.ts new file mode 100644 index 0000000000..f5ab3733b3 --- /dev/null +++ b/types/stale-lru-cache/index.d.ts @@ -0,0 +1,43 @@ +// Type definitions for stale-lru-cache 5.1 +// Project: https://github.com/cyberthom/stale-lru-cache +// Definitions by: Joona Heikkilä +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare class Cache { + constructor(options?: Cache.CacheOptions); + delete(key: K): boolean; + get(key: K): V | undefined; + set(key: K, value: V, options?: string | Cache.SetOptions): boolean; + has(key: K): boolean; + isStale(key: K): boolean; + keys(): K[]; + reset(): void; + size(): number; + values(): V[]; + wrap( + key: K, + revalidate: Cache.RevalidationCallback, + callback: Cache.OptionsCallback + ): void; +} + +declare namespace Cache { + type OptionsCallback = (error: any, value: V, options?: string | SetOptions) => void; + type RevalidationCallback = (key: K, callback: OptionsCallback) => void; + + interface CacheOptions { + maxAge?: number; + staleWhileRevalidate?: number; + revalidate?: RevalidationCallback; + maxSize?: number; + getSize?(value: V, key: K): number; + } + + interface SetOptions { + maxAge?: number; + staleWhileRevalidate?: number; + revalidate?: RevalidationCallback; + } +} + +export = Cache; diff --git a/types/stale-lru-cache/stale-lru-cache-tests.ts b/types/stale-lru-cache/stale-lru-cache-tests.ts new file mode 100644 index 0000000000..c1364baa83 --- /dev/null +++ b/types/stale-lru-cache/stale-lru-cache-tests.ts @@ -0,0 +1,44 @@ +import * as Cache from 'stale-lru-cache'; + +const cache = new Cache({ + maxSize: 100, + maxAge: 600, + staleWhileRevalidate: 86400, + revalidate: (key, callback) => { + callback(null, 'Value of ' + key); + } +}); + +const success: boolean = cache.set('key', 'value'); // true +const value: string = cache.get('key') || 'backup'; // 'value' +const deleteSuccess: boolean = cache.delete('key'); +const has: boolean = cache.has('key'); +const isStale: boolean = cache.isStale('key'); +const keys: string[] = cache.keys(); +cache.reset(); + +cache.set('foo', 'bar'); + +// Get response from cache +const revalidate = (url: string, callback: (error: any, value?: string, options?: string) => any) => { + request(url, (error, response, html) => { + if (error) return callback(error); + callback(null, html, response.headers['cache-control']); + }); +}; + +let request: (url: string, cb: (error: any, response: any, html: string) => any) => any; +cache.wrap('http://www.google.com', revalidate, (error, html) => { + // Do something with cached response +}); + +cache.set('key', 'value'); // true + +cache.set('key', 'value', { maxAge: 600, staleWhileRevalidate: 86400 }); // true +cache.set('key', 'value', { maxAge: 0 }); // false + +cache.set('key', 'value', 'max-age=600, stale-while-revalidate=86400'); // true +cache.set('key', 'value', 'no-cache, no-store, must-revalidate'); // false + +const size: number = cache.size(); +const values: string[] = cache.values(); diff --git a/types/stale-lru-cache/tsconfig.json b/types/stale-lru-cache/tsconfig.json new file mode 100644 index 0000000000..ccd4edb516 --- /dev/null +++ b/types/stale-lru-cache/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "stale-lru-cache-tests.ts" + ] +} diff --git a/types/stale-lru-cache/tslint.json b/types/stale-lru-cache/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/stale-lru-cache/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }