-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
index.test-d.ts
118 lines (97 loc) · 3.29 KB
/
index.test-d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Write a tsd file for the module
import { expectType, expectError, expectNotAssignable, expectAssignable } from "tsd";
import { createCache, Cache, createStorage } from ".";
import { StorageInterface, StorageMemoryOptions } from "./index.js";
// Testing internal types
const storageOptions: StorageMemoryOptions = {
size: 1000,
};
const cache = createCache();
expectType<Cache>(cache);
const storage = createStorage("memory", storageOptions);
expectType<StorageInterface>(storage);
const memoryCache = createCache({
storage: {
type: "memory",
options: storageOptions,
},
});
expectType<Cache>(memoryCache);
const cacheWithTtlAndStale = createCache({
ttl: 1000,
stale: 1000,
});
expectType<Cache>(cacheWithTtlAndStale);
const cacheClass = new Cache({
ttl: 1000,
stale: 1000,
storage: createStorage('memory', {})
});
expectType<Cache>(cacheClass);
// Testing Union Types
const fetchSomething = async (k: any) => {
console.log("query", k);
return { k };
};
export type CachedFunctions = {
fetchSomething: typeof fetchSomething;
fetchSomethingElse: typeof fetchSomething;
fetchSomethingElseWithTtlFunction: typeof fetchSomething;
};
const unionMemoryCache = createCache({
storage: {
type: "memory",
options: storageOptions,
},
});
expectType<Cache>(unionMemoryCache);
const currentCacheInstance = unionMemoryCache
.define("fetchSomething", fetchSomething)
.define(
"fetchSomethingElse",
{ ttl: 1000, stale: 1000, references: (args, key, result) => result.k },
fetchSomething
)
.define(
"fetchSomethingElseWithTtlFunction",
{ ttl: (result) => (result.k ? 1000 : 5), stale: 1000 },
fetchSomething
)
.define(
"fetchSomethingElseWithCustomStorage",
{storage: {'type': 'memory', options: {size: 10}}, stale: 1000 },
fetchSomething
);
expectType<typeof fetchSomething>(currentCacheInstance.fetchSomething);
expectType<typeof fetchSomething>(currentCacheInstance.fetchSomethingElse);
expectType<typeof fetchSomething>(
currentCacheInstance.fetchSomethingElseWithTtlFunction
);
expectType<typeof fetchSomething>(
currentCacheInstance.fetchSomethingElseWithCustomStorage
);
expectType<Promise<void>>(cache.clear());
expectType<Promise<void>>(cache.clear("fetchSomething"));
expectType<Promise<void>>(cache.clear("fetchSomething", "bar"));
const result = await currentCacheInstance.fetchSomething("test");
expectType<{ k: any }>(result);
await unionMemoryCache.invalidateAll("test:*");
await unionMemoryCache.invalidateAll(["test:1", "test:2", "test:3"], "memory");
// Testing define.func only accepts one argument
const fetchFuncSingleArgument = async (args: {k1: string, k2:string}) => {
console.log("query", args.k1, args.k2);
return { k1: args.k1, k2: args.k2 };
};
const fetchFuncMultipleArguments = async (k1: string, k2:string) => {
console.log("query", k1, k2);
return { k1, k2 };
};
expectAssignable<Parameters<typeof unionMemoryCache.define>>(["fetchFuncSingleArgument", fetchFuncSingleArgument]);
expectNotAssignable<Parameters<typeof unionMemoryCache.define>>(["fetchFuncMultipleArguments", fetchFuncMultipleArguments]);
// Testing define.opts.references
memoryCache.define("fetchFuncSingleArgument", {
references: (args, key, result) => {
expectType<{ k1: string; k2: string }>(args);
return [];
}
}, fetchFuncSingleArgument);