UNPKG

2.91 kBTypeScriptView Raw
1import {EventEmitter} from 'events';
2
3type WithRequiredProperties<T, K extends keyof T> = T & Required<Pick<T, K>>;
4
5declare class Keyv<Value = any, Options extends Record<string, any> = Record<string, unknown>> extends EventEmitter {
6 /**
7 * `this.opts` is an object containing at least the properties listed
8 * below. However, `Keyv.Options` allows arbitrary properties as well.
9 * These properties can be specified as the second type parameter to `Keyv`.
10 */
11 opts: WithRequiredProperties<
12 Keyv.Options<Value>,
13 'deserialize' | 'namespace' | 'serialize' | 'store' | 'uri'
14 > &
15 Options;
16
17 /**
18 * @param opts The options object is also passed through to the storage adapter. Check your storage adapter docs for any extra options.
19 */
20 constructor(options?: Keyv.Options<Value> & Options);
21 /**
22 * @param uri The connection string URI.
23 *
24 * Merged into the options object as options.uri.
25 * @param opts The options object is also passed through to the storage adapter. Check your storage adapter docs for any extra options.
26 */
27 constructor(uri?: string, options?: Keyv.Options<Value> & Options);
28
29 /** Returns the value. */
30 get<Raw extends boolean = false>(key: string, options?: {raw?: Raw}):
31 Promise<(Raw extends false
32 ? Value
33 : Keyv.DeserializedData<Value>) | undefined>;
34 /**
35 * Set a value.
36 *
37 * By default keys are persistent. You can set an expiry TTL in milliseconds.
38 */
39 set(key: string, value: Value, ttl?: number): Promise<true>;
40 /**
41 * Deletes an entry.
42 *
43 * Returns `true` if the key existed, `false` if not.
44 */
45 delete(key: string): Promise<boolean>;
46 /** Delete all entries in the current namespace. */
47 clear(): Promise<void>;
48}
49
50declare namespace Keyv {
51 interface Options<Value> {
52 [key: string]: any;
53
54 /** Namespace for the current instance. */
55 namespace?: string | undefined;
56 /** A custom serialization function. */
57 serialize?: ((data: DeserializedData<Value>) => string) | undefined;
58 /** A custom deserialization function. */
59 deserialize?: ((data: string) => DeserializedData<Value> | undefined) | undefined;
60 /** The connection string URI. */
61 uri?: string | undefined;
62 /** The storage adapter instance to be used by Keyv. */
63 store?: Store<Value> | undefined;
64 /** Default TTL. Can be overridden by specififying a TTL on `.set()`. */
65 ttl?: number | undefined;
66 /** Specify an adapter to use. e.g `'redis'` or `'mongodb'`. */
67 adapter?: 'redis' | 'mongodb' | 'mongo' | 'sqlite' | 'postgresql' | 'postgres' | 'mysql' | undefined;
68 }
69
70 interface DeserializedData<Value> {
71 value: Value; expires: number | undefined;
72 }
73
74 interface Store<Value> {
75 get(key: string): Value | Promise<Value | undefined> | undefined;
76 set(key: string, value: Value, ttl?: number): any;
77 delete(key: string): boolean | Promise<boolean>;
78 clear(): void | Promise<void>;
79 }
80}
81
82export = Keyv;
83
\No newline at end of file