Compare commits

...

32 Commits

Author SHA1 Message Date
469d970654 Merge branch 'dev' 2024-10-30 16:29:29 +00:00
217e0ddf01 Merge pull request 'Server/Client middlewares + JSDoc' (#6) from unstable into dev
Reviewed-on: #6
2024-10-30 16:21:29 +00:00
ea572070ef versions rollback 2024-10-30 16:21:03 +00:00
7308fc26e0 v0.2.1-0.2.0-alpha.2.0 2024-10-30 16:18:08 +00:00
ffd542c1de v0.2.1-0.2.0-alpha.1.0 2024-10-30 16:17:28 +00:00
914300924a v0.2.0 2024-10-30 16:16:45 +00:00
bc295a777b jsdoc 2024-10-30 15:38:06 +00:00
cd27764a30 jsdoc 2024-10-30 15:27:22 +00:00
30d125ce8e version bump 2024-10-29 17:06:14 +00:00
d56475d08a Merge pull request 'Consistency updates + cleanup' (#5) from unstable into dev
Reviewed-on: #5
2024-10-29 17:03:31 +00:00
622676d189 feat core
- server/client middlewares
2024-10-29 17:03:07 +00:00
d8df9fa7ef fix core
- renaming done
2024-10-28 17:40:25 +00:00
d9b917c024 core version bump 2024-10-28 17:37:17 +00:00
cbbf4f38e6 upd core
- typed server inner rpc
- client minor fixes
- sharing browser with rpc on client
2024-10-28 17:30:28 +00:00
3bbf1e2209 upd core
- typed client inner rpc
- better type organization
2024-10-28 16:53:42 +00:00
9fe9bad840 upd core
- renamed cef -> browser
- added browser logger + custom logger option
2024-10-28 16:35:16 +00:00
588bb42d04 upd core
- types now use namespace to avoid bloating
- moved items for further usage into data folder
2024-10-28 15:20:43 +00:00
3a4fe19df0 upd readme bump 2024-10-28 15:14:41 +00:00
c8e5bdf375 Merge branch 'dev' into unstable 2024-10-28 15:10:25 +00:00
a675114b41 upd core
- changed local shared types package name
2024-10-28 15:10:00 +00:00
54781d50b2 upd core
- changed local rpc to remote dependency
2024-10-28 15:04:39 +00:00
de33fe4763 versions bump 2024-10-28 13:20:51 +00:00
d3d75b338d v0.2.5 2024-10-28 13:02:24 +00:00
04eb724073 v0.2.4 2024-10-28 12:59:44 +00:00
e4950b6c99 Merge remote-tracking branch 'origin/dev' into dev
# Conflicts:
#	lerna.json
#	rpc/package.json
2024-10-28 12:57:58 +00:00
Oleksandr Honcharov
8f856d929f v0.2.3 2024-10-28 14:54:48 +02:00
Oleksandr Honcharov
ee8210f140 v0.2.2 2024-10-28 14:54:19 +02:00
Oleksandr Honcharov
74c0215366 v0.2.2 2024-10-28 14:53:05 +02:00
19b1b770f6 v0.2.1 2024-10-28 12:51:53 +00:00
Oleksandr Honcharov
96c92f9d19 v0.2.1 2024-10-28 14:51:45 +02:00
2b287f06a2 v0.2.0 2024-10-28 12:46:24 +00:00
2f2b1da99b upd
changed packages to organization
2024-10-28 12:43:15 +00:00
55 changed files with 9512 additions and 7558 deletions

32
browser/package.json Normal file
View File

@ -0,0 +1,32 @@
{
"name": "@entityseven/rage-fw-browser",
"version": "0.2.0",
"main": "dist/index.js",
"types": "dist/src/index.d.ts",
"files": [
"dist/**/*",
"readme.md"
],
"scripts": {
"build": "tsup"
},
"dependencies": {
"@entityseven/rage-fw-rpc": "latest"
},
"peerDependencies": {
"@entityseven/rage-fw-shared-types": "workspace:^",
"@ragempcommunity/types-cef": "^2.1.8"
},
"description": "RageFW CEF side",
"keywords": [],
"author": "SashaGoncharov19",
"contributors": [
{
"name": "rilaxik",
"email": "dev.rilaxik@gmail.com",
"url": "https://github.com/rilaxik"
}
],
"license": "MIT",
"gitHead": "ffd542c1deddb3033e16e0dae7557313ae09b05f"
}

187
browser/src/core/browser.ts Normal file
View File

@ -0,0 +1,187 @@
import { Helper } from './helper'
import { rpc } from './rpc'
import type * as T from '../types'
import {
RageFW_BrowserEvent,
RageFW_ClientEvent,
RageFW_ServerEvent,
} from '../types'
/** Browser-side interactions */
export class Browser extends Helper {
constructor() {
super()
}
/**
* Setter. Enables console debug logs for events
*/
set debugLogs(debug: boolean) {
this.debugLogs_ = debug
}
/**
* Setter. Provides an ability to specify custom logger function to get special formatting. Using this enables ``debugLogs``
*/
set customLogger(
fn: (method: string, eventName: string, ...args: unknown[]) => unknown,
) {
this.customLogger_ = fn
}
/**
* Registers a browser event with an associated callback
*
* @param eventName - The name of the event to register
* @param callback - The callback function to be executed when the event is triggered
* @returns {Browser} The current browser instance, enabling method chaining
*
* @example
* // Registering an event
* fw.event.register("showNotification", (message, color) => {
* // do something
* })
*
* @see {@link https://git.entityseven.com/entityseven/rage-framework/wiki Wiki}
*/
public register<EventName extends T.RageFW_BrowserEvent>(
eventName: EventName,
callback: T.RageFW_BrowserCallback<EventName>,
): Browser {
this.log_('register', eventName, callback)
rpc.register<
Parameters<typeof callback>,
ReturnType<typeof callback>,
EventName
>(eventName, async (...data) => await callback(...data))
return this
}
/**
* Unregisters a browser event, removing the associated callback
*
* @param eventName - The name of the event to unregister
* @returns {Browser} The current browser instance, enabling method chaining
*
* @example
* // Unregistering an event
* fw.event.unregister("showNotification")
*
* @see {@link https://git.entityseven.com/entityseven/rage-framework/wiki Wiki}
*/
public unregister<EventName extends T.RageFW_BrowserEvent>(
eventName: EventName,
): Browser {
rpc.unregister<EventName>(eventName)
return this
}
/**
* Triggers a browser event from the browser with arguments from shared types
*
* Formerly known as ``call`` or ``emit``
*
* @param eventName - The name of the browser event to trigger
* @param [args] - Arguments for the browser event, if present
* @returns {Promise} resolving to the browser's response for the event
*
* @example
* // Triggering a browser event without arguments
* fw.event.trigger("browserEventName")
*
* @example
* // Triggering a browser event with arguments
* fw.event.trigger("browserEventName", ["message to me"])
*
* @see {@link https://git.entityseven.com/entityseven/rage-framework/wiki Wiki}
*/
public async trigger<EventName extends T.RageFW_BrowserEvent>(
eventName: EventName,
...args: T._BrowserEventHasArgs<EventName> extends true
? [T.RageFW_BrowserArgs<EventName>]
: []
): Promise<T.RageFW_BrowserReturn<EventName>> {
this.log_('[RPC](trigger):', eventName, ...args)
return await rpc.call<
typeof args,
EventName,
T.RageFW_BrowserReturn<EventName>
>(eventName, args)
}
/**
* Triggers a server event from the browser with arguments from shared types
*
* Formerly known as ``callServer`` or ``emitServer``
*
* @param eventName - The name of the server event to trigger
* @param [args] - Arguments for the server event, if present
* @returns {Promise} resolving to the server's response for the event
*
* @example
* // Triggering a server event without arguments
* fw.event.triggerServer("serverEventName")
*
* @example
* // Triggering a server event with arguments
* fw.event.triggerServer("serverEventName", ["message to server"])
*
* @see {@link https://git.entityseven.com/entityseven/rage-framework/wiki Wiki}
*/
public async triggerServer<EventName extends T.RageFW_ServerEvent>(
eventName: EventName,
...args: T._ServerEventHasArgs<EventName> extends true
? [T.RageFW_ServerArgs<EventName>]
: []
): Promise<T.RageFW_ServerReturn<EventName>> {
this.log_('[RPC](triggerServer):', eventName, ...args)
return await rpc.callServer<
typeof args,
EventName,
T.RageFW_ServerReturn<EventName>
>(eventName, args)
}
/**
* Triggers a client event from the browser with arguments from shared types
*
* Formerly known as ``callClient`` or ``emitClient``
*
* @param eventName - The name of the client event to trigger
* @param [args] - Arguments for the client event, if present
* @returns {Promise} resolving to the client's response for the event
*
* @example
* // Triggering a client event without arguments
* fw.event.triggerClient("clientEventName")
*
* @example
* // Triggering a client event with arguments
* fw.event.triggerClient("clientEventName", ["message to client"])
*
* @see {@link https://git.entityseven.com/entityseven/rage-framework/wiki Wiki}
*/
public async triggerClient<EventName extends T.RageFW_ClientEvent>(
eventName: EventName,
...args: T._ClientEventHasArgs<EventName> extends true
? [T.RageFW_ClientArgs<EventName>]
: []
): Promise<T.RageFW_ClientReturn<EventName>> {
this.log_('[RPC](triggerClient):', eventName, ...args)
return await rpc.callClient<
typeof args,
EventName,
T.RageFW_ClientReturn<EventName>
>(eventName, args)
}
}
// new Browser()
// .register('customCefEvent', async (a, b) => true)
// .triggerServer('customServerEvent', ['', 1])

View File

@ -0,0 +1,21 @@
export class Helper {
protected debugLogs_: boolean = false
protected customLogger_:
| undefined
| ((method: string, eventName: string, ...args: unknown[]) => unknown) =
undefined
constructor() {}
protected log_(
method: string,
eventName: string,
...args: unknown[]
): void {
if (this.customLogger_) {
this.customLogger_(method, eventName, ...args)
} else if (this.debugLogs_) {
console.log('[RPC](' + method + ')', eventName + ':', ...args)
}
}
}

View File

@ -0,0 +1,3 @@
export * from './browser'
export * from './helper'
export * from './rpc'

6
browser/src/core/rpc.ts Normal file
View File

@ -0,0 +1,6 @@
import { Rpc } from '@entityseven/rage-fw-rpc'
export const rpc = new Rpc({
forceBrowserDevMode: process.env.RageFW_forceBrowserDevMode === 'true',
debugLogs: false,
})

16
browser/src/index.ts Normal file
View File

@ -0,0 +1,16 @@
import { Browser, rpc } from './core'
/**
* Package used on a browser-side of your Rage:MP Server
*
* @see {@link https://git.entityseven.com/entityseven/rage-framework/wiki Wiki}
*/
export const fw = {
/** Browser-side interactions */
event: new Browser(),
/** ``rage-fw-rpc`` instance used under the hood. It is highly recommended to use this one if you need it instead of creating a new instance */
rpc,
}
;(async () => {
await fw.event.triggerClient('cefReady')
})()

View File

@ -0,0 +1,40 @@
import type { RageFW_ICustomBrowserEvent } from '@entityseven/rage-fw-shared-types'
export type { RageFW_ICustomBrowserEvent } from '@entityseven/rage-fw-shared-types'
/**
* Union of all available browser event names
* These only include custom events
*/
export type RageFW_BrowserEvent = keyof RageFW_ICustomBrowserEvent
/**
* Array of arguments of an event you pass as a generic
* These only include custom browser events
*/
export type RageFW_BrowserArgs<K extends RageFW_BrowserEvent> = Parameters<
RageFW_ICustomBrowserEvent[K]
>
/**
* Return type of event you pass as a generic
* These only include custom browser events
*/
export type RageFW_BrowserReturn<K extends RageFW_BrowserEvent> = ReturnType<
RageFW_ICustomBrowserEvent[K]
>
/**
* Callback (function) of event you pass as a generic
* These only include custom browser events
*/
export type RageFW_BrowserCallback<K extends keyof RageFW_ICustomBrowserEvent> =
(...args: RageFW_BrowserArgs<K>) => Promise<RageFW_BrowserReturn<K>>
export type _BrowserEventHasArgs<
EventName extends keyof RageFW_ICustomBrowserEvent,
> = keyof RageFW_ICustomBrowserEvent extends never
? false
: Parameters<RageFW_ICustomBrowserEvent[EventName]>[0] extends undefined
? false
: true

View File

@ -1,6 +1,6 @@
import type { RageFW_ICustomClientEvent } from 'rage-fw-shared-types'
import type { RageFW_ICustomClientEvent } from '@entityseven/rage-fw-shared-types'
export type { RageFW_ICustomClientEvent } from 'rage-fw-shared-types'
export type { RageFW_ICustomClientEvent } from '@entityseven/rage-fw-shared-types'
/**
* Union of all available client event names

View File

@ -1,3 +1,3 @@
export * from './cef'
export * from './browser'
export * from './client'
export * from './server'

View File

@ -1,6 +1,6 @@
import type { RageFW_ICustomServerEvent } from 'rage-fw-shared-types'
import type { RageFW_ICustomServerEvent } from '@entityseven/rage-fw-shared-types'
export type { RageFW_ICustomServerEvent } from 'rage-fw-shared-types'
export type { RageFW_ICustomServerEvent } from '@entityseven/rage-fw-shared-types'
/**
* Union of all available server event names

View File

@ -1,29 +0,0 @@
{
"name": "rage-fw-cef",
"version": "0.1.0",
"main": "dist/index.js",
"types": "dist/src/index.d.ts",
"files": [
"dist/**/*",
"readme.md"
],
"scripts": {
"build": "tsup"
},
"dependencies": {
"rage-fw-rpc": "workspace:^"
},
"peerDependencies": {
"@ragempcommunity/types-cef": "^2.1.8",
"rage-fw-shared-types": "workspace:^"
},
"description": "RageFW CEF side",
"keywords": [],
"author": "SashaGoncharov19",
"contributors": [{
"name": "rilaxik",
"email": "dev.rilaxik@gmail.com",
"url": "https://github.com/rilaxik"
}],
"license": "MIT"
}

