@sebspark/promise-cache 2.0.3 → 2.0.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 CHANGED
@@ -15,8 +15,8 @@ type SetParams<T> = {
15
15
  type PersistorConstructorType = {
16
16
  redis?: RedisClientOptions;
17
17
  clientId?: UUID;
18
- onError?: (c: string) => void;
19
- onSuccess?: (c: string) => void;
18
+ onError: (error: string) => void;
19
+ onSuccess: () => void;
20
20
  };
21
21
  declare class Persistor {
22
22
  client: ReturnType<typeof createClient> | null;
@@ -41,7 +41,7 @@ type PromiseCacheOptions = {
41
41
  ttlInSeconds?: number;
42
42
  caseSensitive?: boolean;
43
43
  redis?: RedisClientOptions;
44
- onError?: () => void;
44
+ onError?: (error: string) => void;
45
45
  onSuccess?: () => void;
46
46
  };
47
47
  declare class PromiseCache<U> {
package/dist/index.d.ts CHANGED
@@ -15,8 +15,8 @@ type SetParams<T> = {
15
15
  type PersistorConstructorType = {
16
16
  redis?: RedisClientOptions;
17
17
  clientId?: UUID;
18
- onError?: (c: string) => void;
19
- onSuccess?: (c: string) => void;
18
+ onError: (error: string) => void;
19
+ onSuccess: () => void;
20
20
  };
21
21
  declare class Persistor {
22
22
  client: ReturnType<typeof createClient> | null;
@@ -41,7 +41,7 @@ type PromiseCacheOptions = {
41
41
  ttlInSeconds?: number;
42
42
  caseSensitive?: boolean;
43
43
  redis?: RedisClientOptions;
44
- onError?: () => void;
44
+ onError?: (error: string) => void;
45
45
  onSuccess?: () => void;
46
46
  };
47
47
  declare class PromiseCache<U> {
package/dist/index.js CHANGED
@@ -116,36 +116,25 @@ var Persistor = class {
116
116
  await (0, import_retry.retry)(() => this.startConnection(), settings);
117
117
  }
118
118
  async startConnection() {
119
- var _a, _b;
120
- try {
121
- this.client = CACHE_CLIENT(this.redis);
122
- this.client.on("error", (err) => {
123
- var _a2, _b2;
124
- this.isConnected = false;
125
- if (this.onError) {
126
- this.onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
127
- }
128
- throw new Error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
129
- });
130
- this.client.on("connect", () => {
131
- var _a2, _b2, _c, _d;
132
- this.isConnected = true;
133
- if (this.onSuccess) {
134
- this.onSuccess(
135
- `\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.name} | ${this.clientId} | ${(_b2 = this.redis) == null ? void 0 : _b2.url}`
136
- );
137
- }
138
- console.log(
139
- `\u{1F4E6} REDIS | Connection Ready | ${(_c = this.redis) == null ? void 0 : _c.name}\xA0| ${this.clientId} | ${(_d = this.redis) == null ? void 0 : _d.url}`
140
- );
141
- });
142
- return await this.client.connect();
143
- } catch (err) {
144
- if (this.onError) {
145
- this.onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
119
+ return new Promise((resolve, reject) => {
120
+ try {
121
+ this.client = CACHE_CLIENT(this.redis);
122
+ this.client.on("error", (err) => {
123
+ this.isConnected = false;
124
+ this.onError(err);
125
+ reject();
126
+ });
127
+ this.client.on("connect", () => {
128
+ this.isConnected = true;
129
+ this.onSuccess();
130
+ resolve(true);
131
+ });
132
+ this.client.connect();
133
+ } catch (err) {
134
+ this.onError(`${err}`);
135
+ reject();
146
136
  }
147
- throw new Error(`\u274C REDIS | Connection Error | ${(_b = this.redis) == null ? void 0 : _b.url} ${err}`);
148
- }
137
+ });
149
138
  }
150
139
  async size() {
151
140
  if (!this.client) {
@@ -180,7 +169,7 @@ var Persistor = class {
180
169
  return {};
181
170
  }
182
171
  async set(key, { value, timestamp, ttl }) {
183
- if (!this.isConnected || !this.client) {
172
+ if (!this.client) {
184
173
  throw new Error("Client not connected");
185
174
  }
186
175
  try {
@@ -192,7 +181,7 @@ var Persistor = class {
192
181
  }
193
182
  }
194
183
  async delete(key) {
195
- if (!this.isConnected || !this.client) {
184
+ if (!this.client) {
196
185
  throw new Error("Client not connected");
197
186
  }
198
187
  try {
@@ -211,12 +200,22 @@ var getPersistor = ({
211
200
  onSuccess,
212
201
  clientId
213
202
  }) => {
214
- const connectionName = (redis == null ? void 0 : redis.name) || "default";
203
+ const connectionName = redis ? (redis == null ? void 0 : redis.name) || "default" : "local";
215
204
  if (!persistors[connectionName]) {
216
205
  persistors[connectionName] = new Persistor({
217
206
  redis,
218
- onError,
219
- onSuccess,
207
+ onError: (error) => {
208
+ onError == null ? void 0 : onError(error);
209
+ console.error(
210
+ `\u274C REDIS | Client Error | ${connectionName} | ${redis == null ? void 0 : redis.url}: ${error}`
211
+ );
212
+ },
213
+ onSuccess: () => {
214
+ onSuccess == null ? void 0 : onSuccess();
215
+ console.log(
216
+ `\u{1F4E6} REDIS | Connection Ready | ${connectionName} | ${redis == null ? void 0 : redis.url}`
217
+ );
218
+ },
220
219
  clientId
221
220
  });
222
221
  }
package/dist/index.mjs CHANGED
@@ -87,36 +87,25 @@ var Persistor = class {
87
87
  await retry(() => this.startConnection(), settings);
88
88
  }
89
89
  async startConnection() {
90
- var _a, _b;
91
- try {
92
- this.client = CACHE_CLIENT(this.redis);
93
- this.client.on("error", (err) => {
94
- var _a2, _b2;
95
- this.isConnected = false;
96
- if (this.onError) {
97
- this.onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
98
- }
99
- throw new Error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
100
- });
101
- this.client.on("connect", () => {
102
- var _a2, _b2, _c, _d;
103
- this.isConnected = true;
104
- if (this.onSuccess) {
105
- this.onSuccess(
106
- `\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.name} | ${this.clientId} | ${(_b2 = this.redis) == null ? void 0 : _b2.url}`
107
- );
108
- }
109
- console.log(
110
- `\u{1F4E6} REDIS | Connection Ready | ${(_c = this.redis) == null ? void 0 : _c.name}\xA0| ${this.clientId} | ${(_d = this.redis) == null ? void 0 : _d.url}`
111
- );
112
- });
113
- return await this.client.connect();
114
- } catch (err) {
115
- if (this.onError) {
116
- this.onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
90
+ return new Promise((resolve, reject) => {
91
+ try {
92
+ this.client = CACHE_CLIENT(this.redis);
93
+ this.client.on("error", (err) => {
94
+ this.isConnected = false;
95
+ this.onError(err);
96
+ reject();
97
+ });
98
+ this.client.on("connect", () => {
99
+ this.isConnected = true;
100
+ this.onSuccess();
101
+ resolve(true);
102
+ });
103
+ this.client.connect();
104
+ } catch (err) {
105
+ this.onError(`${err}`);
106
+ reject();
117
107
  }
118
- throw new Error(`\u274C REDIS | Connection Error | ${(_b = this.redis) == null ? void 0 : _b.url} ${err}`);
119
- }
108
+ });
120
109
  }
121
110
  async size() {
122
111
  if (!this.client) {
@@ -151,7 +140,7 @@ var Persistor = class {
151
140
  return {};
152
141
  }
153
142
  async set(key, { value, timestamp, ttl }) {
154
- if (!this.isConnected || !this.client) {
143
+ if (!this.client) {
155
144
  throw new Error("Client not connected");
156
145
  }
157
146
  try {
@@ -163,7 +152,7 @@ var Persistor = class {
163
152
  }
164
153
  }
165
154
  async delete(key) {
166
- if (!this.isConnected || !this.client) {
155
+ if (!this.client) {
167
156
  throw new Error("Client not connected");
168
157
  }
169
158
  try {
@@ -182,12 +171,22 @@ var getPersistor = ({
182
171
  onSuccess,
183
172
  clientId
184
173
  }) => {
185
- const connectionName = (redis == null ? void 0 : redis.name) || "default";
174
+ const connectionName = redis ? (redis == null ? void 0 : redis.name) || "default" : "local";
186
175
  if (!persistors[connectionName]) {
187
176
  persistors[connectionName] = new Persistor({
188
177
  redis,
189
- onError,
190
- onSuccess,
178
+ onError: (error) => {
179
+ onError == null ? void 0 : onError(error);
180
+ console.error(
181
+ `\u274C REDIS | Client Error | ${connectionName} | ${redis == null ? void 0 : redis.url}: ${error}`
182
+ );
183
+ },
184
+ onSuccess: () => {
185
+ onSuccess == null ? void 0 : onSuccess();
186
+ console.log(
187
+ `\u{1F4E6} REDIS | Connection Ready | ${connectionName} | ${redis == null ? void 0 : redis.url}`
188
+ );
189
+ },
191
190
  clientId
192
191
  });
193
192
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sebspark/promise-cache",
3
- "version": "2.0.3",
3
+ "version": "2.0.4",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",