Skip to content

Commit bd657a9

Browse files
authored
🤖 Merge PR DefinitelyTyped#66625 feat: Add types for route-cache by @yutak23
* feat: Add types for route-cache * fix: change 'typeof import' to RouteCache * fix: no export class
1 parent c7bc073 commit bd657a9

7 files changed

Lines changed: 135 additions & 0 deletions

File tree

types/route-cache/index.d.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Type definitions for route-cache 0.5
2+
// Project: https://github.com/bradoyler/route-cache
3+
// Definitions by: yutak23 <https://github.com/yutak23>
4+
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5+
6+
import * as express from 'express';
7+
8+
export function config(options: ConfigOptions): RouteCache;
9+
export function cacheSeconds(secondsTTL: number, cacheKey: string | CacheKeyFunc): express.RequestHandler;
10+
export function removeCache(url: string): void;
11+
12+
export const cacheStore: Store;
13+
14+
export interface RouteCache {
15+
config(options: ConfigOptions): RouteCache;
16+
cacheSeconds(secondsTTL: number, cacheKey: string | CacheKeyFunc): express.RequestHandler;
17+
removeCache(url: string): void;
18+
cacheStore: Store;
19+
}
20+
21+
export interface CacheKeyFunc {
22+
(req: express.Request, res: express.Response): string | null;
23+
}
24+
25+
export interface ConfigOptions {
26+
max?: number;
27+
cacheStore?: Store;
28+
}
29+
30+
export interface Store {
31+
get(key: string): Promise<any>;
32+
set(key: string, value: any, ttlMillis: number): Promise<'OK'> | Promise<boolean>;
33+
del(key: string): Promise<number> | Promise<void>;
34+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Redis } from 'ioredis';
2+
import { Store } from './index';
3+
4+
export = IoRedisStore;
5+
6+
declare class IoRedisStore implements Store {
7+
client: Redis;
8+
9+
constructor(redisClient: Redis);
10+
11+
get(key: string): Promise<any>;
12+
set(key: string, value: any, ttlMillis: number): Promise<'OK'>;
13+
del(key: string): Promise<number>;
14+
}

types/route-cache/lruStore.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as LRUCache from 'lru-cache';
2+
import { Store } from './index';
3+
4+
export = LruStore;
5+
6+
declare class LruStore implements Store {
7+
lru: LRUCache;
8+
9+
constructor(defaults: DefaultOptions<string, any>);
10+
11+
get(key: string): Promise<any>;
12+
set(key: string, value: any, ttlMillis: number): Promise<boolean>;
13+
del(key: string): Promise<void>;
14+
}
15+
16+
interface DefaultOptions<K, V> {
17+
max?: number;
18+
length?: (value: V, key: K) => number;
19+
maxAge?: number;
20+
}

types/route-cache/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"private": true,
3+
"dependencies": {
4+
"ioredis": "^5.3.2",
5+
"@types/lru-cache": "^4.1.3"
6+
}
7+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import routeCache = require('route-cache');
2+
import IoRedisStore = require('route-cache/ioRedisStore');
3+
import LruStore = require('route-cache/lruStore');
4+
import * as express from 'express';
5+
import Redis from 'ioredis';
6+
7+
const redis = new Redis();
8+
const ioRedisStore: IoRedisStore = new IoRedisStore(redis);
9+
const lruStore: LruStore = new LruStore({ max: 100, length: (value: any, key: string) => 1, maxAge: 1000 });
10+
11+
const configRedisStore: routeCache.ConfigOptions = { max: 100, cacheStore: ioRedisStore };
12+
const configLruStore: routeCache.ConfigOptions = { max: 100, cacheStore: lruStore };
13+
14+
const cacheKeyFunc: routeCache.CacheKeyFunc = (req: express.Request, res: express.Response) => 'foo';
15+
const cacheKeyFuncNull: routeCache.CacheKeyFunc = (req: express.Request, res: express.Response) => null;
16+
17+
routeCache.config({ max: 100, cacheStore: ioRedisStore }); // $ExpectType RouteCache
18+
routeCache.config({ max: 100, cacheStore: lruStore }); // $ExpectType RouteCache
19+
routeCache.config({ max: 10 }).config({ max: 100 }); // $ExpectType RouteCache
20+
21+
routeCache.config(configRedisStore); // $ExpectType RouteCache
22+
routeCache.config(configLruStore); // $ExpectType RouteCache
23+
24+
routeCache.config({ max: 100 }).cacheSeconds(10, 'foo'); // $ExpectType RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
25+
routeCache.cacheSeconds(10, cacheKeyFunc); // $ExpectType RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
26+
routeCache.cacheSeconds(10, cacheKeyFuncNull); // $ExpectType RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
27+
routeCache.cacheSeconds(10, 'foo'); // $ExpectType RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
28+
29+
routeCache.removeCache('foo'); // $ExpectType void
30+
routeCache.config({ max: 100 }).removeCache('foo'); // $ExpectType void
31+
32+
routeCache.cacheStore; // $ExpectType Store
33+
routeCache.config({ max: 100 }).cacheStore; // $ExpectType Store
34+
35+
const app = express();
36+
app.use(routeCache.config({ max: 100 }).cacheSeconds(10, 'foo')); // $ExpectType Express

types/route-cache/tsconfig.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"lib": [
5+
"es6"
6+
],
7+
"noImplicitAny": true,
8+
"noImplicitThis": true,
9+
"strictFunctionTypes": true,
10+
"strictNullChecks": true,
11+
"baseUrl": "../",
12+
"typeRoots": [
13+
"../"
14+
],
15+
"types": [],
16+
"noEmit": true,
17+
"forceConsistentCasingInFileNames": true
18+
},
19+
"files": [
20+
"index.d.ts",
21+
"route-cache-tests.ts"
22+
]
23+
}

types/route-cache/tslint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "extends": "@definitelytyped/dtslint/dt.json" }

0 commit comments

Comments
 (0)