index.d.ts 4.1 KB

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