From 14568e033b08fb4f7cdf56a8ec451ff1ba3fe654 Mon Sep 17 00:00:00 2001 From: AJ Livingston Date: Tue, 12 Jun 2018 13:38:08 -0400 Subject: [PATCH 1/3] added types for express-redis-cache --- .../express-redis-cache-tests.ts | 105 ++++++++++++++++++ types/express-redis-cache/index.d.ts | 83 ++++++++++++++ types/express-redis-cache/tsconfig.json | 23 ++++ types/express-redis-cache/tslint.json | 1 + 4 files changed, 212 insertions(+) create mode 100644 types/express-redis-cache/express-redis-cache-tests.ts create mode 100644 types/express-redis-cache/index.d.ts create mode 100644 types/express-redis-cache/tsconfig.json create mode 100644 types/express-redis-cache/tslint.json diff --git a/types/express-redis-cache/express-redis-cache-tests.ts b/types/express-redis-cache/express-redis-cache-tests.ts new file mode 100644 index 0000000000..ed04fb0790 --- /dev/null +++ b/types/express-redis-cache/express-redis-cache-tests.ts @@ -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) => {}); diff --git a/types/express-redis-cache/index.d.ts b/types/express-redis-cache/index.d.ts new file mode 100644 index 0000000000..0d6d1cc8b6 --- /dev/null +++ b/types/express-redis-cache/index.d.ts @@ -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 +// 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; diff --git a/types/express-redis-cache/tsconfig.json b/types/express-redis-cache/tsconfig.json new file mode 100644 index 0000000000..7884be049e --- /dev/null +++ b/types/express-redis-cache/tsconfig.json @@ -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" + ] +} diff --git a/types/express-redis-cache/tslint.json b/types/express-redis-cache/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/express-redis-cache/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 4314bcb88b813f5b692d176f9cdae28eaafdb3c6 Mon Sep 17 00:00:00 2001 From: AJ Livingston Date: Tue, 12 Jun 2018 14:09:50 -0400 Subject: [PATCH 2/3] lint fixes --- types/express-redis-cache/index.d.ts | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/types/express-redis-cache/index.d.ts b/types/express-redis-cache/index.d.ts index 0d6d1cc8b6..65af3ebdbd 100644 --- a/types/express-redis-cache/index.d.ts +++ b/types/express-redis-cache/index.d.ts @@ -1,11 +1,11 @@ -// Type definitions for express-redis-cache 1.1.1 +// Type definitions for express-redis-cache 1.1 // Project: https://github.com/rv-kip/express-redis-cache // Definitions by: AJ Livingston // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 import { EventEmitter } from 'events'; -import { RequestHandler } from 'express'; +import { RequestHandler, Request, Response } from 'express'; import { RedisClient } from 'redis'; declare module 'express-serve-static-core' { @@ -34,10 +34,8 @@ declare namespace expressRedisCache { 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; + route(nameOrOptions: string | RouteOptions, expire?: ExpireOption): RequestHandler; + route(expire?: number): RequestHandler; size(callback: (error: any, bytes: number) => void): void; } @@ -59,10 +57,7 @@ declare namespace expressRedisCache { } type ExpireOption = number | ExpirationConfig; - - interface ExpirationPolicy { - (req: Request, res: Response): number; - } + type ExpirationPolicy = (req: Request, res: Response) => number; interface Options { auth_pass?: string; From f06fff179c25080dbd0a21abe7ac5b7bde070a5c Mon Sep 17 00:00:00 2001 From: AJ Livingston Date: Wed, 20 Jun 2018 09:42:21 -0500 Subject: [PATCH 3/3] importing with cjs syntax --- .../express-redis-cache-tests.ts | 6 +++--- types/express-redis-cache/index.d.ts | 19 +++++++++---------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/types/express-redis-cache/express-redis-cache-tests.ts b/types/express-redis-cache/express-redis-cache-tests.ts index ed04fb0790..3f8976c812 100644 --- a/types/express-redis-cache/express-redis-cache-tests.ts +++ b/types/express-redis-cache/express-redis-cache-tests.ts @@ -1,6 +1,6 @@ -import * as express from 'express'; -import * as redis from 'redis'; -import * as expressRedisCache from 'express-redis-cache'; +import express = require('express'); +import redis = require('redis'); +import expressRedisCache = require('express-redis-cache'); const app = express(); const cache = expressRedisCache(); diff --git a/types/express-redis-cache/index.d.ts b/types/express-redis-cache/index.d.ts index 65af3ebdbd..ffd8c844a6 100644 --- a/types/express-redis-cache/index.d.ts +++ b/types/express-redis-cache/index.d.ts @@ -4,9 +4,9 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 -import { EventEmitter } from 'events'; -import { RequestHandler, Request, Response } from 'express'; -import { RedisClient } from 'redis'; +import events = require('events'); +import express = require('express'); +import redis = require('redis'); declare module 'express-serve-static-core' { interface Response { @@ -17,7 +17,7 @@ declare module 'express-serve-static-core' { declare function expressRedisCache(options?: expressRedisCache.Options): expressRedisCache.ExpressRedisCache; declare namespace expressRedisCache { - class ExpressRedisCache extends EventEmitter { + class ExpressRedisCache extends events.EventEmitter { constructor(options?: Options); static init(options?: Options): ExpressRedisCache; readonly FOREVER: number; @@ -28,14 +28,14 @@ declare namespace expressRedisCache { auth_pass: string; connected: boolean; expire: number; - client: RedisClient; + client: redis.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(nameOrOptions: string | RouteOptions, expire?: ExpireOption): RequestHandler; - route(expire?: number): RequestHandler; + route(nameOrOptions: string | RouteOptions, expire?: ExpireOption): express.RequestHandler; + route(expire?: number): express.RequestHandler; size(callback: (error: any, bytes: number) => void): void; } @@ -53,15 +53,14 @@ declare namespace expressRedisCache { interface ExpirationConfig { [statusCode: string]: number; - [statusCode: number]: number; } type ExpireOption = number | ExpirationConfig; - type ExpirationPolicy = (req: Request, res: Response) => number; + type ExpirationPolicy = (req: express.Request, res: express.Response) => number; interface Options { auth_pass?: string; - client?: RedisClient; + client?: redis.RedisClient; expire?: number; host?: string; port?: string | number;