Skip to content

Commit 2607cfb

Browse files
authored
Fix/align ts types with code (#143)
* fix(types): align types with code * fix(style): use spaces in types * fix(style): tabs to spaces * fix(ts): export types that might be useful
1 parent 594eeab commit 2607cfb

File tree

3 files changed

+87
-66
lines changed

3 files changed

+87
-66
lines changed

index.d.ts

+55-50
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,91 @@
11
import { Redis } from "ioredis";
22

3-
type StorageOptionsType = "redis" | "memory";
3+
export type StorageOptionsType = "redis" | "memory";
4+
5+
export type StorageOptions = {
6+
type: StorageOptionsType,
7+
options: StorageRedisOptions | StorageMemoryOptions,
8+
}
49

510
type References = string | string[];
611

712
interface LoggerInput {
8-
msg: string;
9-
[key: string]: any;
13+
msg: string;
14+
[key: string]: any;
1015
}
1116

1217
interface Logger {
13-
debug: (input: LoggerInput) => void;
14-
warn: (input: LoggerInput) => void;
15-
error: (input: LoggerInput) => void;
18+
debug: (input: LoggerInput) => void;
19+
warn: (input: LoggerInput) => void;
20+
error: (input: LoggerInput) => void;
1621
}
17-
interface StorageRedisOptions {
18-
client: Redis;
19-
log?: Logger;
20-
invalidation?: { referencesTTL: number } | boolean;
22+
export interface StorageRedisOptions {
23+
client: Redis;
24+
log?: Logger;
25+
invalidation?: { referencesTTL: number } | boolean;
2126
}
2227

23-
interface StorageMemoryOptions {
24-
size?: number;
25-
log?: Logger;
26-
invalidation?: boolean;
28+
export interface StorageMemoryOptions {
29+
size?: number;
30+
log?: Logger;
31+
invalidation?: boolean;
2732
}
2833

2934
interface DataTransformer {
30-
serialize: (data: any) => any;
31-
deserialize: (data: any) => any;
35+
serialize: (data: any) => any;
36+
deserialize: (data: any) => any;
3237
}
3338

3439
type Events = {
35-
onDedupe?: (key: string) => void;
36-
onError?: (err: any) => void;
37-
onHit?: (key: string) => void;
38-
onMiss?: (key: string) => void;
40+
onDedupe?: (key: string) => void;
41+
onError?: (err: any) => void;
42+
onHit?: (key: string) => void;
43+
onMiss?: (key: string) => void;
3944
};
4045

41-
type StorageInputRedis = {
42-
type: "redis";
43-
options?: StorageRedisOptions;
46+
export type StorageInputRedis = {
47+
type: "redis";
48+
options?: StorageRedisOptions;
4449
};
4550

46-
type StorageInputMemory = {
47-
type: "memory";
48-
options?: StorageMemoryOptions;
51+
export type StorageInputMemory = {
52+
type: "memory";
53+
options?: StorageMemoryOptions;
4954
};
5055

51-
declare class StorageInterface {
52-
constructor(options: any);
56+
export declare class StorageInterface {
57+
constructor(options: any);
5358

54-
get(key: string): Promise<undefined | any>;
55-
set(key: string, value: any, ttl: number, references?: References): Promise<void>;
56-
remove(key: string): Promise<void>;
57-
invalidate(references: References): Promise<void>;
58-
clear(name: string): Promise<void>;
59-
refresh(): Promise<void>;
59+
get(key: string): Promise<undefined | any>;
60+
set(key: string, value: any, ttl: number, references?: References): Promise<void>;
61+
remove(key: string): Promise<void>;
62+
invalidate(references: References): Promise<void>;
63+
clear(name: string): Promise<void>;
64+
refresh(): Promise<void>;
6065
}
6166

62-
declare function createCache(
63-
options?: {
64-
storage?: StorageInputRedis | StorageInputMemory;
65-
ttl?: number | ((result: unknown) => number);
66-
transformer?: DataTransformer;
67-
stale?: number | ((result: unknown) => number);
68-
} & Events,
67+
export declare function createCache(
68+
options?: {
69+
storage?: StorageInputRedis | StorageInputMemory;
70+
ttl?: number | ((result: unknown) => number);
71+
transformer?: DataTransformer;
72+
stale?: number | ((result: unknown) => number);
73+
} & Events,
6974
): Cache;
7075

71-
declare class Cache {
76+
export declare class Cache {
7277
constructor(
7378
options: {
7479
ttl: number | ((result: unknown) => number);
75-
storage: StorageOptionsType;
80+
stale?: number | ((result: unknown) => number);
81+
storage: StorageInterface;
7682
} & Events
7783
);
7884

7985
define<T extends (args: any) => any, N extends string, S extends this>(
8086
name: N,
8187
opts: {
82-
storage?: StorageOptionsType;
88+
storage?: StorageOptions;
8389
transformer?: DataTransformer;
8490
ttl?: number | ((result: Awaited<ReturnType<T>>) => number);
8591
stale?: number | ((result: Awaited<ReturnType<T>>) => number);
@@ -119,11 +125,10 @@ declare class Cache {
119125
): Promise<void>;
120126
}
121127

122-
declare function createStorage(type: "redis", options: StorageRedisOptions): StorageInterface;
123-
declare function createStorage(type: "memory", options: StorageMemoryOptions): StorageInterface;
124-
declare function createStorage(
125-
type: StorageOptionsType,
126-
options: StorageRedisOptions | StorageMemoryOptions,
128+
export declare function createStorage(type: "redis", options: StorageRedisOptions): StorageInterface;
129+
export declare function createStorage(type: "memory", options: StorageMemoryOptions): StorageInterface;
130+
export declare function createStorage(
131+
type: StorageOptionsType,
132+
options: StorageRedisOptions | StorageMemoryOptions,
127133
): StorageInterface;
128134

129-
export { createCache, Cache, createStorage, StorageInterface, StorageMemoryOptions };

index.test-d.ts

+31-16
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { StorageInterface, StorageMemoryOptions } from "./index.js";
66
// Testing internal types
77

88
const storageOptions: StorageMemoryOptions = {
9-
size: 1000,
9+
size: 1000,
1010
};
1111

1212
const cache = createCache();
@@ -16,30 +16,37 @@ const storage = createStorage("memory", storageOptions);
1616
expectType<StorageInterface>(storage);
1717

1818
const memoryCache = createCache({
19-
storage: {
20-
type: "memory",
21-
options: storageOptions,
22-
},
19+
storage: {
20+
type: "memory",
21+
options: storageOptions,
22+
},
2323
});
2424
expectType<Cache>(memoryCache);
2525

2626
const cacheWithTtlAndStale = createCache({
27-
ttl: 1000,
28-
stale: 1000,
27+
ttl: 1000,
28+
stale: 1000,
2929
});
3030
expectType<Cache>(cacheWithTtlAndStale);
3131

32+
const cacheClass = new Cache({
33+
ttl: 1000,
34+
stale: 1000,
35+
storage: createStorage('memory', {})
36+
});
37+
expectType<Cache>(cacheClass);
38+
3239
// Testing Union Types
3340

3441
const fetchSomething = async (k: any) => {
35-
console.log("query", k);
36-
return { k };
42+
console.log("query", k);
43+
return { k };
3744
};
3845

3946
export type CachedFunctions = {
40-
fetchSomething: typeof fetchSomething;
41-
fetchSomethingElse: typeof fetchSomething;
42-
fetchSomethingElseWithTtlFunction: typeof fetchSomething;
47+
fetchSomething: typeof fetchSomething;
48+
fetchSomethingElse: typeof fetchSomething;
49+
fetchSomethingElseWithTtlFunction: typeof fetchSomething;
4350
};
4451

4552
const unionMemoryCache = createCache({
@@ -60,12 +67,20 @@ const currentCacheInstance = unionMemoryCache
6067
"fetchSomethingElseWithTtlFunction",
6168
{ ttl: (result) => (result.k ? 1000 : 5), stale: 1000 },
6269
fetchSomething
70+
)
71+
.define(
72+
"fetchSomethingElseWithCustomStorage",
73+
{storage: {'type': 'memory', options: {size: 10}}, stale: 1000 },
74+
fetchSomething
6375
);
6476
expectType<typeof fetchSomething>(currentCacheInstance.fetchSomething);
6577
expectType<typeof fetchSomething>(currentCacheInstance.fetchSomethingElse);
6678
expectType<typeof fetchSomething>(
6779
currentCacheInstance.fetchSomethingElseWithTtlFunction
6880
);
81+
expectType<typeof fetchSomething>(
82+
currentCacheInstance.fetchSomethingElseWithCustomStorage
83+
);
6984

7085
expectType<Promise<void>>(cache.clear());
7186
expectType<Promise<void>>(cache.clear("fetchSomething"));
@@ -80,14 +95,14 @@ await unionMemoryCache.invalidateAll(["test:1", "test:2", "test:3"], "memory");
8095

8196
// Testing define.func only accepts one argument
8297
const fetchFuncSingleArgument = async (args: {k1: string, k2:string}) => {
83-
console.log("query", args.k1, args.k2);
84-
return { k1: args.k1, k2: args.k2 };
98+
console.log("query", args.k1, args.k2);
99+
return { k1: args.k1, k2: args.k2 };
85100
};
86101

87102

88103
const fetchFuncMultipleArguments = async (k1: string, k2:string) => {
89-
console.log("query", k1, k2);
90-
return { k1, k2 };
104+
console.log("query", k1, k2);
105+
return { k1, k2 };
91106
};
92107

93108
expectAssignable<Parameters<typeof unionMemoryCache.define>>(["fetchFuncSingleArgument", fetchFuncSingleArgument]);

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"types": "index.d.ts",
77
"scripts": {
88
"test": "standard | snazzy && c8 --100 node --test test/*test.js && tsd",
9+
"test:types": "tsd",
910
"test:browser": "node test/browser/helpers/runner-browser.mjs",
1011
"lint:fix": "standard --fix",
1112
"redis": "docker run --rm -p 6379:6379 redis redis-server"

0 commit comments

Comments
 (0)