Skip to content

A local-first personal finance app

Fix top-level types of `send` function (#4145)

Julian Dominguez-Schatzdf9e6ec66d0f
diff --git a/packages/desktop-client/src/components/modals/EditAccess.tsx b/packages/desktop-client/src/components/modals/EditAccess.tsx
index 923b69ccab..197c8dd3c2 100644
--- a/packages/desktop-client/src/components/modals/EditAccess.tsx
+++ b/packages/desktop-client/src/components/modals/EditAccess.tsx
@@ -10,7 +10,6 @@ import { View } from '@actual-app/components/view';
import { addNotification, popModal, signOut } from 'loot-core/client/actions';
import { send } from 'loot-core/platform/client/fetch';
import { getUserAccessErrors } from 'loot-core/shared/errors';
-import { type Handlers } from 'loot-core/types/handlers';
import { type UserAccessEntity } from 'loot-core/types/models/userAccess';
import { useDispatch } from '../../redux';
@@ -36,22 +35,20 @@ export function EditUserAccess({
const [availableUsers, setAvailableUsers] = useState<[string, string][]>([]);
useEffect(() => {
- send('access-get-available-users', defaultUserAccess.fileId).then(
- (data: Awaited<ReturnType<Handlers['access-get-available-users']>>) => {
- if ('error' in data) {
- setSetError(data.error);
- } else {
- setAvailableUsers(
- data.map(user => [
- user.userId,
- user.displayName
- ? `${user.displayName} (${user.userName})`
- : user.userName,
- ]),
- );
- }
- },
- );
+ send('access-get-available-users', defaultUserAccess.fileId).then(data => {
+ if ('error' in data) {
+ setSetError(data.error);
+ } else {
+ setAvailableUsers(
+ data.map(user => [
+ user.userId,
+ user.displayName
+ ? `${user.displayName} (${user.userName})`
+ : user.userName,
+ ]),
+ );
+ }
+ });
}, [defaultUserAccess.fileId]);
async function onSave(close: () => void) {
diff --git a/packages/loot-core/src/platform/client/fetch/index.browser.ts b/packages/loot-core/src/platform/client/fetch/index.browser.ts
index 0a7f0d0569..defc478021 100644
--- a/packages/loot-core/src/platform/client/fetch/index.browser.ts
+++ b/packages/loot-core/src/platform/client/fetch/index.browser.ts
@@ -151,10 +151,9 @@ export const init: T.Init = async function (worker) {
};
export const send: T.Send = function (
- name,
- args,
- { catchErrors = false } = {},
-) {
+ ...params: Parameters<T.Send>
+): ReturnType<T.Send> {
+ const [name, args, { catchErrors = false } = {}] = params;
return new Promise((resolve, reject) => {
const id = uuidv4();
@@ -171,8 +170,7 @@ export const send: T.Send = function (
} else {
globalWorker.postMessage(message);
}
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- }) as any;
+ });
};
export const sendCatch: T.SendCatch = function (name, args) {
diff --git a/packages/loot-core/src/platform/client/fetch/index.d.ts b/packages/loot-core/src/platform/client/fetch/index.d.ts
index d6e4d4a31c..dcb660ea90 100644
--- a/packages/loot-core/src/platform/client/fetch/index.d.ts
+++ b/packages/loot-core/src/platform/client/fetch/index.d.ts
@@ -7,25 +7,31 @@ export type Init = typeof init;
export function send<K extends keyof Handlers>(
name: K,
- args?: Parameters<Handlers[K]>[0],
- options?: { catchErrors: true },
-): ReturnType<
- | { data: Handlers[K] }
- | { error: { type: 'APIError' | 'InternalError'; message: string } }
+ args: Parameters<Handlers[K]>[0],
+ options: { catchErrors: true },
+): Promise<
+ | { data: Awaited<ReturnType<Handlers[K]>>; error: undefined }
+ | {
+ data: undefined;
+ error: { type: 'APIError' | 'InternalError'; message: string };
+ }
>;
export function send<K extends keyof Handlers>(
name: K,
args?: Parameters<Handlers[K]>[0],
options?: { catchErrors?: boolean },
-): ReturnType<Handlers[K]>;
+): Promise<Awaited<ReturnType<Handlers[K]>>>;
export type Send = typeof send;
export function sendCatch<K extends keyof Handlers>(
name: K,
args?: Parameters<Handlers[K]>[0],
-): ReturnType<
- | { data: Handlers[K] }
- | { error: { type: 'APIError' | 'InternalError'; message: string } }
+): Promise<
+ | { data: Awaited<ReturnType<Handlers[K]>>; error: undefined }
+ | {
+ data: undefined;
+ error: { type: 'APIError' | 'InternalError'; message: string };
+ }
>;
export type SendCatch = typeof sendCatch;
diff --git a/packages/loot-core/src/platform/client/fetch/index.web.ts b/packages/loot-core/src/platform/client/fetch/index.web.ts
index c74ce488a6..b28c1d680a 100644
--- a/packages/loot-core/src/platform/client/fetch/index.web.ts
+++ b/packages/loot-core/src/platform/client/fetch/index.web.ts
@@ -79,10 +79,9 @@ export const init: T.Init = async function () {
};
export const send: T.Send = function (
- name,
- args,
- { catchErrors = false } = {},
-) {
+ ...params: Parameters<T.Send>
+): ReturnType<T.Send> {
+ const [name, args, { catchErrors = false } = {}] = params;
return new Promise((resolve, reject) => {
const id = uuidv4();
replyHandlers.set(id, { resolve, reject });
diff --git a/upcoming-release-notes/4145.md b/upcoming-release-notes/4145.md
new file mode 100644
index 0000000000..d8f8660e42
--- /dev/null
+++ b/upcoming-release-notes/4145.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [jfdoming]
+---
+
+Fix types of `send` function