Electron Settings - v5.0.0

Properties

Properties

export=: {
    configure: ((customConfig) => void);
    file: (() => string);
    get: {
        (): Promise<SettingsObject>;
        (keyPath): Promise<SettingsValue>;
    };
    getSync: {
        (): SettingsObject;
        (keyPath): SettingsValue;
    };
    has: ((keyPath) => Promise<boolean>);
    hasSync: ((keyPath) => boolean);
    reset: (() => void);
    set: {
        (obj): Promise<void>;
        (keyPath, obj): Promise<void>;
    };
    setSync: {
        (obj): void;
        (keyPath, value): void;
    };
    unset: {
        (): Promise<void>;
        (keyPath): Promise<void>;
    };
    unsetSync: {
        (): void;
        (keyPath): void;
    };
}

Type declaration

  • configure: ((customConfig) => void)
      • (customConfig): void
      • Sets the configuration for Electron Settings. To reset to defaults, use [[reset|reset()]].

        Defaults:

        {
        atomicSave: true,
        fileName: 'settings.json',
        numSpaces: 2,
        prettify: false
        }

        Example

        Update the filename to `cool-settings.json` and prettify
        the output.

        settings.configure({
        fileName: 'cool-settings.json',
        prettify: true
        });

        Parameters

        • customConfig: Partial<Config>

          The custom configuration to use.

        Returns void

  • file: (() => string)
      • (): string
      • Returns the path to the settings file.

        In general, the settings file is stored in your app's user data directory in a file called settings.json. The default user data directory varies by system.

        • macOS - ~/Library/Application\ Support/<Your App>
        • Windows - %APPDATA%/<Your App>
        • Linux - Either $XDG_CONFIG_HOME/<Your App> or ~/.config/<Your App>

        Although it is not recommended, you may change the name or location of the settings file using [[configure|configure()]].

        Returns

        The path to the settings file.

        Example

        Get the path to the settings file.

        settings.file();
        // => /home/nathan/.config/MyApp/settings.json

        Returns string

  • get: {
        (): Promise<SettingsObject>;
        (keyPath): Promise<SettingsValue>;
    }
      • (): Promise<SettingsObject>
      • Gets all settings. For sync, use [[getSync|getSync()]].

        Returns

        A promise which resolves with all settings.

        Example

        Gets all settings.

        const obj = await get();

        Returns Promise<SettingsObject>

      • (keyPath): Promise<SettingsValue>
      • Gets the value at the given key path. For sync, use [[getSync|getSync()]].

        Returns

        A promise which resolves with the value at the given key path.

        Example

        Get the value at `color.name`.

        // Given:
        //
        // {
        // "color": {
        // "name": "cerulean",
        // "code": {
        // "rgb": [0, 179, 230],
        // "hex": "#003BE6"
        // }
        // }
        // }

        const value = await settings.get('color.name');
        // => "cerulean"

        Example

        Get the value at `color.hue`.

        const h = 'hue';
        const value = await settings.get(['color', h]);
        // => undefined

        Example

        Get the value at `color.code.rgb[1]`.

        const h = 'hue';
        const value = await settings.get('color.code.rgb[1]');
        // => 179

        Parameters

        • keyPath: KeyPath

          The key path of the property.

        Returns Promise<SettingsValue>

  • getSync: {
        (): SettingsObject;
        (keyPath): SettingsValue;
    }
      • (): SettingsObject
      • Gets all settings. For async, use [[get|get()]].

        Returns

        All settings.

        Example

        Gets all settings.

        const obj = getSync();

        Returns SettingsObject

      • (keyPath): SettingsValue
      • Gets the value at the given key path. For async, use [[get|get()]].

        Returns

        The value at the given key path.

        Example

        Get the value at `color.name`.

        // Given:
        //
        // {
        // "color": {
        // "name": "cerulean",
        // "code": {
        // "rgb": [0, 179, 230],
        // "hex": "#003BE6"
        // }
        // }
        // }

        const value = settings.getSync('color.name');
        // => "cerulean"

        Example

        Get the value at `color.hue`.

        const h = 'hue';
        const value = settings.getSync(['color', h]);
        // => undefined

        Example

        Get the value at `color.code.rgb[1]`.

        const h = 'hue';
        const value = settings.getSync('color.code.rgb[1]');
        // => 179

        Parameters

        • keyPath: KeyPath

          The key path of the property.

        Returns SettingsValue

  • has: ((keyPath) => Promise<boolean>)
      • (keyPath): Promise<boolean>
      • Checks if the given key path exists. For sync, use [[hasSync|hasSync()]].

        Returns

        A promise which resolves to true if the keyPath exists, else false.

        Example

        Check if the value at `color.name` exists.

        // Given:
        //
        // {
        // "color": {
        // "name": "cerulean",
        // "code": {
        // "rgb": [0, 179, 230],
        // "hex": "#003BE6"
        // }
        // }
        // }

        const exists = await settings.has('color.name');
        // => true

        Example

        Check if the value at `color.hue` exists.

        const h = 'hue';
        const exists = await settings.has(['color', h]);
        // => false

        Example

        Check if the value at `color.code.rgb[1]` exists.

        const exists = await settings.has(color.code.rgb[1]);
        // => true

        Parameters

        • keyPath: KeyPath

          The key path to check.

        Returns Promise<boolean>

  • hasSync: ((keyPath) => boolean)
      • (keyPath): boolean
      • Checks if the given key path exists. For async, use [[hasSync|hasSync()]].

        Returns

        true if the keyPath exists, else false.

        Example

        Check if the value at `color.name` exists.

        // Given:
        //
        // {
        // "color": {
        // "name": "cerulean",
        // "code": {
        // "rgb": [0, 179, 230],
        // "hex": "#003BE6"
        // }
        // }
        // }

        const exists = settings.hasSync('color.name');
        // => true

        Example

        Check if the value at `color.hue` exists.

        const h = 'hue';
        const exists = settings.hasSync(['color', h]);
        // => false

        Example

        Check if the value at `color.code.rgb[1]` exists.

        const exists = settings.hasSync(color.code.rgb[1]);
        // => true

        Parameters

        • keyPath: KeyPath

          The key path to check.

        Returns boolean

  • reset: (() => void)
      • (): void
      • Resets the Electron Settings configuration to defaults.

        Example

        Reset configuration to defaults.

        settings.reset();

        Returns void

  • set: {
        (obj): Promise<void>;
        (keyPath, obj): Promise<void>;
    }
      • (obj): Promise<void>
      • Sets all settings. For sync, use [[setSync|setSync()]].

        Returns

        A promise which resolves when the settings have been set.

        Example

        Set all settings.

        await settings.set({ aqpw: 'nice' });

        Parameters

        • obj: SettingsObject

          The new settings.

        Returns Promise<void>

      • (keyPath, obj): Promise<void>
      • Sets the value at the given key path. For sync, use [[setSync|setSync()]].

        Returns

        A promise which resolves when the setting has been set.

        Example

        Change the value at `color.name` to `sapphire`.

        // Given:
        //
        // {
        // "color": {
        // "name": "cerulean",
        // "code": {
        // "rgb": [0, 179, 230],
        // "hex": "#003BE6"
        // }
        // }
        // }

        await settings.set('color.name', 'sapphire');

        Example

        Set the value of `color.hue` to `blue-ish`.

        const h = 'hue';
        await settings.set(['color', h], 'blue-ish);

        Example

        Change the value of `color.code`.

        await settings.set('color.code', {
        rgb: [16, 31, 134],
        hex: '#101F86'
        });

        Parameters

        • keyPath: KeyPath

          The key path of the property.

        • obj: SettingsValue

        Returns Promise<void>

  • setSync: {
        (obj): void;
        (keyPath, value): void;
    }
      • (obj): void
      • Sets all settings. For async, use [[set|set()]].

        Example

        Set all settings.

        settings.setSync({ aqpw: 'nice' });

        Parameters

        • obj: SettingsObject

          The new settings.

        Returns void

      • (keyPath, value): void
      • Sets the value at the given key path. For async, use [[set|set()]].

        Example

        Change the value at `color.name` to `sapphire`.

        // Given:
        //
        // {
        // "color": {
        // "name": "cerulean",
        // "code": {
        // "rgb": [0, 179, 230],
        // "hex": "#003BE6"
        // }
        // }
        // }

        settings.setSync('color.name', 'sapphire');

        Example

        Set the value of `color.hue` to `blue-ish`.

        const h = 'hue';
        settings.setSync(['color', h], 'blue-ish);

        Example

        Change the value of `color.code`.

        settings.setSync('color.code', {
        rgb: [16, 31, 134],
        hex: '#101F86'
        });

        Parameters

        • keyPath: KeyPath

          The key path of the property.

        • value: SettingsValue

          The value to set.

        Returns void

  • unset: {
        (): Promise<void>;
        (keyPath): Promise<void>;
    }
      • (): Promise<void>
      • Unsets all settings. For sync, use [[unsetSync|unsetSync()]].

        Returns

        A promise which resolves when the settings have been unset.

        Example

        Unsets all settings.

        await settings.unset();

        Returns Promise<void>

      • (keyPath): Promise<void>
      • Unsets the property at the given key path. For sync, use [[unsetSync|unsetSync()]].

        Returns

        A promise which resolves when the setting has been unset.

        Example

        Unset the property `color.name`.

        // Given:
        //
        // {
        // "color": {
        // "name": "cerulean",
        // "code": {
        // "rgb": [0, 179, 230],
        // "hex": "#003BE6"
        // }
        // }
        // }

        await settings.unset('color.name');

        await settings.get('color.name');
        // => undefined

        Example

        Unset the property `color.code.rgba[1]`.

        await settings.unset('color.code.rgba[1]');

        await settings.get('color.code.rgb');
        // => [0, null, 230]

        Parameters

        • keyPath: KeyPath

          The key path of the property.

        Returns Promise<void>

  • unsetSync: {
        (): void;
        (keyPath): void;
    }
      • (): void
      • Unsets all settings. For async, use [[unset|unset()]].

        Example

        Unsets all settings.

        settings.unsetSync();

        Returns void

      • (keyPath): void
      • Unsets the property at the given key path. For async, use [[unset|unset()]].

        Example

        Unset the property `color.name`.

        // Given:
        //
        // {
        // "color": {
        // "name": "cerulean",
        // "code": {
        // "rgb": [0, 179, 230],
        // "hex": "#003BE6"
        // }
        // }
        // }

        settings.unsetSync('color.name');

        settings.getSync('color.name');
        // => undefined

        Example

        Unset the property `color.code.rgba[1]`.

        settings.unsetSync('color.code.rgba[1]');

        settings.getSync('color.code.rgb');
        // => [0, null, 230]

        Parameters

        • keyPath: KeyPath

          The key path of the property.

        Returns void

Generated using TypeDoc