keyv 5.0.2 → 5.1.0

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/README.md CHANGED
@@ -50,6 +50,36 @@ First, create a new Keyv instance.
50
50
  import Keyv from 'keyv';
51
51
  ```
52
52
 
53
+ ### Type-safe Usage
54
+
55
+ You can create a `Keyv` instance with a generic type to enforce type safety for the values stored. Additionally, both the `get` and `set` methods support specifying custom types for specific use cases.
56
+
57
+ #### Example with Instance-level Generic Type:
58
+
59
+ ```ts
60
+ const keyv = new Keyv<number>(); // Instance handles only numbers
61
+ await keyv.set('key1', 123);
62
+ const value = await keyv.get('key1'); // value is inferred as number
63
+ ```
64
+
65
+ #### Example with Method-level Generic Type:
66
+
67
+ You can also specify a type directly in the `get` or `set` methods, allowing flexibility for different types of values within the same instance.
68
+
69
+ ```ts
70
+ const keyv = new Keyv(); // Generic type not specified at instance level
71
+
72
+ await keyv.set<string>('key2', 'some string'); // Method-level type for this value
73
+ const strValue = await keyv.get<string>('key2'); // Explicitly typed as string
74
+
75
+ await keyv.set<number>('key3', 456); // Storing a number in the same instance
76
+ const numValue = await keyv.get<number>('key3'); // Explicitly typed as number
77
+ ```
78
+
79
+ This makes `Keyv` highly adaptable to different data types while maintaining type safety.
80
+
81
+ ### Using Storage Adapters
82
+
53
83
  Once you have created your Keyv instance you can use it as a simple key-value store with `in-memory` by default. To use a storage adapter, create an instance of the adapter and pass it to the Keyv constructor. Here are some examples:
54
84
 
55
85
  ```js
@@ -434,4 +464,4 @@ We welcome contributions to Keyv! 🎉 Here are some guides to get you started w
434
464
 
435
465
  # License
436
466
 
437
- [MIT © Jared Wray](LICENSE)
467
+ [MIT © Jared Wray](LICENSE)
package/dist/index.cjs CHANGED
@@ -389,10 +389,10 @@ var Keyv = class extends event_manager_default {
389
389
  if (typeof value === "symbol") {
390
390
  this.emit("error", "symbol cannot be serialized");
391
391
  }
392
- value = { value, expires };
393
- value = await this.opts.serialize(value);
394
- await store.set(keyPrefixed, value, ttl);
395
- this.hooks.trigger("postSet" /* POST_SET */, { key: keyPrefixed, value, ttl });
392
+ const formattedValue = { value, expires };
393
+ const serializedValue = await this.opts.serialize(formattedValue);
394
+ await store.set(keyPrefixed, serializedValue, ttl);
395
+ this.hooks.trigger("postSet" /* POST_SET */, { key: keyPrefixed, value: serializedValue, ttl });
396
396
  this.stats.set();
397
397
  return true;
398
398
  }
