npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tauri-settings

v0.3.5

Published

A user settings manager for Tauri inspired by electron-settings.

Downloads

562

Readme

Tauri Settings

A user settings manager for Tauri inspired by electron-settings.

Table of Contents

Installation And Usage

Install The Package

The package is available on npm and can be installed using npm or yarn.

# using npm
npm install tauri-settings

# using yarn
yarn add tauri-settings

# using pnpm
pnpm add tauri-settings

Install The Tauri API

If you haven't installed @tauri-apps/api then you will have to install it using npm or yarn as this package internally uses the API.

# using npm
npm install @tauri-apps/api

# using yarn
yarn add @tauri-apps/api

Enable Tauri APIs

The following APIs need to be added to the Tauri allowlist.

{
  "allowlist": {
    "fs": { // see https://tauri.app/v1/api/config/#fsallowlistconfig
      "createDir": true,
      "readDir": true,
      "readFile": true,
      "writeFile": true,
      "scope": ["$APPCONFIG", "$APPCONFIG/*"]
    },
    "path": {
      "all": true
     }
  }
}

Usage

tauri-settings exports a set of standalone functions for quick usage or a SettingsManager class with extra features such as caching. Typescript typings and JSDoc is provided for all the API methods.

The API also uses typescript generics to allow a defined schema to be used. In the following sections, SettingsSchema is an optional generic type for the settings schema. It is highly recommended to use a defined schema to prevent runtime errors.

SettingsManager class can also be initialized with a SettingsSchema generic. This generic will be used by all the methods of the class instance. Apart from basic setters and getters, the SettingsManager class also caches the value of the settings in the memory for quick access. This can also be used to make the api calls synchronous. See Differences From electron-settings: Asynchronous.

Using both the standalone methods and SettingsManager together can cause unexpected behaviour. If a setting is accessed both by the frontend and the backend then not using the caching feature is recommended.

Standalone Functions

tauri-settings exports the following API methods to directly set or get settings for quick usage. Alternatively you can also use SettingsManager. Each of the following methods has an options parameter. See the Config to learn more.

  • async has<SettingsSchema>(key, options = {}): Async function that resolves with a boolean which is true if the given key exists in the settings.
  • async get<SettingsSchema>(key, options = {}): Async function that resolves with the value of the setting corresponding to the given key.
  • async set<SettingsSchema>(key, value, options = {}): Async function that sets the value of a given setting. Resolves with the entire settings object.
  • async getAll<SettingsSchema>(, options = {}): Async function that resolves with the entire settings object.

Here key uses dot notation.

Examples

type Schema = {
  theme: 'dark' | 'light';
  startFullscreen: boolean;
}

get<Schema>('theme').then((theme) => {
  // change the theme
})

// when the theme is changed by the user
set<Schema>('theme').then(() => console.log('theme changed succesfully'));

See the complete API Docs.

SettingsManager

SettingsManager is a class that can be used not only to set and get settings but it is meant to be a complete settings manager. It provides additional features such as caching the settings in the memory, setting defaults and in the future, listening to changes in the settings.

The caching feature stores a copy of the settings on the RAM and does not directly alter the settings file on persistent storage. This can be useful in multiple cases:

The cached settings can be accessed by using the hasCache, getCache or setCache methods. The cache can be synced (written to persistent storage) at any time or the persistent storage can be accessed at any time using the has, get and set methods.

Dot notation is also supported here.

SettingsManager class can also be initialized with the SettingsSchema generic. (see Usage)

Examples

// TypeScript

import { SettingsManager } from 'tauri-settings';

type Schema = {
  theme: 'dark' | 'light';
  startFullscreen: boolean;
}

const settingsManager = new SettingsManager<Schema>(
  { // defaults
    theme: 'light',
    startFullscreen: false
  },
  { // options
    fileName: 'customization-settings'
  }
)

// checks whether the settings file exists and created it if not
// loads the settings if it exists
settingsManager.initialize().then(() => {
  // any key other than 'theme' and 'startFullscreen' will be invalid.
  // theme key will only accept 'dark' or 'light' as a value due to the generic.
  settingsManager.setCache('theme', 'dark');
}

// at a later time
await settingsManager.syncCache();
// JavaScript

import { SettingsManager } from 'tauri-settings';

const settingsManager = new SettingsManager(
  { // defaults
    theme: 'light',
    startFullscreen: false
  },
  { // options
    fileName: 'customization-settings'
  }
);

// checks whether the settings file exists and created it if not
// loads the settings if it exists
settingsManager.initialize().then(() => {
  // there is no schema, so any key will be accepted
  // the user needs to add their own validation scheme
  settingsManager.setCache('theme', 'dark');
}

// at a later time
await settingsManager.syncCache();

See the complete API Docs.

Differences From electron-settings

Asynchronous

Since the Tauri fs API is asynchronous, the API methods exported by tauri-settings are also asynchronous. Methods setSync, getSync, and hasSync from electron-settings are not available.

Even though synchronous fs API is not available, the caching feature of SettingsManager can be used to synchronously set and read the settings.

Dot Notation

electron-settings allows you to access settings by using dot notation. tauri-settings supports (Thanks to https://github.com/harshkhandeparkar/tauri-settings/pull/3) the above feature without the array notation key.array[4].

Example: If the settings schema looks like this:

{
  theme: {
    mode: 'dark',
    accent: 'red'
  }
}

get('theme.mode') will return dark.

The following will NOT work:

{
  search: {
    recents: ['keyword1', 'keyword2', 'keyword3']
  }
}

get('search.recents[3]') will return null whereas get('search.recents') will return the entire recents array.

Config

electron-settings exports a configure() method to configure some of the options such as the fileName. However, tauri-settings doesn't export such a variable due to various reasons. Instead each API method such as get and set, as well as the SettingsManager class have an optional options parameter (See API Docs).


Thank You