Skip to content

Commit

Permalink
Fix top-level types of send function (#4145)
Browse files Browse the repository at this point in the history
* Add release notes

* Fix types of `send` function

* Fix `send` types in a number of places

* PR feedback
  • Loading branch information
jfdoming authored Feb 19, 2025
1 parent a4d3f27 commit df9e6ec
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 36 deletions.
31 changes: 14 additions & 17 deletions packages/desktop-client/src/components/modals/EditAccess.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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) {
Expand Down
10 changes: 4 additions & 6 deletions packages/loot-core/src/platform/client/fetch/index.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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) {
Expand Down
24 changes: 15 additions & 9 deletions packages/loot-core/src/platform/client/fetch/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 3 additions & 4 deletions packages/loot-core/src/platform/client/fetch/index.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/4145.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [jfdoming]
---

Fix types of `send` function

0 comments on commit df9e6ec

Please sign in to comment.