forked from mcollina/async-cache-dedupe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test-d.ts
78 lines (62 loc) · 2.07 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
// Write a tsd file for the module
import { expectType } 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);
// 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,
},
}) as Cache & CachedFunctions;
expectType<Cache & CachedFunctions>(unionMemoryCache);
unionMemoryCache.define("fetchSomething", fetchSomething);
expectType<typeof fetchSomething>(unionMemoryCache.fetchSomething);
unionMemoryCache.define(
"fetchSomethingElse",
{ ttl: 1000, stale: 1000, references: (args, key, result) => result.k },
fetchSomething,
);
expectType<typeof fetchSomething>(unionMemoryCache.fetchSomethingElse);
unionMemoryCache.define(
"fetchSomethingElseWithTtlFunction",
{ ttl: (result) => (result.k ? 1000 : 5), stale: 1000 },
fetchSomething,
);
expectType<typeof fetchSomething>(unionMemoryCache.fetchSomethingElseWithTtlFunction);
expectType<Promise<void>>(cache.clear());
expectType<Promise<void>>(cache.clear("fetchSomething"));
expectType<Promise<void>>(cache.clear("fetchSomething", "bar"));
const result = await unionMemoryCache.fetchSomething("test");
expectType<{ k: any }>(result);
await unionMemoryCache.invalidateAll("test:*");
await unionMemoryCache.invalidateAll(["test:1", "test:2", "test:3"], "memory");