separated types and declarations into shared folder + better code proposals

This commit is contained in:
Danya H 2024-06-09 15:42:45 +01:00
parent dc9e713e6b
commit 788be34951
14 changed files with 166 additions and 98 deletions

14
client/package.json Normal file
View File

@ -0,0 +1,14 @@
{
"name": "rage-fw-client",
"version": "0.0.1",
"main": "dist/index.js",
"type": "module",
"files": ["dist/**/*"],
"scripts": {
"build": "tsup"
},
"keywords": [],
"author": "SashaGoncharov19",
"license": "MIT",
"description": "Client side of rage-fw"
}

23
client/src/index.ts Normal file
View File

@ -0,0 +1,23 @@
/// <reference types="../../shared/client/index.d.ts" />
import rpc from 'rage-rpc'
class Player implements RageFW_Player {
public triggerClient<EventName extends RageFW_ClientEvent>(
player: PlayerMp,
eventName: EventName,
...args: RageFW_ClientEventArguments<EventName>
): Promise<RageFW_ClientEventReturn<EventName>> {
return rpc.callClient(player, eventName, ...args)
}
}
export const rage = {
player: new Player(),
}
const bool = rage.player.triggerClient(
{} as PlayerMp,
'customClientEvent',
'arg1',
2,
)

13
client/tsconfig.json Normal file
View File

@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "ESNext",
"lib": ["ESNext", "ES5"],
"module": "ESNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"types": ["../node_modules/@ragempcommunity/types-server", "../../shared/client"]
}
}

View File

@ -1,5 +1,5 @@
{ {
"name": "rage-f-server", "name": "rage-fw-server",
"version": "0.0.1", "version": "0.0.1",
"main": "dist/index.js", "main": "dist/index.js",
"type": "module", "type": "module",
@ -8,7 +8,7 @@
"build": "tsup" "build": "tsup"
}, },
"keywords": [], "keywords": [],
"author": "SashaGonncharov19", "author": "SashaGoncharov19",
"license": "MIT", "license": "MIT",
"description": "Server side of rage-f" "description": "Server side for rage-fw"
} }

View File

@ -1,93 +1,31 @@
/// <reference types="@ragempcommunity/types-server" /> /// <reference types="../../shared/server/index.d.ts" />
import rpc from 'rage-rpc' import rpc from 'rage-rpc'
type RageFW_ServerEvent<K> = K extends keyof RageFW_ICustomServerEvent class Server implements RageFW_Server {
? K public register<EventName extends RageFW_ServerEvent>(
: K extends keyof IServerEvents eventName: EventName,
? K
: never
type RageFW_ServerEventCallback<K> = K extends keyof RageFW_ICustomServerEvent
? (
player: PlayerMp,
...args: Parameters<RageFW_ICustomServerEvent[K]>
) => ReturnType<RageFW_ICustomServerEvent[K]>
: K extends keyof IServerEvents
? ThisifyServerEvents[K]
: never
type RageFW_ClientEvent<K> = K extends keyof RageFW_ICustomClientEvent
? K
: K extends keyof IServerEvents
? K
: never
type RageFW_ClientEventArguments<K> = K extends keyof RageFW_ICustomClientEvent
? Parameters<RageFW_ICustomClientEvent[K]>
: (K extends keyof IServerEvents ? ThisifyServerEvents[K] : never)[]
type RageFW_ClientEventReturn<K> = K extends keyof RageFW_ICustomClientEvent
? ReturnType<RageFW_ICustomClientEvent[K]>
: never
interface RageFW_ICustomServerEvent {
customEvent(arg1: number, arg2: boolean): void
}
interface RageFW_ICustomClientEvent {
customClientEvent(arg1: number, arg2: boolean): boolean
}
class Server {
register<EventName extends string>(
eventName: RageFW_ServerEvent<EventName>,
callback: RageFW_ServerEventCallback<EventName>, callback: RageFW_ServerEventCallback<EventName>,
): void ): void {
rpc.register(eventName, callback as rpc.ProcedureListener)
public register(
eventName: string,
callback: (...args: unknown[]) => unknown,
) {
rpc.register(eventName, callback)
}
// public registerMultiple(events: {
// [name: string]: (player: PlayerMp, ...args: any[]) => any
// }): void
//
// registerMultiple(events: { [name: string]: (...args: any[]) => any }) {
// Object.entries(events).forEach(([name, callback]) =>
// rpc.register(name, (data: any[]) => {
// return Array.isArray(data) ? callback(...data) : callback(data)
// }),
// )
// }
//
// register() {}
}
class Player {
triggerClient<EventName extends string>(
player: PlayerMp,
eventName: RageFW_ClientEvent<EventName>,
...args: RageFW_ClientEventArguments<EventName>
): RageFW_ClientEventReturn<EventName>
public triggerClient(
player: PlayerMp,
eventName: string,
...args: unknown[]
) {
return rpc.callClient(player, eventName, ...args)
} }
} }
export const rage = { export const rage = {
event: new Server(), event: new Server(),
player: new Player(),
} }
rage.event.register('customEvent', (player, arg1, arg2) => { rage.event.register('customServerEvent', (player, arg1, arg2) => {
rage.player.triggerClient(player, 'customClientEvent', arg1, arg2) return true
}) })
function test() {} // public registerMultiple(events: {
// [name: string]: (player: PlayerMp, ...args: any[]) => any
// }): void
//
// registerMultiple(events: { [name: string]: (...args: any[]) => any }) {
// Object.entries(events).forEach(([name, callback]) =>
// rpc.register(name, (data: any[]) => {
// return Array.isArray(data) ? callback(...data) : callback(data)
// }),
// )
// }

