mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-13 12:37:16 +08:00
added types for express-redis-cache
This commit is contained in:
105
types/express-redis-cache/express-redis-cache-tests.ts
Normal file
105
types/express-redis-cache/express-redis-cache-tests.ts
Normal 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
83
types/express-redis-cache/index.d.ts
vendored
Normal 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;
|
||||
23
types/express-redis-cache/tsconfig.json
Normal file
23
types/express-redis-cache/tsconfig.json
Normal 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"
|
||||
]
|
||||
}
|
||||
1
types/express-redis-cache/tslint.json
Normal file
1
types/express-redis-cache/tslint.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user