Skip to content

A local-first personal finance app

[AI] Add api.getPreferences() for reading synced preferences (#8410)

Matiss Janis Aboltinsaf799718f8ed
diff --git a/packages/api/methods.test.ts b/packages/api/methods.test.ts
index 3625a8efc7..0f3e084d98 100644
--- a/packages/api/methods.test.ts
+++ b/packages/api/methods.test.ts
@@ -1110,3 +1110,23 @@ test('Schedules: successfully complete schedules operations', async () => {
]),
);
});
+
+// apis: getPreferences
+test('Preferences: successfully read synced preferences', async () => {
+ await api.loadBudget(budgetName);
+
+ await api.internal?.send('preferences/save', {
+ id: 'numberFormat',
+ value: '1.234,56',
+ });
+ await api.internal?.send('preferences/save', {
+ id: 'hideFraction',
+ value: 'true',
+ });
+
+ const preferences = await api.getPreferences();
+ expect(preferences).toMatchObject({
+ numberFormat: '1.234,56',
+ hideFraction: 'true',
+ });
+});
diff --git a/packages/api/methods.ts b/packages/api/methods.ts
index 888d99709b..a5188f57d7 100644
--- a/packages/api/methods.ts
+++ b/packages/api/methods.ts
@@ -16,6 +16,7 @@ import type {
RuleEntity,
TransactionEntity,
} from '@actual-app/core/types/models';
+import type { SyncedPrefs } from '@actual-app/core/types/prefs';
export { q } from './app/query';
@@ -359,3 +360,8 @@ export function getIDByName(
export function getServerVersion() {
return send('api/get-server-version');
}
+
+/** Read the budget's synced preferences (number format, currency, etc.). */
+export function getPreferences(): Promise<SyncedPrefs> {
+ return send('preferences/get');
+}
diff --git a/packages/api/models.ts b/packages/api/models.ts
index 53b3d98e44..c4c753fb92 100644
--- a/packages/api/models.ts
+++ b/packages/api/models.ts
@@ -1 +1,2 @@
export type * from '@actual-app/core/server/api-models';
+export type { SyncedPrefs as Preferences } from '@actual-app/core/types/prefs';
diff --git a/packages/docs/docs/api/reference.md b/packages/docs/docs/api/reference.md
index 5ff585d420..59cb12c40e 100644
--- a/packages/docs/docs/api/reference.md
+++ b/packages/docs/docs/api/reference.md
@@ -105,7 +105,8 @@ import APIList from './APIList';
"downloadBudget",
"batchBudgetUpdates",
"runQuery",
-"getIDByName"
+"getIDByName",
+"getPreferences"
]} />
## Types of Methods
@@ -842,3 +843,9 @@ get the ID for any Account, Payee, Category or Schedule by providing the corresp
<Method name="getServerVersion" args={[]} returns="Promise<{error?: string;} | {version: string;}>" />
return error or the current server versions.
+
+#### `getPreferences`
+
+<Method name="getPreferences" args={[]} returns="Promise<SyncedPrefs>" />
+
+Returns the budget's synced preferences — settings that sync across devices, such as the number format (`numberFormat`, `hideFraction`), currency (`defaultCurrencyCode`, `currencySymbolPosition`, `currencySpaceBetweenAmountAndSymbol`), date format (`dateFormat`), and first day of the week (`firstDayOfWeekIdx`). All values are strings (or `undefined` if the preference has never been set). The `SyncedPrefs` type is exported from `@actual-app/api/models`.
diff --git a/upcoming-release-notes/api-get-preferences.md b/upcoming-release-notes/api-get-preferences.md
new file mode 100644
index 0000000000..966e37ba23
--- /dev/null
+++ b/upcoming-release-notes/api-get-preferences.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [MatissJanis]
+---
+
+The API can now read a budget's formatting preferences, such as its number and currency format.