View File

@ -1,113 +0,0 @@
import { Rpc } from 'rage-fw-rpc'
import type {
_CefEventHasArgs,
_ClientEventHasArgs,
_ServerEventHasArgs,
RageFW_CefArgs,
RageFW_CefCallback,
RageFW_CefReturn,
RageFW_ClientArgs,
RageFW_ClientReturn,
RageFW_ICustomCefEvent,
RageFW_ICustomClientEvent,
RageFW_ICustomServerEvent,
RageFW_ServerArgs,
RageFW_ServerReturn,
} from './types'
class Cef {
private _debugMode: boolean = false
private _rpc: Rpc = new Rpc()
constructor() {}
set debug(debug: boolean) {
this._debugMode = debug
}
get rpc(): Rpc {
return this._rpc
}
public register<EventName extends keyof RageFW_ICustomCefEvent>(
eventName: EventName,
callback: RageFW_CefCallback<EventName>,
): void {
if (this._debugMode) {
console.log('[RPC](register):', eventName, callback)
}
if ('mp' in window) {
this._rpc.register(eventName, callback)
}
}
public async trigger<EventName extends keyof RageFW_ICustomCefEvent>(
eventName: EventName,
...args: _CefEventHasArgs<EventName> extends true
? [RageFW_CefArgs<EventName>]
: []
): Promise<RageFW_CefReturn<EventName>> {
if (this._debugMode) {
console.log('[RPC](trigger):', eventName, ...args)
}
if ('mp' in window) {
return await this._rpc.call(eventName, args)
}
return Promise.reject(
'RageFW was started in window which does not contain MP',
)
}
public async triggerServer<
EventName extends keyof RageFW_ICustomServerEvent,
>(
eventName: EventName,
...args: _ServerEventHasArgs<EventName> extends true
? [RageFW_ServerArgs<EventName>]
: []
): Promise<RageFW_ServerReturn<EventName>> {
if (this._debugMode) {
console.log('[RPC](triggerServer):', eventName, ...args)
}
if ('mp' in window) {
return await this._rpc.callServer(eventName, args)
}
return Promise.reject(
'RageFW was started in window which does not contain MP',
)
}
public async triggerClient<
EventName extends keyof RageFW_ICustomClientEvent,
>(
eventName: EventName,
...args: _ClientEventHasArgs<EventName> extends true
? [RageFW_ClientArgs<EventName>]
: []
): Promise<RageFW_ClientReturn<EventName>> {
if (this._debugMode) {
console.log('[RPC](triggerClient):', eventName, ...args)
}
if ('mp' in window) {
return await this._rpc.callClient(eventName, args)
}
return Promise.reject(
'RageFW was started in window which does not contain MP',
)
}
}
export const fw = {
event: new Cef(),
}
;(async () => {
await fw.event.triggerClient('cefReady')
})()

