@sebspark/promise-cache 1.2.2 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +15 -5
- package/dist/index.d.ts +15 -5
- package/dist/index.js +33 -18
- package/dist/index.mjs +33 -18
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -10,19 +10,27 @@ type SetParams<T> = {
|
|
|
10
10
|
timestamp: number;
|
|
11
11
|
ttl?: number;
|
|
12
12
|
};
|
|
13
|
+
type PersistorConstructorType = {
|
|
14
|
+
redis?: RedisClientOptions;
|
|
15
|
+
onError?: () => void;
|
|
16
|
+
onSuccess?: () => void;
|
|
17
|
+
};
|
|
13
18
|
declare class Persistor {
|
|
14
19
|
client: ReturnType<typeof createClient> | null;
|
|
15
|
-
private status;
|
|
16
20
|
private readonly redis?;
|
|
17
|
-
constructor(
|
|
18
|
-
connect(onError?: (message: string) => void,
|
|
21
|
+
constructor(options: PersistorConstructorType);
|
|
22
|
+
connect(onError?: (message: string) => void, onSuccess?: (message: string) => void): Promise<void>;
|
|
19
23
|
size(): Promise<number>;
|
|
20
24
|
get<T>(key: string): Promise<GetType<T> | null>;
|
|
21
25
|
private createOptions;
|
|
22
26
|
set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
|
|
23
27
|
delete(key: string): Promise<void>;
|
|
24
28
|
}
|
|
25
|
-
declare const createPersistor: (redis
|
|
29
|
+
declare const createPersistor: ({ redis, onError, onSuccess, }: {
|
|
30
|
+
redis?: RedisClientOptions;
|
|
31
|
+
onError?: () => void;
|
|
32
|
+
onSuccess?: () => void;
|
|
33
|
+
}) => Persistor;
|
|
26
34
|
declare const clean: () => void;
|
|
27
35
|
|
|
28
36
|
declare class PromiseCache<U> {
|
|
@@ -34,10 +42,12 @@ declare class PromiseCache<U> {
|
|
|
34
42
|
* @param ttlInSeconds Default cache TTL.
|
|
35
43
|
* @param caseSensitive Set to true if you want to differentiate between keys with different casing.
|
|
36
44
|
*/
|
|
37
|
-
constructor({ ttlInSeconds, caseSensitive, redis, }: {
|
|
45
|
+
constructor({ ttlInSeconds, caseSensitive, redis, onSuccess, onError, }: {
|
|
38
46
|
ttlInSeconds?: number;
|
|
39
47
|
caseSensitive?: boolean;
|
|
40
48
|
redis?: RedisClientOptions;
|
|
49
|
+
onError?: () => void;
|
|
50
|
+
onSuccess?: () => void;
|
|
41
51
|
});
|
|
42
52
|
/**
|
|
43
53
|
* Cache size.
|
package/dist/index.d.ts
CHANGED
|
@@ -10,19 +10,27 @@ type SetParams<T> = {
|
|
|
10
10
|
timestamp: number;
|
|
11
11
|
ttl?: number;
|
|
12
12
|
};
|
|
13
|
+
type PersistorConstructorType = {
|
|
14
|
+
redis?: RedisClientOptions;
|
|
15
|
+
onError?: () => void;
|
|
16
|
+
onSuccess?: () => void;
|
|
17
|
+
};
|
|
13
18
|
declare class Persistor {
|
|
14
19
|
client: ReturnType<typeof createClient> | null;
|
|
15
|
-
private status;
|
|
16
20
|
private readonly redis?;
|
|
17
|
-
constructor(
|
|
18
|
-
connect(onError?: (message: string) => void,
|
|
21
|
+
constructor(options: PersistorConstructorType);
|
|
22
|
+
connect(onError?: (message: string) => void, onSuccess?: (message: string) => void): Promise<void>;
|
|
19
23
|
size(): Promise<number>;
|
|
20
24
|
get<T>(key: string): Promise<GetType<T> | null>;
|
|
21
25
|
private createOptions;
|
|
22
26
|
set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
|
|
23
27
|
delete(key: string): Promise<void>;
|
|
24
28
|
}
|
|
25
|
-
declare const createPersistor: (redis
|
|
29
|
+
declare const createPersistor: ({ redis, onError, onSuccess, }: {
|
|
30
|
+
redis?: RedisClientOptions;
|
|
31
|
+
onError?: () => void;
|
|
32
|
+
onSuccess?: () => void;
|
|
33
|
+
}) => Persistor;
|
|
26
34
|
declare const clean: () => void;
|
|
27
35
|
|
|
28
36
|
declare class PromiseCache<U> {
|
|
@@ -34,10 +42,12 @@ declare class PromiseCache<U> {
|
|
|
34
42
|
* @param ttlInSeconds Default cache TTL.
|
|
35
43
|
* @param caseSensitive Set to true if you want to differentiate between keys with different casing.
|
|
36
44
|
*/
|
|
37
|
-
constructor({ ttlInSeconds, caseSensitive, redis, }: {
|
|
45
|
+
constructor({ ttlInSeconds, caseSensitive, redis, onSuccess, onError, }: {
|
|
38
46
|
ttlInSeconds?: number;
|
|
39
47
|
caseSensitive?: boolean;
|
|
40
48
|
redis?: RedisClientOptions;
|
|
49
|
+
onError?: () => void;
|
|
50
|
+
onSuccess?: () => void;
|
|
41
51
|
});
|
|
42
52
|
/**
|
|
43
53
|
* Cache size.
|
package/dist/index.js
CHANGED
|
@@ -57,7 +57,7 @@ var LocalStorage = class {
|
|
|
57
57
|
}
|
|
58
58
|
// This is just for testing
|
|
59
59
|
on(event, callback) {
|
|
60
|
-
if (event === "connect") {
|
|
60
|
+
if (event === "connect" && callback) {
|
|
61
61
|
callback("connect");
|
|
62
62
|
}
|
|
63
63
|
return this;
|
|
@@ -73,28 +73,30 @@ var createLocalMemoryClient = () => {
|
|
|
73
73
|
|
|
74
74
|
// src/persistor.ts
|
|
75
75
|
var CACHE_CLIENT = import_redis.createClient;
|
|
76
|
+
var isTestRunning = process.env.NODE_ENV === "test";
|
|
76
77
|
var Persistor = class {
|
|
77
78
|
client = null;
|
|
78
|
-
status = "disconnected";
|
|
79
79
|
redis;
|
|
80
|
-
constructor(
|
|
81
|
-
|
|
80
|
+
constructor(options) {
|
|
81
|
+
const { redis, onError, onSuccess } = options;
|
|
82
|
+
if (redis && !isTestRunning) {
|
|
82
83
|
this.redis = redis;
|
|
83
84
|
} else {
|
|
84
85
|
CACHE_CLIENT = createLocalMemoryClient;
|
|
85
86
|
}
|
|
86
|
-
this.connect();
|
|
87
|
+
this.connect(onError, onSuccess);
|
|
87
88
|
}
|
|
88
|
-
async connect(onError,
|
|
89
|
-
var _a;
|
|
89
|
+
async connect(onError, onSuccess) {
|
|
90
|
+
var _a, _b;
|
|
90
91
|
try {
|
|
91
92
|
this.client = CACHE_CLIENT(this.redis);
|
|
92
93
|
this.client.on("error", (err) => {
|
|
93
|
-
var _a2;
|
|
94
|
+
var _a2, _b2;
|
|
94
95
|
if (onError) {
|
|
95
96
|
onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
|
|
97
|
+
} else {
|
|
98
|
+
console.error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
|
|
96
99
|
}
|
|
97
|
-
this.status = "disconnected";
|
|
98
100
|
});
|
|
99
101
|
this.client.connect();
|
|
100
102
|
await new Promise((resolve, reject) => {
|
|
@@ -104,18 +106,18 @@ var Persistor = class {
|
|
|
104
106
|
}
|
|
105
107
|
this.client.on("connect", () => {
|
|
106
108
|
var _a2;
|
|
107
|
-
if (
|
|
108
|
-
|
|
109
|
+
if (onSuccess) {
|
|
110
|
+
onSuccess(`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.url}`);
|
|
109
111
|
}
|
|
110
|
-
this.status = "connected";
|
|
111
112
|
resolve(true);
|
|
112
113
|
});
|
|
113
114
|
});
|
|
114
115
|
} catch (err) {
|
|
115
116
|
if (onError) {
|
|
116
117
|
onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
|
|
118
|
+
} else {
|
|
119
|
+
console.error(`\u274C REDIS | Connection Error | ${(_b = this.redis) == null ? void 0 : _b.url} ${err}`);
|
|
117
120
|
}
|
|
118
|
-
this.status = "disconnected";
|
|
119
121
|
}
|
|
120
122
|
}
|
|
121
123
|
async size() {
|
|
@@ -168,15 +170,26 @@ var Persistor = class {
|
|
|
168
170
|
}
|
|
169
171
|
};
|
|
170
172
|
var _persistors = {};
|
|
171
|
-
var createPersistor = (
|
|
173
|
+
var createPersistor = ({
|
|
174
|
+
redis,
|
|
175
|
+
onError,
|
|
176
|
+
onSuccess
|
|
177
|
+
}) => {
|
|
172
178
|
if (redis) {
|
|
173
179
|
const key = JSON.stringify(redis);
|
|
174
180
|
if (!_persistors[key]) {
|
|
175
|
-
_persistors[key] = new Persistor(
|
|
181
|
+
_persistors[key] = new Persistor({
|
|
182
|
+
redis,
|
|
183
|
+
onError,
|
|
184
|
+
onSuccess
|
|
185
|
+
});
|
|
176
186
|
}
|
|
177
187
|
return _persistors[key];
|
|
178
188
|
}
|
|
179
|
-
return new Persistor(
|
|
189
|
+
return new Persistor({
|
|
190
|
+
onSuccess,
|
|
191
|
+
onError
|
|
192
|
+
});
|
|
180
193
|
};
|
|
181
194
|
var clean = () => {
|
|
182
195
|
_persistors = {};
|
|
@@ -196,9 +209,11 @@ var PromiseCache = class {
|
|
|
196
209
|
constructor({
|
|
197
210
|
ttlInSeconds,
|
|
198
211
|
caseSensitive = false,
|
|
199
|
-
redis
|
|
212
|
+
redis,
|
|
213
|
+
onSuccess,
|
|
214
|
+
onError
|
|
200
215
|
}) {
|
|
201
|
-
this.persistor = createPersistor(redis);
|
|
216
|
+
this.persistor = createPersistor({ redis, onError, onSuccess });
|
|
202
217
|
this.caseSensitive = caseSensitive;
|
|
203
218
|
if (ttlInSeconds) {
|
|
204
219
|
this.ttl = ttlInSeconds * 1e3;
|
package/dist/index.mjs
CHANGED
|
@@ -26,7 +26,7 @@ var LocalStorage = class {
|
|
|
26
26
|
}
|
|
27
27
|
// This is just for testing
|
|
28
28
|
on(event, callback) {
|
|
29
|
-
if (event === "connect") {
|
|
29
|
+
if (event === "connect" && callback) {
|
|
30
30
|
callback("connect");
|
|
31
31
|
}
|
|
32
32
|
return this;
|
|
@@ -42,28 +42,30 @@ var createLocalMemoryClient = () => {
|
|
|
42
42
|
|
|
43
43
|
// src/persistor.ts
|
|
44
44
|
var CACHE_CLIENT = createClient;
|
|
45
|
+
var isTestRunning = process.env.NODE_ENV === "test";
|
|
45
46
|
var Persistor = class {
|
|
46
47
|
client = null;
|
|
47
|
-
status = "disconnected";
|
|
48
48
|
redis;
|
|
49
|
-
constructor(
|
|
50
|
-
|
|
49
|
+
constructor(options) {
|
|
50
|
+
const { redis, onError, onSuccess } = options;
|
|
51
|
+
if (redis && !isTestRunning) {
|
|
51
52
|
this.redis = redis;
|
|
52
53
|
} else {
|
|
53
54
|
CACHE_CLIENT = createLocalMemoryClient;
|
|
54
55
|
}
|
|
55
|
-
this.connect();
|
|
56
|
+
this.connect(onError, onSuccess);
|
|
56
57
|
}
|
|
57
|
-
async connect(onError,
|
|
58
|
-
var _a;
|
|
58
|
+
async connect(onError, onSuccess) {
|
|
59
|
+
var _a, _b;
|
|
59
60
|
try {
|
|
60
61
|
this.client = CACHE_CLIENT(this.redis);
|
|
61
62
|
this.client.on("error", (err) => {
|
|
62
|
-
var _a2;
|
|
63
|
+
var _a2, _b2;
|
|
63
64
|
if (onError) {
|
|
64
65
|
onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
|
|
66
|
+
} else {
|
|
67
|
+
console.error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
|
|
65
68
|
}
|
|
66
|
-
this.status = "disconnected";
|
|
67
69
|
});
|
|
68
70
|
this.client.connect();
|
|
69
71
|
await new Promise((resolve, reject) => {
|
|
@@ -73,18 +75,18 @@ var Persistor = class {
|
|
|
73
75
|
}
|
|
74
76
|
this.client.on("connect", () => {
|
|
75
77
|
var _a2;
|
|
76
|
-
if (
|
|
77
|
-
|
|
78
|
+
if (onSuccess) {
|
|
79
|
+
onSuccess(`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.url}`);
|
|
78
80
|
}
|
|
79
|
-
this.status = "connected";
|
|
80
81
|
resolve(true);
|
|
81
82
|
});
|
|
82
83
|
});
|
|
83
84
|
} catch (err) {
|
|
84
85
|
if (onError) {
|
|
85
86
|
onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
|
|
87
|
+
} else {
|
|
88
|
+
console.error(`\u274C REDIS | Connection Error | ${(_b = this.redis) == null ? void 0 : _b.url} ${err}`);
|
|
86
89
|
}
|
|
87
|
-
this.status = "disconnected";
|
|
88
90
|
}
|
|
89
91
|
}
|
|
90
92
|
async size() {
|
|
@@ -137,15 +139,26 @@ var Persistor = class {
|
|
|
137
139
|
}
|
|
138
140
|
};
|
|
139
141
|
var _persistors = {};
|
|
140
|
-
var createPersistor = (
|
|
142
|
+
var createPersistor = ({
|
|
143
|
+
redis,
|
|
144
|
+
onError,
|
|
145
|
+
onSuccess
|
|
146
|
+
}) => {
|
|
141
147
|
if (redis) {
|
|
142
148
|
const key = JSON.stringify(redis);
|
|
143
149
|
if (!_persistors[key]) {
|
|
144
|
-
_persistors[key] = new Persistor(
|
|
150
|
+
_persistors[key] = new Persistor({
|
|
151
|
+
redis,
|
|
152
|
+
onError,
|
|
153
|
+
onSuccess
|
|
154
|
+
});
|
|
145
155
|
}
|
|
146
156
|
return _persistors[key];
|
|
147
157
|
}
|
|
148
|
-
return new Persistor(
|
|
158
|
+
return new Persistor({
|
|
159
|
+
onSuccess,
|
|
160
|
+
onError
|
|
161
|
+
});
|
|
149
162
|
};
|
|
150
163
|
var clean = () => {
|
|
151
164
|
_persistors = {};
|
|
@@ -165,9 +178,11 @@ var PromiseCache = class {
|
|
|
165
178
|
constructor({
|
|
166
179
|
ttlInSeconds,
|
|
167
180
|
caseSensitive = false,
|
|
168
|
-
redis
|
|
181
|
+
redis,
|
|
182
|
+
onSuccess,
|
|
183
|
+
onError
|
|
169
184
|
}) {
|
|
170
|
-
this.persistor = createPersistor(redis);
|
|
185
|
+
this.persistor = createPersistor({ redis, onError, onSuccess });
|
|
171
186
|
this.caseSensitive = caseSensitive;
|
|
172
187
|
if (ttlInSeconds) {
|
|
173
188
|
this.ttl = ttlInSeconds * 1e3;
|