added types for express-redis-cache

This commit is contained in:
AJ Livingston
2018-06-12 13:38:08 -04:00
parent 3b8b13474e
commit 14568e033b
4 changed files with 212 additions and 0 deletions

View File

@@ -0,0 +1,105 @@
import * as express from 'express';
import * as redis from 'redis';
import * as expressRedisCache from 'express-redis-cache';
const app = express();
const cache = expressRedisCache();
expressRedisCache({ host: 'localhsot', port: 6379, auth_pass: 'passw0rd' });
expressRedisCache({ client: redis.createClient() });
cache.on('error', (error) => {
throw new Error('Cache error!');
});
app.get('/',
cache.route(), // cache entry name is `cache.prefix + "/"`
(req, res, next) => { });
app.get('/',
cache.route('home'), // cache entry name is now `cache.prefix + "home"`
(req, res, next) => { });
app.get('/',
cache.route({ name: 'home' }), // cache entry name is `cache.prefix + "home"`
(req, res, next) => { });
app.get('/user/:userid',
// middleware to define cache name
(req, res, next) => {
// set cache name
res.express_redis_cache_name = 'user-' + req.params.userid;
next();
},
// cache middleware
cache.route(),
// content middleware
(req, res) => {
res.render('user');
}
);
app.get('/user',
// middleware to decide if using cache
(req, res, next) => {
// Use only cache if user not signed in
res.use_express_redis_cache = !req.signedCookies.user;
next();
},
cache.route(), // this will be skipped if user is signed in
(req, res) => {
res.render('user');
}
);
// Prefix
expressRedisCache({ prefix: 'test' });
// Expiration
expressRedisCache({ expire: 60 });
app.get('/index.html',
cache.route({ expire: 5000 }), // cache entry will live 5000 seconds
(req, res) => { }
);
// You can also use the number sugar syntax
cache.route(5000);
// Or
cache.route('index', 5000);
app.get('/index.html',
cache.route({
expire: {
200: 5000,
'4xx': 10,
403: 5000,
'5xx': 10,
xxx: 1
}
}),
(req, res) => { }
);
cache.get((error, entries) => {
if (error) throw error;
entries.forEach(console.log);
});
cache.get('home', (error, entries) => {});
cache.add(
'user:info',
JSON.stringify({ id: 1, email: 'john@doe.com' }),
{ expire: 60 * 60 * 24, type: 'json' },
(error, added) => {}
);
cache.add(
'user:info',
JSON.stringify({ id: 1, email: 'john@doe.com' }),
(error, added) => {}
);
cache.del('home', (error, deleted) => {});
cache.size((error, bytes) => {});

83
types/express-redis-cache/index.d.ts vendored Normal file
View File

@@ -0,0 +1,83 @@
// Type definitions for express-redis-cache 1.1.1
// Project: https://github.com/rv-kip/express-redis-cache
// Definitions by: AJ Livingston <https://github.com/ajliv>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
import { EventEmitter } from 'events';
import { RequestHandler } from 'express';
import { RedisClient } from 'redis';
declare module 'express-serve-static-core' {
interface Response {
express_redis_cache_name?: string;
use_express_redis_cache?: boolean;
}
}
declare function expressRedisCache(options?: expressRedisCache.Options): expressRedisCache.ExpressRedisCache;
declare namespace expressRedisCache {
class ExpressRedisCache extends EventEmitter {
constructor(options?: Options);
static init(options?: Options): ExpressRedisCache;
readonly FOREVER: number;
options: Options;
host: string;
port: string | number;
prefix: string;
auth_pass: string;
connected: boolean;
expire: number;
client: RedisClient;
add(name: string, body: string, options: AddOptions, callback: (error: any, added: Entry) => void): void;
add(name: string, body: string, callback: (error: any, added: Entry) => void): void;
del(name: string, callback: (error: any, deleted: number) => void): void;
get(name: string, callback: (error: any, entries: Entry[]) => void): void;
get(callback: (error: any, entries: Entry[]) => void): void;
route(options: RouteOptions, expire?: ExpireOption): RequestHandler;
route(name: string, expire?: ExpireOption): RequestHandler;
route(expire: number): RequestHandler;
route(): RequestHandler;
size(callback: (error: any, bytes: number) => void): void;
}
interface AddOptions {
type?: string;
expire?: number;
}
interface Entry {
body: string;
touched: number;
expire: number;
type: string;
}
interface ExpirationConfig {
[statusCode: string]: number;
[statusCode: number]: number;
}
type ExpireOption = number | ExpirationConfig;
interface ExpirationPolicy {
(req: Request, res: Response): number;
}
interface Options {
auth_pass?: string;
client?: RedisClient;
expire?: number;
host?: string;
port?: string | number;
prefix?: string;
}
interface RouteOptions {
name?: string;
expire?: ExpireOption | ExpirationPolicy;
binary?: boolean;
}
}
export = expressRedisCache;

View File

@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"express-redis-cache-tests.ts"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }