fix bad typing

There were quite some errors, where the type was passed the wrong way
```
// This is invalid
"change-password": ({ old_password: string, new_password: string, verify_password: string})

// What was actually meant
"change-password": (data: { old_password: string, new_password: string, verify_password: string})
```

The whole callback function is also very verbose as is, with fluff we don't need.
It's always a function that returns void, so there's no real information to be gained
by spelling it out time and time again.

Let's use a helper type that just accepts the payload.
That should make the above error impossible to do.
This commit is contained in:
Reto Brunner 2024-03-24 13:13:44 +01:00
parent fceffd42b9
commit e9ef59b641

View file

@ -16,204 +16,155 @@ type Session = {
token: string; token: string;
}; };
type EventHandler<T> = (data: T) => void;
type NoPayloadEventHandler = EventHandler<void>;
interface ServerToClientEvents { interface ServerToClientEvents {
"auth:failed": () => void;
"auth:start": (serverHash: number) => void; "auth:start": (serverHash: number) => void;
"auth:success": () => void; "auth:failed": NoPayloadEventHandler;
"auth:success": NoPayloadEventHandler;
"upload:auth": (token: string) => void; "upload:auth": (token: string) => void;
changelog: (data: SharedChangelogData) => void; changelog: EventHandler<SharedChangelogData>;
"changelog:newversion": () => void; "changelog:newversion": NoPayloadEventHandler;
"channel:state": (data: {chan: number; state: ChanState}) => void; "channel:state": EventHandler<{chan: number; state: ChanState}>;
"change-password": ({success, error}: {success: boolean; error?: any}) => void; "change-password": EventHandler<{success: boolean; error?: any}>;
commands: (data: string[]) => void; commands: EventHandler<string[]>;
configuration: (config: SharedConfiguration | LockedSharedConfiguration) => void; configuration: EventHandler<SharedConfiguration | LockedSharedConfiguration>;
"push:issubscribed": (isSubscribed: boolean) => void; "push:issubscribed": EventHandler<boolean>;
"push:unregister": () => void; "push:unregister": NoPayloadEventHandler;
"sessions:list": (data: Session[]) => void; "sessions:list": EventHandler<Session[]>;
"mentions:list": (data: SharedMention[]) => void; "mentions:list": EventHandler<SharedMention[]>;
"setting:new": ({name: string, value: any}) => void; "setting:new": EventHandler<{name: string; value: any}>;
"setting:all": (settings: {[key: string]: any}) => void; "setting:all": EventHandler<{[key: string]: any}>;
"history:clear": ({target}: {target: number}) => void; "history:clear": EventHandler<{target: number}>;
"mute:changed": (response: {target: number; status: boolean}) => void; "mute:changed": EventHandler<{target: number; status: boolean}>;
names: (data: {id: number; users: SharedUser[]}) => void; names: EventHandler<{id: number; users: SharedUser[]}>;
network: (data: {networks: SharedNetwork[]}) => void; network: EventHandler<{networks: SharedNetwork[]}>;
"network:options": (data: {network: string; serverOptions: {[key: string]: any}}) => void; "network:options": EventHandler<{network: string; serverOptions: {[key: string]: any}}>;
"network:status": (data: {network: string; connected: boolean; secure: boolean}) => void; "network:status": EventHandler<{network: string; connected: boolean; secure: boolean}>;
"network:info": (data: {uuid: string}) => void; "network:info": EventHandler<{uuid: string}>;
"network:name": (data: {uuid: string; name: string}) => void; "network:name": EventHandler<{uuid: string; name: string}>;
nick: (data: {network: string; nick: string}) => void; nick: EventHandler<{network: string; nick: string}>;
open: (id: number) => void; open: (id: number) => void;
part: (data: {chan: number}) => void; part: EventHandler<{chan: number}>;
"sign-out": () => void; "sign-out": NoPayloadEventHandler;
sync_sort: ( sync_sort: EventHandler<
data: | {type: "networks"; order: string[]; target: string}
| { | {type: "channels"; order: number[]; target: string}
type: "networks"; >;
order: string[];
target: string;
}
| {
type: "channels";
order: number[];
target: string;
}
) => void;
topic: (data: {chan: number; topic: string}) => void; topic: EventHandler<{chan: number; topic: string}>;
users: (data: {chan: number}) => void; users: EventHandler<{chan: number}>;
more: ({ more: EventHandler<{chan: number; messages: SharedMsg[]; totalMessages: number}>;
chan,
messages,
totalMessages,
}: {
chan: number;
messages: SharedMsg[];
totalMessages: number;
}) => void;
"msg:preview": ({id, chan, preview}: {id: number; chan: number; preview: LinkPreview}) => void; "msg:preview": EventHandler<{id: number; chan: number; preview: LinkPreview}>;
"msg:special": (data: {chan: number; data?: Record<string, any>}) => void; "msg:special": EventHandler<{chan: number; data?: Record<string, any>}>;
msg: (data: {msg: ClientMessage; chan: number; highlight?: number; unread?: number}) => void; msg: EventHandler<{msg: ClientMessage; chan: number; highlight?: number; unread?: number}>;
init: ({ init: EventHandler<{active: number; networks: SharedNetwork[]; token: string}>;
active,
networks,
token,
}: {
active: number;
networks: SharedNetwork[];
token: string;
}) => void;
"search:results": (response: SearchResponse) => void; "search:results": (response: SearchResponse) => void;
quit: (args: {network: string}) => void; quit: EventHandler<{network: string}>;
error: (error: any) => void; error: (error: any) => void;
connecting: () => void;
join: (args: { connecting: NoPayloadEventHandler;
shouldOpen: boolean;
index: number; join: EventHandler<{shouldOpen: boolean; index: number; network: string; chan: InitClientChan}>;
network: string;
chan: InitClientChan;
}) => void;
} }
interface ClientToServerEvents { interface ClientToServerEvents {
"auth:perform": "auth:perform": EventHandler<
| (({user, password}: {user: string; password: string}) => void) | {user: string; password: string}
| (({ | {
user,
token,
lastMessage,
openChannel,
hasConfig,
}: {
user: string; user: string;
token: string; token: string;
lastMessage: number; lastMessage: number;
openChannel: number | null; openChannel: number | null;
hasConfig: boolean; hasConfig: boolean;
}) => void); }
>;
changelog: () => void; changelog: NoPayloadEventHandler;
"change-password": ({ "change-password": EventHandler<{
old_password: string, old_password: string;
new_password: string, new_password: string;
verify_password: string, verify_password: string;
}) => void; }>;
open: (channelId: number) => void; open: (channelId: number) => void;
names: ({target: number}) => void; names: EventHandler<{target: number}>;
input: ({target, text}: {target: number; text: string}) => void; input: EventHandler<{target: number; text: string}>;
"upload:auth": () => void; "upload:auth": NoPayloadEventHandler;
"upload:ping": (token: string) => void; "upload:ping": (token: string) => void;
"mute:change": (response: {target: number; setMutedTo: boolean}) => void; "mute:change": EventHandler<{target: number; setMutedTo: boolean}>;
"push:register": (subscriptionJson: PushSubscriptionJSON) => void; "push:register": EventHandler<PushSubscriptionJSON>;
"push:unregister": () => void; "push:unregister": NoPayloadEventHandler;
"setting:get": () => void; "setting:get": NoPayloadEventHandler;
"setting:set": ({name: string, value: any}) => void; "setting:set": EventHandler<{name: string; value: any}>;
"sessions:get": () => void; "sessions:get": NoPayloadEventHandler;
sort: ({type, order}: {type: string; order: any; target?: string}) => void; sort: EventHandler<{type: string; order: any; target?: string}>;
"mentions:dismiss": (msgId: number) => void; "mentions:dismiss": (msgId: number) => void;
"mentions:dismiss_all": () => void; "mentions:dismiss_all": NoPayloadEventHandler;
"mentions:get": () => void; "mentions:get": NoPayloadEventHandler;
more: ({ more: EventHandler<{target: number; lastId: number; condensed: boolean}>;
target,
lastId,
condensed,
}: {
target: number;
lastId: number;
condensed: boolean;
}) => void;
"msg:preview:toggle": ({ "msg:preview:toggle": EventHandler<{
target,
messageIds,
msgId,
shown,
link,
}: {
target: number; target: number;
messageIds?: number[]; messageIds?: number[];
msgId?: number; msgId?: number;
shown?: boolean | null; shown?: boolean | null;
link?: string; link?: string;
}) => void; }>;
"network:get": (uuid: string) => void; "network:get": (uuid: string) => void;
// TODO typing
"network:edit": (data: Record<string, any>) => void; "network:edit": (data: Record<string, any>) => void;
"network:new": (data: Record<string, any>) => void; "network:new": (data: Record<string, any>) => void;
"sign-out": (token?: string) => void; "sign-out": (token?: string) => void;
"history:clear": ({target}: {target: number}) => void; "history:clear": EventHandler<{target: number}>;
search: ({ search: EventHandler<{
networkUuid,
channelName,
searchTerm,
offset,
}: {
networkUuid?: string; networkUuid?: string;
channelName?: string; channelName?: string;
searchTerm?: string; searchTerm?: string;
offset: number; offset: number;
}) => void; }>;
} }
// eslint-disable-next-line @typescript-eslint/no-empty-interface // eslint-disable-next-line @typescript-eslint/no-empty-interface