From 75b528ca45141b5a8127351da22bccfdc20824c9 Mon Sep 17 00:00:00 2001 From: Danya H Date: Tue, 18 Jun 2024 21:03:33 +0100 Subject: [PATCH] renamed types to follow similar naming pattern --- cef/src/index.ts | 12 ++--- cef/src/types/cef.ts | 5 ++- cef/src/types/client.ts | 3 +- cef/src/types/server.ts | 3 +- client/src/core/browser.ts | 8 ++++ client/src/core/client.ts | 20 +++++++++ client/src/core/index.ts | 4 ++ client/src/{ => core}/logger.ts | 2 +- client/src/core/player.ts | 49 ++++++++++++++++++++ client/src/index.ts | 79 +-------------------------------- client/src/types/client.ts | 10 ++--- client/src/types/server.ts | 11 +++-- server/src/core/logger.ts | 1 + server/src/core/player.ts | 8 ++-- server/src/core/server.ts | 46 +++++++++---------- server/src/index.ts | 2 +- server/src/types/client.ts | 4 +- server/src/types/server.ts | 18 ++++---- 18 files changed, 143 insertions(+), 142 deletions(-) create mode 100644 client/src/core/browser.ts create mode 100644 client/src/core/client.ts create mode 100644 client/src/core/index.ts rename client/src/{ => core}/logger.ts (93%) create mode 100644 client/src/core/player.ts diff --git a/cef/src/index.ts b/cef/src/index.ts index 9e56774..c451d49 100644 --- a/cef/src/index.ts +++ b/cef/src/index.ts @@ -4,15 +4,15 @@ import { _CefEventHasArgs, _ClientEventHasArgs, _ServerEventHasArgs, - RageFW_CefArguments, + RageFW_CefArgs, RageFW_CefCallback, RageFW_CefReturn, - RageFW_ClientArguments, + RageFW_ClientArgs, RageFW_ClientReturn, RageFW_ICustomCefEvent, RageFW_ICustomClientEvent, RageFW_ICustomServerEvent, - RageFW_ServerArguments, + RageFW_ServerArgs, RageFW_ServerReturn, } from './types' @@ -29,7 +29,7 @@ class Cef { public trigger( eventName: EventName, ...args: _CefEventHasArgs extends true - ? [RageFW_CefArguments] + ? [RageFW_CefArgs] : [] ): Promise> { if ('mp' in window) { @@ -44,7 +44,7 @@ class Cef { public triggerServer( eventName: EventName, ...args: _ServerEventHasArgs extends true - ? [RageFW_ServerArguments] + ? [RageFW_ServerArgs] : [] ): Promise> { if ('mp' in window) { @@ -59,7 +59,7 @@ class Cef { public triggerClient( eventName: EventName, ...args: _ClientEventHasArgs extends true - ? [RageFW_ClientArguments] + ? [RageFW_ClientArgs] : [] ): Promise> { if ('mp' in window) { diff --git a/cef/src/types/cef.ts b/cef/src/types/cef.ts index 3304976..f564053 100644 --- a/cef/src/types/cef.ts +++ b/cef/src/types/cef.ts @@ -1,4 +1,5 @@ import { RageFW_ICustomCefEvent } from 'rage-fw-shared-types' + export { RageFW_ICustomCefEvent } from 'rage-fw-shared-types' /** @@ -11,7 +12,7 @@ export type RageFW_CefEvent = keyof RageFW_ICustomCefEvent * Array of arguments of an event you pass as a generic * These only include custom cef events */ -export type RageFW_CefArguments = Parameters< +export type RageFW_CefArgs = Parameters< RageFW_ICustomCefEvent[K] > @@ -28,7 +29,7 @@ export type RageFW_CefReturn = ReturnType< * These only include custom cef events */ export type RageFW_CefCallback = ( - args: RageFW_CefArguments, + args: RageFW_CefArgs, ) => RageFW_CefReturn export type _CefEventHasArgs = diff --git a/cef/src/types/client.ts b/cef/src/types/client.ts index 89a6bdf..10b3f02 100644 --- a/cef/src/types/client.ts +++ b/cef/src/types/client.ts @@ -1,4 +1,5 @@ import type { RageFW_ICustomClientEvent } from 'rage-fw-shared-types' + export type { RageFW_ICustomClientEvent } from 'rage-fw-shared-types' /** @@ -11,7 +12,7 @@ export type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent * Array of arguments of event you pass as a generic * These only include custom client events */ -export type RageFW_ClientArguments = Parameters< +export type RageFW_ClientArgs = Parameters< RageFW_ICustomClientEvent[K] > diff --git a/cef/src/types/server.ts b/cef/src/types/server.ts index 01f9e71..825ff20 100644 --- a/cef/src/types/server.ts +++ b/cef/src/types/server.ts @@ -1,4 +1,5 @@ import type { RageFW_ICustomServerEvent } from 'rage-fw-shared-types' + export type { RageFW_ICustomServerEvent } from 'rage-fw-shared-types' /** @@ -11,7 +12,7 @@ export type RageFW_ServerEvent = keyof RageFW_ICustomServerEvent * Array of arguments of event you pass as a generic * These only include custom server events */ -export type RageFW_ServerArguments = Parameters< +export type RageFW_ServerArgs = Parameters< RageFW_ICustomServerEvent[K] > diff --git a/client/src/core/browser.ts b/client/src/core/browser.ts new file mode 100644 index 0000000..c348127 --- /dev/null +++ b/client/src/core/browser.ts @@ -0,0 +1,8 @@ +import { Player } from './' + +export class Browser extends Player { + public registerBrowser(browser: BrowserMp) { + this.browser = browser + return browser + } +} diff --git a/client/src/core/client.ts b/client/src/core/client.ts new file mode 100644 index 0000000..b50a126 --- /dev/null +++ b/client/src/core/client.ts @@ -0,0 +1,20 @@ +import rpc from 'rage-rpc' + +import { RageFW_ClientCallback, RageFW_ClientEvent } from '../types' + +export class Client { + public register( + eventName: EventName, + callback: RageFW_ClientCallback, + ): void { + rpc.register(eventName, data => { + return callback(data) + }) + } + + public unregister( + eventName: EventName, + ): void { + rpc.unregister(eventName) + } +} diff --git a/client/src/core/index.ts b/client/src/core/index.ts new file mode 100644 index 0000000..95f461a --- /dev/null +++ b/client/src/core/index.ts @@ -0,0 +1,4 @@ +export * from './client' +export * from './player' +export * from './browser' +export * from './logger' diff --git a/client/src/logger.ts b/client/src/core/logger.ts similarity index 93% rename from client/src/logger.ts rename to client/src/core/logger.ts index c6447fe..6079d7c 100644 --- a/client/src/logger.ts +++ b/client/src/core/logger.ts @@ -1,4 +1,4 @@ -export default class Logger { +export class Logger { public error(message: unknown) { mp.console.logError( `[${new Date().toLocaleTimeString()}] [ERROR] ${message}`, diff --git a/client/src/core/player.ts b/client/src/core/player.ts new file mode 100644 index 0000000..1ccaedb --- /dev/null +++ b/client/src/core/player.ts @@ -0,0 +1,49 @@ +import rpc from 'rage-rpc' +import type { RageFW_ICustomClientEvent } from 'rage-fw-shared-types' + +import { + _CefEventHasArgs, + _ClientEventHasArgs, + _ServerEventHasArgs, + RageFW_CefArgs, + RageFW_CefEvent, + RageFW_CefReturn, + RageFW_ClientEventArguments, + RageFW_ClientEventReturn, + RageFW_ClientServerEvent, + RageFW_ClientServerEventArguments, + RageFW_ClientServerEventReturn, +} from '../types' + +export class Player { + public browser: BrowserMp | undefined + + public trigger( + eventName: EventName, + ...args: _ClientEventHasArgs extends true + ? [RageFW_ClientEventArguments] + : [] + ): Promise> { + return rpc.call>(eventName, args) + } + + public triggerServer( + eventName: EventName, + ...args: _ServerEventHasArgs extends true + ? [RageFW_ClientServerEventArguments] + : [] + ): Promise> { + return rpc.callServer(eventName, args) + } + + public triggerBrowser( + eventName: EventName, + ...args: _CefEventHasArgs extends true + ? [RageFW_CefArgs] + : [] + ): Promise> { + if (!this.browser) + throw new Error('You need to initialize browser first!') + return rpc.callBrowser(this.browser, eventName, args) + } +} diff --git a/client/src/index.ts b/client/src/index.ts index e2d3e38..a82447d 100644 --- a/client/src/index.ts +++ b/client/src/index.ts @@ -1,81 +1,4 @@ -import rpc from 'rage-rpc' - -import Logger from './logger' - -import { - _CefEventHasArgs, - _ClientEventHasArgs, - _ServerEventHasArgs, - RageFW_CefArgs, - RageFW_CefEvent, - RageFW_CefReturn, - RageFW_ClientEvent, - RageFW_ClientEventArguments, - RageFW_ClientEventCallback, - RageFW_ClientEventReturn, - RageFW_ClientServerEvent, - RageFW_ClientServerEventArguments, - RageFW_ClientServerEventReturn, -} from './types' - -import type { RageFW_ICustomClientEvent } from 'rage-fw-shared-types' - -class Client { - public register( - eventName: EventName, - callback: RageFW_ClientEventCallback, - ): void { - rpc.register(eventName, data => { - return callback(data) - }) - } - - public unregister( - eventName: EventName, - ): void { - rpc.unregister(eventName) - } -} - -class Player { - public browser: BrowserMp | undefined - - public trigger( - eventName: EventName, - ...args: _ClientEventHasArgs extends true - ? [RageFW_ClientEventArguments] - : [] - ): Promise> { - return rpc.call>(eventName, args) - } - - public triggerServer( - eventName: EventName, - ...args: _ServerEventHasArgs extends true - ? [RageFW_ClientServerEventArguments] - : [] - ): Promise> { - return rpc.callServer(eventName, args) - } - - public triggerBrowser( - eventName: EventName, - ...args: _CefEventHasArgs extends true - ? [RageFW_CefArgs] - : [] - ): Promise> { - if (!this.browser) - throw new Error('You need to initialize browser first!') - return rpc.callBrowser(this.browser, eventName, args) - } -} - -class Browser extends Player { - public registerBrowser(browser: BrowserMp) { - this.browser = browser - return browser - } -} +import { Browser, Client, Logger, Player } from './core' export const fw = { event: new Client(), diff --git a/client/src/types/client.ts b/client/src/types/client.ts index bbc5ce5..6654dd0 100644 --- a/client/src/types/client.ts +++ b/client/src/types/client.ts @@ -14,7 +14,7 @@ export type RageFW_ClientEvent = * Array of arguments for an event, name of which you pass as a generic * These include custom and system events */ -export type RageFW_ClientEventArguments = +export type RageFW_ClientArgs = K extends keyof RageFW_ICustomClientEvent ? Parameters : K extends keyof IClientEvents @@ -25,15 +25,15 @@ export type RageFW_ClientEventArguments = * Callback (function) for an event, name of which you pass as a generic * These only include custom events */ -export type RageFW_ClientEventCallback = ( - args: RageFW_ClientEventArguments, -) => RageFW_ClientEventReturn +export type RageFW_ClientCallback = ( + args: RageFW_ClientArgs, +) => RageFW_ClientReturn /** * Return type for an event, name of which you pass as a generic * These only include custom events */ -export type RageFW_ClientEventReturn = +export type RageFW_ClientReturn = K extends keyof RageFW_ICustomClientEvent ? ReturnType : never diff --git a/client/src/types/server.ts b/client/src/types/server.ts index c65bccd..78c171f 100644 --- a/client/src/types/server.ts +++ b/client/src/types/server.ts @@ -15,17 +15,16 @@ export type RageFW_ClientServerEvent = keyof RageFW_ICustomServerEvent * Array of arguments for an event, name of which you pass as a generic * These only include custom events */ -export type RageFW_ClientServerEventArguments< - K extends RageFW_ClientServerEvent, -> = K extends keyof RageFW_ICustomServerEvent - ? Parameters - : never +export type RageFW_ClientServerArgs = + K extends keyof RageFW_ICustomServerEvent + ? Parameters + : never /** * Return type for an event, name of which you pass as a generic * These only include custom events */ -export type RageFW_ClientServerEventReturn = +export type RageFW_ClientServerReturn = K extends keyof RageFW_ICustomServerEvent ? ReturnType : never diff --git a/server/src/core/logger.ts b/server/src/core/logger.ts index 5108dee..17e4c23 100644 --- a/server/src/core/logger.ts +++ b/server/src/core/logger.ts @@ -1,4 +1,5 @@ import winston, { format } from 'winston' + const { timestamp, printf, colorize } = format export class Logger { diff --git a/server/src/core/player.ts b/server/src/core/player.ts index d00c588..07f799e 100644 --- a/server/src/core/player.ts +++ b/server/src/core/player.ts @@ -7,8 +7,8 @@ import { RageFW_CefEvent, RageFW_CefReturn, RageFW_ClientEvent, - RageFW_ServerClientEventArguments, - RageFW_ServerClientEventReturn, + RageFW_ServerClientArgs, + RageFW_ServerClientReturn, } from '../types' export class Player { @@ -16,9 +16,9 @@ export class Player { player: PlayerMp, eventName: EventName, ...args: _ClientEventHasArgs extends true - ? [RageFW_ServerClientEventArguments] + ? [RageFW_ServerClientArgs] : [] - ): Promise> { + ): Promise> { return rpc.callClient(player, eventName, args) } diff --git a/server/src/core/server.ts b/server/src/core/server.ts index 73ecb0f..dc39afb 100644 --- a/server/src/core/server.ts +++ b/server/src/core/server.ts @@ -3,19 +3,13 @@ import { RageFW_ICustomServerEvent } from 'rage-fw-shared-types' import { nativeEvents } from '../native.events' import { - _CefEventHasArgs, - _ClientEventHasArgs, _ServerEventHasArgs, - RageFW_CefArgs, - RageFW_CefEvent, - RageFW_ClientEvent, - RageFW_ServerClientEventArguments, + RageFW_ServerArgs, + RageFW_ServerCallback, + RageFW_ServerCallbackCustom, + RageFW_ServerCallbackNative, RageFW_ServerEvent, - RageFW_ServerEventArguments, - RageFW_ServerEventCallback, - RageFW_ServerEventCallbackCustom, - RageFW_ServerEventCallbackNative, - RageFW_ServerEventReturn, + RageFW_ServerReturn, } from '../types' type MiddlewarePoolServer = Partial<{ @@ -25,11 +19,11 @@ type MiddlewarePoolServer = Partial<{ } }> -type MiddlewareFunction = ( +export type MiddlewareFunction = ( player: PlayerMp, eventName: EventName, ...args: _ServerEventHasArgs extends true - ? [RageFW_ServerEventArguments] + ? [RageFW_ServerArgs] : [] ) => void @@ -56,11 +50,11 @@ export class Server { private registerCustom( eventName: EventName, - callback: RageFW_ServerEventCallbackCustom, + callback: RageFW_ServerCallbackCustom, ): void { rpc.register( eventName, - async (args: RageFW_ServerEventArguments, info) => { + async (args: RageFW_ServerArgs, info) => { callback([info.player as PlayerMp, ...args]) }, ) @@ -68,7 +62,7 @@ export class Server { private registerNative( eventName: EventName, - callback: RageFW_ServerEventCallbackNative, + callback: RageFW_ServerCallbackNative, ): void { mp.events.add( eventName, @@ -79,35 +73,35 @@ export class Server { public register( eventName: EventName, - callback: RageFW_ServerEventCallback, + callback: RageFW_ServerCallback, ): void { if (this.isNativeEvent(eventName)) { this.registerNative( eventName, - callback as RageFW_ServerEventCallbackNative, + callback as RageFW_ServerCallbackNative, ) } else { this.registerCustom( eventName, - callback as unknown as RageFW_ServerEventCallbackCustom, + callback as unknown as RageFW_ServerCallbackCustom, ) } } public registerMany(events: { - [event in EventName]: RageFW_ServerEventCallback + [event in EventName]: RageFW_ServerCallback }): void { - Object.entries>(events).map( + Object.entries>(events).map( ([eventName, callback]) => { if (this.isNativeEvent(eventName)) { this.registerNative( eventName, - callback as RageFW_ServerEventCallbackNative, + callback as RageFW_ServerCallbackNative, ) } else { this.registerCustom( eventName as keyof RageFW_ICustomServerEvent, - callback as unknown as RageFW_ServerEventCallbackCustom, + callback as unknown as RageFW_ServerCallbackCustom, ) } }, @@ -139,9 +133,9 @@ export class Server { public trigger( eventName: EventName, ...args: _ServerEventHasArgs extends true - ? [RageFW_ServerEventArguments] + ? [RageFW_ServerArgs] : [] - ): Promise> { - return rpc.call>(eventName, args) + ): Promise> { + return rpc.call>(eventName, args) } } diff --git a/server/src/index.ts b/server/src/index.ts index 4c195f6..0bbda5a 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -1,4 +1,4 @@ -import { Server, Logger, Player } from './core' +import { Logger, Player, Server } from './core' export const fw = { event: new Server(), diff --git a/server/src/types/client.ts b/server/src/types/client.ts index 8b82e75..1c57a06 100644 --- a/server/src/types/client.ts +++ b/server/src/types/client.ts @@ -12,7 +12,7 @@ export type RageFW_ClientEvent = keyof RageFW_ICustomClientEvent * Array of arguments of an event you pass as a generic * These only include custom events */ -export type RageFW_ServerClientEventArguments = +export type RageFW_ServerClientArgs = K extends RageFW_ClientEvent ? Parameters : never @@ -21,7 +21,7 @@ export type RageFW_ServerClientEventArguments = * Return type of event you pass as a generic * These only include custom events */ -export type RageFW_ServerClientEventReturn = +export type RageFW_ServerClientReturn = K extends RageFW_ClientEvent ? ReturnType : never diff --git a/server/src/types/server.ts b/server/src/types/server.ts index a327af0..9939769 100644 --- a/server/src/types/server.ts +++ b/server/src/types/server.ts @@ -18,7 +18,7 @@ export type RageFW_ServerEvent = * Array of arguments for an event, name of which you pass as a generic * These also include system events */ -export type RageFW_ServerEventArguments = +export type RageFW_ServerArgs = K extends keyof RageFW_ICustomServerEvent ? Parameters : K extends keyof IServerEvents @@ -29,18 +29,18 @@ export type RageFW_ServerEventArguments = * Callback (function) for an event, name of which you pass as a generic * These include system and custom events */ -export type RageFW_ServerEventCallback = +export type RageFW_ServerCallback = K extends keyof RageFW_ICustomServerEvent - ? RageFW_ServerEventCallbackCustom + ? RageFW_ServerCallbackCustom : K extends keyof IServerEvents - ? RageFW_ServerEventCallbackNative + ? RageFW_ServerCallbackNative : never /** * Return type for an event, name of which you pass as a generic * These include system and custom events */ -export type RageFW_ServerEventReturn = +export type RageFW_ServerReturn = K extends keyof RageFW_ICustomServerEvent ? ReturnType : K extends keyof IServerEvents @@ -51,17 +51,17 @@ export type RageFW_ServerEventReturn = * Array of arguments for an event, name of which you pass as a generic * These only include custom events */ -export type RageFW_ServerEventCallbackCustom< +export type RageFW_ServerCallbackCustom< K extends keyof RageFW_ICustomServerEvent = keyof RageFW_ICustomServerEvent, > = ( - payload: [player: PlayerMp, ...args: RageFW_ServerEventArguments], -) => RageFW_ServerEventReturn + payload: [player: PlayerMp, ...args: RageFW_ServerArgs], +) => RageFW_ServerReturn /** * Array of arguments for an event, name of which you pass as a generic * These only include system events */ -export type RageFW_ServerEventCallbackNative< +export type RageFW_ServerCallbackNative< K extends keyof IServerEvents = keyof IServerEvents, > = (payload: Parameters) => ReturnType