View File

@ -8,6 +8,6 @@
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"strict": true, "strict": true,
"skipLibCheck": true, "skipLibCheck": true,
"types": ["../node_modules/@ragempcommunity/types-server"] "types": ["../node_modules/@ragempcommunity/types-server", "../../shared/server"]
} }
} }

25
shared/client/index.d.ts vendored Normal file
View File

@ -0,0 +1,25 @@
/// <reference types="@ragempcommunity/types-client" />
interface RageFW_ICustomClientEvent {
customClientEvent(arg1: string, arg2: number): boolean
}
type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent | keyof IClientEvents
type RageFW_ClientEventArguments<K extends RageFW_ClientEvent> =
K extends keyof RageFW_ICustomClientEvent
? Parameters<RageFW_ICustomClientEvent[K]>
: (K extends keyof IClientEvents ? ThisifyClientEvents[K] : never)[]
type RageFW_ClientEventReturn<K extends RageFW_ClientEvent> =
K extends keyof RageFW_ICustomClientEvent
? ReturnType<RageFW_ICustomClientEvent[K]>
: never
declare class RageFW_Player {
declare triggerClient<EventName extends RageFW_ClientEvent>(
player: PlayerMp,
eventName: EventName,
...args: RageFW_ClientEventArguments<EventName>
): Promise<RageFW_ClientEventReturn<EventName>>
}

View File

@ -0,0 +1,16 @@
{
"name": "rage-fw-shared-client",
"version": "0.0.1",
"type": "module",
"main": "index.d.ts",
"files": [
"index.d.ts"
],
"scripts": {
"test": "tsup"
},
"keywords": [],
"author": "SashaGoncharov19",
"license": "MIT",
"description": "Shared client types for rage-fw"
}

View File

@ -1,13 +0,0 @@
{
"name": "rage-f-shared",
"version": "0.0.1",
"main": "dist/index.js",
"type": "module",
"scripts": {
"test": "tsup"
},
"keywords": [],
"author": "SashaGonncharov19",
"license": "MIT",
"description": "Server side of rage-f"
}

26
shared/server/index.d.ts vendored Normal file
View File

@ -0,0 +1,26 @@
/// <reference types="@ragempcommunity/types-server" />
declare interface RageFW_ICustomServerEvent {
customServerEvent(arg1: string, arg2: number): boolean
}
type RageFW_ServerEvent =
| keyof RageFW_ICustomServerEvent
| keyof IServerEvents
type RageFW_ServerEventCallback<
K extends keyof RageFW_ICustomServerEvent | keyof IServerEvents,
> = K extends keyof RageFW_ICustomServerEvent
? (
player: PlayerMp,
...args: Parameters<RageFW_ICustomServerEvent[K]>
) => ReturnType<RageFW_ICustomServerEvent[K]>
: K extends keyof IServerEvents
? ThisifyServerEvents[K]
: never
declare class RageFW_Server {
declare register<EventName extends RageFW_ServerEvent>(
eventName: EventName,
callback: RageFW_ServerEventCallback<EventName>,
): void
}

View File

@ -0,0 +1,16 @@
{
"name": "rage-fw-shared-server",
"version": "0.0.1",
"type": "module",
"main": "index.d.ts",
"files": [
"index.d.ts"
],
"scripts": {
"test": "tsup"
},
"keywords": [],
"author": "SashaGoncharov19",
"license": "MIT",
"description": "Shared server types for rage-fw"
}

View File

@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}

View File