UNPKG

3.47 kBTypeScriptView Raw
1/// <reference types="node" resolution-mode="require"/>
2import { type OnDidChangeCallback, type Options, type Unsubscribe, type OnDidAnyChangeCallback } from './types.js';
3export default class Conf<T extends Record<string, any> = Record<string, unknown>> implements Iterable<[keyof T, T[keyof T]]> {
4 #private;
5 readonly path: string;
6 readonly events: EventTarget;
7 constructor(partialOptions?: Readonly<Partial<Options<T>>>);
8 /**
9 Get an item.
10
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
20 @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.
21 @param value - Must be JSON serializable. Trying to set the type `undefined`, `function`, or `symbol` will result in a `TypeError`.
22 */
23 set<Key extends keyof T>(key: Key, value?: T[Key]): void;
24 set(key: string, value: unknown): void;
25 set(object: Partial<T>): void;
26 /**
27 Check if an item exists.
28
29 @param key - The key of the item to check.
30 */
31 has<Key extends keyof T>(key: Key | string): boolean;
32 /**
33 Reset items to their default values, as defined by the `defaults` or `schema` option.
34
35 @see `clear()` to reset all items.
36
37 @param keys - The keys of the items to reset.
38 */
39 reset<Key extends keyof T>(...keys: Key[]): void;
40 /**
41 Delete an item.
42
43 @param key - The key of the item to delete.
44 */
45 delete<Key extends keyof T>(key: Key): void;
46 /**
47 Delete all items.
48
49 This resets known items to their default values, if defined by the `defaults` or `schema` option.
50 */
51 clear(): void;
52 /**
53 Watches the given `key`, calling `callback` on any changes.
54
55 @param key - The key wo watch.
56 @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`.
57 @returns A function, that when called, will unsubscribe.
58 */
59 onDidChange<Key extends keyof T>(key: Key, callback: OnDidChangeCallback<T[Key]>): Unsubscribe;
60 /**
61 Watches the whole config object, calling `callback` on any changes.
62
63 @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`.
64 @returns A function, that when called, will unsubscribe.
65 */
66 onDidAnyChange(callback: OnDidAnyChangeCallback<T>): Unsubscribe;
67 get size(): number;
68 get store(): T;
69 set store(value: T);
70 [Symbol.iterator](): IterableIterator<[keyof T, T[keyof T]]>;
71 private _encryptData;
72 private _handleChange;
73 private readonly _deserialize;
74 private readonly _serialize;
75 private _validate;
76 private _ensureDirectory;
77 private _write;
78 private _watch;
79 private _migrate;
80 private _containsReservedKey;
81 private _isVersionInRangeFormat;
82 private _shouldPerformMigration;
83 private _get;
84 private _set;
85}
86export type { Options, Schema } from './types.js';