View File

@ -1,40 +0,0 @@
import { RageFW_ICustomCefEvent } from 'rage-fw-shared-types'
export { RageFW_ICustomCefEvent } from 'rage-fw-shared-types'
/**
* Union of all available cef event names
* These only include custom events
*/
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_CefArgs<K extends RageFW_CefEvent> = Parameters<
RageFW_ICustomCefEvent[K]
>
/**
* Return type of event you pass as a generic
* These only include custom cef events
*/
export type RageFW_CefReturn<K extends RageFW_CefEvent> = ReturnType<
RageFW_ICustomCefEvent[K]
>
/**
* Callback (function) of event you pass as a generic
* These only include custom cef events
*/
export type RageFW_CefCallback<K extends keyof RageFW_ICustomCefEvent> = (
args: RageFW_CefArgs<K>,
) => Promise<RageFW_CefReturn<K>>
export type _CefEventHasArgs<EventName extends keyof RageFW_ICustomCefEvent> =
keyof RageFW_ICustomCefEvent extends never
? false
: Parameters<RageFW_ICustomCefEvent[EventName]>[0] extends undefined
? false
: true

View File

@ -1,6 +1,6 @@
{
"name": "create-rage-fw",
"version": "0.1.0",
"name": "@entityseven/create-rage-fw",
"version": "0.1.1",
"bin": {
"rage-fw": "dist/index.js"
},

View File

@ -2,7 +2,7 @@
To make you life easier while using RageFW we created a basic CLI. At the moment automation we have only works via [pnpm](https://pnpm.io/)
``pnpm create rage-fw@latest``
``pnpm create @entityseven/rage-fw@latest``
## TL;DR
- ``Initialize new project`` - create new template project

View File

@ -1,6 +1,6 @@
{
"name": "rage-fw-client",
"version": "0.1.0",
"name": "@entityseven/rage-fw-client",
"version": "0.2.0",
"main": "dist/index.js",
"types": "dist/src/index.d.ts",
"files": [
@ -11,20 +11,22 @@
"build": "tsup"
},
"dependencies": {
"rage-fw-rpc": "workspace:^"
"@entityseven/rage-fw-rpc": "latest"
},
"peerDependencies": {
"@ragempcommunity/types-client": "^2.1.8",
"rage-fw-shared-types": "workspace:^"
"@entityseven/rage-fw-shared-types": "workspace:^",
"@ragempcommunity/types-client": "^2.1.8"
},
"description": "RageFW Client side",
"keywords": [],
"author": "SashaGoncharov19",
"contributors": [{
"name": "rilaxik",
"email": "dev.rilaxik@gmail.com",
"url": "https://github.com/rilaxik"
}],
"contributors": [
{
"name": "rilaxik",
"email": "dev.rilaxik@gmail.com",
"url": "https://github.com/rilaxik"
}
],
"license": "MIT",
"gitHead": "053e4fd12aa120d53e11e0d2009c0df78c1a2ad0"
"gitHead": "ffd542c1deddb3033e16e0dae7557313ae09b05f"
}

View File

@ -1,33 +1,86 @@
import { Rpc } from 'rage-fw-rpc'
import type {
RageFW_ClientArgs,
RageFW_ClientCallback,
RageFW_ClientEvent,
} from '../types'
import { rpc } from './rpc'
import { Middleware } from './middleware'
import type * as T from '../types'
/** Client-side interactions */
export class Client {
private _rpc: Rpc = new Rpc()
/**
* Registers a client event with an associated callback
*
* @param eventName - The name of the event to register
* @param callback - The callback function to be executed when the event is triggered
* @param [options] - Optional settings for callback execution
* @param [options.middlewares] - Middleware functions to be checked before the callback executes
* @returns {Client} The current client instance, enabling method chaining
*
* @example
* // Registering an event
* fw.event.register("playerDeath", (player, reason, killer) => {
* fw.system.log.info(player, reason, killer)
* })
*
* @example
* // Registering an event with middlewares
* fw.event.register("playerDeath", (player, reason, killer) => {
* fw.system.log.info(player, reason, killer)
* }, {
* middlewares: [ignoreSuicide] // <- your middlewares here
* })
*
* // or
*
* fw.event.register("playerDeath", (player, reason, killer) => {
* fw.system.log.info(player, reason, killer)
* }, {
* middlewares: {
* executables: [ignoreSuicide], // <- your middlewares here
* onError: (msg) => fw.system.log.info(`${player.socialClub} has commited suicide`)
* }
* })
*
* @see {@link https://git.entityseven.com/entityseven/rage-framework/wiki Wiki}
*/
public register<EventName extends T.RageFW_ClientEvent>(
eventName: EventName,
callback: T.RageFW_ClientCallback<EventName>,
options?: {
middlewares?: T.RageFW_MiddlewareOptions<EventName>
},
): Client {
rpc.register<
Parameters<typeof callback>,
ReturnType<typeof callback> | Promise<unknown>,
EventName
>(eventName, async (...data) => {
if (!options?.middlewares) return await callback(...data)
get rpc(): Rpc {
return this._rpc
await Middleware.process(options.middlewares, callback, data)
})
return this
}
public register<EventName extends RageFW_ClientEvent>(
/**
* Unregisters a client event, removing the associated callback
*
* @param eventName - The name of the event to unregister
* @returns {Client} The current client instance, enabling method chaining
*
* @example
* // Unregistering an event
* fw.event.unregister("playerDeath")
*
* @see {@link https://git.entityseven.com/entityseven/rage-framework/wiki Wiki}
*/
public unregister<EventName extends T.RageFW_ClientEvent>(
eventName: EventName,
callback: RageFW_ClientCallback<EventName>,
): void {
this._rpc.register(
eventName,
async (data: RageFW_ClientArgs<EventName>) => {
return await callback(data)
},
)
}
): Client {
rpc.unregister<EventName>(eventName)
public unregister<EventName extends RageFW_ClientEvent>(
eventName: EventName,
): void {
this._rpc.unregister(eventName)
return this
}
}
// new Client()
// .register('customClientEvent', async (a, b) => true)
// .unregister('customClientEvent')

View File

@ -1,3 +1,5 @@
export * from './client'
export * from './player'
export * from './logger'
export * from './middleware'
export * from './player'
export * from './rpc'

View File

@ -1,19 +1,40 @@
/**
* Used to log to a client in-game console
*/
export class Logger {
public error(...message: unknown[]) {
mp.console.logError(
`[${new Date().toLocaleTimeString()}] [ERROR] ${message.join(' ')}`,
/**
* Informational logs. Colored in white
*
* @example
* fw.system.log.info('some information to be logged')
*/
public info(...message: unknown[]) {
mp.console.logInfo(
`[${new Date().toLocaleTimeString()}] [INFO] ${message.join(' ')}`,
)
}
/**
* Warning logs. Colored in yellow
*
* @example
* fw.system.log.warn('warning message')
*/
public warn(...message: unknown[]) {
mp.console.logWarning(
`[${new Date().toLocaleTimeString()}] [WARN] ${message.join(' ')}`,
)
}
public info(...message: unknown[]) {
mp.console.logInfo(
`[${new Date().toLocaleTimeString()}] [INFO] ${message.join(' ')}`,
/**
* Error logs. Colored in red
*
* @example
* fw.system.log.info('some error information')
*/
public error(...message: unknown[]) {
mp.console.logError(
`[${new Date().toLocaleTimeString()}] [ERROR] ${message.join(' ')}`,
)
}
}

View File

@ -0,0 +1,54 @@
import type * as T from '../types'
export class Middleware {
constructor() {}
private static async execute<EventName extends T.RageFW_ClientEvent>(
middlewares: T.RageFW_MiddlewareFunction<EventName>[],
args: T.RageFW_ClientArgs<EventName>,
): Promise<T.RageFW_MiddlewareResponseInternal> {
for (let i = 0; i < middlewares.length; i++) {
const result = await middlewares[i](...args)
if (typeof result === 'boolean' && !result)
return { success: result, id: i }
if (typeof result !== 'boolean' && !result.success)
return { ...result, id: i }
}
return {
success: true,
}
}
public static async process<EventName extends T.RageFW_ClientEvent>(
middlewareOptions: T.RageFW_MiddlewareOptions<EventName>,
callback: T.RageFW_ClientCallback<EventName>,
args: T.RageFW_ClientArgs<EventName>,
) {
if (Array.isArray(middlewareOptions)) {
const middlewaresResponse = await Middleware.execute(
middlewareOptions,
args,
)
if (middlewaresResponse.success) return await callback(...args)
} else {
const middlewaresResponse = await Middleware.execute(
middlewareOptions.executables,
args,
)
if (middlewaresResponse.success) {
return await callback(...args)
} else {
middlewareOptions.onError(
middlewaresResponse.message ??
'Middleware with id ' +
middlewaresResponse.id +
' failed',
)
}
}
}
}

View File

@ -1,53 +1,118 @@
import { Rpc } from 'rage-fw-rpc'
import type { RageFW_ICustomClientEvent } from 'rage-fw-shared-types'
import {
_CefEventHasArgs,
_ClientEventHasArgs,
_ServerEventHasArgs,
RageFW_CefArgs,
RageFW_CefEvent,
RageFW_CefReturn,
RageFW_ClientArgs,
RageFW_ClientReturn,
RageFW_ClientServerEvent,
RageFW_ClientServerArgs,
RageFW_ClientServerReturn,
} from '../types'
import { rpc } from './rpc'
import type * as T from '../types'
/** Handles event manipulations that require player to be present in context */
export class Player {
private _rpc: Rpc = new Rpc()
public browser: BrowserMp | undefined
private _browser: BrowserMp | undefined = undefined
get rpc(): Rpc {
return this._rpc
}
public trigger<EventName extends keyof RageFW_ICustomClientEvent>(
eventName: EventName,
...args: _ClientEventHasArgs<EventName> extends true
? [RageFW_ClientArgs<EventName>]
: []
): Promise<RageFW_ClientReturn<EventName>> {
return this._rpc.call(eventName, args)
/**
* Setter. Also shares browser with ``rage-fw-rpc``
*/
set browser(browser: BrowserMp) {
this._browser = browser
rpc.browser = browser
}
public triggerServer<EventName extends RageFW_ClientServerEvent>(
/**
* Triggers a client event from the client with arguments from shared types
*
* Formerly known as ``call`` or ``emit``
*
* @param eventName - The name of the client event to trigger
* @param [args] - Arguments for the client event, if present
* @returns {Promise} resolving to the client's response for the event
*
* @example
* // Triggering a client event without arguments
* fw.player.trigger("clientEventName")
*
* @example
* // Triggering a client event with arguments
* fw.player.trigger("clientEventName", ["message to me"])
*
* @see {@link https://git.entityseven.com/entityseven/rage-framework/wiki Wiki}
*/
public async trigger<EventName extends keyof T.RageFW_ICustomClientEvent>(
eventName: EventName,
...args: _ServerEventHasArgs<EventName> extends true
? [RageFW_ClientServerArgs<EventName>]
...args: T._ClientEventHasArgs<EventName> extends true
? [T.RageFW_ClientArgs<EventName>]
: []
): Promise<RageFW_ClientServerReturn<EventName>> {
return this._rpc.callServer(eventName, args)
): Promise<T.RageFW_ClientReturn<EventName>> {
return await rpc.call<
typeof args,
EventName,
T.RageFW_ClientReturn<EventName>
>(eventName, args)
}
public triggerBrowser<EventName extends RageFW_CefEvent>(
/**
* Triggers a server event from the client with arguments from shared types
*
* Formerly known as ``callServer`` or ``emitServer``
*
* @param eventName - The name of the server event to trigger
* @param [args] - Arguments for the server event, if present
* @returns {Promise} resolving to the server's response for the event
*
* @example
* // Triggering a server event without arguments
* fw.player.triggerServer("serverEventName")
*
* @example
* // Triggering a server event with arguments
* fw.player.triggerServer("serverEventName", ["message to server"])
*
* @see {@link https://git.entityseven.com/entityseven/rage-framework/wiki Wiki}
*/
public async triggerServer<EventName extends T.RageFW_ServerEvent>(
eventName: EventName,
...args: _CefEventHasArgs<EventName> extends true
? [RageFW_CefArgs<EventName>]
...args: T._ServerEventHasArgs<EventName> extends true
? [T.RageFW_ServerArgs<EventName>]
: []
): Promise<RageFW_CefReturn<EventName>> {
if (!this.browser)
): Promise<T.RageFW_ClientServerReturn<EventName>> {
return await rpc.callServer<
typeof args,
EventName,
T.RageFW_ClientServerReturn<EventName>
>(eventName, args)
}
/**
* Triggers a browser event from the client with arguments from shared types
*
* Formerly known as ``callBrowser`` or ``emitBrowser``
*
* @param eventName - The name of the browser event to trigger
* @param [args] - Arguments for the browser event, if present
* @returns {Promise} resolving to the browser's response for the event
*
* @example
* // Triggering a browser event without arguments
* fw.player.triggerBrowser("browserEventName")
*
* @example
* // Triggering a browser event with arguments
* fw.player.triggerBrowser("browserEventName", ["message to browser"])
*
* @see {@link https://git.entityseven.com/entityseven/rage-framework/wiki Wiki}
*/
public async triggerBrowser<EventName extends T.RageFW_BrowserEvent>(
eventName: EventName,
...args: T._BrowserEventHasArgs<EventName> extends true
? [T.RageFW_BrowserArgs<EventName>]
: []
): Promise<T.RageFW_BrowserReturn<EventName>> {
if (!this._browser)
throw new Error('You need to initialize browser first')
return this._rpc.callBrowser(this.browser, eventName, args)
return await rpc.callBrowser<
typeof args,
EventName,
T.RageFW_BrowserReturn<EventName>
>(eventName, args)
}
}
// new Player().trigger('customClientEvent', ['', 1])
// new Player().triggerServer('customServerEvent', ['', 1])
// new Player().triggerBrowser('customCefEvent', ['', 1])

5
client/src/core/rpc.ts Normal file
View File

@ -0,0 +1,5 @@
import { Rpc } from '@entityseven/rage-fw-rpc'
export const rpc = new Rpc({
debugLogs: false,
})

View File

@ -1,9 +1,22 @@
import { Client, Logger, Player } from './core'
import { Client, Logger, Player, rpc } from './core'
export type { RageFW_MiddlewareFunction } from './types'
/**
* Package used on a client-side of your Rage:MP Server
*
* @see {@link https://git.entityseven.com/entityseven/rage-framework/wiki Wiki}
*/
export const fw = {
/** Client-side interactions */
event: new Client(),
/** Handles event manipulations that require player to be present in context */
player: new Player(),
/** Handles functions used to interact with the client environment */
system: {
/** Used to log in a client in-game console */
log: new Logger(),
},
/** ``rage-fw-rpc`` instance used under the hood. It is highly recommended to use this one if you need it instead of creating a new instance */
rpc,
}

View File

@ -1,20 +1,29 @@
/// <reference types="@ragempcommunity/types-client" />
import type { RageFW_ICustomCefEvent } from 'rage-fw-shared-types'
import type { RageFW_ICustomBrowserEvent } from '@entityseven/rage-fw-shared-types'
export type RageFW_CefEvent = keyof RageFW_ICustomCefEvent
/**
* Union of all available browser event names callable from client
* These only include custom events
*/
export type RageFW_BrowserEvent = keyof RageFW_ICustomBrowserEvent
export type RageFW_CefArgs<K extends RageFW_CefEvent> = Parameters<
RageFW_ICustomCefEvent[K]
/**
* Array of arguments for an event, name of which you pass as a generic
* These only include custom events
*/
export type RageFW_BrowserArgs<K extends RageFW_BrowserEvent> = Parameters<
RageFW_ICustomBrowserEvent[K]
>
export type RageFW_CefReturn<K extends RageFW_CefEvent> = ReturnType<
RageFW_ICustomCefEvent[K]
export type RageFW_BrowserReturn<K extends RageFW_BrowserEvent> = ReturnType<
RageFW_ICustomBrowserEvent[K]
>
export type _CefEventHasArgs<EventName extends keyof RageFW_ICustomCefEvent> =
keyof RageFW_ICustomCefEvent extends never
? false
: Parameters<RageFW_ICustomCefEvent[EventName]>[0] extends undefined
? false
: true
export type _BrowserEventHasArgs<
EventName extends keyof RageFW_ICustomBrowserEvent,
> = keyof RageFW_ICustomBrowserEvent extends never
? false
: Parameters<RageFW_ICustomBrowserEvent[EventName]>[0] extends undefined
? false
: true

View File

@ -1,6 +1,8 @@
/// <reference types="@ragempcommunity/types-client" />
import type { RageFW_ICustomClientEvent } from 'rage-fw-shared-types'
import type { RageFW_ICustomClientEvent } from '@entityseven/rage-fw-shared-types'
export type { RageFW_ICustomClientEvent } from '@entityseven/rage-fw-shared-types'
/**
* Union of all available client event names
@ -21,23 +23,28 @@ export type RageFW_ClientArgs<K extends RageFW_ClientEvent> =
? Parameters<IClientEvents[K]>
: never
/**
* Callback (function) for an event, name of which you pass as a generic
* These only include custom events
*/
export type RageFW_ClientCallback<K extends RageFW_ClientEvent> = (
args: RageFW_ClientArgs<K>,
) => Promise<RageFW_ClientReturn<K>>
/**
* Return type for an event, name of which you pass as a generic
* These only include custom events
* These include custom and system events
*/
export type RageFW_ClientReturn<K extends RageFW_ClientEvent> =
K extends keyof RageFW_ICustomClientEvent
? ReturnType<RageFW_ICustomClientEvent[K]>
: void
: K extends keyof IClientEvents
? ReturnType<IClientEvents[K]>
: void
/**
* Callback (function) for an event, name of which you pass as a generic
* These include custom and system events
*/
export type RageFW_ClientCallback<K extends RageFW_ClientEvent> = (
...args: RageFW_ClientArgs<K>
) => Promise<RageFW_ClientReturn<K>>
/**
*
*/
export type _ClientEventHasArgs<
EventName extends keyof RageFW_ICustomClientEvent,
> = keyof RageFW_ICustomClientEvent extends never

View File

@ -1,3 +1,4 @@
export * from './client'
export * from './server'
export * from './browser'
export * from './client'
export * from './middleware'
export * from './server'

View File

@ -0,0 +1,26 @@
import type * as T from './client'
export type RageFW_MiddlewareResponse =
| {
success: boolean
message?: string
}
| boolean
export type RageFW_MiddlewareResponseInternal = {
success: boolean
message?: string
id?: number
}
export type RageFW_MiddlewareFunction<EventName extends T.RageFW_ClientEvent> =
(
...args: T.RageFW_ClientArgs<EventName>
) => Promise<RageFW_MiddlewareResponse>
export type RageFW_MiddlewareOptions<EventName extends T.RageFW_ClientEvent> =
| RageFW_MiddlewareFunction<EventName>[]
| {
executables: RageFW_MiddlewareFunction<EventName>[]
onError: (error: string) => unknown
}

View File

@ -3,19 +3,19 @@
import type {
RageFW_ICustomClientEvent,
RageFW_ICustomServerEvent,
} from 'rage-fw-shared-types'
} from '@entityseven/rage-fw-shared-types'
/**
* Union of all available server event names callable from client
* These only include custom events
*/
export type RageFW_ClientServerEvent = keyof RageFW_ICustomServerEvent
export type RageFW_ServerEvent = 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_ClientServerArgs<K extends RageFW_ClientServerEvent> =
export type RageFW_ServerArgs<K extends RageFW_ServerEvent> =
K extends keyof RageFW_ICustomServerEvent
? Parameters<RageFW_ICustomServerEvent[K]>
: never
@ -24,7 +24,7 @@ export type RageFW_ClientServerArgs<K extends RageFW_ClientServerEvent> =
* Return type for an event, name of which you pass as a generic
* These only include custom events
*/
export type RageFW_ClientServerReturn<K extends RageFW_ClientServerEvent> =
export type RageFW_ClientServerReturn<K extends RageFW_ServerEvent> =
K extends keyof RageFW_ICustomServerEvent
? ReturnType<RageFW_ICustomServerEvent[K]>
: never

View File

@ -1,5 +1,5 @@
{
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"version": "0.1.0",
"version": "0.2.1-0.2.0-alpha.2.0",
"npmClient": "pnpm"
}

View File

@ -1,13 +1,14 @@
{
"private": true,
"scripts": {
"publish": "pnpm build && lerna publish --force-publish",
"publish": "lerna publish --force-publish",
"build": "lerna run build",
"lint": "eslint --c .eslintrc.yaml --ext .ts client/ server/ shared-types/",
"rebuild:cef": "cd cef && pnpm build",
"rebuild:browser": "cd browser && pnpm build",
"rebuild:client": "cd client && pnpm build",
"rebuild:server": "cd server && pnpm build",
"rebuild": "pnpm rebuild:cef && pnpm rebuild:client && pnpm rebuild:server"
"rebuild": "pnpm rebuild:browser && pnpm rebuild:client && pnpm rebuild:server"
},
"dependencies": {
"@microsoft/api-extractor": "^7.47.0",
@ -16,6 +17,7 @@
"@ragempcommunity/types-server": "^2.1.8",
"@typescript-eslint/eslint-plugin": "^7.13.0",