index.d.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /// <reference types="node" />
  2. import { EventEmitter } from 'events';
  3. import { OnDidChangeCallback, Options, Unsubscribe, Schema, OnDidAnyChangeCallback } from './types';
  4. declare class Conf<T extends Record<string, any> = Record<string, unknown>> implements Iterable<[keyof T, T[keyof T]]> {
  5. #private;
  6. readonly path: string;
  7. readonly events: EventEmitter;
  8. constructor(partialOptions?: Readonly<Partial<Options<T>>>);
  9. /**
  10. Get an item.
  11. @param key - The key of the item to get.
  12. @param defaultValue - The default value if the item does not exist.
  13. */
  14. get<Key extends keyof T>(key: Key): T[Key];
  15. get<Key extends keyof T>(key: Key, defaultValue: Required<T>[Key]): Required<T>[Key];
  16. get<Key extends string, Value = unknown>(key: Exclude<Key, keyof T>, defaultValue?: Value): Value;
  17. /**
  18. Set an item or multiple items at once.
  19. @param {key|object} - You can use [dot-notation](https://github.com/sindresorhus/dot-prop) in a key to access nested properties. Or a hashmap of items to set at once.
  20. @param value - Must be JSON serializable. Trying to set the type `undefined`, `function`, or `symbol` will result in a `TypeError`.
  21. */
  22. set<Key extends keyof T>(key: Key, value?: T[Key]): void;
  23. set(key: string, value: unknown): void;
  24. set(object: Partial<T>): void;
  25. /**
  26. Check if an item exists.
  27. @param key - The key of the item to check.
  28. */
  29. has<Key extends keyof T>(key: Key | string): boolean;
  30. /**
  31. Reset items to their default values, as defined by the `defaults` or `schema` option.
  32. @see `clear()` to reset all items.
  33. @param keys - The keys of the items to reset.
  34. */
  35. reset<Key extends keyof T>(...keys: Key[]): void;
  36. /**
  37. Delete an item.
  38. @param key - The key of the item to delete.
  39. */
  40. delete<Key extends keyof T>(key: Key): void;
  41. /**
  42. Delete all items.
  43. This resets known items to their default values, if defined by the `defaults` or `schema` option.
  44. */
  45. clear(): void;
  46. /**
  47. Watches the given `key`, calling `callback` on any changes.
  48. @param key - The key wo watch.
  49. @param callback - A callback function that is called on any changes. When a `key` is first set `oldValue` will be `undefined`, and when a key is deleted `newValue` will be `undefined`.
  50. @returns A function, that when called, will unsubscribe.
  51. */
  52. onDidChange<Key extends keyof T>(key: Key, callback: OnDidChangeCallback<T[Key]>): Unsubscribe;
  53. /**
  54. Watches the whole config object, calling `callback` on any changes.
  55. @param callback - A callback function that is called on any changes. When a `key` is first set `oldValue` will be `undefined`, and when a key is deleted `newValue` will be `undefined`.
  56. @returns A function, that when called, will unsubscribe.
  57. */
  58. onDidAnyChange(callback: OnDidAnyChangeCallback<T>): Unsubscribe;
  59. get size(): number;
  60. get store(): T;
  61. set store(value: T);
  62. [Symbol.iterator](): IterableIterator<[keyof T, T[keyof T]]>;
  63. private _encryptData;
  64. private _handleChange;
  65. private readonly _deserialize;
  66. private readonly _serialize;
  67. private _validate;
  68. private _ensureDirectory;
  69. private _write;
  70. private _watch;
  71. private _migrate;
  72. private _containsReservedKey;
  73. private _isVersionInRangeFormat;
  74. private _shouldPerformMigration;
  75. private _get;
  76. private _set;
  77. }
  78. export { Schema, Options };
  79. export default Conf;