added type definitions for koa-redis-cache

This commit is contained in:
DimaMukhin
2018-08-11 17:39:38 -07:00
parent b3387d6338
commit 6fd3f68820
4 changed files with 150 additions and 0 deletions

93
types/koa-redis-cache/index.d.ts vendored Normal file
View File

@@ -0,0 +1,93 @@
// Type definitions for koa-redis-cache 3.0.1
// Project: https://github.com/coderhaoxin/koa-redis-cache
// Definitions by: Dima Mukhin <https://github.com/dimamukhin>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import * as Koa from "koa";
import * as Redis from "redis";
type onErrorCallback = (error: Error) => void;
type getPrefixCallback = (ctx: Koa.Context) => String;
declare namespace cache {
interface CacheOptions {
/**
* redis key prefix, default is koa-redis-cache:
* If a function is supplied, its signature should be function(ctx) {} and it should return a string to use as the redis key prefix
*/
prefix?: String | getPrefixCallback;
/**
* redis expire time (second), default is 30 * 60 (30 min)
*/
expire?: Number;
/**
* if the passParam exists in the query string, skip the cache
*/
passParam?: String;
/**
* max length of body size (in bytes) to cache.
* if the size of the body exceeds maxLength, the body will not be cached.
* default is: Infinity
*/
maxLength?: Number;
/**
* the routes to cache, default is ['(.*)'].
* can be set to an array of routes (String), or an array of RouteOptions
*/
routes?: Array<RouteOptions> | Array<String>;
/**
* the routes to exclude, default is [].
* example: ['/api/(.*)', '/view/:id']
*/
exclude?: Array<String>;
/**
* callback function for error, default is function() {}
*/
onerror?: onErrorCallback;
/**
* redis options
*/
redis?: RedisOptions;
}
interface RouteOptions {
/**
* the route to cache, example: '/api/(.*)'
*/
route: String;
/**
* expiration time in seconds for cached responses for the route
*/
expire?: Number;
}
interface RedisOptions {
/**
* host name of the redis server, default: 'localhost'
*/
host?: String,
/**
* port number of the redis server, default: 6379
*/
port?: Number,
/**
* node_redis options
*/
options?: Redis.ClientOpts
}
}
declare function cache(opts?: cache.CacheOptions): Koa.Middleware;
export = cache;

View File

@@ -0,0 +1,34 @@
import * as Koa from "koa";
import * as cache from 'koa-redis-cache';
const app = new Koa();
const routeOptions: Array<cache.RouteOptions> = [
{
route: '/api/test',
expire: 60
},
{
route: '/api/users'
}
];
const redisOptions: cache.RedisOptions = {
port: 6379,
host: 'localhost'
};
const options: cache.CacheOptions = {
prefix: (ctx: Koa.Context) => 'koa-redis-cache:',
expire: 30 * 30,
passParam: 'skip',
maxLength: 1024,
routes: routeOptions,
exclude: ['/api/(.*)', '/view/:id'],
onerror: (error: Error) => console.log(error),
redis: redisOptions
};
app.use(cache(options));
app.listen(80);

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",
"koa-redis-cache-tests.ts"
]
}

View File

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