From 6fd3f6882094c7230a4b5ff2b60d43cc765c2a07 Mon Sep 17 00:00:00 2001 From: DimaMukhin Date: Sat, 11 Aug 2018 17:39:38 -0700 Subject: [PATCH 1/2] added type definitions for koa-redis-cache --- types/koa-redis-cache/index.d.ts | 93 +++++++++++++++++++ .../koa-redis-cache/koa-redis-cache-tests.ts | 34 +++++++ types/koa-redis-cache/tsconfig.json | 22 +++++ types/koa-redis-cache/tslint.json | 1 + 4 files changed, 150 insertions(+) create mode 100644 types/koa-redis-cache/index.d.ts create mode 100644 types/koa-redis-cache/koa-redis-cache-tests.ts create mode 100644 types/koa-redis-cache/tsconfig.json create mode 100644 types/koa-redis-cache/tslint.json diff --git a/types/koa-redis-cache/index.d.ts b/types/koa-redis-cache/index.d.ts new file mode 100644 index 0000000000..2d983f862d --- /dev/null +++ b/types/koa-redis-cache/index.d.ts @@ -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 +// 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 | Array; + + /** + * the routes to exclude, default is []. + * example: ['/api/(.*)', '/view/:id'] + */ + exclude?: Array; + + /** + * 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; diff --git a/types/koa-redis-cache/koa-redis-cache-tests.ts b/types/koa-redis-cache/koa-redis-cache-tests.ts new file mode 100644 index 0000000000..9c5f528ba7 --- /dev/null +++ b/types/koa-redis-cache/koa-redis-cache-tests.ts @@ -0,0 +1,34 @@ +import * as Koa from "koa"; +import * as cache from 'koa-redis-cache'; + +const app = new Koa(); + +const routeOptions: Array = [ + { + 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); diff --git a/types/koa-redis-cache/tsconfig.json b/types/koa-redis-cache/tsconfig.json new file mode 100644 index 0000000000..dbf782ccae --- /dev/null +++ b/types/koa-redis-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", + "koa-redis-cache-tests.ts" + ] +} \ No newline at end of file diff --git a/types/koa-redis-cache/tslint.json b/types/koa-redis-cache/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/koa-redis-cache/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 8586e357ea6edf12d7484811ae86dccc98e2fec8 Mon Sep 17 00:00:00 2001 From: DimaMukhin Date: Sat, 11 Aug 2018 18:08:17 -0700 Subject: [PATCH 2/2] fixed failing tests --- types/koa-redis-cache/index.d.ts | 29 ++++++++++--------- .../koa-redis-cache/koa-redis-cache-tests.ts | 2 +- types/koa-redis-cache/tsconfig.json | 1 + 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/types/koa-redis-cache/index.d.ts b/types/koa-redis-cache/index.d.ts index 2d983f862d..58f7f120ca 100644 --- a/types/koa-redis-cache/index.d.ts +++ b/types/koa-redis-cache/index.d.ts @@ -1,14 +1,15 @@ -// Type definitions for koa-redis-cache 3.0.1 +// Type definitions for koa-redis-cache 3.0 // Project: https://github.com/coderhaoxin/koa-redis-cache // Definitions by: Dima Mukhin // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 import * as Koa from "koa"; import * as Redis from "redis"; type onErrorCallback = (error: Error) => void; -type getPrefixCallback = (ctx: Koa.Context) => String; +type getPrefixCallback = (ctx: Koa.Context) => string; declare namespace cache { interface CacheOptions { @@ -16,36 +17,36 @@ declare namespace cache { * 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; + prefix?: string | getPrefixCallback; /** * redis expire time (second), default is 30 * 60 (30 min) */ - expire?: Number; + expire?: number; /** * if the passParam exists in the query string, skip the cache */ - passParam?: String; + 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; + maxLength?: number; /** * the routes to cache, default is ['(.*)']. - * can be set to an array of routes (String), or an array of RouteOptions + * can be set to an array of routes (string), or an array of RouteOptions */ - routes?: Array | Array; + routes?: RouteOptions[] | string[]; /** * the routes to exclude, default is []. * example: ['/api/(.*)', '/view/:id'] */ - exclude?: Array; + exclude?: string[]; /** * callback function for error, default is function() {} @@ -62,29 +63,29 @@ declare namespace cache { /** * the route to cache, example: '/api/(.*)' */ - route: String; + route: string; /** * expiration time in seconds for cached responses for the route */ - expire?: Number; + expire?: number; } interface RedisOptions { /** * host name of the redis server, default: 'localhost' */ - host?: String, + host?: string; /** * port number of the redis server, default: 6379 */ - port?: Number, + port?: number; /** * node_redis options */ - options?: Redis.ClientOpts + options?: Redis.ClientOpts; } } diff --git a/types/koa-redis-cache/koa-redis-cache-tests.ts b/types/koa-redis-cache/koa-redis-cache-tests.ts index 9c5f528ba7..b83d0b3aea 100644 --- a/types/koa-redis-cache/koa-redis-cache-tests.ts +++ b/types/koa-redis-cache/koa-redis-cache-tests.ts @@ -3,7 +3,7 @@ import * as cache from 'koa-redis-cache'; const app = new Koa(); -const routeOptions: Array = [ +const routeOptions: cache.RouteOptions[] = [ { route: '/api/test', expire: 60 diff --git a/types/koa-redis-cache/tsconfig.json b/types/koa-redis-cache/tsconfig.json index dbf782ccae..897679810e 100644 --- a/types/koa-redis-cache/tsconfig.json +++ b/types/koa-redis-cache/tsconfig.json @@ -7,6 +7,7 @@ "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": true, + "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ "../"