package/dist/index.d.cts CHANGED
@@ -41,7 +41,7 @@ declare class StatsManager extends EventManager {
41
41
 
42
42
  type DeserializedData<Value> = {
43
43
  value?: Value;
44
- expires?: number;
44
+ expires?: number | null;
45
45
  };
46
46
  interface CompressionAdapter {
47
47
  compress(value: any, options?: any): Promise<any>;
@@ -63,7 +63,7 @@ type StoredDataNoRaw<Value> = Value | undefined;
63
63
  type StoredDataRaw<Value> = DeserializedData<Value> | undefined;
64
64
  type StoredData<Value> = StoredDataNoRaw<Value> | StoredDataRaw<Value>;
65
65
  interface IEventEmitter {
66
- on(event: string, listener: (...arguments_: any[]) => void): void;
66
+ on(event: string, listener: (...arguments_: any[]) => void): this;
67
67
  }
68
68
  interface KeyvStoreAdapter extends IEventEmitter {
69
69
  opts: any;
@@ -100,7 +100,7 @@ type KeyvOptions_ = Omit<KeyvOptions, 'store'> & {
100
100
  store: KeyvStoreAdapter | Map<any, any> & KeyvStoreAdapter;
101
101
  };
102
102
  type IteratorFunction = (argument: any) => AsyncGenerator<any, void>;
103
- declare class Keyv extends EventManager {
103
+ declare class Keyv<GenericValue = any> extends EventManager {
104
104
  opts: KeyvOptions_;
105
105
  iterator?: IteratorFunction;
106
106
  hooks: HooksManager;
@@ -113,19 +113,19 @@ declare class Keyv extends EventManager {
113
113
  _getKeyPrefixArray(keys: string[]): string[];
114
114
  _getKeyUnprefix(key: string): string;
115
115
  _isValidStorageAdapter(store: KeyvStoreAdapter | any): boolean;
116
- get<Value>(key: string, options?: {
116
+ get<Value = GenericValue>(key: string, options?: {
117
117
  raw: false;
118
118
  }): Promise<StoredDataNoRaw<Value>>;
119
- get<Value>(key: string, options?: {
119
+ get<Value = GenericValue>(key: string, options?: {
120
120
  raw: true;
121
121
  }): Promise<StoredDataRaw<Value>>;
122
- get<Value>(key: string[], options?: {
122
+ get<Value = GenericValue>(key: string[], options?: {
123
123
  raw: false;
124
124
  }): Promise<Array<StoredDataNoRaw<Value>>>;
125
- get<Value>(key: string[], options?: {
125
+ get<Value = GenericValue>(key: string[], options?: {
126
126
  raw: true;
127
127
  }): Promise<Array<StoredDataRaw<Value>>>;
128
- set(key: string, value: any, ttl?: number): Promise<boolean>;
128
+ set<Value = GenericValue>(key: string, value: Value, ttl?: number): Promise<boolean>;
129
129
  delete(key: string | string[]): Promise<boolean>;
130
130
  clear(): Promise<void>;
131
131
  has(key: string): Promise<boolean>;
package/dist/index.d.ts CHANGED
@@ -41,7 +41,7 @@ declare class StatsManager extends EventManager {
41
41
 
42
42
  type DeserializedData<Value> = {
43
43
  value?: Value;
44
- expires?: number;
44
+ expires?: number | null;
45
45
  };
46
46
  interface CompressionAdapter {
47
47
  compress(value: any, options?: any): Promise<any>;
@@ -63,7 +63,7 @@ type StoredDataNoRaw<Value> = Value | undefined;
63
63
  type StoredDataRaw<Value> = DeserializedData<Value> | undefined;
64
64
  type StoredData<Value> = StoredDataNoRaw<Value> | StoredDataRaw<Value>;
65
65
  interface IEventEmitter {
66
- on(event: string, listener: (...arguments_: any[]) => void): void;
66
+ on(event: string, listener: (...arguments_: any[]) => void): this;
67
67
  }
68
68
  interface KeyvStoreAdapter extends IEventEmitter {
69
69
  opts: any;
@@ -100,7 +100,7 @@ type KeyvOptions_ = Omit<KeyvOptions, 'store'> & {
100
100
  store: KeyvStoreAdapter | Map<any, any> & KeyvStoreAdapter;
101
101
  };
102
102
  type IteratorFunction = (argument: any) => AsyncGenerator<any, void>;
103
- declare class Keyv extends EventManager {
103
+ declare class Keyv<GenericValue = any> extends EventManager {
104
104
  opts: KeyvOptions_;
105
105
  iterator?: IteratorFunction;
106
106
  hooks: HooksManager;
@@ -113,19 +113,19 @@ declare class Keyv extends EventManager {
113
113
  _getKeyPrefixArray(keys: string[]): string[];
114
114
  _getKeyUnprefix(key: string): string;
115
115
  _isValidStorageAdapter(store: KeyvStoreAdapter | any): boolean;
116
- get<Value>(key: string, options?: {
116
+ get<Value = GenericValue>(key: string, options?: {
117
117
  raw: false;
118
118
  }): Promise<StoredDataNoRaw<Value>>;
119
- get<Value>(key: string, options?: {
119
+ get<Value = GenericValue>(key: string, options?: {
120
120
  raw: true;
121
121
  }): Promise<StoredDataRaw<Value>>;
122
- get<Value>(key: string[], options?: {
122
+ get<Value = GenericValue>(key: string[], options?: {
123
123
  raw: false;
124
124
  }): Promise<Array<StoredDataNoRaw<Value>>>;
125
- get<Value>(key: string[], options?: {
125
+ get<Value = GenericValue>(key: string[], options?: {
126
126
  raw: true;
127
127
  }): Promise<Array<StoredDataRaw<Value>>>;
128
- set(key: string, value: any, ttl?: number): Promise<boolean>;
128
+ set<Value = GenericValue>(key: string, value: Value, ttl?: number): Promise<boolean>;
129
129
  delete(key: string | string[]): Promise<boolean>;
130
130
  clear(): Promise<void>;
131
131
  has(key: string): Promise<boolean>;
package/dist/index.js CHANGED
@@ -363,10 +363,10 @@ var Keyv = class extends event_manager_default {
363
363
  if (typeof value === "symbol") {
364
364
  this.emit("error", "symbol cannot be serialized");
365
365
  }
366
- value = { value, expires };
367
- value = await this.opts.serialize(value);
368
- await store.set(keyPrefixed, value, ttl);
369
- this.hooks.trigger("postSet" /* POST_SET */, { key: keyPrefixed, value, ttl });
366
+ const formattedValue = { value, expires };
367
+ const serializedValue = await this.opts.serialize(formattedValue);
368
+ await store.set(keyPrefixed, serializedValue, ttl);
369
+ this.hooks.trigger("postSet" /* POST_SET */, { key: keyPrefixed, value: serializedValue, ttl });
370
370
  this.stats.set();
371
371
  return true;
372
372
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keyv",
3
- "version": "5.0.2",
3
+ "version": "5.1.0",
4
4
  "description": "Simple key-value storage with support for multiple backends",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",