Skip to content

Commit

Permalink
typings: Update RPC.send + fix RPC.register
Browse files Browse the repository at this point in the history
  • Loading branch information
xLuxy committed Oct 31, 2023
1 parent 3f2665c commit 9549e08
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
7 changes: 4 additions & 3 deletions types/client/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1420,12 +1420,13 @@ declare module "@altv/client" {
}

export namespace RPC {
export type GenericRpcEventHandler<T extends unknown[]> = (...args: T) => Promise<unknown> | unknown;
export type GenericRpcEventHandler<T extends unknown[] = unknown[], U = unknown> = (...args: T) => Promise<U> | U;

export function send<E extends keyof altShared.RPC.CustomPlayerToServerRpcEvent>(rpcName: E, ...args: Parameters<altShared.RPC.CustomPlayerToServerRpcEvent[E]>): Promise<ReturnType<altShared.RPC.CustomPlayerToServerRpcEvent[E]>>;
export function send<E extends string>(rpcName: Exclude<E, keyof altShared.RPC.CustomPlayerToServerRpcEvent>, ...args: unknown[]): Promise<unknown>;

export function register<E extends keyof altShared.RPC.CustomServerToPlayerRpcEvent>(rpcName: E, handler: GenericRpcEventHandler<Parameters<altShared.RPC.CustomServerToPlayerRpcEvent[E]>>): RPCHandler;
export function register<E extends string>(rpcName: Exclude<E, keyof altShared.RPC.CustomServerToPlayerRpcEvent>, handler: GenericRpcEventHandler<unknown[]>): RPCHandler;
export function register<E extends keyof altShared.RPC.CustomServerToPlayerRpcEvent>(rpcName: E, handler: GenericRpcEventHandler<Parameters<altShared.RPC.CustomServerToPlayerRpcEvent[E]>, ReturnType<altShared.RPC.CustomServerToPlayerRpcEvent[E]>>): RPCHandler;
export function register<E extends string>(rpcName: Exclude<E, keyof altShared.RPC.CustomServerToPlayerRpcEvent>, handler: GenericRpcEventHandler): RPCHandler;
}

// DO NOT TOUCH THIS - This is only here so client / server can extend Utils namespace using merging
Expand Down
2 changes: 1 addition & 1 deletion types/client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@altv/client",
"version": "0.0.7",
"version": "0.0.8",
"description": "This package contains the type definitions for the alt:V JS module v2 client types",
"types": "index.d.ts",
"files": [
Expand Down
10 changes: 7 additions & 3 deletions types/server/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ declare module "@altv/server" {
playScenario(name: string): void;

sendRPC<E extends keyof altShared.RPC.CustomServerToPlayerRpcEvent>(rpcName: E, ...args: Parameters<altShared.RPC.CustomServerToPlayerRpcEvent[E]>): Promise<ReturnType<altShared.RPC.CustomServerToPlayerRpcEvent[E]>>;
sendRPC<E extends string>(rpcName: Exclude<E, keyof altShared.RPC.CustomServerToPlayerRpcEvent>, ...args: unknown[]): Promise<unknown>;

readonly meta: PlayerMeta;
readonly localMeta: PlayerLocalMeta;
Expand Down Expand Up @@ -915,10 +916,13 @@ declare module "@altv/server" {
}

export namespace RPC {
export type CustomPlayerRpcEventHandler<T extends unknown[], U extends Player = Player> = (player: U, ...args: T) => unknown | Promise<unknown>;
export type CustomPlayerRpcEventHandler<T extends unknown[], U extends Player = Player, V = unknown> = (player: U, ...args: T) => Promise<V> | V;

export function register<U extends Player = Player, E extends keyof altShared.RPC.CustomPlayerToServerRpcEvent>(rpcName: E, handler: CustomPlayerRpcEventHandler<Parameters<altShared.RPC.CustomPlayerToServerRpcEvent[E]>, U>): RPCHandler;
export function register<U extends Player = Player, E extends string>(rpcName: Exclude<E, keyof altShared.RPC.CustomPlayerToServerRpcEvent>, handler: CustomPlayerRpcEventHandler<unknown[], U>): RPCHandler;
export function register<U extends Player, E extends keyof altShared.RPC.CustomPlayerToServerRpcEvent = keyof altShared.RPC.CustomPlayerToServerRpcEvent>(
rpcName: E,
handler: CustomPlayerRpcEventHandler<Parameters<altShared.RPC.CustomPlayerToServerRpcEvent[E]>, U, ReturnType<altShared.RPC.CustomPlayerToServerRpcEvent[E]>>
): RPCHandler;
export function register<U extends Player, E extends string = string>(rpcName: Exclude<E, keyof altShared.RPC.CustomPlayerToServerRpcEvent>, handler: CustomPlayerRpcEventHandler<unknown[], U>): RPCHandler;
}

export abstract class ConnectionInfo {
Expand Down
2 changes: 1 addition & 1 deletion types/server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@altv/server",
"version": "0.0.7",
"version": "0.0.8",
"description": "This package contains the type definitions for the alt:V JS module v2 server types",
"types": "index.d.ts",
"files": [
Expand Down

0 comments on commit 9549e08

Please sign in to comment.