Add types for stale-lru-cache

This commit is contained in:
Joona Heikkilä
2017-05-12 16:23:47 +03:00
parent 96ae5a8560
commit b4dc0dce63
4 changed files with 110 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
// Type definitions for stale-lru-cache 5.1
// Project: https://github.com/cyberthom/stale-lru-cache
// Definitions by: Joona Heikkilä <https://github.com/cxcorp/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare class Cache<K, V> {
constructor(options?: Cache.CacheOptions<K, V>);
delete(key: K): boolean;
get(key: K): V | undefined;
set(key: K, value: V, options?: string | Cache.SetOptions<K, V>): boolean;
has(key: K): boolean;
isStale(key: K): boolean;
keys(): K[];
reset(): void;
size(): number;
values(): V[];
wrap(
key: K,
revalidate: Cache.RevalidationCallback<K, V>,
callback: Cache.OptionsCallback<K, V>
): void;
}
declare namespace Cache {
type OptionsCallback<K, V> = (error: any, value: V, options?: string | SetOptions<K, V>) => void;
type RevalidationCallback<K, V> = (key: K, callback: OptionsCallback<K, V>) => void;
interface CacheOptions<K, V> {
maxAge?: number;
staleWhileRevalidate?: number;
revalidate?: RevalidationCallback<K, V>;
maxSize?: number;
getSize?(value: V, key: K): number;
}
interface SetOptions<K, V> {
maxAge?: number;
staleWhileRevalidate?: number;
revalidate?: RevalidationCallback<K, V>;
}
}
export = Cache;
@@ -0,0 +1,44 @@
import * as Cache from 'stale-lru-cache';
const cache = new Cache<string, string>({
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();
+22
View File
@@ -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"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }